jat 0.0.6 → 0.0.10
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 +5 -18
- data/lib/jat/config.rb +5 -20
- data/lib/jat/plugins/{common/_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +3 -3
- data/lib/jat/plugins/{common/_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +3 -1
- data/lib/jat/plugins/{common/_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +3 -3
- data/lib/jat/plugins/{common/_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +3 -3
- data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
- data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +1 -10
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +1 -10
- data/lib/jat/plugins/json_api/lib/map.rb +5 -15
- data/lib/jat/plugins/json_api/lib/response.rb +2 -15
- data/lib/jat/plugins/json_api/lib/response_piece.rb +10 -19
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +1 -1
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +1 -1
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +1 -1
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +1 -3
- data/lib/jat/plugins/presenter/presenter.rb +79 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +21 -46
- data/lib/jat/plugins/simple_api/lib/map.rb +5 -15
- data/lib/jat/plugins/simple_api/lib/response.rb +2 -15
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +2 -15
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +1 -1
- data/lib/jat/plugins/simple_api/simple_api.rb +1 -1
- data/lib/jat/plugins/types/types.rb +55 -0
- data/lib/jat/plugins.rb +38 -25
- data/lib/jat/utils/jat_class.rb +21 -0
- data/lib/jat.rb +9 -6
- data/test/lib/jat/attribute_test.rb +8 -0
- data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
- data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
- data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
- data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
- data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -1
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -11
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
- data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -1
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +1 -1
- data/test/lib/jat/plugins/{json_api_camel_lower/json_api_camel_lower_test.rb → json_api_lower_camel_case/json_api_lower_camel_case_test.rb} +6 -0
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +16 -2
- data/test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb +0 -3
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
- data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
- data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
- data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
- data/test/lib/jat/plugins/presenter/presenter_test.rb +215 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +57 -43
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
- data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +17 -11
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +36 -1
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +1 -1
- data/test/lib/jat/plugins/{simple_api_camel_lower/simple_api_camel_lower_test.rb → simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb} +7 -1
- data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +1 -6
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +16 -2
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
- data/test/lib/jat/plugins/types/types_test.rb +83 -0
- data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
- data/test/lib/jat/plugins_test.rb +89 -0
- data/test/test_helper.rb +14 -4
- metadata +72 -67
- data/test/lib/plugin_test.rb +0 -49
- data/test/test_plugin.rb +0 -56
@@ -0,0 +1,215 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Presenter" do
|
6
|
+
describe "before load" do
|
7
|
+
it "raises error if no response plugin is loaded" do
|
8
|
+
jat_class = Class.new(Jat)
|
9
|
+
err = assert_raises(Jat::Error) { jat_class.plugin :presenter }
|
10
|
+
assert_equal "Please load :json_api or :simple_api plugin first", err.message
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "ClassMethods" do
|
15
|
+
let(:jat_class) do
|
16
|
+
jat_class = Class.new(Jat)
|
17
|
+
jat_class.plugin :simple_api
|
18
|
+
jat_class.plugin :presenter
|
19
|
+
jat_class
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:presenter_class) { jat_class::Presenter }
|
23
|
+
|
24
|
+
describe ".jat_class=" do
|
25
|
+
it "assigns @jat_class" do
|
26
|
+
presenter_class.jat_class = :foo
|
27
|
+
assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".jat_class" do
|
32
|
+
it "returns self @jat_class" do
|
33
|
+
assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
|
34
|
+
assert_same jat_class, presenter_class.jat_class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ".inspect" do
|
39
|
+
it "returns likely name of a class" do
|
40
|
+
assert_equal "#{jat_class}::Presenter", presenter_class.inspect
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns self name for top level presenter class" do
|
44
|
+
Jat::Plugins.find_plugin(:presenter)
|
45
|
+
assert_equal "Jat::Plugins::Presenter::Presenter", Jat::Plugins::Presenter::Presenter.inspect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "with simple_api" do
|
51
|
+
let(:serializer) do
|
52
|
+
Class.new(Jat) do |base|
|
53
|
+
base.plugin :simple_api
|
54
|
+
base.plugin :presenter
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".inherited" do
|
59
|
+
let(:parent) { serializer }
|
60
|
+
|
61
|
+
it "inherits presenter class" do
|
62
|
+
child = Class.new(parent)
|
63
|
+
assert_equal parent::Presenter, child::Presenter.superclass
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "adds presenter methods when adding attribute" do
|
68
|
+
serializer.attribute :length
|
69
|
+
assert_includes serializer::Presenter.instance_methods, :length
|
70
|
+
end
|
71
|
+
|
72
|
+
it "adds presenter methods when adding attribute with key" do
|
73
|
+
serializer.attribute :length, key: :size
|
74
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
75
|
+
end
|
76
|
+
|
77
|
+
it "does not add presenter methods when adding attribute with block" do
|
78
|
+
serializer.attribute(:length) {}
|
79
|
+
refute_includes serializer::Presenter.instance_methods, :length
|
80
|
+
end
|
81
|
+
|
82
|
+
it "adds presenter methods used in block after first serialization" do
|
83
|
+
serializer.attribute(:length) { |obj| obj.size }
|
84
|
+
|
85
|
+
refute_includes serializer::Presenter.instance_methods, :size
|
86
|
+
serializer.to_h("")
|
87
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
88
|
+
end
|
89
|
+
|
90
|
+
it "allows to use custom methods defined directly in Presenter class" do
|
91
|
+
serializer::Presenter.class_exec do
|
92
|
+
def rev
|
93
|
+
reverse
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
serializer.attribute(:rev) { |obj| obj.rev }
|
98
|
+
rev = serializer.to_h("jingle")[:rev]
|
99
|
+
assert_equal "elgnij", rev
|
100
|
+
end
|
101
|
+
|
102
|
+
it "allows to override attribute methods" do
|
103
|
+
serializer.attribute :qrcode1
|
104
|
+
serializer.attribute :qrcode2
|
105
|
+
|
106
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode1
|
107
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode2
|
108
|
+
serializer::Presenter.class_exec do
|
109
|
+
def qrcode1
|
110
|
+
common
|
111
|
+
end
|
112
|
+
|
113
|
+
def qrcode2
|
114
|
+
common
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def common
|
120
|
+
@qrcode = "QRCODE"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
res = serializer.to_h(nil)
|
125
|
+
qrcode1 = res[:qrcode1]
|
126
|
+
qrcode2 = res[:qrcode2]
|
127
|
+
assert_equal "QRCODE", qrcode1
|
128
|
+
assert_equal "QRCODE", qrcode2
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "with json_api" do
|
133
|
+
let(:serializer) do
|
134
|
+
Class.new(Jat) do |base|
|
135
|
+
base.plugin :json_api
|
136
|
+
base.plugin :presenter
|
137
|
+
base.type :test
|
138
|
+
base.id { |obj| obj }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe ".inherited" do
|
143
|
+
let(:parent) { serializer }
|
144
|
+
|
145
|
+
it "inherits presenter class" do
|
146
|
+
child = Class.new(parent)
|
147
|
+
assert_equal parent::Presenter, child::Presenter.superclass
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it "adds presenter methods when adding attribute" do
|
152
|
+
serializer.attribute :length
|
153
|
+
assert_includes serializer::Presenter.instance_methods, :length
|
154
|
+
end
|
155
|
+
|
156
|
+
it "adds presenter methods when adding attribute with key" do
|
157
|
+
serializer.attribute :length, key: :size
|
158
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
159
|
+
end
|
160
|
+
|
161
|
+
it "does not add presenter methods when adding attribute with block" do
|
162
|
+
serializer.attribute(:length) {}
|
163
|
+
refute_includes serializer::Presenter.instance_methods, :length
|
164
|
+
end
|
165
|
+
|
166
|
+
it "adds presenter methods used in block after first serialization" do
|
167
|
+
serializer.attribute(:length) { |obj| obj.size }
|
168
|
+
|
169
|
+
refute_includes serializer::Presenter.instance_methods, :size
|
170
|
+
serializer.to_h("")
|
171
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
172
|
+
end
|
173
|
+
|
174
|
+
it "allows to use custom methods defined directly in Presenter class" do
|
175
|
+
serializer::Presenter.class_exec do
|
176
|
+
def rev
|
177
|
+
reverse
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
serializer.attribute(:rev) { |obj| obj.rev }
|
182
|
+
rev = serializer.to_h("jingle")[:data][:attributes][:rev]
|
183
|
+
assert_equal "elgnij", rev
|
184
|
+
end
|
185
|
+
|
186
|
+
it "allows to override attribute methods" do
|
187
|
+
serializer.attribute :qrcode1
|
188
|
+
serializer.attribute :qrcode2
|
189
|
+
|
190
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode1
|
191
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode2
|
192
|
+
serializer::Presenter.class_exec do
|
193
|
+
def qrcode1
|
194
|
+
common
|
195
|
+
end
|
196
|
+
|
197
|
+
def qrcode2
|
198
|
+
common
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
def common
|
204
|
+
@qrcode = "QRCODE"
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
res = serializer.to_h(nil)[:data][:attributes]
|
209
|
+
qrcode1 = res[:qrcode1]
|
210
|
+
qrcode2 = res[:qrcode2]
|
211
|
+
assert_equal "QRCODE", qrcode1
|
212
|
+
assert_equal "QRCODE", qrcode2
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
@@ -13,65 +13,79 @@ describe "Jat::Plugins::SimpleApi::FieldsParamParser" do
|
|
13
13
|
ser
|
14
14
|
end
|
15
15
|
|
16
|
+
let(:described_class) { jat_class::FieldsParamParser }
|
17
|
+
|
16
18
|
def parse(str)
|
17
19
|
jat_class::FieldsParamParser.parse(str)
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
+
describe ".inspect" do
|
23
|
+
it "returns self name" do
|
24
|
+
assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
|
28
|
+
describe ".parse" do
|
29
|
+
it "returns empty hash when nil provided" do
|
30
|
+
assert_equal({}, parse(nil))
|
31
|
+
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
it "returns empty hash when empty string provided" do
|
34
|
+
assert_equal({}, parse(""))
|
35
|
+
end
|
31
36
|
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
it "parses single field" do
|
38
|
+
assert_equal({id: {}}, parse("id"))
|
39
|
+
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
it "parses multiple fields" do
|
42
|
+
assert_equal({id: {}, name: {}}, parse("id, name"))
|
43
|
+
end
|
39
44
|
|
40
|
-
|
41
|
-
|
42
|
-
|
45
|
+
it "parses single resource with single field" do
|
46
|
+
assert_equal({users: {id: {}}}, parse("users(id)"))
|
47
|
+
end
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
it "parses fields started with open PAREN" do
|
50
|
+
assert_equal({users: {id: {}}}, parse("(users(id))"))
|
51
|
+
end
|
47
52
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
id: {},
|
52
|
-
posts: {title: {}, text: {}},
|
53
|
-
news: {title: {}, text: {}}
|
54
|
-
}
|
53
|
+
it "parses fields started with extra close PAREN" do
|
54
|
+
assert_equal({users: {}}, parse(")users)"))
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
it "parses single resource with multiple fields" do
|
58
|
+
assert_equal({users: {id: {}, name: {}}}, parse("users(id,name)"))
|
59
|
+
end
|
60
|
+
|
61
|
+
it "parses multiple resources with fields" do
|
62
|
+
fields = "id,posts(title,text),news(title,text)"
|
63
|
+
resp = {
|
64
|
+
id: {},
|
65
|
+
posts: {title: {}, text: {}},
|
66
|
+
news: {title: {}, text: {}}
|
67
|
+
}
|
68
|
+
|
69
|
+
assert_equal(resp, parse(fields))
|
70
|
+
end
|
58
71
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
72
|
+
it "parses included resources" do
|
73
|
+
fields = "id,posts(title,text,comments(author(name),comment))"
|
74
|
+
resp = {
|
75
|
+
id: {},
|
76
|
+
posts: {
|
77
|
+
title: {},
|
78
|
+
text: {},
|
79
|
+
comments: {
|
80
|
+
author: {
|
81
|
+
name: {}
|
82
|
+
},
|
83
|
+
comment: {}
|
84
|
+
}
|
71
85
|
}
|
72
86
|
}
|
73
|
-
}
|
74
87
|
|
75
|
-
|
88
|
+
assert_equal(resp, parse(fields))
|
89
|
+
end
|
76
90
|
end
|
77
91
|
end
|
@@ -45,89 +45,97 @@ describe "Jat::Plugins::SimpleApi::Map" do
|
|
45
45
|
ser
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
48
|
+
describe ".call" do
|
49
|
+
it "returns all attributes when {exposed: :all} provided " do
|
50
|
+
result = described_class.call(exposed: :all)
|
51
|
+
expected_result = {
|
52
|
+
a1: {},
|
53
|
+
a2: {},
|
54
|
+
a3: {},
|
55
|
+
b: {b1: {}, b2: {}, b3: {}},
|
56
|
+
c: {c1: {}, c2: {}, c3: {}},
|
57
|
+
d: {d1: {}, d2: {}, d3: {}}
|
58
|
+
}
|
59
|
+
|
60
|
+
assert_equal expected_result, result
|
61
|
+
end
|
62
|
+
|
63
|
+
it "returns exposed attributes when {exposed: :default} provided" do
|
64
|
+
result = described_class.call(exposed: :default)
|
65
|
+
expected_result = {
|
66
|
+
a1: {},
|
67
|
+
a2: {},
|
68
|
+
d: {d1: {}, d2: {}}
|
69
|
+
}
|
70
|
+
|
71
|
+
assert_equal expected_result, result
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns exposed attributes when no :exposed param provided" do
|
75
|
+
result = described_class.call({})
|
76
|
+
expected_result = {
|
77
|
+
a1: {},
|
78
|
+
a2: {},
|
79
|
+
d: {d1: {}, d2: {}}
|
80
|
+
}
|
81
|
+
|
82
|
+
assert_equal expected_result, result
|
83
|
+
end
|
84
|
+
|
85
|
+
it "returns no attributes when `{exposed: :none}` provided" do
|
86
|
+
result = described_class.call(exposed: :none)
|
87
|
+
assert_equal({}, result)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "returns only manually exposed attributes when `{exposed: :none}` type provided" do
|
91
|
+
fields = "a2,a3,c(c2,c3),d(d2,d3)"
|
92
|
+
result = described_class.call(exposed: :none, fields: fields)
|
93
|
+
|
94
|
+
expected_result = {
|
95
|
+
a2: {},
|
96
|
+
a3: {},
|
97
|
+
c: {c2: {}, c3: {}},
|
98
|
+
d: {d2: {}, d3: {}}
|
99
|
+
}
|
100
|
+
|
101
|
+
assert_equal expected_result, result
|
102
|
+
end
|
103
|
+
|
104
|
+
it "returns combined auto-exposed and manually exposed attributes when `default` type provided" do
|
105
|
+
fields = "b(b3),c"
|
106
|
+
result = described_class.call(exposed: :default, fields: fields)
|
107
|
+
expected_result = {
|
108
|
+
a1: {},
|
109
|
+
a2: {},
|
110
|
+
b: {b1: {}, b2: {}, b3: {}},
|
111
|
+
c: {c1: {}, c2: {}},
|
112
|
+
d: {d1: {}, d2: {}}
|
113
|
+
}
|
114
|
+
|
115
|
+
assert_equal expected_result, result
|
116
|
+
end
|
117
|
+
|
118
|
+
it "raises error with informative message about recursive serialization" do
|
119
|
+
a.relationship :b, serializer: -> { b }, exposed: true
|
120
|
+
b.relationship :a, serializer: -> { a }, exposed: true
|
121
|
+
|
122
|
+
error = assert_raises(Jat::Error) { described_class.call({}) }
|
123
|
+
assert_includes "Recursive serialization: b -> a -> b", error.message
|
124
|
+
end
|
125
|
+
|
126
|
+
it "raises error with informative message about recursive serialization through 3 serializers" do
|
127
|
+
a.relationship :b, serializer: -> { b }, exposed: true
|
128
|
+
b.relationship :c, serializer: -> { c }, exposed: true
|
129
|
+
c.relationship :a, serializer: -> { a }, exposed: true
|
130
|
+
|
131
|
+
error = assert_raises(Jat::Error) { described_class.call({}) }
|
132
|
+
assert_equal "Recursive serialization: b -> c -> a -> b", error.message
|
133
|
+
end
|
60
134
|
end
|
61
135
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
a2: {},
|
67
|
-
d: {d1: {}, d2: {}}
|
68
|
-
}
|
69
|
-
|
70
|
-
assert_equal expected_result, result
|
71
|
-
end
|
72
|
-
|
73
|
-
it "returns exposed attributes when no :exposed param provided" do
|
74
|
-
result = described_class.call({})
|
75
|
-
expected_result = {
|
76
|
-
a1: {},
|
77
|
-
a2: {},
|
78
|
-
d: {d1: {}, d2: {}}
|
79
|
-
}
|
80
|
-
|
81
|
-
assert_equal expected_result, result
|
82
|
-
end
|
83
|
-
|
84
|
-
it "returns no attributes when `{exposed: :none}` provided" do
|
85
|
-
result = described_class.call(exposed: :none)
|
86
|
-
assert_equal({}, result)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "returns only manually exposed attributes when `{exposed: :none}` type provided" do
|
90
|
-
fields = "a2,a3,c(c2,c3),d(d2,d3)"
|
91
|
-
result = described_class.call(exposed: :none, fields: fields)
|
92
|
-
|
93
|
-
expected_result = {
|
94
|
-
a2: {},
|
95
|
-
a3: {},
|
96
|
-
c: {c2: {}, c3: {}},
|
97
|
-
d: {d2: {}, d3: {}}
|
98
|
-
}
|
99
|
-
|
100
|
-
assert_equal expected_result, result
|
101
|
-
end
|
102
|
-
|
103
|
-
it "returns combined auto-exposed and manually exposed attributes when `default` type provided" do
|
104
|
-
fields = "b(b3),c"
|
105
|
-
result = described_class.call(exposed: :default, fields: fields)
|
106
|
-
expected_result = {
|
107
|
-
a1: {},
|
108
|
-
a2: {},
|
109
|
-
b: {b1: {}, b2: {}, b3: {}},
|
110
|
-
c: {c1: {}, c2: {}},
|
111
|
-
d: {d1: {}, d2: {}}
|
112
|
-
}
|
113
|
-
|
114
|
-
assert_equal expected_result, result
|
115
|
-
end
|
116
|
-
|
117
|
-
it "raises error with informative message about recursive serialization" do
|
118
|
-
a.relationship :b, serializer: -> { b }, exposed: true
|
119
|
-
b.relationship :a, serializer: -> { a }, exposed: true
|
120
|
-
|
121
|
-
error = assert_raises(Jat::Error) { described_class.call({}) }
|
122
|
-
assert_includes "Recursive serialization: b -> a -> b", error.message
|
123
|
-
end
|
124
|
-
|
125
|
-
it "raises error with informative message about recursive serialization through 3 serializers" do
|
126
|
-
a.relationship :b, serializer: -> { b }, exposed: true
|
127
|
-
b.relationship :c, serializer: -> { c }, exposed: true
|
128
|
-
c.relationship :a, serializer: -> { a }, exposed: true
|
129
|
-
|
130
|
-
error = assert_raises(Jat::Error) { described_class.call({}) }
|
131
|
-
assert_equal "Recursive serialization: b -> c -> a -> b", error.message
|
136
|
+
describe ".inspect" do
|
137
|
+
it "returns self name" do
|
138
|
+
assert_equal "#{a}::Map", described_class.inspect
|
139
|
+
end
|
132
140
|
end
|
133
141
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::SimpleApi::ResponsePiece" do
|
6
|
+
let(:jat_class) { Class.new(Jat) { plugin :simple_api } }
|
7
|
+
|
8
|
+
describe ".inspect" do
|
9
|
+
it "returns self name" do
|
10
|
+
assert_equal "#{jat_class}::ResponsePiece", jat_class::ResponsePiece.inspect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -92,7 +92,8 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
assert_equal({foo: {id: "1"}}, str_serializer.to_h("1", root: :foo))
|
95
|
-
assert_equal({foo:
|
95
|
+
assert_equal({foo: {id: "1"}}, str_serializer.to_h("1", root: "foo"))
|
96
|
+
assert_equal({id: "1"}, str_serializer.to_h("1", root: nil))
|
96
97
|
end
|
97
98
|
|
98
99
|
it "returns correct structure with data multiple attributes" do
|
@@ -131,13 +132,11 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
131
132
|
end
|
132
133
|
|
133
134
|
it "does not return has-one relationship when not exposed" do
|
134
|
-
|
135
|
-
attribute(:id) { |obj| obj }
|
136
|
-
end
|
135
|
+
nested_serializer = Class.new(base_class)
|
137
136
|
|
138
137
|
str_serializer = Class.new(base_class) do
|
139
138
|
attribute(:id) { |obj| obj[0] }
|
140
|
-
attribute :length, serializer:
|
139
|
+
attribute :length, serializer: nested_serializer, exposed: false
|
141
140
|
end
|
142
141
|
|
143
142
|
assert_equal({id: "S"}, str_serializer.to_h("STRING"))
|
@@ -242,12 +241,12 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
242
241
|
# All fields are not exposed in this serializers,
|
243
242
|
# We will show only attributes provided in `fields` param
|
244
243
|
chr_serializer = Class.new(base_class) do
|
245
|
-
attribute(:id, exposed: false)
|
244
|
+
attribute(:id, exposed: false)
|
246
245
|
attribute :next, exposed: false
|
247
246
|
end
|
248
247
|
|
249
248
|
str_serializer = Class.new(base_class) do
|
250
|
-
attribute(:id, exposed: false)
|
249
|
+
attribute(:id, exposed: false)
|
251
250
|
attribute :chars, serializer: chr_serializer, many: true, exposed: false
|
252
251
|
end
|
253
252
|
|
@@ -269,7 +268,8 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
269
268
|
empty_serializer.meta_key :metadata
|
270
269
|
empty_serializer.meta(:foo) { :bar }
|
271
270
|
|
272
|
-
assert_equal({metadata: {foo: :bar
|
271
|
+
assert_equal({metadata: {foo: :bar}}, empty_serializer.to_h(nil))
|
272
|
+
assert_equal({new_meta: {foo: :bar}}, empty_serializer.to_h(nil, meta_key: "new_meta"))
|
273
273
|
end
|
274
274
|
|
275
275
|
it "returns correct structure with data and meta" do
|
@@ -306,14 +306,14 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
306
306
|
it "does not overwrite manually added meta" do
|
307
307
|
str_serializer = Class.new(base_class) do
|
308
308
|
root(:root)
|
309
|
-
meta(:
|
309
|
+
meta(:length)
|
310
310
|
|
311
311
|
attribute(:id) { |obj| obj }
|
312
312
|
end
|
313
313
|
|
314
314
|
assert_equal(
|
315
|
-
{root: {id: "1"}, meta: {
|
316
|
-
str_serializer.to_h("1", meta: {
|
315
|
+
{root: {id: "1"}, meta: {length: "33"}},
|
316
|
+
str_serializer.to_h("1", meta: {length: "33"})
|
317
317
|
)
|
318
318
|
end
|
319
319
|
|
@@ -345,4 +345,10 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
345
345
|
assert_equal({root: {id: "1"}}, str_serializer.to_h("1", bazz: nil))
|
346
346
|
end
|
347
347
|
end
|
348
|
+
|
349
|
+
describe ".inspect" do
|
350
|
+
it "returns self name" do
|
351
|
+
assert_equal "#{base_class}::Response", base_class::Response.inspect
|
352
|
+
end
|
353
|
+
end
|
348
354
|
end
|
@@ -15,6 +15,16 @@ describe "Jat::Plugins::SimpleApi" do
|
|
15
15
|
|
16
16
|
let(:plugin) { @plugin }
|
17
17
|
|
18
|
+
describe ".before_load" do
|
19
|
+
it "raises error if response plugin was already loaded" do
|
20
|
+
jat_class = Class.new(Jat)
|
21
|
+
jat_class.config[:response_plugin_loaded] = :foobar
|
22
|
+
|
23
|
+
err = assert_raises(Jat::Error) { jat_class.plugin(:simple_api) }
|
24
|
+
assert_equal("Response plugin `foobar` was already loaded before", err.message)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
18
28
|
describe ".after_load" do
|
19
29
|
it "adds default `:meta` meta_key config option" do
|
20
30
|
jat_class = Class.new(Jat)
|
@@ -46,7 +56,7 @@ describe "Jat::Plugins::SimpleApi" do
|
|
46
56
|
describe "#map" do
|
47
57
|
it "returns map for provided context" do
|
48
58
|
jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
|
49
|
-
assert_equal "MAP", jat_class.
|
59
|
+
assert_equal "MAP", jat_class.new("CONTEXT").map
|
50
60
|
end
|
51
61
|
end
|
52
62
|
|
@@ -135,5 +145,30 @@ describe "Jat::Plugins::SimpleApi" do
|
|
135
145
|
assert_equal :metadata, jat_class.config[:meta_key]
|
136
146
|
end
|
137
147
|
end
|
148
|
+
|
149
|
+
describe ".inherited" do
|
150
|
+
it "inherits root" do
|
151
|
+
jat_class.root(:foo)
|
152
|
+
child = Class.new(jat_class)
|
153
|
+
assert_equal :foo, child.config[:root_one]
|
154
|
+
assert_equal :foo, child.config[:root_many]
|
155
|
+
end
|
156
|
+
|
157
|
+
it "inherits meta" do
|
158
|
+
jat_class.meta(:foo) { "bar" }
|
159
|
+
child = Class.new(jat_class)
|
160
|
+
|
161
|
+
assert_equal "bar", child.added_meta[:foo].value(nil, nil)
|
162
|
+
end
|
163
|
+
|
164
|
+
it "does not change parents meta when children meta changed" do
|
165
|
+
jat_class.meta(:foo) { "foo" }
|
166
|
+
child = Class.new(jat_class)
|
167
|
+
child.meta(:foo) { "bazz" }
|
168
|
+
|
169
|
+
assert_equal("foo", jat_class.added_meta[:foo].value(nil, nil))
|
170
|
+
assert_equal("bazz", child.added_meta[:foo].value(nil, nil))
|
171
|
+
end
|
172
|
+
end
|
138
173
|
end
|
139
174
|
end
|
@@ -19,6 +19,6 @@ describe "Jat::Plugins::SimpleApiActiverecord" do
|
|
19
19
|
jat_class.plugin :simple_api_activerecord
|
20
20
|
|
21
21
|
assert jat_class.plugin_used?(:simple_api_preloads)
|
22
|
-
assert jat_class.plugin_used?(:
|
22
|
+
assert jat_class.plugin_used?(:base_activerecord_preloads)
|
23
23
|
end
|
24
24
|
end
|
@@ -10,8 +10,14 @@ describe "Jat::Plugins::SimpleApiLowerCamelCase" do
|
|
10
10
|
new_class
|
11
11
|
end
|
12
12
|
|
13
|
+
it "checks simple_api plugin loaded before" do
|
14
|
+
jat_class = Class.new(Jat)
|
15
|
+
error = assert_raises(Jat::Error) { jat_class.plugin :simple_api_lower_camel_case }
|
16
|
+
assert_match(/simple_api/, error.message)
|
17
|
+
end
|
18
|
+
|
13
19
|
it "loads _lower_camel_case plugin" do
|
14
|
-
assert jat_class.plugin_used?(:
|
20
|
+
assert jat_class.plugin_used?(:base_lower_camel_case)
|
15
21
|
end
|
16
22
|
|
17
23
|
it "returns attributes in lowerCamelCase case" do
|