@@ -34,6 +34,7 @@ class UncachedAuthor < Author
3434 end
3535
3636 class Article < ::Model
37+ attributes :title
3738 # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
3839 undef_method :cache_key
3940 end
@@ -48,6 +49,10 @@ class InheritedRoleSerializer < RoleSerializer
4849 attribute :special_attribute
4950 end
5051
52+ class Comment < ::Model
53+ attributes :body , :post , :author
54+ end
55+
5156 setup do
5257 cache_store . clear
5358 @comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
@@ -271,7 +276,7 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
271276 ended_at : nil ,
272277 updated_at : alert . updated_at ,
273278 created_at : alert . created_at
274- }
279+ } . with_indifferent_access
275280 expected_cached_jsonapi_attributes = {
276281 id : '1' ,
277282 type : 'alerts' ,
@@ -283,15 +288,15 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
283288 updated_at : alert . updated_at ,
284289 created_at : alert . created_at
285290 }
286- }
291+ } . with_indifferent_access
287292
288293 # Assert attributes are serialized correctly
289294 serializable_alert = serializable ( alert , serializer : AlertSerializer , adapter : :attributes )
290- attributes_serialization = serializable_alert . as_json
295+ attributes_serialization = serializable_alert . as_json . with_indifferent_access
291296 assert_equal expected_fetch_attributes , alert . attributes
292297 assert_equal alert . attributes , attributes_serialization
293298 attributes_cache_key = serializable_alert . adapter . serializer . cache_key ( serializable_alert . adapter )
294- assert_equal attributes_serialization , cache_store . fetch ( attributes_cache_key )
299+ assert_equal attributes_serialization , cache_store . fetch ( attributes_cache_key ) . with_indifferent_access
295300
296301 serializable_alert = serializable ( alert , serializer : AlertSerializer , adapter : :json_api )
297302 jsonapi_cache_key = serializable_alert . adapter . serializer . cache_key ( serializable_alert . adapter )
@@ -303,7 +308,7 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
303308 serializable_alert = serializable ( alert , serializer : UncachedAlertSerializer , adapter : :json_api )
304309 assert_equal serializable_alert . as_json , jsonapi_serialization
305310
306- cached_serialization = cache_store . fetch ( jsonapi_cache_key )
311+ cached_serialization = cache_store . fetch ( jsonapi_cache_key ) . with_indifferent_access
307312 assert_equal expected_cached_jsonapi_attributes , cached_serialization
308313 ensure
309314 Object . send ( :remove_const , :Alert )
@@ -323,17 +328,26 @@ def test_cache_digest_definition
323328 end
324329
325330 def test_object_cache_keys
331+ class << @comment
332+ def cache_key
333+ "comment/#{ id } "
334+ end
335+ end
326336 serializable = ActiveModelSerializers ::SerializableResource . new ( [ @comment , @comment ] )
327337 include_directive = JSONAPI ::IncludeDirective . new ( '*' , allow_wildcard : true )
328338
329339 actual = ActiveModel ::Serializer . object_cache_keys ( serializable . adapter . serializer , serializable . adapter , include_directive )
330340
331341 assert_equal 3 , actual . size
332- assert actual . any? { |key | key == "comment/1/#{ serializable . adapter . cache_key } " }
333- assert actual . any? { |key | key =~ %r{post/post-\d +} }
334- assert actual . any? { |key | key =~ %r{author/author-\d +} }
342+ expected_key = "comment/1/#{ serializable . adapter . cache_key } "
343+ assert actual . any? { |key | key == expected_key } , "actual '#{ actual } ' should include #{ expected_key } "
344+ expected_key = %r{post/post-\d +}
345+ assert actual . any? { |key | key =~ expected_key } , "actual '#{ actual } ' should match '#{ expected_key } '"
346+ expected_key = %r{author/author-\d +}
347+ assert actual . any? { |key | key =~ expected_key } , "actual '#{ actual } ' should match '#{ expected_key } '"
335348 end
336349
350+ # rubocop:disable Metrics/AbcSize
337351 def test_fetch_attributes_from_cache
338352 serializers = ActiveModel ::Serializer ::CollectionSerializer . new ( [ @comment , @comment ] )
339353
@@ -344,20 +358,21 @@ def test_fetch_attributes_from_cache
344358 adapter_options = { }
345359 adapter_instance = ActiveModelSerializers ::Adapter ::Attributes . new ( serializers , adapter_options )
346360 serializers . serializable_hash ( adapter_options , options , adapter_instance )
347- cached_attributes = adapter_options . fetch ( :cached_attributes )
361+ cached_attributes = adapter_options . fetch ( :cached_attributes ) . with_indifferent_access
348362
349363 include_directive = ActiveModelSerializers . default_include_directive
350- manual_cached_attributes = ActiveModel ::Serializer . cache_read_multi ( serializers , adapter_instance , include_directive )
364+ manual_cached_attributes = ActiveModel ::Serializer . cache_read_multi ( serializers , adapter_instance , include_directive ) . with_indifferent_access
351365 assert_equal manual_cached_attributes , cached_attributes
352366
353- assert_equal cached_attributes [ "#{ @comment . cache_key } /#{ adapter_instance . cache_key } " ] , Comment . new ( id : 1 , body : 'ZOMG A COMMENT' ) . attributes
354- assert_equal cached_attributes [ "#{ @comment . post . cache_key } /#{ adapter_instance . cache_key } " ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
367+ assert_equal cached_attributes [ "#{ @comment . cache_key } /#{ adapter_instance . cache_key } " ] , Comment . new ( id : 1 , body : 'ZOMG A COMMENT' ) . attributes . reject { | _ , v | v . nil? }
368+ assert_equal cached_attributes [ "#{ @comment . post . cache_key } /#{ adapter_instance . cache_key } " ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes . reject { | _ , v | v . nil? }
355369
356370 writer = @comment . post . blog . writer
357371 writer_cache_key = writer . cache_key
358- assert_equal cached_attributes [ "#{ writer_cache_key } /#{ adapter_instance . cache_key } " ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
372+ assert_equal cached_attributes [ "#{ writer_cache_key } /#{ adapter_instance . cache_key } " ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes . reject { | _ , v | v . nil? }
359373 end
360374 end
375+ # rubocop:enable Metrics/AbcSize
361376
362377 def test_cache_read_multi_with_fragment_cache_enabled
363378 post_serializer = Class . new ( ActiveModel ::Serializer ) do
0 commit comments