2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2007-09-10 23:25:59 -04:00
|
|
|
|
|
|
|
class Mixin < ActiveRecord::Base
|
|
|
|
end
|
2004-12-14 19:46:26 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
# Let us control what Time.now returns for the TouchTest suite
|
|
|
|
class Time
|
|
|
|
@@forced_now_time = nil
|
|
|
|
cattr_accessor :forced_now_time
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
class << self
|
|
|
|
def now_with_forcing
|
|
|
|
if @@forced_now_time
|
|
|
|
@@forced_now_time
|
|
|
|
else
|
|
|
|
now_without_forcing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method_chain :now, :forcing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-15 21:49:18 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class TouchTest < ActiveRecord::TestCase
|
2004-12-14 19:46:26 -05:00
|
|
|
fixtures :mixins
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
def setup
|
|
|
|
Time.forced_now_time = Time.now
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
def teardown
|
|
|
|
Time.forced_now_time = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_mocking
|
|
|
|
five_minutes_ago = 5.minutes.ago
|
|
|
|
Time.forced_now_time = five_minutes_ago
|
|
|
|
assert_equal five_minutes_ago, Time.now
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
Time.forced_now_time = nil
|
|
|
|
assert_not_equal five_minutes_ago, Time.now
|
|
|
|
end
|
2005-11-10 01:24:33 -05:00
|
|
|
|
2004-12-14 19:46:26 -05:00
|
|
|
def test_update
|
2005-11-10 01:24:33 -05:00
|
|
|
stamped = Mixin.new
|
|
|
|
|
2004-12-19 08:01:47 -05:00
|
|
|
assert_nil stamped.updated_at
|
|
|
|
assert_nil stamped.created_at
|
|
|
|
stamped.save
|
2006-09-13 20:50:42 -04:00
|
|
|
assert_equal Time.now, stamped.updated_at
|
|
|
|
assert_equal Time.now, stamped.created_at
|
2005-11-10 01:24:33 -05:00
|
|
|
end
|
2004-12-14 19:46:26 -05:00
|
|
|
|
|
|
|
def test_create
|
2006-09-13 20:50:42 -04:00
|
|
|
obj = Mixin.create
|
|
|
|
assert_equal Time.now, obj.updated_at
|
|
|
|
assert_equal Time.now, obj.created_at
|
2005-11-10 01:24:33 -05:00
|
|
|
end
|
2004-12-19 08:01:47 -05:00
|
|
|
|
|
|
|
def test_many_updates
|
2005-11-10 01:24:33 -05:00
|
|
|
stamped = Mixin.new
|
2004-12-19 08:01:47 -05:00
|
|
|
|
|
|
|
assert_nil stamped.updated_at
|
|
|
|
assert_nil stamped.created_at
|
|
|
|
stamped.save
|
2006-09-13 20:50:42 -04:00
|
|
|
assert_equal Time.now, stamped.created_at
|
|
|
|
assert_equal Time.now, stamped.updated_at
|
2005-11-10 01:24:33 -05:00
|
|
|
|
2004-12-19 08:01:47 -05:00
|
|
|
old_updated_at = stamped.updated_at
|
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
Time.forced_now_time = 5.minutes.from_now
|
2008-03-30 23:57:25 -04:00
|
|
|
stamped.lft_will_change!
|
2005-11-10 01:24:33 -05:00
|
|
|
stamped.save
|
2004-12-19 08:01:47 -05:00
|
|
|
|
2006-09-13 20:50:42 -04:00
|
|
|
assert_equal Time.now, stamped.updated_at
|
|
|
|
assert_equal old_updated_at, stamped.created_at
|
2004-12-19 08:01:47 -05:00
|
|
|
end
|
|
|
|
|
2004-12-16 10:21:16 -05:00
|
|
|
def test_create_turned_off
|
|
|
|
Mixin.record_timestamps = false
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
mixin = Mixin.new
|
2004-12-16 10:21:16 -05:00
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
assert_nil mixin.updated_at
|
|
|
|
mixin.save
|
|
|
|
assert_nil mixin.updated_at
|
|
|
|
|
|
|
|
# Make sure Mixin.record_timestamps gets reset, even if this test fails,
|
|
|
|
# so that other tests do not fail because Mixin.record_timestamps == false
|
|
|
|
rescue Exception => e
|
|
|
|
raise e
|
|
|
|
ensure
|
2004-12-16 10:21:16 -05:00
|
|
|
Mixin.record_timestamps = true
|
|
|
|
end
|
2005-09-09 04:32:47 -04:00
|
|
|
|
2007-09-11 02:47:27 -04:00
|
|
|
end
|