Skip to content

Commit

Permalink
Confirm caching attributes with different key json_api vs. attributes…
Browse files Browse the repository at this point in the history
… adapter

Adapted from @kevintyll's original test
#1644 (comment)
  • Loading branch information
bf4 committed Apr 1, 2016
1 parent 447786c commit b3d0e90
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down

0 comments on commit b3d0e90

Please sign in to comment.