@@ -44,10 +44,88 @@ class CacheMeWithVersion < ActiveRecord::Base
4444
4545 test "cache_key_with_version always has both key and version" do
4646 r1 = CacheMeWithVersion . create
47- assert_equal "active_record/cache_key_test/cache_me_with_versions/#{ r1 . id } -#{ r1 . updated_at . to_s ( :usec ) } " , r1 . cache_key_with_version
47+ assert_equal "active_record/cache_key_test/cache_me_with_versions/#{ r1 . id } -#{ r1 . updated_at . utc . to_s ( :usec ) } " , r1 . cache_key_with_version
4848
4949 r2 = CacheMe . create
50- assert_equal "active_record/cache_key_test/cache_mes/#{ r2 . id } -#{ r2 . updated_at . to_s ( :usec ) } " , r2 . cache_key_with_version
50+ assert_equal "active_record/cache_key_test/cache_mes/#{ r2 . id } -#{ r2 . updated_at . utc . to_s ( :usec ) } " , r2 . cache_key_with_version
51+ end
52+
53+ test "cache_version is the same when it comes from the DB or from the user" do
54+ skip ( "Mysql2 does not return a string value for updated_at" ) if current_adapter? ( :Mysql2Adapter )
55+
56+ record = CacheMeWithVersion . create
57+ record_from_db = CacheMeWithVersion . find ( record . id )
58+ assert_not_called ( record_from_db , :updated_at ) do
59+ record_from_db . cache_version
60+ end
61+
62+ assert_equal record . cache_version , record_from_db . cache_version
63+ end
64+
65+ test "cache_version does not truncate zeros when timestamp ends in zeros" do
66+ skip ( "Mysql2 does not return a string value for updated_at" ) if current_adapter? ( :Mysql2Adapter )
67+
68+ travel_to Time . now . beginning_of_day do
69+ record = CacheMeWithVersion . create
70+ record_from_db = CacheMeWithVersion . find ( record . id )
71+ assert_not_called ( record_from_db , :updated_at ) do
72+ record_from_db . cache_version
73+ end
74+
75+ assert_equal record . cache_version , record_from_db . cache_version
76+ end
77+ end
78+
79+ test "cache_version calls updated_at when the value is generated at create time" do
80+ record = CacheMeWithVersion . create
81+ assert_called ( record , :updated_at ) do
82+ record . cache_version
83+ end
84+ end
85+
86+ test "cache_version does NOT call updated_at when value is from the database" do
87+ skip ( "Mysql2 does not return a string value for updated_at" ) if current_adapter? ( :Mysql2Adapter )
88+
89+ record = CacheMeWithVersion . create
90+ record_from_db = CacheMeWithVersion . find ( record . id )
91+ assert_not_called ( record_from_db , :updated_at ) do
92+ record_from_db . cache_version
93+ end
94+ end
95+
96+ test "cache_version does call updated_at when it is assigned via a Time object" do
97+ record = CacheMeWithVersion . create
98+ record_from_db = CacheMeWithVersion . find ( record . id )
99+ assert_called ( record_from_db , :updated_at ) do
100+ record_from_db . updated_at = Time . now
101+ record_from_db . cache_version
102+ end
103+ end
104+
105+ test "cache_version does call updated_at when it is assigned via a string" do
106+ record = CacheMeWithVersion . create
107+ record_from_db = CacheMeWithVersion . find ( record . id )
108+ assert_called ( record_from_db , :updated_at ) do
109+ record_from_db . updated_at = Time . now . to_s
110+ record_from_db . cache_version
111+ end
112+ end
113+
114+ test "cache_version does call updated_at when it is assigned via a hash" do
115+ record = CacheMeWithVersion . create
116+ record_from_db = CacheMeWithVersion . find ( record . id )
117+ assert_called ( record_from_db , :updated_at ) do
118+ record_from_db . updated_at = { 1 => 2016 , 2 => 11 , 3 => 12 , 4 => 1 , 5 => 2 , 6 => 3 , 7 => 22 }
119+ record_from_db . cache_version
120+ end
121+ end
122+
123+ test "updated_at on class but not on instance raises an error" do
124+ record = CacheMeWithVersion . create
125+ record_from_db = CacheMeWithVersion . where ( id : record . id ) . select ( :id ) . first
126+ assert_raises ( ActiveModel ::MissingAttributeError ) do
127+ record_from_db . cache_version
128+ end
51129 end
52130 end
53131end
0 commit comments