Permalink
Browse files

Improve `belongs_to touch: true` timestamp test

Round off time to a whole second value to compensate for databases
that don't support fractional timestamps. Also add a assertion to
check that the old record is touched when the association is cleared.
  • Loading branch information...
1 parent dce398d commit 155f86675ce6f2995a61eee9e3d814c974d2bec8 @pixeltrix pixeltrix committed Apr 5, 2013
Showing with 21 additions and 7 deletions.
  1. +21 −7 activerecord/test/cases/timestamp_test.rb
@@ -187,17 +187,11 @@ def self.name; 'Toy'; end
toy2 = klass.find(2)
new_pet = toy2.pet
- time = 3.days.ago
+ time = 3.days.ago.at_beginning_of_hour
old_pet.update_columns(updated_at: time)
new_pet.update_columns(updated_at: time)
- old_pet.reload
- new_pet.reload
-
- assert_equal time, new_pet.updated_at
- assert_equal time, old_pet.updated_at
-
toy1.pet = new_pet
toy1.save!
@@ -208,6 +202,26 @@ def self.name; 'Toy'; end
assert_not_equal time, old_pet.updated_at
end
+ def test_clearing_association_touches_the_old_record
+ klass = Class.new(ActiveRecord::Base) do
+ def self.name; 'Toy'; end
+ belongs_to :pet, touch: true
+ end
+
+ toy = klass.find(1)
+ pet = toy.pet
+ time = 3.days.ago.at_beginning_of_hour
+
+ pet.update_columns(updated_at: time)
+
+ toy.pet = nil
+ toy.save!
+
+ pet.reload
+
+ assert_not_equal time, pet.updated_at
+ end
+
def test_timestamp_attributes_for_create
toy = Toy.first
assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]

1 comment on commit 155f866

And we are 💚, yay!

Please sign in to comment.