|
4 | 4 | require "models/author"
|
5 | 5 | require "models/book"
|
6 | 6 | require "models/cart"
|
| 7 | +require "models/ship" |
7 | 8 | require "models/speedometer"
|
8 | 9 | require "models/subscription"
|
9 | 10 | require "models/subscriber"
|
@@ -393,6 +394,62 @@ def test_upsert_all_uses_given_updated_on_over_implicit_updated_on
|
393 | 394 | assert_equal updated_on, Book.find(101).updated_on
|
394 | 395 | end
|
395 | 396 |
|
| 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 | + |
396 | 453 | def test_insert_all_raises_on_unknown_attribute
|
397 | 454 | assert_raise ActiveRecord::UnknownAttributeError do
|
398 | 455 | Book.insert_all! [{ unknown_attribute: "Test" }]
|
@@ -515,4 +572,12 @@ def capture_log_output
|
515 | 572 | ActiveRecord::Base.logger = old_logger
|
516 | 573 | end
|
517 | 574 | 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 |
518 | 583 | end
|
0 commit comments