Skip to content

Commit eba7595

Browse files
committed
Add tests for existing timestamp behaviour
These tests document the fact that upsert_all - never sets creation timestamps - always sets update timestamps, on update regardless of record_timestamps config.
1 parent 7c618b8 commit eba7595

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

activerecord/test/cases/insert_all_test.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "models/author"
55
require "models/book"
66
require "models/cart"
7+
require "models/ship"
78
require "models/speedometer"
89
require "models/subscription"
910
require "models/subscriber"
@@ -393,6 +394,62 @@ def test_upsert_all_uses_given_updated_on_over_implicit_updated_on
393394
assert_equal updated_on, Book.find(101).updated_on
394395
end
395396

397+
def test_upsert_all_does_not_implicitly_set_timestamps_on_create_even_when_model_record_timestamps_is_true
398+
with_record_timestamps(Ship, true) do
399+
Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }]
400+
401+
ship = Ship.find(101)
402+
assert_nil ship.created_at
403+
assert_nil ship.created_on
404+
assert_nil ship.updated_at
405+
assert_nil ship.updated_on
406+
end
407+
end
408+
409+
def test_upsert_all_does_not_implicitly_set_timestamps_on_create_when_model_record_timestamps_is_false
410+
with_record_timestamps(Ship, false) do
411+
Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }]
412+
413+
ship = Ship.find(101)
414+
assert_nil ship.created_at
415+
assert_nil ship.created_on
416+
assert_nil ship.updated_at
417+
assert_nil ship.updated_on
418+
end
419+
end
420+
421+
def test_upsert_all_implicitly_sets_timestamps_on_update_when_model_record_timestamps_is_true
422+
skip unless supports_insert_on_duplicate_update?
423+
424+
with_record_timestamps(Ship, true) do
425+
travel_to(Date.new(2016, 4, 17)) { Ship.create! id: 101, name: "RSS Boaty McBoatface" }
426+
427+
Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }]
428+
429+
ship = Ship.find(101)
430+
assert_equal 2016, ship.created_at.year
431+
assert_equal 2016, ship.created_on.year
432+
assert_equal Time.now.year, ship.updated_at.year
433+
assert_equal Time.now.year, ship.updated_on.year
434+
end
435+
end
436+
437+
def test_upsert_all_implicitly_sets_timestamps_on_update_even_when_model_record_timestamps_is_false
438+
skip unless supports_insert_on_duplicate_update?
439+
440+
with_record_timestamps(Ship, false) do
441+
Ship.create! id: 101, name: "RSS Boaty McBoatface"
442+
443+
Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }]
444+
445+
ship = Ship.find(101)
446+
assert_nil ship.created_at
447+
assert_nil ship.created_on
448+
assert_equal Time.now.year, ship.updated_at.year
449+
assert_equal Time.now.year, ship.updated_on.year
450+
end
451+
end
452+
396453
def test_insert_all_raises_on_unknown_attribute
397454
assert_raise ActiveRecord::UnknownAttributeError do
398455
Book.insert_all! [{ unknown_attribute: "Test" }]
@@ -515,4 +572,12 @@ def capture_log_output
515572
ActiveRecord::Base.logger = old_logger
516573
end
517574
end
575+
576+
def with_record_timestamps(model, value)
577+
original = model.record_timestamps
578+
model.record_timestamps = value
579+
yield
580+
ensure
581+
model.record_timestamps = original
582+
end
518583
end

0 commit comments

Comments
 (0)