44
55module ActiveModelSerializers
66 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+
722 InheritedRoleSerializer = Class . new ( RoleSerializer ) do
823 cache key : 'inherited_role' , only : [ :name , :special_attribute ]
924 attribute :special_attribute
@@ -81,15 +96,27 @@ def test_cache_key_definition
8196 assert_equal ( nil , @comment_serializer . class . _cache_key )
8297 end
8398
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 )
88106 end
89107
90108 def test_default_cache_key_fallback
91109 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 )
93120 end
94121
95122 def test_cache_options_definition
@@ -111,8 +138,10 @@ def test_associations_separately_cache
111138 Timecop . freeze ( Time . current ) do
112139 render_object_with_cache ( @post )
113140
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 ) )
116145 end
117146 end
118147
@@ -122,8 +151,10 @@ def test_associations_cache_when_updated
122151 render_object_with_cache ( @post )
123152
124153 # 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 ) )
127158
128159 # Simulating update on comments relationship with Post
129160 new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
@@ -134,8 +165,10 @@ def test_associations_cache_when_updated
134165 render_object_with_cache ( @post )
135166
136167 # 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 ) )
139172 end
140173 end
141174
@@ -163,11 +196,12 @@ def test_fragment_cache_with_inheritance
163196
164197 def test_uses_file_digest_in_cache_key
165198 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 ) )
167201 end
168202
169203 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 )
171205 end
172206
173207 def test_object_cache_keys
@@ -179,7 +213,7 @@ def test_object_cache_keys
179213 assert_equal actual . size , 3
180214 assert actual . any? { |key | key == 'comment/1' }
181215 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 +} }
183217 end
184218
185219 def test_cached_attributes
@@ -196,7 +230,7 @@ def test_cached_attributes
196230 assert_equal cached_attributes [ @comment . post . cache_key ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
197231
198232 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
200234
201235 assert_equal cached_attributes [ writer_cache_key ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
202236 end
0 commit comments