jat 0.0.1 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jat/attribute.rb +107 -0
- data/lib/jat/config.rb +35 -0
- data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
- data/lib/jat/plugins/cache/cache.rb +47 -0
- data/lib/jat/plugins/common/_activerecord_preloads/_activerecord_preloads.rb +38 -0
- data/lib/jat/plugins/common/_activerecord_preloads/lib/preloader.rb +93 -0
- data/lib/jat/plugins/common/_lower_camel_case/_lower_camel_case.rb +36 -0
- data/lib/jat/plugins/common/_preloads/_preloads.rb +63 -0
- data/lib/jat/plugins/common/_preloads/lib/format_user_preloads.rb +52 -0
- data/lib/jat/plugins/common/_preloads/lib/preloads_with_path.rb +78 -0
- data/lib/jat/plugins/json_api/json_api.rb +251 -0
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
- data/lib/jat/plugins/json_api/lib/map.rb +119 -0
- data/lib/jat/plugins/json_api/lib/params/fields/parse.rb +27 -0
- data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +58 -0
- data/lib/jat/plugins/json_api/lib/params/fields.rb +23 -0
- data/lib/jat/plugins/json_api/lib/params/include/parse.rb +55 -0
- data/lib/jat/plugins/json_api/lib/params/include/validate.rb +29 -0
- data/lib/jat/plugins/json_api/lib/params/include.rb +49 -0
- data/lib/jat/plugins/json_api/lib/response.rb +123 -0
- data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/lib/preloads.rb +76 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
- data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
- data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
- data/lib/jat/plugins/preloads/preloads.rb +23 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
- data/lib/jat/plugins/simple_api/lib/map.rb +99 -0
- data/lib/jat/plugins/simple_api/lib/response.rb +119 -0
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
- data/lib/jat/plugins/simple_api/simple_api.rb +125 -0
- data/lib/jat/plugins/to_str/to_str.rb +54 -0
- data/lib/jat/plugins/types/types.rb +54 -0
- data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
- data/lib/jat/plugins.rb +39 -0
- data/lib/jat/utils/enum_deep_dup.rb +29 -0
- data/lib/jat/utils/enum_deep_freeze.rb +19 -0
- data/lib/jat.rb +66 -141
- data/test/lib/jat/attribute_test.rb +152 -0
- data/test/lib/jat/config_test.rb +57 -0
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +59 -0
- data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +84 -0
- data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
- data/test/lib/jat/plugins/_preloads/_preloads_test.rb +68 -0
- data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +47 -0
- data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +33 -0
- data/test/lib/jat/plugins/cache/cache_test.rb +82 -0
- data/test/lib/jat/plugins/json_api/json_api_test.rb +162 -0
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +38 -0
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +41 -0
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +188 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +489 -0
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
- data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +37 -0
- data/test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb +197 -0
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +77 -0
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +133 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +348 -0
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +139 -0
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
- data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
- data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +37 -0
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +52 -0
- data/test/lib/jat/plugins/types/types_test.rb +79 -0
- data/test/lib/jat/utils/enum_deep_dup_test.rb +31 -0
- data/test/lib/jat/utils/enum_deep_freeze_test.rb +28 -0
- data/test/lib/jat_test.rb +143 -0
- data/test/lib/plugin_test.rb +49 -0
- data/test/support/activerecord.rb +24 -0
- data/test/test_helper.rb +13 -0
- data/test/test_plugin.rb +56 -0
- metadata +243 -11
@@ -0,0 +1,489 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi::Response" do
|
6
|
+
let(:base_class) { Class.new(Jat) { plugin :json_api } }
|
7
|
+
|
8
|
+
it "returns empty hash when nothing to serialize" do
|
9
|
+
empty_serializer = Class.new(base_class) { type :foo }
|
10
|
+
|
11
|
+
assert_equal({}, empty_serializer.to_h(nil))
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns correct structure with data" do
|
15
|
+
str_serializer = Class.new(base_class) do
|
16
|
+
type "str"
|
17
|
+
id { |_| "STRING" }
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_equal({data: {type: :str, id: "STRING"}}, str_serializer.to_h("STRING"))
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns correct structure with array data" do
|
24
|
+
str_serializer = Class.new(base_class) do
|
25
|
+
type "str"
|
26
|
+
id { |obj| obj }
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal(
|
30
|
+
{data: [{type: :str, id: "1"}, {type: :str, id: "2"}]},
|
31
|
+
str_serializer.to_h(%w[1 2], many: true)
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns correct structure with data with attributes" do
|
36
|
+
str_serializer = Class.new(base_class) do
|
37
|
+
type "str"
|
38
|
+
id { |obj| obj[0] }
|
39
|
+
attribute :length
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal(
|
43
|
+
{data: {type: :str, id: "S", attributes: {length: 6}}},
|
44
|
+
str_serializer.to_h("STRING")
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns correct structure with has-one relationship" do
|
49
|
+
int_serializer = Class.new(base_class) do
|
50
|
+
type "int"
|
51
|
+
id { |obj| obj }
|
52
|
+
end
|
53
|
+
|
54
|
+
str_serializer = Class.new(base_class) do
|
55
|
+
type "str"
|
56
|
+
id { |obj| obj[0] }
|
57
|
+
relationship :length, serializer: int_serializer, exposed: true
|
58
|
+
end
|
59
|
+
|
60
|
+
assert_equal(
|
61
|
+
{
|
62
|
+
data: {
|
63
|
+
type: :str, id: "S",
|
64
|
+
relationships: {
|
65
|
+
length: {data: {type: :int, id: 6}}
|
66
|
+
}
|
67
|
+
},
|
68
|
+
included: [
|
69
|
+
{type: :int, id: 6}
|
70
|
+
]
|
71
|
+
},
|
72
|
+
str_serializer.to_h("STRING")
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "does not return has-one relationship when not exposed" do
|
77
|
+
int_serializer = Class.new(base_class) do
|
78
|
+
type "int"
|
79
|
+
id { |obj| obj }
|
80
|
+
end
|
81
|
+
|
82
|
+
str_serializer = Class.new(base_class) do
|
83
|
+
type "str"
|
84
|
+
id { |obj| obj[0] }
|
85
|
+
relationship :length, serializer: int_serializer # relationships are not exposed by default
|
86
|
+
end
|
87
|
+
|
88
|
+
assert_equal({data: {type: :str, id: "S"}}, str_serializer.to_h("STRING"))
|
89
|
+
end
|
90
|
+
|
91
|
+
it "returns correct structure with empty has-one relationship" do
|
92
|
+
str_serializer = Class.new(base_class) do
|
93
|
+
type "str"
|
94
|
+
id { |obj| obj[0] }
|
95
|
+
relationship(:length, serializer: self, exposed: true) { |_obj| nil }
|
96
|
+
end
|
97
|
+
|
98
|
+
assert_equal(
|
99
|
+
{
|
100
|
+
data: {
|
101
|
+
type: :str, id: "S",
|
102
|
+
relationships: {length: {data: nil}}
|
103
|
+
}
|
104
|
+
},
|
105
|
+
str_serializer.to_h("STRING")
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns correct structure with has-one relationship with attributes" do
|
110
|
+
int_serializer = Class.new(base_class) do
|
111
|
+
type "int"
|
112
|
+
id { |obj| obj }
|
113
|
+
attribute(:next) { |obj| obj + 1 }
|
114
|
+
end
|
115
|
+
|
116
|
+
str_serializer = Class.new(base_class) do
|
117
|
+
type "str"
|
118
|
+
id { |obj| obj[0] }
|
119
|
+
relationship :length, serializer: int_serializer, exposed: true
|
120
|
+
end
|
121
|
+
|
122
|
+
assert_equal(
|
123
|
+
{
|
124
|
+
data: {
|
125
|
+
type: :str, id: "S",
|
126
|
+
relationships: {
|
127
|
+
length: {data: {type: :int, id: 6}}
|
128
|
+
}
|
129
|
+
},
|
130
|
+
included: [
|
131
|
+
{type: :int, id: 6, attributes: {next: 7}}
|
132
|
+
]
|
133
|
+
},
|
134
|
+
str_serializer.to_h("STRING")
|
135
|
+
)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "returns correct structure with empty has-many relationship" do
|
139
|
+
chr_serializer = Class.new(base_class) do
|
140
|
+
type "chr"
|
141
|
+
end
|
142
|
+
|
143
|
+
str_serializer = Class.new(base_class) do
|
144
|
+
type "str"
|
145
|
+
id { |_obj| "id" }
|
146
|
+
relationship :chars, serializer: chr_serializer, many: true, exposed: true
|
147
|
+
end
|
148
|
+
|
149
|
+
assert_equal(
|
150
|
+
{
|
151
|
+
data: {
|
152
|
+
type: :str, id: "id",
|
153
|
+
relationships: {chars: {data: []}}
|
154
|
+
}
|
155
|
+
},
|
156
|
+
str_serializer.to_h("")
|
157
|
+
)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "returns correct structure with has-many relationship" do
|
161
|
+
chr_serializer = Class.new(base_class) do
|
162
|
+
type "chr"
|
163
|
+
id { |obj| obj }
|
164
|
+
end
|
165
|
+
|
166
|
+
str_serializer = Class.new(base_class) do
|
167
|
+
type "str"
|
168
|
+
id { |obj| obj[0] }
|
169
|
+
relationship :chars, serializer: chr_serializer, many: true, exposed: true
|
170
|
+
end
|
171
|
+
|
172
|
+
assert_equal(
|
173
|
+
{
|
174
|
+
data: {
|
175
|
+
type: :str, id: "a",
|
176
|
+
relationships: {
|
177
|
+
chars: {data: [{type: :chr, id: "a"}, {type: :chr, id: "b"}]}
|
178
|
+
}
|
179
|
+
},
|
180
|
+
included: [
|
181
|
+
{type: :chr, id: "a"}, {type: :chr, id: "b"}
|
182
|
+
]
|
183
|
+
},
|
184
|
+
str_serializer.to_h("ab")
|
185
|
+
)
|
186
|
+
end
|
187
|
+
|
188
|
+
it "returns correct structure with has-many relationship with attributes" do
|
189
|
+
chr_serializer = Class.new(base_class) do
|
190
|
+
type "chr"
|
191
|
+
id { |obj| obj }
|
192
|
+
attribute :next
|
193
|
+
end
|
194
|
+
|
195
|
+
str_serializer = Class.new(base_class) do
|
196
|
+
type "str"
|
197
|
+
id { |obj| obj[0] }
|
198
|
+
relationship :chars, serializer: chr_serializer, many: true, exposed: true
|
199
|
+
end
|
200
|
+
|
201
|
+
assert_equal(
|
202
|
+
{
|
203
|
+
data: {
|
204
|
+
type: :str, id: "a",
|
205
|
+
relationships: {
|
206
|
+
chars: {data: [{type: :chr, id: "a"}, {type: :chr, id: "b"}]}
|
207
|
+
}
|
208
|
+
},
|
209
|
+
included: [
|
210
|
+
{type: :chr, id: "a", attributes: {next: "b"}},
|
211
|
+
{type: :chr, id: "b", attributes: {next: "c"}}
|
212
|
+
]
|
213
|
+
},
|
214
|
+
str_serializer.to_h("ab")
|
215
|
+
)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "accepts includes param" do
|
219
|
+
chr_serializer = Class.new(base_class) do
|
220
|
+
type "chr"
|
221
|
+
id { |obj| obj }
|
222
|
+
end
|
223
|
+
|
224
|
+
str_serializer = Class.new(base_class) do
|
225
|
+
type "str"
|
226
|
+
id { |obj| obj[0] }
|
227
|
+
relationship :chars, serializer: chr_serializer, many: true, exposed: false
|
228
|
+
end
|
229
|
+
|
230
|
+
assert_equal(
|
231
|
+
{
|
232
|
+
data: {
|
233
|
+
type: :str, id: "a",
|
234
|
+
relationships: {
|
235
|
+
chars: {data: [{type: :chr, id: "a"}, {type: :chr, id: "b"}]}
|
236
|
+
}
|
237
|
+
},
|
238
|
+
included: [
|
239
|
+
{type: :chr, id: "a"}, {type: :chr, id: "b"}
|
240
|
+
]
|
241
|
+
},
|
242
|
+
str_serializer.to_h("ab", include: "chars")
|
243
|
+
)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "accepts sparse_fieldset" do
|
247
|
+
chr_serializer = Class.new(base_class) do
|
248
|
+
type "chr"
|
249
|
+
id { |obj| obj }
|
250
|
+
end
|
251
|
+
|
252
|
+
str_serializer = Class.new(base_class) do
|
253
|
+
type "str"
|
254
|
+
id { |obj| obj[0] }
|
255
|
+
relationship :chars, serializer: chr_serializer, many: true, exposed: false
|
256
|
+
end
|
257
|
+
|
258
|
+
assert_equal(
|
259
|
+
{
|
260
|
+
data: {
|
261
|
+
type: :str, id: "a",
|
262
|
+
relationships: {
|
263
|
+
chars: {data: [{type: :chr, id: "a"}, {type: :chr, id: "b"}]}
|
264
|
+
}
|
265
|
+
},
|
266
|
+
included: [
|
267
|
+
{type: :chr, id: "a"}, {type: :chr, id: "b"}
|
268
|
+
]
|
269
|
+
},
|
270
|
+
str_serializer.to_h("ab", fields: {str: "chars"})
|
271
|
+
)
|
272
|
+
end
|
273
|
+
|
274
|
+
describe "json_api" do
|
275
|
+
let(:serializer) do
|
276
|
+
Class.new(base_class) do
|
277
|
+
type :foo
|
278
|
+
id { |obj| obj }
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
it "adds `jsonapi` fields defined in serializer" do
|
283
|
+
serializer.jsonapi(:version) { "1.2.3" }
|
284
|
+
serializer.jsonapi(:uid) { |obj, context| [obj, context[:time]] }
|
285
|
+
|
286
|
+
response = serializer.to_h("bar", time: "12:00")
|
287
|
+
jsonapi = response.dig(:jsonapi)
|
288
|
+
assert_equal({version: "1.2.3", uid: ["bar", "12:00"]}, jsonapi)
|
289
|
+
end
|
290
|
+
|
291
|
+
it "does not overwrite manually added data" do
|
292
|
+
serializer.jsonapi(:version) { "1.2.3" }
|
293
|
+
serializer.jsonapi(:foo) { :bar }
|
294
|
+
|
295
|
+
response = serializer.to_h("bar", jsonapi: {version: "3.2.1"})
|
296
|
+
jsonapi = response.dig(:jsonapi)
|
297
|
+
assert_equal({version: "3.2.1", foo: :bar}, jsonapi)
|
298
|
+
end
|
299
|
+
|
300
|
+
it "does not add jsonapi attributes with nil values" do
|
301
|
+
serializer.jsonapi(:foo) {}
|
302
|
+
serializer.jsonapi(:bar) { false }
|
303
|
+
|
304
|
+
response = serializer.to_h("bar")
|
305
|
+
jsonapi = response.dig(:jsonapi)
|
306
|
+
assert_equal({bar: false}, jsonapi)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
describe "document_meta" do
|
311
|
+
let(:serializer) do
|
312
|
+
Class.new(base_class) do
|
313
|
+
type :foo
|
314
|
+
id { |obj| obj }
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
it "adds document meta defined in serializer" do
|
319
|
+
serializer.document_meta(:version) { "1.2.3" }
|
320
|
+
serializer.document_meta(:uid) { |obj, context| [obj, context[:time]] }
|
321
|
+
|
322
|
+
response = serializer.to_h("bar", time: "12:00")
|
323
|
+
meta = response.dig(:meta)
|
324
|
+
assert_equal({version: "1.2.3", uid: ["bar", "12:00"]}, meta)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "does not overwrite manually added meta" do
|
328
|
+
serializer.document_meta(:version) { "1.2.3" }
|
329
|
+
serializer.document_meta(:foo) { :bar }
|
330
|
+
|
331
|
+
response = serializer.to_h("bar", meta: {version: "3.2.1"})
|
332
|
+
meta = response.dig(:meta)
|
333
|
+
assert_equal({version: "3.2.1", foo: :bar}, meta)
|
334
|
+
end
|
335
|
+
|
336
|
+
it "does not add meta with nil values" do
|
337
|
+
serializer.document_meta(:foo) {}
|
338
|
+
serializer.document_meta(:bar) { false }
|
339
|
+
|
340
|
+
response = serializer.to_h("bar")
|
341
|
+
meta = response.dig(:meta)
|
342
|
+
assert_equal({bar: false}, meta)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
describe "resource meta" do
|
347
|
+
let(:serializer) do
|
348
|
+
Class.new(base_class) do
|
349
|
+
type :foo
|
350
|
+
id { |obj| obj }
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
it "adds meta defined in serializer" do
|
355
|
+
serializer.object_meta(:version) { "1.2.3" }
|
356
|
+
serializer.object_meta(:uid) { |obj, context| [obj, context[:time]] }
|
357
|
+
|
358
|
+
response = serializer.to_h("bar", time: "12:00")
|
359
|
+
meta = response.dig(:data, :meta)
|
360
|
+
assert_equal({version: "1.2.3", uid: ["bar", "12:00"]}, meta)
|
361
|
+
end
|
362
|
+
|
363
|
+
it "does not add meta with nil values" do
|
364
|
+
serializer.object_meta(:foo) {}
|
365
|
+
serializer.object_meta(:bar) { false }
|
366
|
+
|
367
|
+
response = serializer.to_h("bar")
|
368
|
+
meta = response.dig(:data, :meta)
|
369
|
+
assert_equal({bar: false}, meta)
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
describe "relationship meta" do
|
374
|
+
let(:bar_serializer) do
|
375
|
+
Class.new(base_class) do
|
376
|
+
type :bar
|
377
|
+
id { |obj| obj }
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
let(:foo_serializer) do
|
382
|
+
Class.new(base_class) do
|
383
|
+
type :foo
|
384
|
+
id { |obj| obj }
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
it "adds relationship meta defined in serializer" do
|
389
|
+
foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
|
390
|
+
bar_serializer.relationship_meta(:version) { "1.2.3" }
|
391
|
+
bar_serializer.relationship_meta(:uid) { |obj, context| [context[:parent_object], obj, context[:time]] }
|
392
|
+
bar_serializer.relationship_meta(:null) {}
|
393
|
+
|
394
|
+
response = foo_serializer.to_h("foo", time: "12:00")
|
395
|
+
meta = response.dig(:data, :relationships, :bar, :meta)
|
396
|
+
assert_equal({version: "1.2.3", uid: ["foo", "bar", "12:00"]}, meta)
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
describe "document_link" do
|
401
|
+
let(:serializer) do
|
402
|
+
Class.new(base_class) do
|
403
|
+
type :foo
|
404
|
+
id { |obj| obj }
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
it "adds document links defined in serializer" do
|
409
|
+
serializer.document_link(:self) { "/self" }
|
410
|
+
serializer.document_link(:related) { |obj| "/#{obj}/self" }
|
411
|
+
|
412
|
+
response = serializer.to_h("bar")
|
413
|
+
links = response.dig(:links)
|
414
|
+
assert_equal({self: "/self", related: "/bar/self"}, links)
|
415
|
+
end
|
416
|
+
|
417
|
+
it "does not overwrite manually added links" do
|
418
|
+
serializer.document_link(:self) { "/self" }
|
419
|
+
serializer.document_link(:related) { |obj| "/#{obj}/self" }
|
420
|
+
|
421
|
+
response = serializer.to_h("bar", links: {self: "/foo"})
|
422
|
+
links = response.dig(:links)
|
423
|
+
assert_equal({self: "/foo", related: "/bar/self"}, links)
|
424
|
+
end
|
425
|
+
|
426
|
+
it "does not add links with nil values" do
|
427
|
+
serializer.document_link(:foo) {}
|
428
|
+
serializer.document_link(:bar) { false }
|
429
|
+
|
430
|
+
response = serializer.to_h("bar")
|
431
|
+
links = response.dig(:links)
|
432
|
+
assert_equal({bar: false}, links)
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
describe "resource links" do
|
437
|
+
let(:serializer) do
|
438
|
+
Class.new(base_class) do
|
439
|
+
type :foo
|
440
|
+
id { |obj| obj }
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
it "adds links defined in serializer" do
|
445
|
+
serializer.object_link(:self) { "/self" }
|
446
|
+
serializer.object_link(:related) { |obj| "/#{obj}/self" }
|
447
|
+
|
448
|
+
response = serializer.to_h("bar", time: "12:00")
|
449
|
+
links = response.dig(:data, :links)
|
450
|
+
assert_equal({self: "/self", related: "/bar/self"}, links)
|
451
|
+
end
|
452
|
+
|
453
|
+
it "does not add links with nil values" do
|
454
|
+
serializer.object_link(:foo) {}
|
455
|
+
serializer.object_link(:bar) { false }
|
456
|
+
|
457
|
+
response = serializer.to_h("bar")
|
458
|
+
links = response.dig(:data, :links)
|
459
|
+
assert_equal({bar: false}, links)
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
describe "relationship links" do
|
464
|
+
let(:bar_serializer) do
|
465
|
+
Class.new(base_class) do
|
466
|
+
type :bar
|
467
|
+
id { |obj| obj }
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
let(:foo_serializer) do
|
472
|
+
Class.new(base_class) do
|
473
|
+
type :foo
|
474
|
+
id { |obj| obj }
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
it "adds relationship link defined in serializer" do
|
479
|
+
foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
|
480
|
+
bar_serializer.relationship_link(:self) { "/self" }
|
481
|
+
bar_serializer.relationship_link(:related) { |_obj, ctx| "/#{ctx[:parent_object]}/self" }
|
482
|
+
bar_serializer.relationship_link(:null) {}
|
483
|
+
|
484
|
+
response = foo_serializer.to_h("foo")
|
485
|
+
links = response.dig(:data, :relationships, :bar, :links)
|
486
|
+
assert_equal({self: "/self", related: "/foo/self"}, links)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApiActiverecord" do
|
6
|
+
before do
|
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)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "loads other plugins" do
|
17
|
+
jat_class = Class.new(Jat)
|
18
|
+
jat_class.plugin :json_api
|
19
|
+
jat_class.plugin :json_api_activerecord
|
20
|
+
|
21
|
+
assert jat_class.plugin_used?(:json_api_preloads)
|
22
|
+
assert jat_class.plugin_used?(:_activerecord_preloads)
|
23
|
+
end
|
24
|
+
end
|
@@ -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
|