4
4
5
5
module ActiveModelSerializers
6
6
class CacheTest < ActiveSupport ::TestCase
7
+ UncachedAuthor = Class . new ( Author ) do
8
+ # To confirm cache_key is set using updated_at and cache_key option passed to cache
9
+ undef_method :cache_key
10
+ end
11
+
12
+ Article = Class . new ( ::Model ) do
13
+ # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
14
+ undef_method :cache_key
15
+ end
16
+
17
+ ArticleSerializer = Class . new ( ActiveModel ::Serializer ) do
18
+ cache only : [ :place ] , skip_digest : true
19
+ attributes :title
20
+ end
21
+
7
22
InheritedRoleSerializer = Class . new ( RoleSerializer ) do
8
23
cache key : 'inherited_role' , only : [ :name , :special_attribute ]
9
24
attribute :special_attribute
@@ -81,15 +96,26 @@ def test_cache_key_definition
81
96
assert_equal ( nil , @comment_serializer . class . _cache_key )
82
97
end
83
98
84
- def test_cache_key_interpolation_with_updated_at
85
- render_object_with_cache ( @author )
86
- assert_equal ( nil , cache_store . fetch ( @author . cache_key ) )
87
- assert_equal ( @author_serializer . attributes . to_json , cache_store . fetch ( "#{ @author_serializer . class . _cache_key } /#{ @author_serializer . object . id } -#{ @author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } " ) . to_json )
99
+
100
+ def test_error_is_raised_if_cache_key_is_not_defined_on_object_or_passed_as_cache_option
101
+ article = Article . new ( title : 'Must Read' )
102
+ assert_raises ActiveModel ::Serializer ::Adapter ::CachedSerializer ::UndefinedCacheKey do
103
+ render_object_with_cache ( article )
104
+ end
105
+ end
106
+
107
+ def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_on_object
108
+ uncached_author = UncachedAuthor . new ( name : 'Joao M. D. Moura' )
109
+ uncached_author_serializer = AuthorSerializer . new ( uncached_author )
110
+
111
+ render_object_with_cache ( uncached_author )
112
+ key = cache_key_with_adapter ( "#{ uncached_author_serializer . class . _cache_key } /#{ uncached_author_serializer . object . id } -#{ uncached_author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } " )
113
+ assert_equal ( uncached_author_serializer . attributes . to_json , ActionController ::Base . cache_store . fetch ( key ) . to_json )
88
114
end
89
115
90
116
def test_default_cache_key_fallback
91
117
render_object_with_cache ( @comment )
92
- assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( @comment . cache_key ) . to_json )
118
+ assert_equal ( @comment_serializer . attributes . to_json , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) . to_json )
93
119
end
94
120
95
121
def test_cache_options_definition
@@ -111,8 +137,8 @@ def test_associations_separately_cache
111
137
Timecop . freeze ( Time . current ) do
112
138
render_object_with_cache ( @post )
113
139
114
- assert_equal ( @post_serializer . attributes , cache_store . fetch ( @post . cache_key ) )
115
- assert_equal ( @comment_serializer . attributes , cache_store . fetch ( @comment . cache_key ) )
140
+ assert_equal ( @post_serializer . attributes , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
141
+ assert_equal ( @comment_serializer . attributes , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) )
116
142
end
117
143
end
118
144
@@ -122,8 +148,9 @@ def test_associations_cache_when_updated
122
148
render_object_with_cache ( @post )
123
149
124
150
# Check if it cached the objects separately
125
- assert_equal ( @post_serializer . attributes , cached_serialization ( @post_serializer ) )
126
- assert_equal ( @comment_serializer . attributes , cached_serialization ( @comment_serializer ) )
151
+ assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
152
+ assert_equal ( @comment_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) )
153
+
127
154
128
155
# Simulating update on comments relationship with Post
129
156
new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
@@ -134,8 +161,8 @@ def test_associations_cache_when_updated
134
161
render_object_with_cache ( @post )
135
162
136
163
# Check if the the new comment was cached
137
- assert_equal ( new_comment_serializer . attributes , cached_serialization ( new_comment_serializer ) )
138
- assert_equal ( @post_serializer . attributes , cached_serialization ( @post_serializer ) )
164
+ assert_equal ( new_comment_serializer . attributes , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( new_comment . cache_key ) ) )
165
+ assert_equal ( @post_serializer . attributes , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
139
166
end
140
167
end
141
168
@@ -150,7 +177,7 @@ def test_fragment_fetch_with_virtual_associations
150
177
hash = render_object_with_cache ( @location )
151
178
152
179
assert_equal ( hash , expected_result )
153
- assert_equal ( { place : 'Nowhere' } , cache_store . fetch ( @location . cache_key ) )
180
+ assert_equal ( { place : 'Nowhere' } , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @location . cache_key ) ) )
154
181
end
155
182
156
183
def test_fragment_cache_with_inheritance
@@ -161,9 +188,14 @@ def test_fragment_cache_with_inheritance
161
188
refute_includes ( base . keys , :special_attribute )
162
189
end
163
190
191
+ def test_uses_adapter_in_cache_key
192
+ render_object_with_cache ( @post )
193
+ assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( "#{ @post . cache_key } /#{ adapter . class . to_s . demodulize . underscore } " ) )
194
+ end
195
+
164
196
def test_uses_file_digest_in_cache_key
165
197
render_object_with_cache ( @blog )
166
- assert_equal ( @blog_serializer . attributes , cache_store . fetch ( @blog . cache_key_with_digest ) )
198
+ assert_equal ( @blog_serializer . attributes , ActionController :: Base . cache_store . fetch ( " #{ cache_key_with_adapter ( @blog . cache_key ) } / #{ @blog . class :: FILE_DIGEST } " ) )
167
199
end
168
200
169
201
def test_cache_digest_definition
@@ -254,7 +286,16 @@ def test_warn_on_serializer_not_defined_in_file
254
286
private
255
287
256
288
def render_object_with_cache ( obj , options = { } )
257
- serializable ( obj , options ) . serializable_hash
289
+ @serializable_resource = serializable ( obj , options ) . serializable_hash
290
+ @serializable_resource . serializable_hash
291
+ end
292
+
293
+ def adapter
294
+ @serializable_resource . adapter
295
+ end
296
+
297
+ def cache_key_with_adapter ( key )
298
+ "#{ key } /#{ adapter . name . underscore } "
258
299
end
259
300
260
301
def cache_store
0 commit comments