jat 0.0.3 → 0.0.8
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 +26 -5
- data/lib/jat/config.rb +4 -7
- data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
- data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
- data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
- data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
- data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
- data/lib/jat/plugins/cache/cache.rb +14 -6
- data/lib/jat/plugins/json_api/json_api.rb +150 -106
- 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 +92 -27
- data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
- data/lib/jat/plugins/json_api/lib/response.rb +81 -197
- 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_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
- 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 +80 -10
- data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
- 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 +87 -27
- data/lib/jat/plugins/to_str/to_str.rb +15 -5
- 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 +42 -30
- data/lib/jat.rb +40 -35
- data/test/lib/jat/attribute_test.rb +22 -4
- data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
- data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
- data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
- data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
- 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/cache/cache_test.rb +11 -11
- data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
- 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 +38 -32
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -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 +51 -0
- data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
- 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/simple_api/lib/fields_param_parser_test.rb +87 -0
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
- 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 +90 -76
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -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 +51 -0
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
- data/test/lib/jat/plugins/types/types_test.rb +84 -0
- data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
- data/test/lib/jat/plugins_test.rb +88 -0
- data/test/lib/jat_test.rb +47 -24
- data/test/lib/plugin_test.rb +3 -3
- data/test/test_helper.rb +7 -6
- data/test/test_plugin.rb +9 -12
- metadata +107 -88
- data/CHANGELOG.md +0 -7
- data/README.md +0 -21
- data/jat.gemspec +0 -37
- data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
- data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
- data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
- data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
- data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
- data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
- data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
- data/lib/jat/presenter.rb +0 -51
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
- data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
- data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
- data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
- data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
- data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
- data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
- data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
- data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
- data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
- data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
- data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
- data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
- data/test/lib/jat/presenter_test.rb +0 -61
@@ -3,115 +3,194 @@
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
5
|
describe "Jat::Plugins::JsonApi::Map" do
|
6
|
-
|
6
|
+
before { Jat::Plugins.find_plugin(:json_api) }
|
7
7
|
|
8
|
-
let(:
|
9
|
-
|
10
|
-
plugin :json_api
|
11
|
-
type :jat
|
12
|
-
end
|
13
|
-
end
|
14
|
-
let(:jat) { jat_class.new(nil, params: {fields: param_fields, include: param_includes}) }
|
15
|
-
|
16
|
-
let(:default_map) { {a: :a1, b: :b1, c: :c1} }
|
17
|
-
let(:includes_map) { {b: :b2, c: :c2} }
|
18
|
-
let(:fields_map) { {c: :c3} }
|
8
|
+
let(:described_class) { a::Map }
|
9
|
+
let(:base_class) { Class.new(Jat) { plugin :json_api } }
|
19
10
|
|
20
|
-
|
21
|
-
|
22
|
-
|
11
|
+
let(:a) do
|
12
|
+
ser = Class.new(base_class)
|
13
|
+
ser.type :a
|
23
14
|
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
ser.attribute :a1
|
16
|
+
ser.attribute :a2
|
17
|
+
ser.attribute :a3, exposed: false
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
|
19
|
+
ser.relationship :b, serializer: b, exposed: true
|
20
|
+
ser.relationship :c, serializer: c
|
21
|
+
ser.relationship :d, serializer: d
|
22
|
+
ser
|
31
23
|
end
|
32
24
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
constructor.expects(:to_h).returns(fields_map)
|
25
|
+
let(:b) do
|
26
|
+
ser = Class.new(base_class)
|
27
|
+
ser.type :b
|
28
|
+
ser.attribute :b1
|
29
|
+
ser.attribute :b2
|
30
|
+
ser.attribute :b3, exposed: false
|
40
31
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
.returns("PARSED_FIELDS")
|
45
|
-
|
46
|
-
Jat::Plugins::JsonApi::ConstructTraversalMap
|
47
|
-
.expects(:new)
|
48
|
-
.with(jat_class, :manual, manually_exposed: "PARSED_FIELDS")
|
49
|
-
.returns(constructor)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "constructs map with default and provided fields" do
|
53
|
-
assert_equal({a: :a1, b: :b1, c: :c3}, subject)
|
54
|
-
end
|
32
|
+
ser.relationship :c, serializer: c
|
33
|
+
ser.relationship :d, serializer: d
|
34
|
+
ser
|
55
35
|
end
|
56
36
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
37
|
+
let(:c) do
|
38
|
+
ser = Class.new(base_class)
|
39
|
+
ser.type :c
|
40
|
+
ser.attribute :c1
|
41
|
+
ser.attribute :c2
|
42
|
+
ser.attribute :c3, exposed: false
|
43
|
+
ser
|
44
|
+
end
|
64
45
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
46
|
+
let(:d) do
|
47
|
+
ser = Class.new(base_class)
|
48
|
+
ser.type :d
|
49
|
+
ser.attribute :d1
|
50
|
+
ser.attribute :d2
|
51
|
+
ser.attribute :d3, exposed: false
|
52
|
+
ser
|
53
|
+
end
|
69
54
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
.
|
55
|
+
describe ".call" do
|
56
|
+
describe "with default :exposed option" do
|
57
|
+
it "returns exposed by default attributes" do
|
58
|
+
result = described_class.call(exposed: :default)
|
59
|
+
expected_result = {
|
60
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
61
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
|
62
|
+
}
|
63
|
+
|
64
|
+
assert_equal expected_result, result
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns exposed by default attributes when no :exposed param provided" do
|
68
|
+
result = described_class.call({})
|
69
|
+
expected_result = {
|
70
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
71
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
|
72
|
+
}
|
73
|
+
|
74
|
+
assert_equal expected_result, result
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns additionally included fields specified in :includes option" do
|
78
|
+
includes = "c,b.d"
|
79
|
+
result = described_class.call(include: includes)
|
80
|
+
expected_result = {
|
81
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
|
82
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[d]},
|
83
|
+
c: {serializer: c, attributes: %i[c1 c2], relationships: %i[]},
|
84
|
+
d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
|
85
|
+
}
|
86
|
+
|
87
|
+
assert_equal expected_result, result
|
88
|
+
end
|
89
|
+
|
90
|
+
it "returns fields specified in :fields option (only specified fields for specified type)" do
|
91
|
+
fields = {b: "b1,d"}
|
92
|
+
result = described_class.call(fields: fields)
|
93
|
+
expected_result = {
|
94
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
95
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
|
96
|
+
d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
|
97
|
+
}
|
98
|
+
|
99
|
+
assert_equal expected_result, result
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns fields specified in :includes and :fields options" do
|
103
|
+
includes = "c"
|
104
|
+
fields = {c: "c1"}
|
105
|
+
result = described_class.call(include: includes, fields: fields)
|
106
|
+
expected_result = {
|
107
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
|
108
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]},
|
109
|
+
c: {serializer: c, attributes: %i[c1], relationships: %i[]}
|
110
|
+
}
|
111
|
+
|
112
|
+
assert_equal expected_result, result
|
113
|
+
end
|
74
114
|
end
|
75
115
|
|
76
|
-
|
77
|
-
|
116
|
+
describe "with exposed: :none option" do
|
117
|
+
it "returns no attributes" do
|
118
|
+
result = described_class.call(exposed: :none)
|
119
|
+
expected_result = {
|
120
|
+
a: {serializer: a, attributes: %i[], relationships: %i[]}
|
121
|
+
}
|
122
|
+
|
123
|
+
assert_equal expected_result, result
|
124
|
+
end
|
125
|
+
|
126
|
+
it "returns additionally included fields specified in :includes option" do
|
127
|
+
includes = "b.c"
|
128
|
+
result = described_class.call(exposed: :none, include: includes)
|
129
|
+
expected_result = {
|
130
|
+
a: {serializer: a, attributes: %i[], relationships: %i[b]},
|
131
|
+
b: {serializer: b, attributes: %i[], relationships: %i[c]},
|
132
|
+
c: {serializer: c, attributes: %i[], relationships: %i[]}
|
133
|
+
}
|
134
|
+
|
135
|
+
assert_equal expected_result, result
|
136
|
+
end
|
137
|
+
|
138
|
+
it "returns fields specified in :fields option (only specified fields for specified type)" do
|
139
|
+
fields = {a: "a1,b", b: "b1,d"}
|
140
|
+
result = described_class.call(exposed: :none, fields: fields)
|
141
|
+
expected_result = {
|
142
|
+
a: {serializer: a, attributes: %i[a1], relationships: %i[b]},
|
143
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
|
144
|
+
d: {serializer: d, attributes: %i[], relationships: %i[]}
|
145
|
+
}
|
146
|
+
|
147
|
+
assert_equal expected_result, result
|
148
|
+
end
|
149
|
+
|
150
|
+
it "returns fields specified in :includes and :fields options" do
|
151
|
+
includes = "c"
|
152
|
+
fields = {c: "c1"}
|
153
|
+
result = described_class.call(exposed: :none, include: includes, fields: fields)
|
154
|
+
expected_result = {
|
155
|
+
a: {serializer: a, attributes: %i[], relationships: %i[c]},
|
156
|
+
c: {serializer: c, attributes: %i[c1], relationships: %i[]}
|
157
|
+
}
|
158
|
+
|
159
|
+
assert_equal expected_result, result
|
160
|
+
end
|
78
161
|
end
|
79
|
-
end
|
80
|
-
|
81
|
-
describe "when fields and includes given" do
|
82
|
-
let(:param_fields) { "FIELDS" }
|
83
|
-
let(:param_includes) { "INCLUDES" }
|
84
|
-
|
85
|
-
before do
|
86
|
-
constructor1 = Jat::Plugins::JsonApi::ConstructTraversalMap.allocate
|
87
|
-
constructor1.expects(:to_h).returns(fields_map)
|
88
162
|
|
89
|
-
|
90
|
-
|
91
|
-
.
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
.
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
163
|
+
describe "with exposed: :all option" do
|
164
|
+
it "returns all attributes" do
|
165
|
+
result = described_class.call(exposed: :all)
|
166
|
+
expected_result = {
|
167
|
+
a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
|
168
|
+
b: {serializer: b, attributes: %i[b1 b2 b3], relationships: %i[c d]},
|
169
|
+
c: {serializer: c, attributes: %i[c1 c2 c3], relationships: %i[]},
|
170
|
+
d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
|
171
|
+
}
|
172
|
+
|
173
|
+
assert_equal expected_result, result
|
174
|
+
end
|
175
|
+
|
176
|
+
it "returns only specified fields for specified fields types" do
|
177
|
+
fields = {b: "b1", c: "c2"}
|
178
|
+
result = described_class.call(exposed: :all, fields: fields)
|
179
|
+
expected_result = {
|
180
|
+
a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
|
181
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[]},
|
182
|
+
c: {serializer: c, attributes: %i[c2], relationships: %i[]},
|
183
|
+
d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
|
184
|
+
}
|
185
|
+
|
186
|
+
assert_equal expected_result, result
|
187
|
+
end
|
111
188
|
end
|
189
|
+
end
|
112
190
|
|
113
|
-
|
114
|
-
|
191
|
+
describe ".inspect" do
|
192
|
+
it "returns self name" do
|
193
|
+
assert_equal "#{a}::Map", described_class.inspect
|
115
194
|
end
|
116
195
|
end
|
117
196
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi::ResponsePiece" do
|
6
|
+
let(:jat_class) { Class.new(Jat) { plugin :json_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
|
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
51
|
+
id { |obj| obj }
|
52
52
|
end
|
53
53
|
|
54
54
|
str_serializer = Class.new(base_class) do
|
55
55
|
type "str"
|
56
|
-
|
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
|
-
|
79
|
+
id { |obj| obj }
|
80
80
|
end
|
81
81
|
|
82
82
|
str_serializer = Class.new(base_class) do
|
83
83
|
type "str"
|
84
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
163
|
+
id { |obj| obj }
|
164
164
|
end
|
165
165
|
|
166
166
|
str_serializer = Class.new(base_class) do
|
167
167
|
type "str"
|
168
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
221
|
+
id { |obj| obj }
|
222
222
|
end
|
223
223
|
|
224
224
|
str_serializer = Class.new(base_class) do
|
225
225
|
type "str"
|
226
|
-
|
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",
|
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
|
-
|
249
|
+
id { |obj| obj }
|
250
250
|
end
|
251
251
|
|
252
252
|
str_serializer = Class.new(base_class) do
|
253
253
|
type "str"
|
254
|
-
|
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",
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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) { |
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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) { |
|
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")
|
@@ -486,4 +486,10 @@ describe "Jat::Plugins::JsonApi::Response" do
|
|
486
486
|
assert_equal({self: "/self", related: "/foo/self"}, links)
|
487
487
|
end
|
488
488
|
end
|
489
|
+
|
490
|
+
describe ".inspect" do
|
491
|
+
it "returns self name" do
|
492
|
+
assert_equal "#{base_class}::Response", base_class::Response.inspect
|
493
|
+
end
|
494
|
+
end
|
489
495
|
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?(:base_activerecord_preloads)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,85 @@
|
|
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 "checks json_api plugin loaded before" do
|
19
|
+
jat_class = Class.new(Jat)
|
20
|
+
error = assert_raises(Jat::Error) { jat_class.plugin :json_api_lower_camel_case }
|
21
|
+
assert_match(/json_api/, error.message)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns attributes in lowerCamelCase case" do
|
25
|
+
jat_class.attribute(:foo_bar) { 1 }
|
26
|
+
|
27
|
+
assert_equal({fooBar: 1}, jat_class.to_h(true).dig(:data, :attributes))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "accepts `fields` in lowerCamelCase format" do
|
31
|
+
jat_class.attribute(:foo_bar, exposed: false) { 1 }
|
32
|
+
|
33
|
+
response = jat_class.to_h(true, fields: {foo: "fooBar"})
|
34
|
+
assert_equal({fooBar: 1}, response.dig(:data, :attributes))
|
35
|
+
end
|
36
|
+
|
37
|
+
it "accepts `include` in lowerCamelCase format" do
|
38
|
+
new_serializer = Class.new(Jat)
|
39
|
+
new_serializer.plugin(:json_api)
|
40
|
+
new_serializer.type :new
|
41
|
+
new_serializer.id { |object| object }
|
42
|
+
|
43
|
+
jat_class.relationship(:foo_bar, serializer: new_serializer) { 1 }
|
44
|
+
|
45
|
+
response = jat_class.to_h(true, include: "fooBar")
|
46
|
+
response_relationships = response.dig(:data, :relationships).keys
|
47
|
+
assert_includes(response_relationships, :fooBar)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns document meta keys in lowerCamelCase format" do
|
51
|
+
jat_class.document_meta(:user_agent) { "Firefox" }
|
52
|
+
|
53
|
+
response = jat_class.to_h(nil)
|
54
|
+
assert_equal({meta: {userAgent: "Firefox"}}, response)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns document jsonapi keys in lowerCamelCase format" do
|
58
|
+
jat_class.jsonapi(:user_agent) { "Firefox" }
|
59
|
+
|
60
|
+
response = jat_class.to_h(nil)
|
61
|
+
assert_equal({jsonapi: {userAgent: "Firefox"}}, response)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns document links keys in lowerCamelCase format" do
|
65
|
+
jat_class.document_link(:user_agent) { "Firefox" }
|
66
|
+
|
67
|
+
response = jat_class.to_h(nil)
|
68
|
+
assert_equal({links: {userAgent: "Firefox"}}, response)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns context meta keys in lowerCamelCase format" do
|
72
|
+
response = jat_class.to_h(nil, meta: {user_agent: "Chrome"})
|
73
|
+
assert_equal({meta: {userAgent: "Chrome"}}, response)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns context jsonapi keys in lowerCamelCase format" do
|
77
|
+
response = jat_class.to_h(nil, jsonapi: {user_agent: "Chrome"})
|
78
|
+
assert_equal({jsonapi: {userAgent: "Chrome"}}, response)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns context links keys in lowerCamelCase format" do
|
82
|
+
response = jat_class.to_h(nil, links: {user_agent: "Chrome"})
|
83
|
+
assert_equal({links: {userAgent: "Chrome"}}, response)
|
84
|
+
end
|
85
|
+
end
|