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,26 @@ 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+
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 )
88114 end
89115
90116 def test_default_cache_key_fallback
91117 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 )
93119 end
94120
95121 def test_cache_options_definition
@@ -111,8 +137,8 @@ def test_associations_separately_cache
111137 Timecop . freeze ( Time . current ) do
112138 render_object_with_cache ( @post )
113139
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 ) ) )
116142 end
117143 end
118144
@@ -122,8 +148,9 @@ def test_associations_cache_when_updated
122148 render_object_with_cache ( @post )
123149
124150 # 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+
127154
128155 # Simulating update on comments relationship with Post
129156 new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
@@ -134,8 +161,8 @@ def test_associations_cache_when_updated
134161 render_object_with_cache ( @post )
135162
136163 # 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 ) ) )
139166 end
140167 end
141168
@@ -150,7 +177,7 @@ def test_fragment_fetch_with_virtual_associations
150177 hash = render_object_with_cache ( @location )
151178
152179 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 ) ) )
154181 end
155182
156183 def test_fragment_cache_with_inheritance
@@ -161,9 +188,14 @@ def test_fragment_cache_with_inheritance
161188 refute_includes ( base . keys , :special_attribute )
162189 end
163190
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+
164196 def test_uses_file_digest_in_cache_key
165197 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 } " ) )
167199 end
168200
169201 def test_cache_digest_definition
@@ -254,7 +286,16 @@ def test_warn_on_serializer_not_defined_in_file
254286 private
255287
256288 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 } "
258299 end
259300
260301 def cache_store
0 commit comments