active_model_serializers 0.8.3 → 0.10.8

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