|
4 | 4 | require "models/author"
|
5 | 5 | require "models/book"
|
6 | 6 | require "models/cart"
|
| 7 | +require "models/developer" |
| 8 | +require "models/ship" |
7 | 9 | require "models/speedometer"
|
8 | 10 | require "models/subscription"
|
9 | 11 | require "models/subscriber"
|
@@ -393,6 +395,148 @@ def test_upsert_all_uses_given_updated_on_over_implicit_updated_on
|
393 | 395 | assert_equal updated_on, Book.find(101).updated_on
|
394 | 396 | end
|
395 | 397 |
|
| 398 | + def test_upsert_all_implicitly_sets_timestamps_on_create_when_model_record_timestamps_is_true |
| 399 | + with_record_timestamps(Ship, true) do |
| 400 | + Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }] |
| 401 | + |
| 402 | + ship = Ship.find(101) |
| 403 | + assert_equal Time.new.year, ship.created_at.year |
| 404 | + assert_equal Time.new.year, ship.created_on.year |
| 405 | + assert_equal Time.new.year, ship.updated_at.year |
| 406 | + assert_equal Time.new.year, ship.updated_on.year |
| 407 | + end |
| 408 | + end |
| 409 | + |
| 410 | + def test_upsert_all_does_not_implicitly_set_timestamps_on_create_when_model_record_timestamps_is_true_but_overridden |
| 411 | + with_record_timestamps(Ship, true) do |
| 412 | + Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }], record_timestamps: false |
| 413 | + |
| 414 | + ship = Ship.find(101) |
| 415 | + assert_nil ship.created_at |
| 416 | + assert_nil ship.created_on |
| 417 | + assert_nil ship.updated_at |
| 418 | + assert_nil ship.updated_on |
| 419 | + end |
| 420 | + end |
| 421 | + |
| 422 | + def test_upsert_all_does_not_implicitly_set_timestamps_on_create_when_model_record_timestamps_is_false |
| 423 | + with_record_timestamps(Ship, false) do |
| 424 | + Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }] |
| 425 | + |
| 426 | + ship = Ship.find(101) |
| 427 | + assert_nil ship.created_at |
| 428 | + assert_nil ship.created_on |
| 429 | + assert_nil ship.updated_at |
| 430 | + assert_nil ship.updated_on |
| 431 | + end |
| 432 | + end |
| 433 | + |
| 434 | + def test_upsert_all_implicitly_sets_timestamps_on_create_when_model_record_timestamps_is_false_but_overridden |
| 435 | + with_record_timestamps(Ship, false) do |
| 436 | + Ship.upsert_all [{ id: 101, name: "RSS Boaty McBoatface" }], record_timestamps: true |
| 437 | + |
| 438 | + ship = Ship.find(101) |
| 439 | + assert_equal Time.now.year, ship.created_at.year |
| 440 | + assert_equal Time.now.year, ship.created_on.year |
| 441 | + assert_equal Time.now.year, ship.updated_at.year |
| 442 | + assert_equal Time.now.year, ship.updated_on.year |
| 443 | + end |
| 444 | + end |
| 445 | + |
| 446 | + def test_upsert_all_respects_created_at_precision_when_touched_implicitly |
| 447 | + skip unless supports_datetime_with_precision? |
| 448 | + |
| 449 | + Book.upsert_all [{ id: 101, name: "Out of the Silent Planet", published_on: Date.new(1938, 4, 8) }] |
| 450 | + |
| 451 | + assert_not_predicate Book.find(101).created_at.usec, :zero?, "created_at should have sub-second precision" |
| 452 | + end |
| 453 | + |
| 454 | + def test_upsert_all_implicitly_sets_timestamps_on_update_when_model_record_timestamps_is_true |
| 455 | + skip unless supports_insert_on_duplicate_update? |
| 456 | + |
| 457 | + with_record_timestamps(Ship, true) do |
| 458 | + travel_to(Date.new(2016, 4, 17)) { Ship.create! id: 101, name: "RSS Boaty McBoatface" } |
| 459 | + |
| 460 | + Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }] |
| 461 | + |
| 462 | + ship = Ship.find(101) |
| 463 | + assert_equal 2016, ship.created_at.year |
| 464 | + assert_equal 2016, ship.created_on.year |
| 465 | + assert_equal Time.now.year, ship.updated_at.year |
| 466 | + assert_equal Time.now.year, ship.updated_on.year |
| 467 | + end |
| 468 | + end |
| 469 | + |
| 470 | + def test_upsert_all_does_not_implicitly_set_timestamps_on_update_when_model_record_timestamps_is_true_but_overridden |
| 471 | + skip unless supports_insert_on_duplicate_update? |
| 472 | + |
| 473 | + with_record_timestamps(Ship, true) do |
| 474 | + travel_to(Date.new(2016, 4, 17)) { Ship.create! id: 101, name: "RSS Boaty McBoatface" } |
| 475 | + |
| 476 | + Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }], record_timestamps: false |
| 477 | + |
| 478 | + ship = Ship.find(101) |
| 479 | + assert_equal 2016, ship.created_at.year |
| 480 | + assert_equal 2016, ship.created_on.year |
| 481 | + assert_equal 2016, ship.updated_at.year |
| 482 | + assert_equal 2016, ship.updated_on.year |
| 483 | + end |
| 484 | + end |
| 485 | + |
| 486 | + def test_upsert_all_does_not_implicitly_set_timestamps_on_update_when_model_record_timestamps_is_false |
| 487 | + skip unless supports_insert_on_duplicate_update? |
| 488 | + |
| 489 | + with_record_timestamps(Ship, false) do |
| 490 | + Ship.create! id: 101, name: "RSS Boaty McBoatface" |
| 491 | + |
| 492 | + Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }] |
| 493 | + |
| 494 | + ship = Ship.find(101) |
| 495 | + assert_nil ship.created_at |
| 496 | + assert_nil ship.created_on |
| 497 | + assert_nil ship.updated_at |
| 498 | + assert_nil ship.updated_on |
| 499 | + end |
| 500 | + end |
| 501 | + |
| 502 | + def test_upsert_all_implicitly_sets_timestamps_on_update_when_model_record_timestamps_is_false_but_overridden |
| 503 | + skip unless supports_insert_on_duplicate_update? |
| 504 | + |
| 505 | + with_record_timestamps(Ship, false) do |
| 506 | + Ship.create! id: 101, name: "RSS Boaty McBoatface" |
| 507 | + |
| 508 | + Ship.upsert_all [{ id: 101, name: "RSS Sir David Attenborough" }], record_timestamps: true |
| 509 | + |
| 510 | + ship = Ship.find(101) |
| 511 | + assert_nil ship.created_at |
| 512 | + assert_nil ship.created_on |
| 513 | + assert_equal Time.now.year, ship.updated_at.year |
| 514 | + assert_equal Time.now.year, ship.updated_on.year |
| 515 | + end |
| 516 | + end |
| 517 | + |
| 518 | + def test_upsert_all_implicitly_sets_timestamps_even_when_columns_are_aliased |
| 519 | + skip unless supports_insert_on_duplicate_update? |
| 520 | + |
| 521 | + Developer.upsert_all [{ id: 101, name: "Alice" }] |
| 522 | + alice = Developer.find(101) |
| 523 | + |
| 524 | + assert_not_nil alice.created_at |
| 525 | + assert_not_nil alice.created_on |
| 526 | + assert_not_nil alice.updated_at |
| 527 | + assert_not_nil alice.updated_on |
| 528 | + |
| 529 | + alice.update!(created_at: nil, created_on: nil, updated_at: nil, updated_on: nil) |
| 530 | + |
| 531 | + Developer.upsert_all [{ id: alice.id, name: alice.name, salary: alice.salary * 2 }] |
| 532 | + alice.reload |
| 533 | + |
| 534 | + assert_nil alice.created_at |
| 535 | + assert_nil alice.created_on |
| 536 | + assert_not_nil alice.updated_at |
| 537 | + assert_not_nil alice.updated_on |
| 538 | + end |
| 539 | + |
396 | 540 | def test_insert_all_raises_on_unknown_attribute
|
397 | 541 | assert_raise ActiveRecord::UnknownAttributeError do
|
398 | 542 | Book.insert_all! [{ unknown_attribute: "Test" }]
|
@@ -515,4 +659,12 @@ def capture_log_output
|
515 | 659 | ActiveRecord::Base.logger = old_logger
|
516 | 660 | end
|
517 | 661 | end
|
| 662 | + |
| 663 | + def with_record_timestamps(model, value) |
| 664 | + original = model.record_timestamps |
| 665 | + model.record_timestamps = value |
| 666 | + yield |
| 667 | + ensure |
| 668 | + model.record_timestamps = original |
| 669 | + end |
518 | 670 | end
|
0 commit comments