aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/timestamp_test.rb
blob: 39b40e34118fb32d61bf4bdee0ff54ad296a0fa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
require "cases/helper"
require "support/ddl_helper"
require "models/developer"
require "models/computer"
require "models/owner"
require "models/pet"
require "models/toy"
require "models/car"
require "models/task"

class TimestampTest < ActiveRecord::TestCase
  fixtures :developers, :owners, :pets, :toys, :cars, :tasks

  def setup
    @developer = Developer.first
    @owner = Owner.first
    @developer.update_columns(updated_at: Time.now.prev_month)
    @previously_updated_at = @developer.updated_at
  end

  def test_saving_a_changed_record_updates_its_timestamp
    @developer.name = "Jack Bauer"
    @developer.save!

    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_saving_a_unchanged_record_doesnt_update_its_timestamp
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_a_record_updates_its_timestamp
    previous_salary = @developer.salary
    @developer.salary = previous_salary + 10000
    @developer.touch

    assert_not_equal @previously_updated_at, @developer.updated_at
    assert_equal previous_salary + 10000, @developer.salary
    assert @developer.salary_changed?, "developer salary should have changed"
    assert @developer.changed?, "developer should be marked as changed"
    @developer.reload
    assert_equal previous_salary, @developer.salary
  end

  def test_touching_a_record_with_default_scope_that_excludes_it_updates_its_timestamp
    developer = @developer.becomes(DeveloperCalledJamis)

    developer.touch
    assert_not_equal @previously_updated_at, developer.updated_at
    developer.reload
    assert_not_equal @previously_updated_at, developer.updated_at
  end

  def test_saving_when_record_timestamps_is_false_doesnt_update_its_timestamp
    Developer.record_timestamps = false
    @developer.name = "John Smith"
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  ensure
    Developer.record_timestamps = true
  end

  def test_saving_when_instance_record_timestamps_is_false_doesnt_update_its_timestamp
    @developer.record_timestamps = false
    assert Developer.record_timestamps

    @developer.name = "John Smith"
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_updates_timestamp_with_given_time
    previously_updated_at = @developer.updated_at
    new_time = Time.utc(2015, 2, 16, 0, 0, 0)
    @developer.touch(time: new_time)

    assert_not_equal previously_updated_at, @developer.updated_at
    assert_equal new_time, @developer.updated_at
  end

  def test_touching_an_attribute_updates_timestamp
    previously_created_at = @developer.created_at
    travel(1.second) do
      @developer.touch(:created_at)
    end

    assert !@developer.created_at_changed? , "created_at should not be changed"
    assert !@developer.changed?, "record should not be changed"
    assert_not_equal previously_created_at, @developer.created_at
    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_an_attribute_updates_it
    task = Task.first
    previous_value = task.ending
    task.touch(:ending)

    now = Time.now.change(usec: 0)

    assert_not_equal previous_value, task.ending
    assert_in_delta now, task.ending, 1
  end

  def test_touching_an_attribute_updates_timestamp_with_given_time
    previously_updated_at = @developer.updated_at
    previously_created_at = @developer.created_at
    new_time = Time.utc(2015, 2, 16, 4, 54, 0)
    @developer.touch(:created_at, time: new_time)

    assert_not_equal previously_created_at, @developer.created_at
    assert_not_equal previously_updated_at, @developer.updated_at
    assert_equal new_time, @developer.created_at
    assert_equal new_time, @developer.updated_at
  end

  def test_touching_many_attributes_updates_them
    task = Task.first
    previous_starting = task.starting
    previous_ending = task.ending
    task.touch(:starting, :ending)

    now = Time.now.change(usec: 0)

    assert_not_equal previous_starting, task.starting
    assert_not_equal previous_ending, task.ending
    assert_in_delta now, task.starting, 1
    assert_in_delta now, task.ending, 1
  end

  def test_touching_a_record_without_timestamps_is_unexceptional
    assert_nothing_raised { Car.first.touch }
  end

  def test_touching_a_no_touching_object
    Developer.no_touching do
      assert @developer.no_touching?
      assert !@owner.no_touching?
      @developer.touch
    end

    assert !@developer.no_touching?
    assert !@owner.no_touching?
    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_related_objects
    @owner = Owner.first
    @previously_updated_at = @owner.updated_at

    Owner.no_touching do
      @owner.pets.first.touch
    end

    assert_equal @previously_updated_at, @owner.updated_at
  end

  def test_global_no_touching
    ActiveRecord::Base.no_touching do
      assert @developer.no_touching?
      assert @owner.no_touching?
      @developer.touch
    end

    assert !@developer.no_touching?
    assert !@owner.no_touching?
    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_no_touching_threadsafe
    Thread.new do
      Developer.no_touching do
        assert @developer.no_touching?

        sleep(1)
      end
    end

    assert !@developer.no_touching?
  end

  def test_no_touching_with_callbacks
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "developers"

      attr_accessor :after_touch_called

      after_touch do |user|
        user.after_touch_called = true
      end
    end

    developer = klass.first

    klass.no_touching do
      developer.touch
      assert_not developer.after_touch_called
    end
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    travel(1.second) do
      pet.name = "Fluffy the Third"
      pet.save
    end

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_destroying_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    travel(1.second) do
      pet.destroy
    end

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_saving_a_new_record_belonging_to_invalid_parent_with_touch_should_not_raise_exception
    klass = Class.new(Owner) do
      def self.name; "Owner"; end
      validate { errors.add(:base, :invalid) }
    end

    pet = Pet.new(owner: klass.new)
    pet.save!

    assert pet.owner.new_record?
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_a_specific_attribute_the_parent_should_update_that_attribute
    klass = Class.new(ActiveRecord::Base) do
      def self.name; "Pet"; end
      belongs_to :owner, touch: :happy_at
    end

    pet   = klass.first
    owner = pet.owner
    previously_owner_happy_at = owner.happy_at

    pet.name = "Fluffy the Third"
    pet.save

    assert_not_equal previously_owner_happy_at, pet.owner.happy_at
  end

  def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_update_the_parent
    klass = Class.new(ActiveRecord::Base) do
      def self.name; "Pet"; end
      belongs_to :owner, counter_cache: :use_count, touch: true
    end

    pet = klass.first
    owner = pet.owner
    owner.update_columns(happy_at: 3.days.ago)
    previously_owner_updated_at = owner.updated_at

    travel(1.second) do
      pet.name = "I'm a parrot"
      pet.save
    end

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_touching_a_record_touches_parent_record_and_grandparent_record
    klass = Class.new(ActiveRecord::Base) do
      def self.name; "Toy"; end
      belongs_to :pet, touch: true
    end

    toy = klass.first
    pet = toy.pet
    owner = pet.owner
    time = 3.days.ago

    owner.update_columns(updated_at: time)
    toy.touch
    owner.reload

    assert_not_equal time, owner.updated_at
  end

  def test_touching_a_record_touches_polymorphic_record
    klass = Class.new(ActiveRecord::Base) do
      def self.name; "Toy"; end
    end

    wheel_klass = Class.new(ActiveRecord::Base) do
      def self.name; "Wheel"; end
      belongs_to :wheelable, polymorphic: true, touch: true
    end

    toy = klass.first
    time = 3.days.ago
    toy.update_columns(updated_at: time)

    wheel = wheel_klass.new
    wheel.wheelable = toy
    wheel.save
    wheel.touch

    assert_not_equal time, toy.updated_at
  end

  def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record
    klass = Class.new(ActiveRecord::Base) do
      def self.name; "Toy"; end
      belongs_to :pet, touch: true
    end

    toy1 = klass.find(1)
    old_pet = toy1.pet

    toy2 = klass.find(2)
    new_pet = toy2.pet
    time = 3.days.ago.at_beginning_of_hour

    old_pet.update_columns(updated_at: time)
    new_pet.update_columns(updated_at: time)

    toy1.pet = new_pet
    toy1.save!

    old_pet.reload
    new_pet.reload

    assert_not_equal time, new_pet.updated_at
    assert_not_equal time, old_pet.updated_at
  end

  def test_changing_parent_of_a_record_touches_both_new_and_old_polymorphic_parent_record_changes_within_same_class
    car_class = Class.new(ActiveRecord::Base) do
      def self.name; "Car"; end
    end

    wheel_class = Class.new(ActiveRecord::Base) do
      def self.name; "Wheel"; end
      belongs_to :wheelable, polymorphic: true, touch: true
    end

    car1 = car_class.find(1)
    car2 = car_class.find(2)

    wheel = wheel_class.create!(wheelable: car1)

    time = 3.days.ago.at_beginning_of_hour

    car1.update_columns(updated_at: time)
    car2.update_columns(updated_at: time)

    wheel.wheelable = car2
    wheel.save!

    assert_not_equal time, car1.reload.updated_at
    assert_not_equal time, car2.reload.updated_at
  end

  def test_changing_parent_of_a_record_touches_both_new_and_old_polymorphic_parent_record_changes_with_other_class
    car_class = Class.new(ActiveRecord::Base) do
      def self.name; "Car"; end
    end

    toy_class = Class.new(ActiveRecord::Base) do
      def self.name; "Toy"; end
    end

    wheel_class = Class.new(ActiveRecord::Base) do
      def self.name; "Wheel"; end
      belongs_to :wheelable, polymorphic: true, touch: true
    end

    car = car_class.find(1)
    toy = toy_class.find(3)

    wheel = wheel_class.create!(wheelable: car)

    time = 3.days.ago.at_beginning_of_hour

    car.update_columns(updated_at: time)
    toy.update_columns(updated_at: time)

    wheel.wheelable = toy
    wheel.save!

    assert_not_equal time, car.reload.updated_at
    assert_not_equal time, toy.reload.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_column_values_are_present_in_the_callbacks
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "people"

      before_create do
        self.born_at = created_at
      end
    end

    person = klass.create first_name: "David"
    assert_not_equal person.born_at, nil
  end

  def test_timestamp_attributes_for_create_in_model
    toy = Toy.first
    assert_equal ["created_at"], toy.send(:timestamp_attributes_for_create_in_model)
  end

  def test_timestamp_attributes_for_update_in_model
    toy = Toy.first
    assert_equal ["updated_at"], toy.send(:timestamp_attributes_for_update_in_model)
  end

  def test_all_timestamp_attributes_in_model
    toy = Toy.first
    assert_equal ["created_at", "updated_at"], toy.send(:all_timestamp_attributes_in_model)
  end

  def test_index_is_created_for_both_timestamps
    ActiveRecord::Base.connection.create_table(:foos, force: true) do |t|
      t.timestamps null: true, index: true
    end

    indexes = ActiveRecord::Base.connection.indexes("foos")
    assert_equal ["created_at", "updated_at"], indexes.flat_map(&:columns).sort
  ensure
    ActiveRecord::Base.connection.drop_table(:foos)
  end
end

class TimestampsWithoutTransactionTest < ActiveRecord::TestCase
  include DdlHelper
  self.use_transactional_tests = false

  class TimestampAttributePost < ActiveRecord::Base
    attr_accessor :created_at, :updated_at
  end

  def test_do_not_write_timestamps_on_save_if_they_are_not_attributes
    with_example_table ActiveRecord::Base.connection, "timestamp_attribute_posts", "id integer primary key" do
      post = TimestampAttributePost.new(id: 1)
      post.save! # should not try to assign and persist created_at, updated_at
      assert_nil post.created_at
      assert_nil post.updated_at
    end
  end
end