From b3d0e900fdb9b739b84df4b6bc163a91365db370 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 31 Mar 2016 23:42:25 -0500 Subject: [PATCH] Confirm caching attributes with different key json_api vs. attributes adapter Adapted from @kevintyll's original test https://github.com/rails-api/active_model_serializers/pull/1644#issuecomment-204147094 --- test/cache_test.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/test/cache_test.rb b/test/cache_test.rb index f9e58bed2..0b33eb09a 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -171,6 +171,78 @@ def test_uses_adapter_in_cache_key assert_equal(@post_serializer.attributes, cache_store.fetch(key)) end + # Based on original failing test by @kevintyll + # rubocop:disable Metrics/AbcSize + def test_a_serializer_rendered_by_two_adapter_returns_differently_cached_attributes + Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do + attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at + end) + Object.const_set(:UncachedAlertSerializer, Class.new(ActiveModel::Serializer) do + attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at + end) + Object.const_set(:AlertSerializer, Class.new(UncachedAlertSerializer) do + cache + end) + + alert = Alert.new( + id: 1, + status: 'fail', + resource: 'resource-1', + started_at: Time.new(2016, 3, 31, 21, 36, 35, 0), + ended_at: nil, + updated_at: Time.new(2016, 3, 31, 21, 27, 35, 0), + created_at: Time.new(2016, 3, 31, 21, 37, 35, 0) + ) + + expected_cached_attributes = { + id: 1, + status: 'fail', + resource: 'resource-1', + started_at: alert.started_at, + ended_at: nil, + updated_at: alert.updated_at, + created_at: alert.created_at + } + expected_cached_jsonapi_attributes = { + id: '1', + type: 'alerts', + attributes: { + status: 'fail', + resource: 'resource-1', + started_at: alert.started_at, + ended_at: nil, + updated_at: alert.updated_at, + created_at: alert.created_at + } + } + + # Assert attributes are serialized correctly + serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :attributes) + attributes_serialization = serializable_alert.as_json + assert_equal expected_cached_attributes, alert.attributes + assert_equal alert.attributes, attributes_serialization + attributes_cache_key = CachedSerializer.new(serializable_alert.adapter.serializer).cache_key(serializable_alert.adapter) + assert_equal attributes_serialization, cache_store.fetch(attributes_cache_key) + + serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :json_api) + jsonapi_cache_key = CachedSerializer.new(serializable_alert.adapter.serializer).cache_key(serializable_alert.adapter) + # Assert cache keys differ + refute_equal attributes_cache_key, jsonapi_cache_key + # Assert (cached) serializations differ + jsonapi_serialization = serializable_alert.as_json + assert_equal alert.status, jsonapi_serialization.fetch(:data).fetch(:attributes).fetch(:status) + serializable_alert = serializable(alert, serializer: UncachedAlertSerializer, adapter: :json_api) + assert_equal serializable_alert.as_json, jsonapi_serialization + + cached_serialization = cache_store.fetch(jsonapi_cache_key) + assert_equal expected_cached_jsonapi_attributes, cached_serialization + ensure + Object.send(:remove_const, :Alert) + Object.send(:remove_const, :AlertSerializer) + Object.send(:remove_const, :UncachedAlertSerializer) + end + # rubocop:enable Metrics/AbcSize + def test_uses_file_digest_in_cache_key render_object_with_cache(@blog) key = "#{@blog.cache_key}/#{adapter.cached_name}/#{@blog.class.const_get(:FILE_DIGEST)}"