Skip to content

Commit b3d0e90

Browse files
committed
Confirm caching attributes with different key json_api vs. attributes adapter
Adapted from @kevintyll's original test #1644 (comment)
1 parent 447786c commit b3d0e90

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/cache_test.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,78 @@ def test_uses_adapter_in_cache_key
171171
assert_equal(@post_serializer.attributes, cache_store.fetch(key))
172172
end
173173

174+
# Based on original failing test by @kevintyll
175+
# rubocop:disable Metrics/AbcSize
176+
def test_a_serializer_rendered_by_two_adapter_returns_differently_cached_attributes
177+
Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do
178+
attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
179+
end)
180+
Object.const_set(:UncachedAlertSerializer, Class.new(ActiveModel::Serializer) do
181+
attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
182+
end)
183+
Object.const_set(:AlertSerializer, Class.new(UncachedAlertSerializer) do
184+
cache
185+
end)
186+
187+
alert = Alert.new(
188+
id: 1,
189+
status: 'fail',
190+
resource: 'resource-1',
191+
started_at: Time.new(2016, 3, 31, 21, 36, 35, 0),
192+
ended_at: nil,
193+
updated_at: Time.new(2016, 3, 31, 21, 27, 35, 0),
194+
created_at: Time.new(2016, 3, 31, 21, 37, 35, 0)
195+
)
196+
197+
expected_cached_attributes = {
198+
id: 1,
199+
status: 'fail',
200+
resource: 'resource-1',
201+
started_at: alert.started_at,
202+
ended_at: nil,
203+
updated_at: alert.updated_at,
204+
created_at: alert.created_at
205+
}
206+
expected_cached_jsonapi_attributes = {
207+
id: '1',
208+
type: 'alerts',
209+
attributes: {
210+
status: 'fail',
211+
resource: 'resource-1',
212+
started_at: alert.started_at,
213+
ended_at: nil,
214+
updated_at: alert.updated_at,
215+
created_at: alert.created_at
216+
}
217+
}
218+
219+
# Assert attributes are serialized correctly
220+
serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :attributes)
221+
attributes_serialization = serializable_alert.as_json
222+
assert_equal expected_cached_attributes, alert.attributes
223+
assert_equal alert.attributes, attributes_serialization
224+
attributes_cache_key = CachedSerializer.new(serializable_alert.adapter.serializer).cache_key(serializable_alert.adapter)
225+
assert_equal attributes_serialization, cache_store.fetch(attributes_cache_key)
226+
227+
serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :json_api)
228+
jsonapi_cache_key = CachedSerializer.new(serializable_alert.adapter.serializer).cache_key(serializable_alert.adapter)
229+
# Assert cache keys differ
230+
refute_equal attributes_cache_key, jsonapi_cache_key
231+
# Assert (cached) serializations differ
232+
jsonapi_serialization = serializable_alert.as_json
233+
assert_equal alert.status, jsonapi_serialization.fetch(:data).fetch(:attributes).fetch(:status)
234+
serializable_alert = serializable(alert, serializer: UncachedAlertSerializer, adapter: :json_api)
235+
assert_equal serializable_alert.as_json, jsonapi_serialization
236+
237+
cached_serialization = cache_store.fetch(jsonapi_cache_key)
238+
assert_equal expected_cached_jsonapi_attributes, cached_serialization
239+
ensure
240+
Object.send(:remove_const, :Alert)
241+
Object.send(:remove_const, :AlertSerializer)
242+
Object.send(:remove_const, :UncachedAlertSerializer)
243+
end
244+
# rubocop:enable Metrics/AbcSize
245+
174246
def test_uses_file_digest_in_cache_key
175247
render_object_with_cache(@blog)
176248
key = "#{@blog.cache_key}/#{adapter.cached_name}/#{@blog.class.const_get(:FILE_DIGEST)}"

0 commit comments

Comments
 (0)