Skip to content

Commit 2789cc5

Browse files
committed
Test CachedSerializer#fragment_cached?
1 parent 4974295 commit 2789cc5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Features:
77
relationship-level links and meta attributes. (@beauby)
88
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
99
Fixes:
10+
- [#1477](https://github.com/rails-api/active_model_serializers/pull/1477) Fix `fragment_cached?`
11+
method to check if caching (@bdmac)
1012
- [#1501](https://github.com/rails-api/active_model_serializers/pull/1501) Adds tests for SerializableResource::use_adapter?,doc typos (@domitian)
1113
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)
1214
Misc:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'test_helper'
2+
module ActiveModel
3+
class Serializer
4+
module Adapter
5+
class CachedSerializerTest < ActiveSupport::TestCase
6+
def test_cached_false_without_cache_store
7+
cached_serializer = build do |serializer|
8+
serializer._cache = nil
9+
end
10+
refute cached_serializer.cached?
11+
end
12+
13+
def test_cached_true_with_cache_store_and_without_cache_only_and_cache_except
14+
cached_serializer = build do |serializer|
15+
serializer._cache = Object
16+
end
17+
assert cached_serializer.cached?
18+
end
19+
20+
def test_cached_false_with_cache_store_and_with_cache_only
21+
cached_serializer = build do |serializer|
22+
serializer._cache = Object
23+
serializer._cache_only = [:name]
24+
end
25+
refute cached_serializer.cached?
26+
end
27+
28+
def test_cached_false_with_cache_store_and_with_cache_except
29+
cached_serializer = build do |serializer|
30+
serializer._cache = Object
31+
serializer._cache_except = [:content]
32+
end
33+
refute cached_serializer.cached?
34+
end
35+
36+
def test_fragment_cached_false_without_cache_store
37+
cached_serializer = build do |serializer|
38+
serializer._cache = nil
39+
serializer._cache_only = [:name]
40+
end
41+
refute cached_serializer.fragment_cached?
42+
end
43+
44+
def test_fragment_cached_true_with_cache_store_and_cache_only
45+
cached_serializer = build do |serializer|
46+
serializer._cache = Object
47+
serializer._cache_only = [:name]
48+
end
49+
assert cached_serializer.fragment_cached?
50+
end
51+
52+
def test_fragment_cached_true_with_cache_store_and_cache_except
53+
cached_serializer = build do |serializer|
54+
serializer._cache = Object
55+
serializer._cache_except = [:content]
56+
end
57+
assert cached_serializer.fragment_cached?
58+
end
59+
60+
def test_fragment_cached_false_with_cache_store_and_cache_except_and_cache_only
61+
cached_serializer = build do |serializer|
62+
serializer._cache = Object
63+
serializer._cache_except = [:content]
64+
serializer._cache_only = [:name]
65+
end
66+
refute cached_serializer.fragment_cached?
67+
end
68+
69+
private
70+
71+
def build
72+
serializer = Class.new(ActiveModel::Serializer)
73+
serializer._cache_key = nil
74+
serializer._cache_options = nil
75+
yield serializer if block_given?
76+
serializer_instance = serializer.new(Object)
77+
ActiveModel::Serializer::Adapter::CachedSerializer.new(serializer_instance)
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)