提交 70d3c384 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #28833 from bogdanvlviv/add-test-cases-for-optimistic-locking

Add test cases for optimistic locking
......@@ -62,8 +62,8 @@ def locking_enabled? #:nodoc:
def increment_lock
lock_col = self.class.locking_column
previous_lock_value = send(lock_col).to_i
send(lock_col + "=", previous_lock_value + 1)
previous_lock_value = send(lock_col)
send("#{lock_col}=", previous_lock_value + 1)
end
def _create_record(attribute_names = self.attribute_names, *)
......@@ -107,7 +107,8 @@ def _update_record(attribute_names = self.attribute_names)
# If something went wrong, revert the locking_column value.
rescue Exception
send(lock_col + "=", previous_lock_value.to_i)
send("#{lock_col}=", previous_lock_value.to_i)
raise
end
end
......
......@@ -167,6 +167,12 @@ def test_lock_new_when_explicitly_passing_nil
assert_equal 0, p1.lock_version
end
def test_lock_new_when_explicitly_passing_value
p1 = Person.new(first_name: "Douglas Adams", lock_version: 42)
p1.save!
assert_equal 42, p1.lock_version
end
def test_touch_existing_lock
p1 = Person.find(1)
assert_equal 0, p1.lock_version
......@@ -186,6 +192,19 @@ def test_touch_stale_object
end
end
def test_explicit_update_lock_column_raise_error
person = Person.find(1)
assert_raises(ActiveRecord::StaleObjectError) do
person.first_name = "Douglas Adams"
person.lock_version = 42
assert person.lock_version_changed?
person.save
end
end
def test_lock_column_name_existing
t1 = LegacyThing.find(1)
t2 = LegacyThing.find(1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册