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,27 @@ 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
+ def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_on_object
100
+ uncached_author = UncachedAuthor . new ( name : 'Joao M. D. Moura' )
101
+ uncached_author_serializer = AuthorSerializer . new ( uncached_author )
102
+
103
+ render_object_with_cache ( uncached_author )
104
+ key = "#{ 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" ) } "
105
+ assert_equal ( uncached_author_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
88
106
end
89
107
90
108
def test_default_cache_key_fallback
91
109
render_object_with_cache ( @comment )
92
- assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( @comment . cache_key ) . to_json )
110
+ key = @comment . cache_key
111
+ assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
112
+ end
113
+
114
+ def test_error_is_raised_if_cache_key_is_not_defined_on_object_or_passed_as_cache_option
115
+ article = Article . new ( title : 'Must Read' )
116
+ e = assert_raises ActiveModelSerializers ::CachedSerializer ::UndefinedCacheKey do
117
+ render_object_with_cache ( article )
118
+ end
119
+ assert_match ( /ActiveModelSerializers::CacheTest::Article must define #cache_key, or the 'key:' option must be passed into 'CachedActiveModelSerializers_CacheTest_ArticleSerializer.cache'/ , e . message )
93
120
end
94
121
95
122
def test_cache_options_definition
@@ -111,8 +138,10 @@ def test_associations_separately_cache
111
138
Timecop . freeze ( Time . current ) do
112
139
render_object_with_cache ( @post )
113
140
114
- assert_equal ( @post_serializer . attributes , cache_store . fetch ( @post . cache_key ) )
115
- assert_equal ( @comment_serializer . attributes , cache_store . fetch ( @comment . cache_key ) )
141
+ key = @post . cache_key
142
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
143
+ key = @comment . cache_key
144
+ assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
116
145
end
117
146
end
118
147
@@ -122,8 +151,10 @@ def test_associations_cache_when_updated
122
151
render_object_with_cache ( @post )
123
152
124
153
# 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 ) )
154
+ key = @post . cache_key
155
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
156
+ key = @comment . cache_key
157
+ assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
127
158
128
159
# Simulating update on comments relationship with Post
129
160
new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
@@ -134,8 +165,10 @@ def test_associations_cache_when_updated
134
165
render_object_with_cache ( @post )
135
166
136
167
# 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 ) )
168
+ key = new_comment . cache_key
169
+ assert_equal ( new_comment_serializer . attributes , cache_store . fetch ( key ) )
170
+ key = @post . cache_key
171
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
139
172
end
140
173
end
141
174
@@ -163,11 +196,12 @@ def test_fragment_cache_with_inheritance
163
196
164
197
def test_uses_file_digest_in_cache_key
165
198
render_object_with_cache ( @blog )
166
- assert_equal ( @blog_serializer . attributes , cache_store . fetch ( @blog . cache_key_with_digest ) )
199
+ key = "#{ @blog . cache_key } /#{ ::Model ::FILE_DIGEST } "
200
+ assert_equal ( @blog_serializer . attributes , cache_store . fetch ( key ) )
167
201
end
168
202
169
203
def test_cache_digest_definition
170
- assert_equal ( FILE_DIGEST , @post_serializer . class . _cache_digest )
204
+ assert_equal ( :: Model :: FILE_DIGEST , @post_serializer . class . _cache_digest )
171
205
end
172
206
173
207
def test_object_cache_keys
@@ -179,7 +213,7 @@ def test_object_cache_keys
179
213
assert_equal actual . size , 3
180
214
assert actual . any? { |key | key == 'comment/1' }
181
215
assert actual . any? { |key | key =~ %r{post/post-\d +} }
182
- assert actual . any? { |key | key =~ %r{writer /author-\d +} }
216
+ assert actual . any? { |key | key =~ %r{author /author-\d +} }
183
217
end
184
218
185
219
def test_cached_attributes
@@ -196,7 +230,7 @@ def test_cached_attributes
196
230
assert_equal cached_attributes [ @comment . post . cache_key ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
197
231
198
232
writer = @comment . post . blog . writer
199
- writer_cache_key = " writer/ #{ writer . id } - #{ writer . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } "
233
+ writer_cache_key = writer . cache_key
200
234
201
235
assert_equal cached_attributes [ writer_cache_key ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
202
236
end
0 commit comments