forked from brendon/positioning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_optimistic_locking.rb
More file actions
32 lines (23 loc) · 1002 Bytes
/
test_optimistic_locking.rb
File metadata and controls
32 lines (23 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "test_helper"
class TestOptimisticLocking < Minitest::Test
include Minitest::Hooks
def around
ActiveRecord::Base.transaction do
super
raise ActiveRecord::Rollback
end
end
def test_position_updates_do_not_allow_stale_writes_on_loaded_models
list = List.create name: "List"
first_item = list.optimistic_locking_items.create name: "First Item"
second_item = list.optimistic_locking_items.create name: "Second Item"
third_item = list.optimistic_locking_items.create name: "Third Item"
scope = list.optimistic_locking_items.order(:position)
assert_equal [1, 2, 3], scope.pluck(:position)
third_item.update! position: 1
assert_equal [third_item.id, first_item.id, second_item.id], scope.pluck(:id)
assert_equal 0, OptimisticLockingItem.where(id: first_item.id).pick(:lock_version)
first_item.update! name: "Updated First Item"
assert_equal "Updated First Item", OptimisticLockingItem.find(first_item.id).name
end
end