active_model_serializers 0.10.9 → 0.10.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -2
- data/README.md +18 -19
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer/collection_serializer.rb +11 -2
- data/lib/active_model/serializer/concerns/caching.rb +2 -1
- data/lib/active_model/serializer/fieldset.rb +1 -1
- data/lib/active_model/serializer/reflection.rb +4 -4
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +13 -4
- data/lib/active_model_serializers/adapter/attributes.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +5 -1
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
- data/lib/active_model_serializers/model/caching.rb +26 -0
- metadata +20 -292
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -109
- data/.simplecov +0 -110
- data/.travis.yml +0 -63
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -74
- data/Rakefile +0 -76
- data/active_model_serializers.gemspec +0 -64
- data/appveyor.yml +0 -28
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -269
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -185
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -495
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -62
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -64
- data/test/action_controller/explicit_serializer_test.rb +0 -137
- data/test/action_controller/json/include_test.rb +0 -248
- data/test/action_controller/json_api/deserialization_test.rb +0 -114
- data/test/action_controller/json_api/errors_test.rb +0 -42
- data/test/action_controller/json_api/fields_test.rb +0 -68
- data/test/action_controller/json_api/linked_test.rb +0 -204
- data/test/action_controller/json_api/pagination_test.rb +0 -126
- data/test/action_controller/json_api/transform_test.rb +0 -191
- data/test/action_controller/lookup_proc_test.rb +0 -51
- data/test/action_controller/namespace_lookup_test.rb +0 -234
- data/test/action_controller/serialization_scope_name_test.rb +0 -237
- data/test/action_controller/serialization_test.rb +0 -480
- data/test/active_model_serializers/adapter_for_test.rb +0 -210
- data/test/active_model_serializers/json_pointer_test.rb +0 -24
- data/test/active_model_serializers/logging_test.rb +0 -79
- data/test/active_model_serializers/model_test.rb +0 -144
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
- data/test/active_model_serializers/test/schema_test.rb +0 -133
- data/test/active_model_serializers/test/serializer_test.rb +0 -64
- data/test/active_record_test.rb +0 -11
- data/test/adapter/attributes_test.rb +0 -42
- data/test/adapter/deprecation_test.rb +0 -102
- data/test/adapter/json/belongs_to_test.rb +0 -47
- data/test/adapter/json/collection_test.rb +0 -106
- data/test/adapter/json/has_many_test.rb +0 -55
- data/test/adapter/json/transform_test.rb +0 -95
- data/test/adapter/json_api/belongs_to_test.rb +0 -157
- data/test/adapter/json_api/collection_test.rb +0 -98
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -98
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
- data/test/adapter/json_api/has_many_test.rb +0 -175
- data/test/adapter/json_api/has_one_test.rb +0 -82
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -415
- data/test/adapter/json_api/links_test.rb +0 -112
- data/test/adapter/json_api/pagination_links_test.rb +0 -208
- data/test/adapter/json_api/parse_test.rb +0 -139
- data/test/adapter/json_api/relationship_test.rb +0 -399
- data/test/adapter/json_api/resource_meta_test.rb +0 -102
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
- data/test/adapter/json_api/transform_test.rb +0 -514
- data/test/adapter/json_api/type_test.rb +0 -195
- data/test/adapter/json_test.rb +0 -48
- data/test/adapter/null_test.rb +0 -24
- data/test/adapter/polymorphic_test.rb +0 -220
- data/test/adapter_test.rb +0 -69
- data/test/array_serializer_test.rb +0 -24
- data/test/benchmark/app.rb +0 -67
- data/test/benchmark/benchmarking_support.rb +0 -69
- data/test/benchmark/bm_active_record.rb +0 -83
- data/test/benchmark/bm_adapter.rb +0 -40
- data/test/benchmark/bm_caching.rb +0 -121
- data/test/benchmark/bm_lookup_chain.rb +0 -85
- data/test/benchmark/bm_transform.rb +0 -47
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -85
- data/test/benchmark/fixtures.rb +0 -221
- data/test/cache_test.rb +0 -717
- data/test/collection_serializer_test.rb +0 -129
- data/test/fixtures/active_record.rb +0 -115
- data/test/fixtures/poro.rb +0 -227
- data/test/generators/scaffold_controller_generator_test.rb +0 -26
- data/test/generators/serializer_generator_test.rb +0 -77
- data/test/grape_test.rb +0 -198
- data/test/lint_test.rb +0 -51
- data/test/logger_test.rb +0 -22
- data/test/poro_test.rb +0 -11
- data/test/serializable_resource_test.rb +0 -81
- data/test/serializers/association_macros_test.rb +0 -39
- data/test/serializers/associations_test.rb +0 -520
- data/test/serializers/attribute_test.rb +0 -155
- data/test/serializers/attributes_test.rb +0 -54
- data/test/serializers/caching_configuration_test_isolated.rb +0 -172
- data/test/serializers/configuration_test.rb +0 -34
- data/test/serializers/fieldset_test.rb +0 -16
- data/test/serializers/meta_test.rb +0 -204
- data/test/serializers/options_test.rb +0 -34
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
- data/test/serializers/reflection_test.rb +0 -481
- data/test/serializers/root_test.rb +0 -23
- data/test/serializers/serialization_test.rb +0 -57
- data/test/serializers/serializer_for_test.rb +0 -138
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -86
- data/test/support/rails5_shims.rb +0 -55
- data/test/support/rails_app.rb +0 -40
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -81
- data/test/test_helper.rb +0 -72
|
@@ -1,480 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'test_helper'
|
|
4
|
-
|
|
5
|
-
module ActionController
|
|
6
|
-
module Serialization
|
|
7
|
-
class ImplicitSerializerTest < ActionController::TestCase
|
|
8
|
-
class ImplicitSerializationTestController < ActionController::Base
|
|
9
|
-
include SerializationTesting
|
|
10
|
-
def render_using_implicit_serializer
|
|
11
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
12
|
-
render json: @profile
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def render_using_default_adapter_root
|
|
16
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
17
|
-
render json: @profile
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def render_array_using_custom_root
|
|
21
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
22
|
-
render json: [@profile], root: 'custom_root'
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def render_array_that_is_empty_using_custom_root
|
|
26
|
-
render json: [], root: 'custom_root'
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def render_object_using_custom_root
|
|
30
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
31
|
-
render json: @profile, root: 'custom_root'
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def render_array_using_implicit_serializer
|
|
35
|
-
array = [
|
|
36
|
-
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
|
37
|
-
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
|
38
|
-
]
|
|
39
|
-
render json: array
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def render_array_using_implicit_serializer_and_meta
|
|
43
|
-
@profiles = [
|
|
44
|
-
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
45
|
-
]
|
|
46
|
-
render json: @profiles, meta: { total: 10 }
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def render_array_using_implicit_serializer_and_links
|
|
50
|
-
with_adapter ActiveModelSerializers::Adapter::JsonApi do
|
|
51
|
-
@profiles = [
|
|
52
|
-
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def render_object_with_cache_enabled
|
|
60
|
-
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
61
|
-
@author = Author.new(id: 1, name: 'Joao Moura.')
|
|
62
|
-
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
|
|
63
|
-
|
|
64
|
-
generate_cached_serializer(@post)
|
|
65
|
-
|
|
66
|
-
@post.title = 'ZOMG a New Post'
|
|
67
|
-
render json: @post
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
def render_json_object_without_serializer
|
|
71
|
-
render json: { error: 'Result is Invalid' }
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def render_json_array_object_without_serializer
|
|
75
|
-
render json: [{ error: 'Result is Invalid' }]
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def update_and_render_object_with_cache_enabled
|
|
79
|
-
@post.updated_at = Time.zone.now
|
|
80
|
-
|
|
81
|
-
generate_cached_serializer(@post)
|
|
82
|
-
render json: @post
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def render_object_expired_with_cache_enabled
|
|
86
|
-
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
87
|
-
author = Author.new(id: 1, name: 'Joao Moura.')
|
|
88
|
-
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
|
|
89
|
-
|
|
90
|
-
generate_cached_serializer(post)
|
|
91
|
-
|
|
92
|
-
post.title = 'ZOMG a New Post'
|
|
93
|
-
|
|
94
|
-
expires_in = [
|
|
95
|
-
PostSerializer._cache_options[:expires_in],
|
|
96
|
-
CommentSerializer._cache_options[:expires_in]
|
|
97
|
-
].max + 200
|
|
98
|
-
|
|
99
|
-
Timecop.travel(Time.zone.now + expires_in) do
|
|
100
|
-
render json: post
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def render_changed_object_with_cache_enabled
|
|
105
|
-
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
106
|
-
author = Author.new(id: 1, name: 'Joao Moura.')
|
|
107
|
-
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
|
|
108
|
-
|
|
109
|
-
render json: post
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def render_fragment_changed_object_with_only_cache_enabled
|
|
113
|
-
author = Author.new(id: 1, name: 'Joao Moura.')
|
|
114
|
-
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
|
|
115
|
-
|
|
116
|
-
generate_cached_serializer(role)
|
|
117
|
-
role.name = 'lol'
|
|
118
|
-
role.description = 'HUEHUEBRBR'
|
|
119
|
-
|
|
120
|
-
render json: role
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def render_fragment_changed_object_with_except_cache_enabled
|
|
124
|
-
author = Author.new(id: 1, name: 'Joao Moura.')
|
|
125
|
-
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
|
|
126
|
-
|
|
127
|
-
generate_cached_serializer(bio)
|
|
128
|
-
bio.content = 'lol'
|
|
129
|
-
bio.rating = 0
|
|
130
|
-
|
|
131
|
-
render json: bio
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
def render_fragment_changed_object_with_relationship
|
|
135
|
-
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
136
|
-
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
|
|
137
|
-
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
|
138
|
-
|
|
139
|
-
generate_cached_serializer(like)
|
|
140
|
-
like.likeable = comment2
|
|
141
|
-
like.time = Time.zone.now.to_s
|
|
142
|
-
|
|
143
|
-
render json: like
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
tests ImplicitSerializationTestController
|
|
148
|
-
|
|
149
|
-
# We just have Null for now, this will change
|
|
150
|
-
def test_render_using_implicit_serializer
|
|
151
|
-
get :render_using_implicit_serializer
|
|
152
|
-
|
|
153
|
-
expected = {
|
|
154
|
-
name: 'Name 1',
|
|
155
|
-
description: 'Description 1'
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
assert_equal 'application/json', @response.content_type
|
|
159
|
-
assert_equal expected.to_json, @response.body
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
def test_render_using_default_root
|
|
163
|
-
with_adapter :json_api do
|
|
164
|
-
get :render_using_default_adapter_root
|
|
165
|
-
end
|
|
166
|
-
expected = {
|
|
167
|
-
data: {
|
|
168
|
-
id: @controller.instance_variable_get(:@profile).id.to_s,
|
|
169
|
-
type: 'profiles',
|
|
170
|
-
attributes: {
|
|
171
|
-
name: 'Name 1',
|
|
172
|
-
description: 'Description 1'
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
assert_equal 'application/json', @response.content_type
|
|
178
|
-
assert_equal expected.to_json, @response.body
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
def test_render_array_using_custom_root
|
|
182
|
-
with_adapter :json do
|
|
183
|
-
get :render_array_using_custom_root
|
|
184
|
-
end
|
|
185
|
-
expected = { custom_root: [{ name: 'Name 1', description: 'Description 1' }] }
|
|
186
|
-
assert_equal 'application/json', @response.content_type
|
|
187
|
-
assert_equal expected.to_json, @response.body
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
def test_render_array_that_is_empty_using_custom_root
|
|
191
|
-
with_adapter :json do
|
|
192
|
-
get :render_array_that_is_empty_using_custom_root
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
expected = { custom_root: [] }
|
|
196
|
-
assert_equal 'application/json', @response.content_type
|
|
197
|
-
assert_equal expected.to_json, @response.body
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
def test_render_object_using_custom_root
|
|
201
|
-
with_adapter :json do
|
|
202
|
-
get :render_object_using_custom_root
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
|
206
|
-
assert_equal 'application/json', @response.content_type
|
|
207
|
-
assert_equal expected.to_json, @response.body
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
def test_render_json_object_without_serializer
|
|
211
|
-
get :render_json_object_without_serializer
|
|
212
|
-
|
|
213
|
-
assert_equal 'application/json', @response.content_type
|
|
214
|
-
expected_body = { error: 'Result is Invalid' }
|
|
215
|
-
assert_equal expected_body.to_json, @response.body
|
|
216
|
-
end
|
|
217
|
-
|
|
218
|
-
def test_render_json_array_object_without_serializer
|
|
219
|
-
get :render_json_array_object_without_serializer
|
|
220
|
-
|
|
221
|
-
assert_equal 'application/json', @response.content_type
|
|
222
|
-
expected_body = [{ error: 'Result is Invalid' }]
|
|
223
|
-
assert_equal expected_body.to_json, @response.body
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
def test_render_array_using_implicit_serializer
|
|
227
|
-
get :render_array_using_implicit_serializer
|
|
228
|
-
assert_equal 'application/json', @response.content_type
|
|
229
|
-
|
|
230
|
-
expected = [
|
|
231
|
-
{
|
|
232
|
-
name: 'Name 1',
|
|
233
|
-
description: 'Description 1'
|
|
234
|
-
},
|
|
235
|
-
{
|
|
236
|
-
name: 'Name 2',
|
|
237
|
-
description: 'Description 2'
|
|
238
|
-
}
|
|
239
|
-
]
|
|
240
|
-
|
|
241
|
-
assert_equal expected.to_json, @response.body
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
def test_render_array_using_implicit_serializer_and_meta
|
|
245
|
-
with_adapter :json_api do
|
|
246
|
-
get :render_array_using_implicit_serializer_and_meta
|
|
247
|
-
end
|
|
248
|
-
expected = {
|
|
249
|
-
data: [
|
|
250
|
-
{
|
|
251
|
-
id: @controller.instance_variable_get(:@profiles).first.id.to_s,
|
|
252
|
-
type: 'profiles',
|
|
253
|
-
attributes: {
|
|
254
|
-
name: 'Name 1',
|
|
255
|
-
description: 'Description 1'
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
],
|
|
259
|
-
meta: {
|
|
260
|
-
total: 10
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
assert_equal 'application/json', @response.content_type
|
|
265
|
-
assert_equal expected.to_json, @response.body
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
def test_render_array_using_implicit_serializer_and_links
|
|
269
|
-
get :render_array_using_implicit_serializer_and_links
|
|
270
|
-
|
|
271
|
-
expected = {
|
|
272
|
-
data: [
|
|
273
|
-
{
|
|
274
|
-
id: @controller.instance_variable_get(:@profiles).first.id.to_s,
|
|
275
|
-
type: 'profiles',
|
|
276
|
-
attributes: {
|
|
277
|
-
name: 'Name 1',
|
|
278
|
-
description: 'Description 1'
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
],
|
|
282
|
-
links: {
|
|
283
|
-
self: 'http://example.com/api/profiles/1'
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
assert_equal 'application/json', @response.content_type
|
|
288
|
-
assert_equal expected.to_json, @response.body
|
|
289
|
-
end
|
|
290
|
-
|
|
291
|
-
def test_render_with_cache_enable
|
|
292
|
-
expected = {
|
|
293
|
-
id: 1,
|
|
294
|
-
title: 'New Post',
|
|
295
|
-
body: 'Body',
|
|
296
|
-
comments: [
|
|
297
|
-
{
|
|
298
|
-
id: 1,
|
|
299
|
-
body: 'ZOMG A COMMENT'
|
|
300
|
-
}
|
|
301
|
-
],
|
|
302
|
-
blog: {
|
|
303
|
-
id: 999,
|
|
304
|
-
name: 'Custom blog'
|
|
305
|
-
},
|
|
306
|
-
author: {
|
|
307
|
-
id: 1,
|
|
308
|
-
name: 'Joao Moura.'
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
ActionController::Base.cache_store.clear
|
|
313
|
-
Timecop.freeze(Time.zone.now) do
|
|
314
|
-
get :render_object_with_cache_enabled
|
|
315
|
-
|
|
316
|
-
assert_equal 'application/json', @response.content_type
|
|
317
|
-
assert_equal expected.to_json, @response.body
|
|
318
|
-
|
|
319
|
-
get :render_changed_object_with_cache_enabled
|
|
320
|
-
assert_equal expected.to_json, @response.body
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
ActionController::Base.cache_store.clear
|
|
324
|
-
get :render_changed_object_with_cache_enabled
|
|
325
|
-
assert_not_equal expected.to_json, @response.body
|
|
326
|
-
end
|
|
327
|
-
|
|
328
|
-
def test_render_with_cache_enable_and_expired
|
|
329
|
-
ActionController::Base.cache_store.clear
|
|
330
|
-
get :render_object_expired_with_cache_enabled
|
|
331
|
-
|
|
332
|
-
expected = {
|
|
333
|
-
id: 1,
|
|
334
|
-
title: 'ZOMG a New Post',
|
|
335
|
-
body: 'Body',
|
|
336
|
-
comments: [
|
|
337
|
-
{
|
|
338
|
-
id: 1,
|
|
339
|
-
body: 'ZOMG A COMMENT'
|
|
340
|
-
}
|
|
341
|
-
],
|
|
342
|
-
blog: {
|
|
343
|
-
id: 999,
|
|
344
|
-
name: 'Custom blog'
|
|
345
|
-
},
|
|
346
|
-
author: {
|
|
347
|
-
id: 1,
|
|
348
|
-
name: 'Joao Moura.'
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
assert_equal 'application/json', @response.content_type
|
|
353
|
-
actual = @response.body
|
|
354
|
-
expected = expected.to_json
|
|
355
|
-
if ENV['APPVEYOR'] && actual != expected
|
|
356
|
-
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
|
357
|
-
else
|
|
358
|
-
assert_equal actual, expected
|
|
359
|
-
end
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
def test_render_with_fragment_only_cache_enable
|
|
363
|
-
ActionController::Base.cache_store.clear
|
|
364
|
-
get :render_fragment_changed_object_with_only_cache_enabled
|
|
365
|
-
response = JSON.parse(@response.body)
|
|
366
|
-
|
|
367
|
-
assert_equal 'application/json', @response.content_type
|
|
368
|
-
assert_equal 'ZOMG A ROLE', response['name']
|
|
369
|
-
assert_equal 'HUEHUEBRBR', response['description']
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
def test_render_with_fragment_except_cache_enable
|
|
373
|
-
ActionController::Base.cache_store.clear
|
|
374
|
-
get :render_fragment_changed_object_with_except_cache_enabled
|
|
375
|
-
response = JSON.parse(@response.body)
|
|
376
|
-
|
|
377
|
-
assert_equal 'application/json', @response.content_type
|
|
378
|
-
assert_equal 5, response['rating']
|
|
379
|
-
assert_equal 'lol', response['content']
|
|
380
|
-
end
|
|
381
|
-
|
|
382
|
-
def test_render_fragment_changed_object_with_relationship
|
|
383
|
-
ActionController::Base.cache_store.clear
|
|
384
|
-
|
|
385
|
-
Timecop.freeze(Time.zone.now) do
|
|
386
|
-
get :render_fragment_changed_object_with_relationship
|
|
387
|
-
response = JSON.parse(@response.body)
|
|
388
|
-
|
|
389
|
-
expected_return = {
|
|
390
|
-
'id' => 1,
|
|
391
|
-
'time' => Time.zone.now.to_s,
|
|
392
|
-
'likeable' => {
|
|
393
|
-
'id' => 1,
|
|
394
|
-
'body' => 'ZOMG A COMMENT'
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
assert_equal 'application/json', @response.content_type
|
|
399
|
-
assert_equal expected_return, response
|
|
400
|
-
end
|
|
401
|
-
end
|
|
402
|
-
|
|
403
|
-
def test_cache_expiration_on_update
|
|
404
|
-
ActionController::Base.cache_store.clear
|
|
405
|
-
get :render_object_with_cache_enabled
|
|
406
|
-
|
|
407
|
-
expected = {
|
|
408
|
-
id: 1,
|
|
409
|
-
title: 'ZOMG a New Post',
|
|
410
|
-
body: 'Body',
|
|
411
|
-
comments: [
|
|
412
|
-
{
|
|
413
|
-
id: 1,
|
|
414
|
-
body: 'ZOMG A COMMENT'
|
|
415
|
-
}
|
|
416
|
-
],
|
|
417
|
-
blog: {
|
|
418
|
-
id: 999,
|
|
419
|
-
name: 'Custom blog'
|
|
420
|
-
},
|
|
421
|
-
author: {
|
|
422
|
-
id: 1,
|
|
423
|
-
name: 'Joao Moura.'
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
get :update_and_render_object_with_cache_enabled
|
|
428
|
-
|
|
429
|
-
assert_equal 'application/json', @response.content_type
|
|
430
|
-
actual = @response.body
|
|
431
|
-
expected = expected.to_json
|
|
432
|
-
if ENV['APPVEYOR'] && actual != expected
|
|
433
|
-
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
|
434
|
-
else
|
|
435
|
-
assert_equal actual, expected
|
|
436
|
-
end
|
|
437
|
-
end
|
|
438
|
-
|
|
439
|
-
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
|
|
440
|
-
controller = Class.new(ImplicitSerializationTestController) do
|
|
441
|
-
def use_adapter?
|
|
442
|
-
false
|
|
443
|
-
end
|
|
444
|
-
end.new
|
|
445
|
-
assert_output(nil, /adapter: false/) do
|
|
446
|
-
controller.get_serializer(Profile.new)
|
|
447
|
-
end
|
|
448
|
-
end
|
|
449
|
-
|
|
450
|
-
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
|
451
|
-
controller = Class.new(ImplicitSerializationTestController) do
|
|
452
|
-
def use_adapter?
|
|
453
|
-
true
|
|
454
|
-
end
|
|
455
|
-
end.new
|
|
456
|
-
assert_output(nil, '') do
|
|
457
|
-
controller.get_serializer(Profile.new)
|
|
458
|
-
end
|
|
459
|
-
end
|
|
460
|
-
|
|
461
|
-
def test_render_event_is_emitted
|
|
462
|
-
subscriber = ::ActiveSupport::Notifications.subscribe('render.active_model_serializers') do |subscribed_event|
|
|
463
|
-
@subscribed_event = subscribed_event
|
|
464
|
-
end
|
|
465
|
-
|
|
466
|
-
get :render_using_implicit_serializer
|
|
467
|
-
|
|
468
|
-
subscribed_event_name =
|
|
469
|
-
if @subscribed_event.is_a?(String)
|
|
470
|
-
@subscribed_event
|
|
471
|
-
else
|
|
472
|
-
@subscribed_event.name # is a ActiveSupport::Notifications::Event
|
|
473
|
-
end
|
|
474
|
-
assert_equal 'render.active_model_serializers', subscribed_event_name
|
|
475
|
-
ensure
|
|
476
|
-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
|
|
477
|
-
end
|
|
478
|
-
end
|
|
479
|
-
end
|
|
480
|
-
end
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'test_helper'
|
|
4
|
-
|
|
5
|
-
module ActiveModelSerializers
|
|
6
|
-
class AdapterForTest < ::ActiveSupport::TestCase
|
|
7
|
-
UnknownAdapterError = ::ActiveModelSerializers::Adapter::UnknownAdapterError
|
|
8
|
-
|
|
9
|
-
def test_serializer_adapter_returns_configured_adapter
|
|
10
|
-
assert_output(nil, /ActiveModelSerializers::Adapter.configured_adapter/) do
|
|
11
|
-
assert_equal ActiveModelSerializers::Adapter.configured_adapter, ActiveModel::Serializer.adapter
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def test_returns_default_adapter
|
|
16
|
-
with_adapter_config_setup do
|
|
17
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
18
|
-
assert_equal ActiveModelSerializers::Adapter::Attributes, adapter
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def test_overwrite_adapter_with_symbol
|
|
23
|
-
with_adapter_config_setup do
|
|
24
|
-
ActiveModelSerializers.config.adapter = :null
|
|
25
|
-
|
|
26
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
27
|
-
assert_equal ActiveModelSerializers::Adapter::Null, adapter
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def test_overwrite_adapter_with_camelcased_symbol
|
|
32
|
-
with_adapter_config_setup do
|
|
33
|
-
ActiveModelSerializers.config.adapter = :JsonApi
|
|
34
|
-
|
|
35
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
36
|
-
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def test_overwrite_adapter_with_string
|
|
41
|
-
with_adapter_config_setup do
|
|
42
|
-
ActiveModelSerializers.config.adapter = 'json_api'
|
|
43
|
-
|
|
44
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
45
|
-
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def test_overwrite_adapter_with_a_camelcased_string
|
|
50
|
-
with_adapter_config_setup do
|
|
51
|
-
ActiveModelSerializers.config.adapter = 'JsonApi'
|
|
52
|
-
|
|
53
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
54
|
-
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def test_overwrite_adapter_with_class
|
|
59
|
-
with_adapter_config_setup do
|
|
60
|
-
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::Null
|
|
61
|
-
|
|
62
|
-
adapter = ActiveModelSerializers::Adapter.configured_adapter
|
|
63
|
-
assert_equal ActiveModelSerializers::Adapter::Null, adapter
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def test_raises_exception_if_invalid_symbol_given
|
|
68
|
-
with_adapter_config_setup do
|
|
69
|
-
ActiveModelSerializers.config.adapter = :unknown
|
|
70
|
-
|
|
71
|
-
assert_raises UnknownAdapterError do
|
|
72
|
-
ActiveModelSerializers::Adapter.configured_adapter
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter
|
|
78
|
-
with_adapter_config_setup do
|
|
79
|
-
ActiveModelSerializers.config.adapter = 42
|
|
80
|
-
|
|
81
|
-
assert_raises UnknownAdapterError do
|
|
82
|
-
ActiveModelSerializers::Adapter.configured_adapter
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def test_adapter_class_for_known_adapter
|
|
88
|
-
klass = ActiveModelSerializers::Adapter.adapter_class(:json_api)
|
|
89
|
-
assert_equal ActiveModelSerializers::Adapter::JsonApi, klass
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def test_adapter_class_for_unknown_adapter
|
|
93
|
-
assert_raises UnknownAdapterError do
|
|
94
|
-
ActiveModelSerializers::Adapter.adapter_class(:json_simple)
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def test_adapter_map
|
|
99
|
-
expected_adapter_map = {
|
|
100
|
-
'null'.freeze => ActiveModelSerializers::Adapter::Null,
|
|
101
|
-
'json'.freeze => ActiveModelSerializers::Adapter::Json,
|
|
102
|
-
'attributes'.freeze => ActiveModelSerializers::Adapter::Attributes,
|
|
103
|
-
'json_api'.freeze => ActiveModelSerializers::Adapter::JsonApi
|
|
104
|
-
}
|
|
105
|
-
actual = ActiveModelSerializers::Adapter.adapter_map
|
|
106
|
-
assert_equal actual, expected_adapter_map
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def test_adapters
|
|
110
|
-
assert_equal ActiveModelSerializers::Adapter.adapters.sort, [
|
|
111
|
-
'attributes'.freeze,
|
|
112
|
-
'json'.freeze,
|
|
113
|
-
'json_api'.freeze,
|
|
114
|
-
'null'.freeze
|
|
115
|
-
]
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def test_lookup_adapter_by_string_name
|
|
119
|
-
assert_equal ActiveModelSerializers::Adapter.lookup('json'.freeze), ActiveModelSerializers::Adapter::Json
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
def test_lookup_adapter_by_symbol_name
|
|
123
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(:json), ActiveModelSerializers::Adapter::Json
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def test_lookup_adapter_by_class
|
|
127
|
-
klass = ActiveModelSerializers::Adapter::Json
|
|
128
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(klass), klass
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def test_lookup_adapter_from_environment_registers_adapter
|
|
132
|
-
ActiveModelSerializers::Adapter.const_set(:AdapterFromEnvironment, Class.new)
|
|
133
|
-
klass = ::ActiveModelSerializers::Adapter::AdapterFromEnvironment
|
|
134
|
-
name = 'adapter_from_environment'.freeze
|
|
135
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(name), klass
|
|
136
|
-
assert ActiveModelSerializers::Adapter.adapters.include?(name)
|
|
137
|
-
ensure
|
|
138
|
-
ActiveModelSerializers::Adapter.adapter_map.delete(name)
|
|
139
|
-
ActiveModelSerializers::Adapter.send(:remove_const, :AdapterFromEnvironment)
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
def test_lookup_adapter_for_unknown_name
|
|
143
|
-
assert_raises UnknownAdapterError do
|
|
144
|
-
ActiveModelSerializers::Adapter.lookup(:json_simple)
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def test_adapter
|
|
149
|
-
assert_equal ActiveModelSerializers.config.adapter, :attributes
|
|
150
|
-
assert_equal ActiveModelSerializers::Adapter.configured_adapter, ActiveModelSerializers::Adapter::Attributes
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def test_register_adapter
|
|
154
|
-
new_adapter_name = :foo
|
|
155
|
-
new_adapter_klass = Class.new
|
|
156
|
-
ActiveModelSerializers::Adapter.register(new_adapter_name, new_adapter_klass)
|
|
157
|
-
assert ActiveModelSerializers::Adapter.adapters.include?('foo'.freeze)
|
|
158
|
-
assert ActiveModelSerializers::Adapter.lookup(:foo), new_adapter_klass
|
|
159
|
-
ensure
|
|
160
|
-
ActiveModelSerializers::Adapter.adapter_map.delete(new_adapter_name.to_s)
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def test_inherited_adapter_hooks_register_adapter
|
|
164
|
-
Object.const_set(:MyAdapter, Class.new)
|
|
165
|
-
my_adapter = MyAdapter
|
|
166
|
-
ActiveModelSerializers::Adapter::Base.inherited(my_adapter)
|
|
167
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(:my_adapter), my_adapter
|
|
168
|
-
ensure
|
|
169
|
-
ActiveModelSerializers::Adapter.adapter_map.delete('my_adapter'.freeze)
|
|
170
|
-
Object.send(:remove_const, :MyAdapter)
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
def test_inherited_adapter_hooks_register_namespaced_adapter
|
|
174
|
-
Object.const_set(:MyNamespace, Module.new)
|
|
175
|
-
MyNamespace.const_set(:MyAdapter, Class.new)
|
|
176
|
-
my_adapter = MyNamespace::MyAdapter
|
|
177
|
-
ActiveModelSerializers::Adapter::Base.inherited(my_adapter)
|
|
178
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(:'my_namespace/my_adapter'), my_adapter
|
|
179
|
-
ensure
|
|
180
|
-
ActiveModelSerializers::Adapter.adapter_map.delete('my_namespace/my_adapter'.freeze)
|
|
181
|
-
MyNamespace.send(:remove_const, :MyAdapter)
|
|
182
|
-
Object.send(:remove_const, :MyNamespace)
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
def test_inherited_adapter_hooks_register_subclass_of_registered_adapter
|
|
186
|
-
Object.const_set(:MyAdapter, Class.new)
|
|
187
|
-
my_adapter = MyAdapter
|
|
188
|
-
Object.const_set(:MySubclassedAdapter, Class.new(MyAdapter))
|
|
189
|
-
my_subclassed_adapter = MySubclassedAdapter
|
|
190
|
-
ActiveModelSerializers::Adapter::Base.inherited(my_adapter)
|
|
191
|
-
ActiveModelSerializers::Adapter::Base.inherited(my_subclassed_adapter)
|
|
192
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(:my_adapter), my_adapter
|
|
193
|
-
assert_equal ActiveModelSerializers::Adapter.lookup(:my_subclassed_adapter), my_subclassed_adapter
|
|
194
|
-
ensure
|
|
195
|
-
ActiveModelSerializers::Adapter.adapter_map.delete('my_adapter'.freeze)
|
|
196
|
-
ActiveModelSerializers::Adapter.adapter_map.delete('my_subclassed_adapter'.freeze)
|
|
197
|
-
Object.send(:remove_const, :MyAdapter)
|
|
198
|
-
Object.send(:remove_const, :MySubclassedAdapter)
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
private
|
|
202
|
-
|
|
203
|
-
def with_adapter_config_setup
|
|
204
|
-
previous_adapter = ActiveModelSerializers.config.adapter
|
|
205
|
-
yield
|
|
206
|
-
ensure
|
|
207
|
-
ActiveModelSerializers.config.adapter = previous_adapter
|
|
208
|
-
end
|
|
209
|
-
end
|
|
210
|
-
end
|