jat 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +36 -4
  3. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +7 -3
  4. data/lib/jat/plugins/_activerecord_preloads/lib/preloader.rb +7 -13
  5. data/lib/jat/plugins/_lower_camel_case/_lower_camel_case.rb +32 -0
  6. data/lib/jat/plugins/_preloads/_preloads.rb +8 -2
  7. data/lib/jat/plugins/cache/cache.rb +9 -5
  8. data/lib/jat/plugins/json_api/json_api.rb +145 -105
  9. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  10. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  11. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  12. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  13. data/lib/jat/plugins/json_api/lib/response.rb +84 -196
  14. data/lib/jat/plugins/json_api/lib/response_piece.rb +166 -0
  15. data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +31 -0
  16. data/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads.rb +16 -24
  17. data/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +30 -0
  18. data/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache.rb +54 -0
  19. data/lib/jat/plugins/json_api_validate_params/json_api_validate_params.rb +57 -0
  20. data/lib/jat/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  21. data/lib/jat/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  22. data/lib/jat/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  23. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  24. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  25. data/lib/jat/plugins/simple_api/lib/response.rb +78 -89
  26. data/lib/jat/plugins/simple_api/lib/response_piece.rb +84 -0
  27. data/lib/jat/plugins/simple_api/simple_api.rb +83 -24
  28. data/lib/jat/plugins/simple_api_activerecord/lib/preloads.rb +55 -0
  29. data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +31 -0
  30. data/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +30 -0
  31. data/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +48 -0
  32. data/lib/jat/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  33. data/lib/jat/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  34. data/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params.rb +45 -0
  35. data/lib/jat/plugins/to_str/to_str.rb +10 -4
  36. data/lib/jat/plugins.rb +3 -16
  37. data/lib/jat.rb +28 -30
  38. data/test/lib/jat/attribute_test.rb +15 -5
  39. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +34 -15
  40. data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +10 -24
  41. data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
  42. data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +1 -1
  43. data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +1 -1
  44. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  45. data/test/lib/jat/plugins/json_api/json_api_test.rb +63 -47
  46. data/test/lib/jat/plugins/json_api/lib/{params/fields_test.rb → fields_param_parser_test.rb} +7 -6
  47. data/test/lib/jat/plugins/json_api/lib/{params/include_test.rb → include_param_parser_test.rb} +4 -4
  48. data/test/lib/jat/plugins/json_api/lib/map_test.rb +150 -79
  49. data/test/lib/jat/plugins/json_api/lib/response_test.rb +32 -32
  50. data/test/lib/jat/plugins/{_json_api_activerecord/_json_api_activerecord_test.rb → json_api_activerecord/json_api_activerecord_test.rb} +14 -5
  51. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads_test.rb +11 -10
  52. data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
  53. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  54. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
  55. data/test/lib/jat/plugins/simple_api/lib/{params/parse_test.rb → fields_param_parser_test.rb} +10 -4
  56. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +111 -34
  57. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +80 -74
  58. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +91 -25
  59. data/test/lib/jat/plugins/simple_api_activerecord/lib/preloads_test.rb +135 -0
  60. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +38 -0
  61. data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
  62. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  63. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
  64. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  65. data/test/lib/jat_test.rb +47 -24
  66. data/test/lib/plugin_test.rb +3 -3
  67. data/test/test_helper.rb +0 -3
  68. data/test/test_plugin.rb +9 -12
  69. metadata +60 -71
  70. data/CHANGELOG.md +0 -7
  71. data/README.md +0 -21
  72. data/jat.gemspec +0 -37
  73. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  74. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  75. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  76. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  77. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  78. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  79. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  80. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  81. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  82. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  83. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  84. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  85. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  86. data/lib/jat/presenter.rb +0 -51
  87. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  88. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  89. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  90. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  91. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  92. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  93. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  94. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  95. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  96. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  97. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  98. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  99. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  100. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  101. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  102. data/test/lib/jat/presenter_test.rb +0 -61
@@ -14,7 +14,7 @@ describe "Jat::Plugins::JsonApi::Response" do
14
14
  it "returns correct structure with data" do
15
15
  str_serializer = Class.new(base_class) do
16
16
  type "str"
17
- attribute(:id) { |_| "STRING" }
17
+ id { |_| "STRING" }
18
18
  end
19
19
 
20
20
  assert_equal({data: {type: :str, id: "STRING"}}, str_serializer.to_h("STRING"))
@@ -23,7 +23,7 @@ describe "Jat::Plugins::JsonApi::Response" do
23
23
  it "returns correct structure with array data" do
24
24
  str_serializer = Class.new(base_class) do
25
25
  type "str"
26
- attribute(:id) { |obj| obj }
26
+ id { |obj| obj }
27
27
  end
28
28
 
29
29
  assert_equal(
@@ -35,7 +35,7 @@ describe "Jat::Plugins::JsonApi::Response" do
35
35
  it "returns correct structure with data with attributes" do
36
36
  str_serializer = Class.new(base_class) do
37
37
  type "str"
38
- attribute(:id) { |obj| obj[0] }
38
+ id { |obj| obj[0] }
39
39
  attribute :length
40
40
  end
41
41
 
@@ -48,12 +48,12 @@ describe "Jat::Plugins::JsonApi::Response" do
48
48
  it "returns correct structure with has-one relationship" do
49
49
  int_serializer = Class.new(base_class) do
50
50
  type "int"
51
- attribute(:id) { |obj| obj }
51
+ id { |obj| obj }
52
52
  end
53
53
 
54
54
  str_serializer = Class.new(base_class) do
55
55
  type "str"
56
- attribute(:id) { |obj| obj[0] }
56
+ id { |obj| obj[0] }
57
57
  relationship :length, serializer: int_serializer, exposed: true
58
58
  end
59
59
 
@@ -76,12 +76,12 @@ describe "Jat::Plugins::JsonApi::Response" do
76
76
  it "does not return has-one relationship when not exposed" do
77
77
  int_serializer = Class.new(base_class) do
78
78
  type "int"
79
- attribute(:id) { |obj| obj }
79
+ id { |obj| obj }
80
80
  end
81
81
 
82
82
  str_serializer = Class.new(base_class) do
83
83
  type "str"
84
- attribute(:id) { |obj| obj[0] }
84
+ id { |obj| obj[0] }
85
85
  relationship :length, serializer: int_serializer # relationships are not exposed by default
86
86
  end
87
87
 
@@ -91,7 +91,7 @@ describe "Jat::Plugins::JsonApi::Response" do
91
91
  it "returns correct structure with empty has-one relationship" do
92
92
  str_serializer = Class.new(base_class) do
93
93
  type "str"
94
- attribute(:id) { |obj| obj[0] }
94
+ id { |obj| obj[0] }
95
95
  relationship(:length, serializer: self, exposed: true) { |_obj| nil }
96
96
  end
97
97
 
@@ -109,13 +109,13 @@ describe "Jat::Plugins::JsonApi::Response" do
109
109
  it "returns correct structure with has-one relationship with attributes" do
110
110
  int_serializer = Class.new(base_class) do
111
111
  type "int"
112
- attribute(:id) { |obj| obj }
112
+ id { |obj| obj }
113
113
  attribute(:next) { |obj| obj + 1 }
114
114
  end
115
115
 
116
116
  str_serializer = Class.new(base_class) do
117
117
  type "str"
118
- attribute(:id) { |obj| obj[0] }
118
+ id { |obj| obj[0] }
119
119
  relationship :length, serializer: int_serializer, exposed: true
120
120
  end
121
121
 
@@ -142,7 +142,7 @@ describe "Jat::Plugins::JsonApi::Response" do
142
142
 
143
143
  str_serializer = Class.new(base_class) do
144
144
  type "str"
145
- attribute(:id) { |_obj| "id" }
145
+ id { |_obj| "id" }
146
146
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
147
147
  end
148
148
 
@@ -160,12 +160,12 @@ describe "Jat::Plugins::JsonApi::Response" do
160
160
  it "returns correct structure with has-many relationship" do
161
161
  chr_serializer = Class.new(base_class) do
162
162
  type "chr"
163
- attribute(:id) { |obj| obj }
163
+ id { |obj| obj }
164
164
  end
165
165
 
166
166
  str_serializer = Class.new(base_class) do
167
167
  type "str"
168
- attribute(:id) { |obj| obj[0] }
168
+ id { |obj| obj[0] }
169
169
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
170
170
  end
171
171
 
@@ -188,13 +188,13 @@ describe "Jat::Plugins::JsonApi::Response" do
188
188
  it "returns correct structure with has-many relationship with attributes" do
189
189
  chr_serializer = Class.new(base_class) do
190
190
  type "chr"
191
- attribute(:id) { |obj| obj }
191
+ id { |obj| obj }
192
192
  attribute :next
193
193
  end
194
194
 
195
195
  str_serializer = Class.new(base_class) do
196
196
  type "str"
197
- attribute(:id) { |obj| obj[0] }
197
+ id { |obj| obj[0] }
198
198
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
199
199
  end
200
200
 
@@ -218,12 +218,12 @@ describe "Jat::Plugins::JsonApi::Response" do
218
218
  it "accepts includes param" do
219
219
  chr_serializer = Class.new(base_class) do
220
220
  type "chr"
221
- attribute(:id) { |obj| obj }
221
+ id { |obj| obj }
222
222
  end
223
223
 
224
224
  str_serializer = Class.new(base_class) do
225
225
  type "str"
226
- attribute(:id) { |obj| obj[0] }
226
+ id { |obj| obj[0] }
227
227
  relationship :chars, serializer: chr_serializer, many: true, exposed: false
228
228
  end
229
229
 
@@ -239,19 +239,19 @@ describe "Jat::Plugins::JsonApi::Response" do
239
239
  {type: :chr, id: "a"}, {type: :chr, id: "b"}
240
240
  ]
241
241
  },
242
- str_serializer.to_h("ab", params: {include: "chars"})
242
+ str_serializer.to_h("ab", include: "chars")
243
243
  )
244
244
  end
245
245
 
246
246
  it "accepts sparse_fieldset" do
247
247
  chr_serializer = Class.new(base_class) do
248
248
  type "chr"
249
- attribute(:id) { |obj| obj }
249
+ id { |obj| obj }
250
250
  end
251
251
 
252
252
  str_serializer = Class.new(base_class) do
253
253
  type "str"
254
- attribute(:id) { |obj| obj[0] }
254
+ id { |obj| obj[0] }
255
255
  relationship :chars, serializer: chr_serializer, many: true, exposed: false
256
256
  end
257
257
 
@@ -267,7 +267,7 @@ describe "Jat::Plugins::JsonApi::Response" do
267
267
  {type: :chr, id: "a"}, {type: :chr, id: "b"}
268
268
  ]
269
269
  },
270
- str_serializer.to_h("ab", params: {fields: {str: "chars"}})
270
+ str_serializer.to_h("ab", fields: {str: "chars"})
271
271
  )
272
272
  end
273
273
 
@@ -275,7 +275,7 @@ describe "Jat::Plugins::JsonApi::Response" do
275
275
  let(:serializer) do
276
276
  Class.new(base_class) do
277
277
  type :foo
278
- attribute(:id) { |obj| obj }
278
+ id { |obj| obj }
279
279
  end
280
280
  end
281
281
 
@@ -311,7 +311,7 @@ describe "Jat::Plugins::JsonApi::Response" do
311
311
  let(:serializer) do
312
312
  Class.new(base_class) do
313
313
  type :foo
314
- attribute(:id) { |obj| obj }
314
+ id { |obj| obj }
315
315
  end
316
316
  end
317
317
 
@@ -347,7 +347,7 @@ describe "Jat::Plugins::JsonApi::Response" do
347
347
  let(:serializer) do
348
348
  Class.new(base_class) do
349
349
  type :foo
350
- attribute(:id) { |obj| obj }
350
+ id { |obj| obj }
351
351
  end
352
352
  end
353
353
 
@@ -374,21 +374,21 @@ describe "Jat::Plugins::JsonApi::Response" do
374
374
  let(:bar_serializer) do
375
375
  Class.new(base_class) do
376
376
  type :bar
377
- attribute(:id) { |obj| obj }
377
+ id { |obj| obj }
378
378
  end
379
379
  end
380
380
 
381
381
  let(:foo_serializer) do
382
382
  Class.new(base_class) do
383
383
  type :foo
384
- attribute(:id) { |obj| obj }
384
+ id { |obj| obj }
385
385
  end
386
386
  end
387
387
 
388
388
  it "adds relationship meta defined in serializer" do
389
389
  foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
390
390
  bar_serializer.relationship_meta(:version) { "1.2.3" }
391
- bar_serializer.relationship_meta(:uid) { |parent, obj, context| [parent, obj, context[:time]] }
391
+ bar_serializer.relationship_meta(:uid) { |obj, context| [context[:parent_object], obj, context[:time]] }
392
392
  bar_serializer.relationship_meta(:null) {}
393
393
 
394
394
  response = foo_serializer.to_h("foo", time: "12:00")
@@ -401,7 +401,7 @@ describe "Jat::Plugins::JsonApi::Response" do
401
401
  let(:serializer) do
402
402
  Class.new(base_class) do
403
403
  type :foo
404
- attribute(:id) { |obj| obj }
404
+ id { |obj| obj }
405
405
  end
406
406
  end
407
407
 
@@ -437,7 +437,7 @@ describe "Jat::Plugins::JsonApi::Response" do
437
437
  let(:serializer) do
438
438
  Class.new(base_class) do
439
439
  type :foo
440
- attribute(:id) { |obj| obj }
440
+ id { |obj| obj }
441
441
  end
442
442
  end
443
443
 
@@ -464,21 +464,21 @@ describe "Jat::Plugins::JsonApi::Response" do
464
464
  let(:bar_serializer) do
465
465
  Class.new(base_class) do
466
466
  type :bar
467
- attribute(:id) { |obj| obj }
467
+ id { |obj| obj }
468
468
  end
469
469
  end
470
470
 
471
471
  let(:foo_serializer) do
472
472
  Class.new(base_class) do
473
473
  type :foo
474
- attribute(:id) { |obj| obj }
474
+ id { |obj| obj }
475
475
  end
476
476
  end
477
477
 
478
478
  it "adds relationship link defined in serializer" do
479
479
  foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
480
480
  bar_serializer.relationship_link(:self) { "/self" }
481
- bar_serializer.relationship_link(:related) { |parent_obj| "/#{parent_obj}/self" }
481
+ bar_serializer.relationship_link(:related) { |_obj, ctx| "/#{ctx[:parent_object]}/self" }
482
482
  bar_serializer.relationship_link(:null) {}
483
483
 
484
484
  response = foo_serializer.to_h("foo")
@@ -4,26 +4,35 @@ require "test_helper"
4
4
 
5
5
  describe "Jat::Plugins::JsonApiActiverecord" do
6
6
  before do
7
- @plugin = Jat::Plugins.load_plugin(:_json_api_activerecord)
7
+ @plugin = Jat::Plugins.find_plugin(:json_api_activerecord)
8
+ end
9
+
10
+ it "checks json_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/json_api/, error.message)
8
14
  end
9
15
 
10
16
  it "loads other plugins in after_load" do
11
17
  jat_class = Class.new(Jat)
18
+ jat_class.plugin :json_api
19
+
12
20
  jat_class.expects(:plugin).with(:_preloads, foo: :bar)
13
21
  jat_class.expects(:plugin).with(:_activerecord_preloads, foo: :bar)
14
22
 
15
- Jat::Plugins.after_load(@plugin, jat_class, foo: :bar)
23
+ @plugin.after_load(jat_class, foo: :bar)
16
24
  end
17
25
 
18
- describe "ClassMethods" do
19
- it "add .jat_preloads method as a delegator to #{@plugin}::Preloads" do
26
+ describe "InstanceMethods" do
27
+ it "add .preloads method as a delegator to #{@plugin}::Preloads" do
20
28
  jat_class = Class.new(Jat)
29
+ jat_class.plugin :json_api
21
30
  jat_class.plugin @plugin
22
31
  jat = jat_class.allocate
23
32
 
24
33
  @plugin::Preloads.expects(:call).with(jat).returns("RES")
25
34
 
26
- assert_equal "RES", jat_class.jat_preloads(jat)
35
+ assert_equal "RES", jat.preloads
27
36
  end
28
37
  end
29
38
  end
@@ -16,7 +16,7 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
16
16
  let(:described_class) { Jat::Plugins::JsonApiActiverecord::Preloads }
17
17
 
18
18
  def define_map(map)
19
- jat_user.traversal_map.expects(:current).returns(map)
19
+ jat_user.expects(:map).returns(map)
20
20
  end
21
21
 
22
22
  it "returns empty hash when no attributes requested" do
@@ -129,7 +129,7 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
129
129
  assert_equal({company: {profile: {}, confirmed_email: {}, unconfirmed_email: {}}}, result)
130
130
  end
131
131
 
132
- it "raises error if with 2 serializers have recursive preloads" do
132
+ it "does not raise error if 2 serializers with recursive preloads specified" do
133
133
  user_serializer.relationship :profile, serializer: profile_serializer, preload: :profile
134
134
  profile_serializer.relationship :user, serializer: user_serializer, preload: :user
135
135
 
@@ -138,11 +138,11 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
138
138
  profile: {attributes: [], relationships: %i[user]}
139
139
  )
140
140
 
141
- error = assert_raises(Jat::Error) { described_class.call(jat_user) }
142
- assert_match(/recursive preloads/, error.message)
141
+ expected_result = {profile: {user: {}}}
142
+ assert_equal(expected_result, described_class.call(jat_user))
143
143
  end
144
144
 
145
- it "raises error if 3 serializers recursive preloads" do
145
+ it "does not raise error if 3 serializers with recursive preloads specified" do
146
146
  user_serializer.relationship :profile, serializer: profile_serializer, preload: :profile
147
147
  profile_serializer.relationship :email, serializer: email_serializer, preload: :email
148
148
  email_serializer.relationship :user, serializer: user_serializer, preload: :user
@@ -153,8 +153,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
153
153
  email: {attributes: [], relationships: %i[user]}
154
154
  )
155
155
 
156
- error = assert_raises(Jat::Error) { described_class.call(jat_user) }
157
- assert_match(/recursive preloads/, error.message)
156
+ expected_result = {profile: {email: {user: {}}}}
157
+ assert_equal(expected_result, described_class.call(jat_user))
158
158
  end
159
159
 
160
160
  it "does not raises error if 2 serializers preloads same preloads" do
@@ -168,7 +168,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
168
168
  email: {attributes: [], relationships: %i[]}
169
169
  )
170
170
 
171
- described_class.call(jat_user) # should not raise
171
+ expected_result = {profile: {email: {}}, email: {}}
172
+ assert_equal(expected_result, described_class.call(jat_user))
172
173
  end
173
174
 
174
175
  it "merges preloads the same way regardless of order of preloads" do
@@ -179,8 +180,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
179
180
  jat_a1 = a.allocate
180
181
  jat_a2 = a.allocate
181
182
 
182
- jat_a1.traversal_map.expects(:current).returns(a: {attributes: %i[a1 a2], relationships: %i[]})
183
- jat_a2.traversal_map.expects(:current).returns(a: {attributes: %i[a2 a1], relationships: %i[]})
183
+ jat_a1.expects(:map).returns(a: {attributes: %i[a1 a2], relationships: %i[]})
184
+ jat_a2.expects(:map).returns(a: {attributes: %i[a2 a1], relationships: %i[]})
184
185
 
185
186
  result1 = described_class.call(jat_a1)
186
187
  result2 = described_class.call(jat_a2)
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiLowerCamelCase" do
6
+ let(:jat_class) do
7
+ new_class = Class.new(Jat)
8
+ new_class.plugin(:json_api)
9
+ new_class.plugin(:json_api_lower_camel_case)
10
+ new_class
11
+ end
12
+
13
+ before do
14
+ jat_class.type :foo
15
+ jat_class.id { |object| object }
16
+ end
17
+
18
+ it "returns attributes in lowerCamelCase case" do
19
+ jat_class.attribute(:foo_bar) { 1 }
20
+
21
+ assert_equal({fooBar: 1}, jat_class.to_h(true).dig(:data, :attributes))
22
+ end
23
+
24
+ it "accepts `fields` in lowerCamelCase format" do
25
+ jat_class.attribute(:foo_bar, exposed: false) { 1 }
26
+
27
+ response = jat_class.to_h(true, fields: {foo: "fooBar"})
28
+ assert_equal({fooBar: 1}, response.dig(:data, :attributes))
29
+ end
30
+
31
+ it "accepts `include` in lowerCamelCase format" do
32
+ new_serializer = Class.new(Jat)
33
+ new_serializer.plugin(:json_api)
34
+ new_serializer.type :new
35
+ new_serializer.id { |object| object }
36
+
37
+ jat_class.relationship(:foo_bar, serializer: new_serializer) { 1 }
38
+
39
+ response = jat_class.to_h(true, include: "fooBar")
40
+ response_relationships = response.dig(:data, :relationships).keys
41
+ assert_includes(response_relationships, :fooBar)
42
+ end
43
+
44
+ it "returns document meta keys in lowerCamelCase format" do
45
+ jat_class.document_meta(:user_agent) { "Firefox" }
46
+
47
+ response = jat_class.to_h(nil)
48
+ assert_equal({meta: {userAgent: "Firefox"}}, response)
49
+ end
50
+
51
+ it "returns document jsonapi keys in lowerCamelCase format" do
52
+ jat_class.jsonapi(:user_agent) { "Firefox" }
53
+
54
+ response = jat_class.to_h(nil)
55
+ assert_equal({jsonapi: {userAgent: "Firefox"}}, response)
56
+ end
57
+
58
+ it "returns document links keys in lowerCamelCase format" do
59
+ jat_class.document_link(:user_agent) { "Firefox" }
60
+
61
+ response = jat_class.to_h(nil)
62
+ assert_equal({links: {userAgent: "Firefox"}}, response)
63
+ end
64
+
65
+ it "returns context meta keys in lowerCamelCase format" do
66
+ response = jat_class.to_h(nil, meta: {user_agent: "Chrome"})
67
+ assert_equal({meta: {userAgent: "Chrome"}}, response)
68
+ end
69
+
70
+ it "returns context jsonapi keys in lowerCamelCase format" do
71
+ response = jat_class.to_h(nil, jsonapi: {user_agent: "Chrome"})
72
+ assert_equal({jsonapi: {userAgent: "Chrome"}}, response)
73
+ end
74
+
75
+ it "returns context links keys in lowerCamelCase format" do
76
+ response = jat_class.to_h(nil, links: {user_agent: "Chrome"})
77
+ assert_equal({links: {userAgent: "Chrome"}}, response)
78
+ end
79
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiMapsCache" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:json_api_maps_cache)
8
+ end
9
+
10
+ it "checks json_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/json_api/, error.message)
14
+ end
15
+
16
+ it "adds config variable how many maps to store per serializer" do
17
+ jat_class = Class.new(Jat)
18
+
19
+ @plugin.after_load(jat_class)
20
+ assert_equal(100, jat_class.config[:cached_maps_count]) # default 100
21
+
22
+ @plugin.after_load(jat_class, cached_maps_count: 10) # change value via opts
23
+ assert_equal(10, jat_class.config[:cached_maps_count])
24
+ end
25
+
26
+ describe "Test maps responses" do
27
+ let(:jat_class) do
28
+ Class.new(Jat) do
29
+ plugin(:json_api)
30
+ plugin(:json_api_maps_cache)
31
+
32
+ type :foo
33
+
34
+ attribute(:attr1)
35
+ attribute(:attr2)
36
+
37
+ relationship(:rel1, serializer: self)
38
+ relationship(:rel2, serializer: self)
39
+ end
40
+ end
41
+
42
+ it "returns same maps when requested with same params" do
43
+ full_map1 = jat_class::Map.call(exposed: :all)
44
+ full_map2 = jat_class::Map.call(exposed: :all)
45
+
46
+ exposed_map1 = jat_class::Map.call(exposed: :default)
47
+ exposed_map2 = jat_class::Map.call(exposed: :default)
48
+
49
+ current_map1 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
50
+ current_map2 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
51
+
52
+ assert_same(full_map1, full_map2)
53
+ assert_same(exposed_map1, exposed_map2)
54
+ assert_same(current_map1, current_map2)
55
+
56
+ # check different maps are not same
57
+
58
+ # should not match, include is not same
59
+ current_map3 = jat_class::Map.call(include: "rel1", fields: {foo: "attr1,rel1"})
60
+ # should not match, fields are not same
61
+ current_map4 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel2"})
62
+
63
+ assert !current_map1.equal?(current_map3)
64
+ assert !current_map1.equal?(current_map4)
65
+ assert !current_map3.equal?(current_map4)
66
+ end
67
+
68
+ it "stores different maps keys" do
69
+ # key 1
70
+ jat_class::Map.call(exposed: :all)
71
+ jat_class::Map.call(exposed: :all)
72
+
73
+ # key 2
74
+ jat_class::Map.call(exposed: :default)
75
+ jat_class::Map.call(exposed: :default)
76
+
77
+ # key 3
78
+ jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
79
+ jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
80
+
81
+ # key 4
82
+ jat_class::Map.call(include: "rel1", fields: {foo: "attr1,rel1"})
83
+
84
+ # key 5
85
+ jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel2"})
86
+
87
+ assert_equal 5, jat_class::Map.maps_cache.keys.count
88
+ end
89
+
90
+ it "clears old results when there are too many cache keys" do
91
+ jat_class.config[:cached_maps_count] = 1
92
+
93
+ full_map1 = jat_class::Map.call(exposed: :all)
94
+ full_map2 = jat_class::Map.call(exposed: :all)
95
+
96
+ # ensure maps refer to same object
97
+ assert_same(full_map1, full_map2)
98
+
99
+ # replace single possible cache key with another `exposed` map
100
+ jat_class::Map.call(exposed: :default)
101
+
102
+ # calculate full map again, it should not match
103
+ full_map3 = jat_class::Map.call(exposed: :all)
104
+ assert !full_map1.equal?(full_map3)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiValidateParams" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:json_api_validate_params)
8
+ end
9
+
10
+ it "checks json_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/json_api/, error.message)
14
+ end
15
+
16
+ let(:serializer) do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :json_api
19
+ jat_class.plugin @plugin
20
+
21
+ jat_class.type "foo"
22
+ jat_class.attribute :foo_bar
23
+ jat_class.relationship :foo_bazz, serializer: jat_class
24
+ jat_class
25
+ end
26
+
27
+ let(:serializer_lower_camel_case) do
28
+ jat_class = Class.new(Jat)
29
+ jat_class.plugin :json_api
30
+ jat_class.plugin :json_api_lower_camel_case
31
+ jat_class.plugin @plugin
32
+
33
+ jat_class.type "foo"
34
+ jat_class.attribute :foo_bar
35
+ jat_class.relationship :foo_bazz, serializer: jat_class
36
+ jat_class
37
+ end
38
+
39
+ it "validates fields types" do
40
+ jat = serializer.new(fields: {bar: "any"})
41
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
42
+
43
+ expected_message = "Response does not have resources with type 'bar'. Existing types are: 'foo'"
44
+ assert_equal(expected_message, error.message)
45
+ end
46
+
47
+ it "validates attributes" do
48
+ jat = serializer.new(fields: {foo: "foo_bar,foo_baz"})
49
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
50
+
51
+ expected_message = "No attribute 'foo_baz' in resource type 'foo'. Existing attributes are: 'foo_bar', 'foo_bazz'"
52
+ assert_equal(expected_message, error.message)
53
+ end
54
+
55
+ it "validates attributes correctly when lower_camel_case plugin loaded" do
56
+ jat = serializer_lower_camel_case.new(fields: {foo: "fooBar,fooBaz"})
57
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
58
+
59
+ expected_message = "No attribute 'fooBaz' in resource type 'foo'. Existing attributes are: 'fooBar', 'fooBazz'"
60
+ assert_equal(expected_message, error.message)
61
+ end
62
+
63
+ it "validates includes" do
64
+ jat = serializer.new(include: "extra")
65
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
66
+ expected_message = "Type 'foo' has no included 'extra' relationship. Existing relationships are: 'foo_bazz'"
67
+ assert_equal(expected_message, error.message)
68
+ end
69
+
70
+ it "validates includes when parameter named as existing attribute (not relationship)" do
71
+ jat = serializer.new(include: "foo_bar")
72
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
73
+ expected_message = "Type 'foo' has no included 'foo_bar' relationship. Existing relationships are: 'foo_bazz'"
74
+ assert_equal(expected_message, error.message)
75
+ end
76
+
77
+ it "validates includes correctly when lower_camel_case plugin loaded" do
78
+ jat = serializer_lower_camel_case.new(include: "extra")
79
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
80
+
81
+ expected_message = "Type 'foo' has no included 'extra' relationship. Existing relationships are: 'fooBazz'"
82
+ assert_equal(expected_message, error.message)
83
+ end
84
+ end
@@ -2,13 +2,19 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::SimpleApi::Params::Parse" do
5
+ describe "Jat::Plugins::SimpleApi::FieldsParamParser" do
6
6
  before do
7
- Jat::Plugins.load_plugin :simple_api
7
+ Jat::Plugins.find_plugin :simple_api
8
+ end
9
+
10
+ let(:jat_class) do
11
+ ser = Class.new(Jat)
12
+ ser.plugin :simple_api
13
+ ser
8
14
  end
9
15
 
10
16
  def parse(str)
11
- Jat::Plugins::SimpleApi::Params::Parse.new(str).parse
17
+ jat_class::FieldsParamParser.parse(str)
12
18
  end
13
19
 
14
20
  it "returns empty hash when nil provided" do
@@ -24,7 +30,7 @@ describe "Jat::Plugins::SimpleApi::Params::Parse" do
24
30
  end
25
31
 
26
32
  it "parses multiple fields" do
27
- assert_equal({id: {}, name: {}}, parse("id,name"))
33
+ assert_equal({id: {}, name: {}}, parse("id, name"))
28
34
  end
29
35
 
30
36
  it "parses single resource with single field" do