Skip to content

Commit

Permalink
fix(bookings): details specs for desired behaviour [PROJ-636] (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach authored Feb 29, 2024
1 parent 333c121 commit 997e897
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 5 deletions.
212 changes: 212 additions & 0 deletions spec/booking_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,216 @@ module PlaceOS::Model
query_check.attendees.to_a.size.should eq 0
end
end

it "successfully saves a booking" do
user_email = "[email protected]"
tenant_id = Generator.tenant.id

# classic
booking = Booking.new(
booking_type: "desk",
asset_id: "desk1",
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk1"]
booking.persisted?.should be_true

# new
booking = Booking.new(
booking_type: "desk",
asset_ids: ["desk2"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_id.should eq "desk2"
booking.persisted?.should be_true

# combined
booking = Booking.new(
booking_type: "desk",
asset_id: "desk3",
asset_ids: ["desk3", "desk4"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk3", "desk4"]
booking.persisted?.should be_true

# mismatch 1
booking = Booking.new(
booking_type: "desk",
asset_id: "desk5",
asset_ids: ["desk6", "desk7"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk6", "desk7"]
booking.asset_id.should eq "desk6"
booking.persisted?.should be_true

# mismatch 2
booking = Booking.new(
booking_type: "desk",
asset_ids: ["desk8"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk8"]
booking.asset_id.should eq "desk8"
booking.persisted?.should be_true

booking.asset_id = "desk9"
booking.save!
booking.asset_ids.should eq ["desk9"]
booking.asset_id.should eq "desk9"
booking.persisted?.should be_true

booking.asset_id = "desk10"
booking.asset_ids = ["desk11"]
booking.save!
booking.asset_ids.should eq ["desk11"]
booking.asset_id.should eq "desk11"
booking.persisted?.should be_true
end

it "rejects a booking that clashes" do
user_email = "[email protected]"
tenant_id = Generator.tenant.id

saved = Booking.new(
booking_type: "desk",
asset_id: "desk1",
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!

# new
not_saved = Booking.new(
booking_type: "desk",
asset_ids: ["desk1"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
)
not_saved.save

saved.persisted?.should be_true
not_saved.persisted?.should be_false
end

it "rejects a concurrent booking that clashes" do
user_email = "[email protected]"
tenant_id = Generator.tenant.id

wait = Channel(Booking).new
spawn do
booking = Booking.new(
booking_type: "desk",
asset_id: "desk1",
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
)
booking.save
wait.send booking
end

local = Booking.new(
booking_type: "desk",
asset_ids: ["desk1"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
)
local.save

spawned = wait.receive
saved = 0
saved += 1 if local.persisted?
saved += 1 if spawned.persisted?

saved.should eq 1
end

it "rejects a booking with multiple same asset ids" do
user_email = "[email protected]"
tenant_id = Generator.tenant.id

local = Booking.new(
booking_type: "desk",
asset_ids: ["desk1", "desk2", "desk1"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
)
local.save.should be_false
local.persisted?.should be_false
end
end
11 changes: 6 additions & 5 deletions src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ module PlaceOS::Model
end

def update_assets
if (single_id = self.asset_id) && !asset_ids.includes?(single_id)
asset_ids.insert(0, single_id)
@asset_ids_changed = true
if asset_ids.size == 1 && !@asset_ids_changed && @asset_id_changed
asset_ids[0] = asset_id
elsif asset_ids.empty?
asset_ids.insert(0, asset_id)
end
self.asset_id = asset_ids.first
@asset_id = asset_ids.first
end

def asset_ids=(vals : Array(String))
Expand Down Expand Up @@ -185,7 +186,7 @@ module PlaceOS::Model

def set_created
self.last_changed = self.created = Time.utc.to_unix
self.asset_id = self.asset_ids.first unless self.asset_ids.empty?
@asset_id ||= self.asset_ids.first unless self.asset_ids.empty?
end

def change_extension_data(data : JSON::Any)
Expand Down

0 comments on commit 997e897

Please sign in to comment.