@@ -9,8 +9,8 @@ class CacheTest < ActiveSupport::TestCase
9
9
attribute :special_attribute
10
10
end
11
11
12
- def setup
13
- ActionController :: Base . cache_store . clear
12
+ setup do
13
+ cache_store . clear
14
14
@comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
15
15
@post = Post . new ( title : 'New Post' , body : 'Body' )
16
16
@bio = Bio . new ( id : 1 , content : 'AMS Contributor' )
@@ -70,9 +70,9 @@ def test_override_cache_configuration
70
70
end
71
71
72
72
def test_cache_definition
73
- assert_equal ( ActionController :: Base . cache_store , @post_serializer . class . _cache )
74
- assert_equal ( ActionController :: Base . cache_store , @author_serializer . class . _cache )
75
- assert_equal ( ActionController :: Base . cache_store , @comment_serializer . class . _cache )
73
+ assert_equal ( cache_store , @post_serializer . class . _cache )
74
+ assert_equal ( cache_store , @author_serializer . class . _cache )
75
+ assert_equal ( cache_store , @comment_serializer . class . _cache )
76
76
end
77
77
78
78
def test_cache_key_definition
@@ -83,13 +83,13 @@ def test_cache_key_definition
83
83
84
84
def test_cache_key_interpolation_with_updated_at
85
85
render_object_with_cache ( @author )
86
- assert_equal ( nil , ActionController :: Base . cache_store . fetch ( @author . cache_key ) )
87
- assert_equal ( @author_serializer . attributes . to_json , ActionController :: Base . cache_store . fetch ( "#{ @author_serializer . class . _cache_key } /#{ @author_serializer . object . id } -#{ @author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } " ) . to_json )
86
+ assert_equal ( nil , cache_store . fetch ( @author . cache_key ) )
87
+ assert_equal ( @author_serializer . attributes . to_json , cache_store . fetch ( "#{ @author_serializer . class . _cache_key } /#{ @author_serializer . object . id } -#{ @author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } " ) . to_json )
88
88
end
89
89
90
90
def test_default_cache_key_fallback
91
91
render_object_with_cache ( @comment )
92
- assert_equal ( @comment_serializer . attributes . to_json , ActionController :: Base . cache_store . fetch ( @comment . cache_key ) . to_json )
92
+ assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( @comment . cache_key ) . to_json )
93
93
end
94
94
95
95
def test_cache_options_definition
@@ -104,41 +104,38 @@ def test_fragment_cache_definition
104
104
end
105
105
106
106
def test_associations_separately_cache
107
- ActionController :: Base . cache_store . clear
108
- assert_equal ( nil , ActionController :: Base . cache_store . fetch ( @post . cache_key ) )
109
- assert_equal ( nil , ActionController :: Base . cache_store . fetch ( @comment . cache_key ) )
107
+ cache_store . clear
108
+ assert_equal ( nil , cache_store . fetch ( @post . cache_key ) )
109
+ assert_equal ( nil , cache_store . fetch ( @comment . cache_key ) )
110
110
111
111
Timecop . freeze ( Time . current ) do
112
112
render_object_with_cache ( @post )
113
113
114
- assert_equal ( @post_serializer . attributes , ActionController :: Base . cache_store . fetch ( @post . cache_key ) )
115
- assert_equal ( @comment_serializer . attributes , ActionController :: Base . cache_store . fetch ( @comment . cache_key ) )
114
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( @post . cache_key ) )
115
+ assert_equal ( @comment_serializer . attributes , cache_store . fetch ( @comment . cache_key ) )
116
116
end
117
117
end
118
118
119
119
def test_associations_cache_when_updated
120
- # Clean the Cache
121
- ActionController ::Base . cache_store . clear
122
-
123
120
Timecop . freeze ( Time . current ) do
124
121
# Generate a new Cache of Post object and each objects related to it.
125
122
render_object_with_cache ( @post )
126
123
127
124
# Check if it cached the objects separately
128
- assert_equal ( @post_serializer . attributes , ActionController :: Base . cache_store . fetch ( @post . cache_key ) )
129
- assert_equal ( @comment_serializer . attributes , ActionController :: Base . cache_store . fetch ( @comment . cache_key ) )
125
+ assert_equal ( @post_serializer . attributes , cached_serialization ( @post_serializer ) )
126
+ assert_equal ( @comment_serializer . attributes , cached_serialization ( @comment_serializer ) )
130
127
131
128
# Simulating update on comments relationship with Post
132
- new_comment = Comment . new ( id : 2 , body : 'ZOMG A NEW COMMENT' )
129
+ new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
133
130
new_comment_serializer = CommentSerializer . new ( new_comment )
134
131
@post . comments = [ new_comment ]
135
132
136
133
# Ask for the serialized object
137
134
render_object_with_cache ( @post )
138
135
139
136
# Check if the the new comment was cached
140
- assert_equal ( new_comment_serializer . attributes , ActionController :: Base . cache_store . fetch ( new_comment . cache_key ) )
141
- assert_equal ( @post_serializer . attributes , ActionController :: Base . cache_store . fetch ( @post . cache_key ) )
137
+ assert_equal ( new_comment_serializer . attributes , cached_serialization ( new_comment_serializer ) )
138
+ assert_equal ( @post_serializer . attributes , cached_serialization ( @post_serializer ) )
142
139
end
143
140
end
144
141
@@ -153,7 +150,7 @@ def test_fragment_fetch_with_virtual_associations
153
150
hash = render_object_with_cache ( @location )
154
151
155
152
assert_equal ( hash , expected_result )
156
- assert_equal ( { place : 'Nowhere' } , ActionController :: Base . cache_store . fetch ( @location . cache_key ) )
153
+ assert_equal ( { place : 'Nowhere' } , cache_store . fetch ( @location . cache_key ) )
157
154
end
158
155
159
156
def test_fragment_cache_with_inheritance
@@ -166,11 +163,11 @@ def test_fragment_cache_with_inheritance
166
163
167
164
def test_uses_file_digest_in_cache_key
168
165
render_object_with_cache ( @blog )
169
- assert_equal ( @blog_serializer . attributes , ActionController :: Base . cache_store . fetch ( @blog . cache_key_with_digest ) )
166
+ assert_equal ( @blog_serializer . attributes , cache_store . fetch ( @blog . cache_key_with_digest ) )
170
167
end
171
168
172
169
def test_cache_digest_definition
173
- assert_equal ( :: Model :: FILE_DIGEST , @post_serializer . class . _cache_digest )
170
+ assert_equal ( FILE_DIGEST , @post_serializer . class . _cache_digest )
174
171
end
175
172
176
173
def test_object_cache_keys
@@ -257,7 +254,16 @@ def test_warn_on_serializer_not_defined_in_file
257
254
private
258
255
259
256
def render_object_with_cache ( obj , options = { } )
260
- SerializableResource . new ( obj , options ) . serializable_hash
257
+ serializable ( obj , options ) . serializable_hash
258
+ end
259
+
260
+ def cache_store
261
+ ActiveModelSerializers . config . cache_store
262
+ end
263
+
264
+ def cached_serialization ( serializer )
265
+ cache_key = CachedSerializer . new ( serializer ) . cache_key
266
+ cache_store . fetch ( cache_key )
261
267
end
262
268
end
263
269
end
0 commit comments