2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/invoice"
|
|
|
|
require "models/line_item"
|
|
|
|
require "models/topic"
|
|
|
|
require "models/node"
|
|
|
|
require "models/tree"
|
2015-03-13 13:14:55 -04:00
|
|
|
|
|
|
|
class TouchLaterTest < ActiveRecord::TestCase
|
2015-06-25 08:23:06 -04:00
|
|
|
fixtures :nodes, :trees
|
2015-03-13 13:14:55 -04:00
|
|
|
|
2019-04-05 09:27:23 -04:00
|
|
|
def test_touch_later_raise_if_non_persisted
|
2015-03-13 13:14:55 -04:00
|
|
|
invoice = Invoice.new
|
|
|
|
Invoice.transaction do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate invoice, :persisted?
|
2015-03-13 13:14:55 -04:00
|
|
|
assert_raises(ActiveRecord::ActiveRecordError) do
|
|
|
|
invoice.touch_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_later_dont_set_dirty_attributes
|
|
|
|
invoice = Invoice.create!
|
|
|
|
invoice.touch_later
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate invoice, :changed?
|
2015-03-13 13:14:55 -04:00
|
|
|
end
|
|
|
|
|
2016-08-16 09:20:50 -04:00
|
|
|
def test_touch_later_respects_no_touching_policy
|
|
|
|
time = Time.now.utc - 25.days
|
|
|
|
topic = Topic.create!(updated_at: time, created_at: time)
|
|
|
|
Topic.no_touching do
|
|
|
|
topic.touch_later
|
|
|
|
end
|
|
|
|
assert_equal time.to_i, topic.updated_at.to_i
|
|
|
|
end
|
|
|
|
|
2015-03-13 13:14:55 -04:00
|
|
|
def test_touch_later_update_the_attributes
|
|
|
|
time = Time.now.utc - 25.days
|
|
|
|
topic = Topic.create!(updated_at: time, created_at: time)
|
|
|
|
assert_equal time.to_i, topic.updated_at.to_i
|
|
|
|
assert_equal time.to_i, topic.created_at.to_i
|
|
|
|
|
|
|
|
Topic.transaction do
|
|
|
|
topic.touch_later(:created_at)
|
|
|
|
assert_not_equal time.to_i, topic.updated_at.to_i
|
|
|
|
assert_not_equal time.to_i, topic.created_at.to_i
|
|
|
|
|
|
|
|
assert_equal time.to_i, topic.reload.updated_at.to_i
|
|
|
|
assert_equal time.to_i, topic.reload.created_at.to_i
|
|
|
|
end
|
|
|
|
assert_not_equal time.to_i, topic.reload.updated_at.to_i
|
|
|
|
assert_not_equal time.to_i, topic.reload.created_at.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_touches_immediately
|
|
|
|
time = Time.now.utc - 25.days
|
|
|
|
topic = Topic.create!(updated_at: time, created_at: time)
|
|
|
|
assert_equal time.to_i, topic.updated_at.to_i
|
|
|
|
assert_equal time.to_i, topic.created_at.to_i
|
|
|
|
|
|
|
|
Topic.transaction do
|
|
|
|
topic.touch_later(:created_at)
|
|
|
|
topic.touch
|
|
|
|
|
|
|
|
assert_not_equal time, topic.reload.updated_at
|
|
|
|
assert_not_equal time, topic.reload.created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_later_an_association_dont_autosave_parent
|
|
|
|
time = Time.now.utc - 25.days
|
|
|
|
line_item = LineItem.create!(amount: 1)
|
|
|
|
invoice = Invoice.create!(line_items: [line_item])
|
|
|
|
invoice.touch(time: time)
|
|
|
|
|
|
|
|
Invoice.transaction do
|
|
|
|
line_item.update(amount: 2)
|
|
|
|
assert_equal time.to_i, invoice.reload.updated_at.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_equal time.to_i, invoice.updated_at.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_touches_immediately_with_a_custom_time
|
2015-09-23 11:33:43 -04:00
|
|
|
time = (Time.now.utc - 25.days).change(nsec: 0)
|
2015-03-13 13:14:55 -04:00
|
|
|
topic = Topic.create!(updated_at: time, created_at: time)
|
|
|
|
assert_equal time, topic.updated_at
|
|
|
|
assert_equal time, topic.created_at
|
|
|
|
|
|
|
|
Topic.transaction do
|
|
|
|
topic.touch_later(:created_at)
|
|
|
|
time = Time.now.utc - 2.days
|
|
|
|
topic.touch(time: time)
|
|
|
|
|
|
|
|
assert_equal time.to_i, topic.reload.updated_at.to_i
|
|
|
|
assert_equal time.to_i, topic.reload.created_at.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_touch_later_dont_hit_the_db
|
|
|
|
invoice = Invoice.create!
|
2018-10-04 14:07:12 -04:00
|
|
|
assert_no_queries do
|
2015-03-13 13:14:55 -04:00
|
|
|
invoice.touch_later
|
|
|
|
end
|
|
|
|
end
|
2015-06-25 08:23:06 -04:00
|
|
|
|
|
|
|
def test_touching_three_deep
|
|
|
|
previous_tree_updated_at = trees(:root).updated_at
|
|
|
|
previous_grandparent_updated_at = nodes(:grandparent).updated_at
|
|
|
|
previous_parent_updated_at = nodes(:parent_a).updated_at
|
|
|
|
previous_child_updated_at = nodes(:child_one_of_a).updated_at
|
|
|
|
|
2015-12-12 21:29:37 -05:00
|
|
|
travel 5.seconds do
|
|
|
|
Node.create! parent: nodes(:child_one_of_a), tree: trees(:root)
|
|
|
|
end
|
2015-06-25 08:23:06 -04:00
|
|
|
|
|
|
|
assert_not_equal nodes(:child_one_of_a).reload.updated_at, previous_child_updated_at
|
|
|
|
assert_not_equal nodes(:parent_a).reload.updated_at, previous_parent_updated_at
|
|
|
|
assert_not_equal nodes(:grandparent).reload.updated_at, previous_grandparent_updated_at
|
|
|
|
assert_not_equal trees(:root).reload.updated_at, previous_tree_updated_at
|
|
|
|
end
|
2015-03-13 13:14:55 -04:00
|
|
|
end
|