jat 0.0.7 → 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 +4 -5
- 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/response_piece.rb +2 -2
- 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/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/simple_api.rb +1 -1
- data/lib/jat/plugins.rb +37 -25
- data/lib/jat.rb +8 -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 -9
- 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 -0
- 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_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/simple_api/lib/fields_param_parser_test.rb +53 -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 +10 -2
- 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/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 +5 -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/test_helper.rb +7 -3
- metadata +40 -24
@@ -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
|
@@ -269,7 +270,8 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
269
270
|
empty_serializer.meta_key :metadata
|
270
271
|
empty_serializer.meta(:foo) { :bar }
|
271
272
|
|
272
|
-
assert_equal({metadata: {foo: :bar
|
273
|
+
assert_equal({metadata: {foo: :bar}}, empty_serializer.to_h(nil))
|
274
|
+
assert_equal({new_meta: {foo: :bar}}, empty_serializer.to_h(nil, meta_key: "new_meta"))
|
273
275
|
end
|
274
276
|
|
275
277
|
it "returns correct structure with data and meta" do
|
@@ -345,4 +347,10 @@ describe "Jat::Plugins::SimpleApi::Response" do
|
|
345
347
|
assert_equal({root: {id: "1"}}, str_serializer.to_h("1", bazz: nil))
|
346
348
|
end
|
347
349
|
end
|
350
|
+
|
351
|
+
describe ".inspect" do
|
352
|
+
it "returns self name" do
|
353
|
+
assert_equal "#{base_class}::Response", base_class::Response.inspect
|
354
|
+
end
|
355
|
+
end
|
348
356
|
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
|
@@ -17,13 +17,13 @@ describe "Jat::Plugins::SimpleApiPreloads" do
|
|
17
17
|
jat_class = Class.new(Jat)
|
18
18
|
jat_class.plugin :simple_api
|
19
19
|
|
20
|
-
jat_class.expects(:plugin).with(:
|
20
|
+
jat_class.expects(:plugin).with(:base_preloads, foo: :bar)
|
21
21
|
|
22
22
|
@plugin.before_load(jat_class, foo: :bar)
|
23
23
|
end
|
24
24
|
|
25
25
|
describe "InstanceMethods" do
|
26
|
-
it "
|
26
|
+
it "adds #preloads method as a delegator to #{@plugin}::Preloads" do
|
27
27
|
jat_class = Class.new(Jat)
|
28
28
|
jat_class.plugin :simple_api
|
29
29
|
jat_class.plugin @plugin
|
@@ -34,4 +34,18 @@ describe "Jat::Plugins::SimpleApiPreloads" do
|
|
34
34
|
assert_equal "RES", jat.preloads
|
35
35
|
end
|
36
36
|
end
|
37
|
+
|
38
|
+
describe "ClassMethods" do
|
39
|
+
it "adds .preloads method as a delegator to #{@plugin}::Preloads" do
|
40
|
+
jat_class = Class.new(Jat)
|
41
|
+
jat_class.plugin :simple_api
|
42
|
+
jat_class.plugin @plugin
|
43
|
+
|
44
|
+
jat = jat_class.allocate
|
45
|
+
jat_class.expects(:new).with("CONTEXT").returns(jat)
|
46
|
+
@plugin::Preloads.expects(:call).with(jat).returns("RES")
|
47
|
+
|
48
|
+
assert_equal "RES", jat_class.preloads("CONTEXT")
|
49
|
+
end
|
50
|
+
end
|
37
51
|
end
|
@@ -36,6 +36,11 @@ describe "Jat::Plugins::SimpleApiValidateParams" do
|
|
36
36
|
jat_class
|
37
37
|
end
|
38
38
|
|
39
|
+
it "returns true when provided fields present" do
|
40
|
+
jat = serializer.new(fields: "foo_bar,foo_bazz(foo_bar)")
|
41
|
+
assert jat.validate
|
42
|
+
end
|
43
|
+
|
39
44
|
it "validates fields" do
|
40
45
|
jat = serializer.new(fields: "foo_bar,extra")
|
41
46
|
error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
|
@@ -43,6 +48,13 @@ describe "Jat::Plugins::SimpleApiValidateParams" do
|
|
43
48
|
assert_equal(expected_message, error.message)
|
44
49
|
end
|
45
50
|
|
51
|
+
it "validates fields when using lower_camel_case plugin" do
|
52
|
+
jat = serializer_lower_camel_case.new(fields: "fooBar,extra")
|
53
|
+
error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
|
54
|
+
expected_message = "Field 'extra' not exists"
|
55
|
+
assert_equal(expected_message, error.message)
|
56
|
+
end
|
57
|
+
|
46
58
|
it "validates deeply nested fields" do
|
47
59
|
c = Class.new(base_serializer)
|
48
60
|
c.attribute :c1
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::ValidateParams" do
|
6
|
+
it "raises error if no response plugin is loaded" do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
err = assert_raises(Jat::Error) { new_class.plugin :validate_params }
|
9
|
+
assert_equal "Please load :json_api or :simple_api plugin first", err.message
|
10
|
+
end
|
11
|
+
|
12
|
+
it "loads simple_api compatible plugin" do
|
13
|
+
new_class = Class.new(Jat)
|
14
|
+
new_class.plugin :simple_api
|
15
|
+
new_class.plugin :validate_params
|
16
|
+
|
17
|
+
assert new_class.plugin_used?(:simple_api_validate_params)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "loads json_api compatible plugin" do
|
21
|
+
new_class = Class.new(Jat)
|
22
|
+
new_class.plugin :json_api
|
23
|
+
new_class.plugin :validate_params
|
24
|
+
|
25
|
+
assert new_class.plugin_used?(:json_api_validate_params)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe Jat::Plugins do
|
6
|
+
let(:described_module) { Jat::Plugins }
|
7
|
+
|
8
|
+
describe ".register_plugin" do
|
9
|
+
it "adds plugin to the @plugins list" do
|
10
|
+
plugin = Module.new
|
11
|
+
plugin_name = :new_plugin
|
12
|
+
described_module.register_plugin(plugin_name, plugin)
|
13
|
+
|
14
|
+
assert_equal plugin, described_module.instance_variable_get(:@plugins).fetch(plugin_name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".find_plugin" do
|
19
|
+
it "returns module if module provided" do
|
20
|
+
plugin = Module.new
|
21
|
+
assert_equal plugin, described_module.find_plugin(plugin)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns already registered plugin found by name" do
|
25
|
+
plugin = Module.new
|
26
|
+
plugin_name = :new_plugin
|
27
|
+
described_module.register_plugin(plugin_name, plugin)
|
28
|
+
|
29
|
+
assert_equal plugin, described_module.find_plugin(plugin_name)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns global plugins found by name" do
|
33
|
+
assert_equal "Jat::Plugins::Activerecord", described_module.find_plugin(:activerecord).name
|
34
|
+
assert_equal "Jat::Plugins::Cache", described_module.find_plugin(:cache).name
|
35
|
+
assert_equal "Jat::Plugins::JsonApi", described_module.find_plugin(:json_api).name
|
36
|
+
assert_equal "Jat::Plugins::LowerCamelCase", described_module.find_plugin(:lower_camel_case).name
|
37
|
+
assert_equal "Jat::Plugins::MapsCache", described_module.find_plugin(:maps_cache).name
|
38
|
+
assert_equal "Jat::Plugins::Preloads", described_module.find_plugin(:preloads).name
|
39
|
+
assert_equal "Jat::Plugins::SimpleApi", described_module.find_plugin(:simple_api).name
|
40
|
+
assert_equal "Jat::Plugins::ToStr", described_module.find_plugin(:to_str).name
|
41
|
+
assert_equal "Jat::Plugins::Types", described_module.find_plugin(:types).name
|
42
|
+
assert_equal "Jat::Plugins::ValidateParams", described_module.find_plugin(:validate_params).name
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns json_api only plugins found by name (name has `json_api_` prefix)" do
|
46
|
+
assert_equal "Jat::Plugins::JsonApiActiverecord", described_module.find_plugin(:json_api_activerecord).name
|
47
|
+
assert_equal "Jat::Plugins::JsonApiLowerCamelCase", described_module.find_plugin(:json_api_lower_camel_case).name
|
48
|
+
assert_equal "Jat::Plugins::JsonApiMapsCache", described_module.find_plugin(:json_api_maps_cache).name
|
49
|
+
assert_equal "Jat::Plugins::JsonApiPreloads", described_module.find_plugin(:json_api_preloads).name
|
50
|
+
assert_equal "Jat::Plugins::JsonApiValidateParams", described_module.find_plugin(:json_api_validate_params).name
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns simple_api only plugins found by name (name has `simple_api_` prefix)" do
|
54
|
+
assert_equal "Jat::Plugins::SimpleApiActiverecord", described_module.find_plugin(:simple_api_activerecord).name
|
55
|
+
assert_equal "Jat::Plugins::SimpleApiLowerCamelCase", described_module.find_plugin(:simple_api_lower_camel_case).name
|
56
|
+
assert_equal "Jat::Plugins::SimpleApiMapsCache", described_module.find_plugin(:simple_api_maps_cache).name
|
57
|
+
assert_equal "Jat::Plugins::SimpleApiPreloads", described_module.find_plugin(:simple_api_preloads).name
|
58
|
+
assert_equal "Jat::Plugins::SimpleApiValidateParams", described_module.find_plugin(:simple_api_validate_params).name
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns base plugins found by name (name has `base_` prefix)" do
|
62
|
+
assert_equal "Jat::Plugins::BaseActiverecordPreloads", described_module.find_plugin(:base_activerecord_preloads).name
|
63
|
+
assert_equal "Jat::Plugins::BaseLowerCamelCase", described_module.find_plugin(:base_lower_camel_case).name
|
64
|
+
assert_equal "Jat::Plugins::BasePreloads", described_module.find_plugin(:base_preloads).name
|
65
|
+
end
|
66
|
+
|
67
|
+
it "raises specific error if plugin not found" do
|
68
|
+
err = assert_raises(Jat::PluginLoadError) { described_module.find_plugin(:foo) }
|
69
|
+
assert_equal "Plugin 'foo' does not exist", err.message
|
70
|
+
end
|
71
|
+
|
72
|
+
it "raises specific error if plugin was found by name but was not registered" do
|
73
|
+
plugin_name = "test_foo"
|
74
|
+
|
75
|
+
# Add plugin folder and file in plugins directory
|
76
|
+
plugin_dir = File.join(__dir__, "../../../lib/jat/plugins", plugin_name)
|
77
|
+
plugin_path = File.join(plugin_dir, "#{plugin_name}.rb")
|
78
|
+
Dir.mkdir(plugin_dir)
|
79
|
+
File.new(plugin_path, File::CREAT)
|
80
|
+
|
81
|
+
err = assert_raises(Jat::PluginLoadError) { described_module.find_plugin(plugin_name) }
|
82
|
+
assert_equal "Plugin '#{plugin_name}' did not register itself correctly", err.message
|
83
|
+
ensure
|
84
|
+
File.unlink(plugin_path)
|
85
|
+
Dir.unlink(plugin_dir)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
# ARGV is populated with all test file names when running `rake`
|
4
|
+
# ARGV is not populated when running `ruby -Itest {file}`
|
5
|
+
# ARGV is not populated when running test by `m` gem - `m {file}`
|
6
|
+
# ARGV is not populated when running tests by `m` gem with line number - `m {file}:{line_number}`
|
7
|
+
if ARGV.any?
|
8
|
+
require "simplecov"
|
9
|
+
SimpleCov.start
|
6
10
|
end
|
7
11
|
|
8
12
|
require "bundler/setup"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -121,13 +121,13 @@ files:
|
|
121
121
|
- lib/jat/config.rb
|
122
122
|
- lib/jat/plugins.rb
|
123
123
|
- lib/jat/plugins/activerecord/activerecord.rb
|
124
|
+
- lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb
|
125
|
+
- lib/jat/plugins/base/base_activerecord_preloads/lib/preloader.rb
|
126
|
+
- lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb
|
127
|
+
- lib/jat/plugins/base/base_preloads/base_preloads.rb
|
128
|
+
- lib/jat/plugins/base/base_preloads/lib/format_user_preloads.rb
|
129
|
+
- lib/jat/plugins/base/base_preloads/lib/preloads_with_path.rb
|
124
130
|
- lib/jat/plugins/cache/cache.rb
|
125
|
-
- lib/jat/plugins/common/_activerecord_preloads/_activerecord_preloads.rb
|
126
|
-
- lib/jat/plugins/common/_activerecord_preloads/lib/preloader.rb
|
127
|
-
- lib/jat/plugins/common/_lower_camel_case/_lower_camel_case.rb
|
128
|
-
- lib/jat/plugins/common/_preloads/_preloads.rb
|
129
|
-
- lib/jat/plugins/common/_preloads/lib/format_user_preloads.rb
|
130
|
-
- lib/jat/plugins/common/_preloads/lib/preloads_with_path.rb
|
131
131
|
- lib/jat/plugins/json_api/json_api.rb
|
132
132
|
- lib/jat/plugins/json_api/lib/fields_param_parser.rb
|
133
133
|
- lib/jat/plugins/json_api/lib/include_param_parser.rb
|
@@ -172,36 +172,44 @@ files:
|
|
172
172
|
- lib/jat/utils/enum_deep_freeze.rb
|
173
173
|
- test/lib/jat/attribute_test.rb
|
174
174
|
- test/lib/jat/config_test.rb
|
175
|
-
- test/lib/jat/plugins/
|
176
|
-
- test/lib/jat/plugins/
|
177
|
-
- test/lib/jat/plugins/
|
178
|
-
- test/lib/jat/plugins/
|
179
|
-
- test/lib/jat/plugins/
|
180
|
-
- test/lib/jat/plugins/
|
175
|
+
- test/lib/jat/plugins/activerecord/activerecord_test.rb
|
176
|
+
- test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb
|
177
|
+
- test/lib/jat/plugins/base_activerecord_preloads/lib/preloader_test.rb
|
178
|
+
- test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb
|
179
|
+
- test/lib/jat/plugins/base_preloads/base_preloads_test.rb
|
180
|
+
- test/lib/jat/plugins/base_preloads/lib/format_user_preloads_test.rb
|
181
|
+
- test/lib/jat/plugins/base_preloads/lib/preloads_with_path_test.rb
|
181
182
|
- test/lib/jat/plugins/cache/cache_test.rb
|
182
183
|
- test/lib/jat/plugins/json_api/json_api_test.rb
|
183
184
|
- test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb
|
184
185
|
- test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb
|
185
186
|
- test/lib/jat/plugins/json_api/lib/map_test.rb
|
187
|
+
- test/lib/jat/plugins/json_api/lib/response_piece_test.rb
|
186
188
|
- test/lib/jat/plugins/json_api/lib/response_test.rb
|
187
189
|
- test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb
|
188
|
-
- test/lib/jat/plugins/
|
190
|
+
- test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb
|
189
191
|
- test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb
|
190
192
|
- test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb
|
191
193
|
- test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb
|
192
194
|
- test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb
|
195
|
+
- test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb
|
196
|
+
- test/lib/jat/plugins/maps_cache/maps_cache_test.rb
|
197
|
+
- test/lib/jat/plugins/preloads/preloads_test.rb
|
193
198
|
- test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb
|
194
199
|
- test/lib/jat/plugins/simple_api/lib/map_test.rb
|
200
|
+
- test/lib/jat/plugins/simple_api/lib/response_piece_test.rb
|
195
201
|
- test/lib/jat/plugins/simple_api/lib/response_test.rb
|
196
202
|
- test/lib/jat/plugins/simple_api/simple_api_test.rb
|
197
203
|
- test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb
|
198
|
-
- test/lib/jat/plugins/
|
204
|
+
- test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb
|
199
205
|
- test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb
|
200
206
|
- test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb
|
201
207
|
- test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb
|
202
208
|
- test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb
|
203
209
|
- test/lib/jat/plugins/to_str/to_str_test.rb
|
204
210
|
- test/lib/jat/plugins/types/types_test.rb
|
211
|
+
- test/lib/jat/plugins/validate_params/validate_params_test.rb
|
212
|
+
- test/lib/jat/plugins_test.rb
|
205
213
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
206
214
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
207
215
|
- test/lib/jat_test.rb
|
@@ -238,36 +246,44 @@ summary: JSON API TOOLKIT
|
|
238
246
|
test_files:
|
239
247
|
- test/lib/jat/attribute_test.rb
|
240
248
|
- test/lib/jat/config_test.rb
|
241
|
-
- test/lib/jat/plugins/
|
242
|
-
- test/lib/jat/plugins/
|
243
|
-
- test/lib/jat/plugins/
|
244
|
-
- test/lib/jat/plugins/
|
245
|
-
- test/lib/jat/plugins/
|
246
|
-
- test/lib/jat/plugins/
|
249
|
+
- test/lib/jat/plugins/activerecord/activerecord_test.rb
|
250
|
+
- test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb
|
251
|
+
- test/lib/jat/plugins/base_activerecord_preloads/lib/preloader_test.rb
|
252
|
+
- test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb
|
253
|
+
- test/lib/jat/plugins/base_preloads/base_preloads_test.rb
|
254
|
+
- test/lib/jat/plugins/base_preloads/lib/format_user_preloads_test.rb
|
255
|
+
- test/lib/jat/plugins/base_preloads/lib/preloads_with_path_test.rb
|
247
256
|
- test/lib/jat/plugins/cache/cache_test.rb
|
248
257
|
- test/lib/jat/plugins/json_api/json_api_test.rb
|
249
258
|
- test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb
|
250
259
|
- test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb
|
251
260
|
- test/lib/jat/plugins/json_api/lib/map_test.rb
|
261
|
+
- test/lib/jat/plugins/json_api/lib/response_piece_test.rb
|
252
262
|
- test/lib/jat/plugins/json_api/lib/response_test.rb
|
253
263
|
- test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb
|
254
|
-
- test/lib/jat/plugins/
|
264
|
+
- test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb
|
255
265
|
- test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb
|
256
266
|
- test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb
|
257
267
|
- test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb
|
258
268
|
- test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb
|
269
|
+
- test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb
|
270
|
+
- test/lib/jat/plugins/maps_cache/maps_cache_test.rb
|
271
|
+
- test/lib/jat/plugins/preloads/preloads_test.rb
|
259
272
|
- test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb
|
260
273
|
- test/lib/jat/plugins/simple_api/lib/map_test.rb
|
274
|
+
- test/lib/jat/plugins/simple_api/lib/response_piece_test.rb
|
261
275
|
- test/lib/jat/plugins/simple_api/lib/response_test.rb
|
262
276
|
- test/lib/jat/plugins/simple_api/simple_api_test.rb
|
263
277
|
- test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb
|
264
|
-
- test/lib/jat/plugins/
|
278
|
+
- test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb
|
265
279
|
- test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb
|
266
280
|
- test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb
|
267
281
|
- test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb
|
268
282
|
- test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb
|
269
283
|
- test/lib/jat/plugins/to_str/to_str_test.rb
|
270
284
|
- test/lib/jat/plugins/types/types_test.rb
|
285
|
+
- test/lib/jat/plugins/validate_params/validate_params_test.rb
|
286
|
+
- test/lib/jat/plugins_test.rb
|
271
287
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
272
288
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
273
289
|
- test/lib/jat_test.rb
|