@@ -305,6 +305,43 @@ def test_touch_existing_lock_without_default_should_work_with_null_in_the_databa
305
305
assert_equal [ "lock_version" , "updated_at" ] , t1 . saved_changes . keys . sort
306
306
end
307
307
308
+ def test_update_lock_version_to_nil_without_validation_or_constraint_raises_error
309
+ t1 = LockWithoutDefault . create! ( title : "title1" )
310
+ error = assert_raises ( RuntimeError ) { t1 . update ( lock_version : nil ) }
311
+
312
+ assert_match ( locking_column_nil_error_message , error . message )
313
+
314
+ error = assert_raises ( RuntimeError ) { t1 . update! ( lock_version : nil ) }
315
+
316
+ assert_match ( locking_column_nil_error_message , error . message )
317
+ end
318
+
319
+ def test_update_lock_version_to_nil_without_validation_raises
320
+ person = Person . find ( 1 )
321
+ error = assert_raises ( RuntimeError ) { person . update ( lock_version : nil ) }
322
+
323
+ assert_match ( locking_column_nil_error_message , error . message )
324
+
325
+ error = assert_raises ( RuntimeError ) { person . update! ( lock_version : nil ) }
326
+
327
+ assert_match ( locking_column_nil_error_message , error . message )
328
+ end
329
+
330
+ def test_update_lock_version_to_nil_with_validation_does_not_raise_runtime_lock_version_error
331
+ person = LockVersionValidatedPerson . find ( 1 )
332
+ assert_nothing_raised { person . update ( lock_version : nil ) }
333
+
334
+ assert_equal [ "is not a number" ] , person . errors [ :lock_version ]
335
+ end
336
+
337
+ def test_update_bang_lock_version_to_nil_with_validation_does_not_raise_runtime_lock_version_error
338
+ error = assert_raises ( ActiveRecord ::RecordInvalid ) do
339
+ LockVersionValidatedPerson . find ( 1 ) . update! ( lock_version : nil )
340
+ end
341
+
342
+ assert_equal [ "is not a number" ] , error . record . errors [ :lock_version ]
343
+ end
344
+
308
345
def test_touch_stale_object_with_lock_without_default
309
346
t1 = LockWithoutDefault . create! ( title : "title1" )
310
347
stale_object = LockWithoutDefault . find ( t1 . id )
@@ -557,6 +594,11 @@ def test_yaml_dumping_with_lock_column
557
594
558
595
assert_equal t1 . attributes , t2 . attributes
559
596
end
597
+
598
+ private
599
+ def locking_column_nil_error_message
600
+ /'lock_version' should not be set to `nil`/
601
+ end
560
602
end
561
603
562
604
class OptimisticLockingWithSchemaChangeTest < ActiveRecord ::TestCase
0 commit comments