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
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApiMapsCache" do
|
6
|
+
before do
|
7
|
+
@plugin = Jat::Plugins.find_plugin(:json_api_maps_cache)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "checks json_api plugin loaded before" do
|
11
|
+
jat_class = Class.new(Jat)
|
12
|
+
error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
|
13
|
+
assert_match(/json_api/, error.message)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "adds config variable how many maps to store per serializer" do
|
17
|
+
jat_class = Class.new(Jat)
|
18
|
+
|
19
|
+
@plugin.after_load(jat_class)
|
20
|
+
assert_equal(100, jat_class.config[:cached_maps_count]) # default 100
|
21
|
+
|
22
|
+
@plugin.after_load(jat_class, cached_maps_count: 10) # change value via opts
|
23
|
+
assert_equal(10, jat_class.config[:cached_maps_count])
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Test maps responses" do
|
27
|
+
let(:jat_class) do
|
28
|
+
Class.new(Jat) do
|
29
|
+
plugin(:json_api)
|
30
|
+
plugin(:json_api_maps_cache)
|
31
|
+
|
32
|
+
type :foo
|
33
|
+
|
34
|
+
attribute(:attr1)
|
35
|
+
attribute(:attr2)
|
36
|
+
|
37
|
+
relationship(:rel1, serializer: self)
|
38
|
+
relationship(:rel2, serializer: self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns same maps when requested with same params" do
|
43
|
+
full_map1 = jat_class::Map.call(exposed: :all)
|
44
|
+
full_map2 = jat_class::Map.call(exposed: :all)
|
45
|
+
|
46
|
+
exposed_map1 = jat_class::Map.call(exposed: :default)
|
47
|
+
exposed_map2 = jat_class::Map.call(exposed: :default)
|
48
|
+
|
49
|
+
current_map1 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
|
50
|
+
current_map2 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
|
51
|
+
|
52
|
+
assert_same(full_map1, full_map2)
|
53
|
+
assert_same(exposed_map1, exposed_map2)
|
54
|
+
assert_same(current_map1, current_map2)
|
55
|
+
|
56
|
+
# check different maps are not same
|
57
|
+
|
58
|
+
# should not match, include is not same
|
59
|
+
current_map3 = jat_class::Map.call(include: "rel1", fields: {foo: "attr1,rel1"})
|
60
|
+
# should not match, fields are not same
|
61
|
+
current_map4 = jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel2"})
|
62
|
+
|
63
|
+
assert !current_map1.equal?(current_map3)
|
64
|
+
assert !current_map1.equal?(current_map4)
|
65
|
+
assert !current_map3.equal?(current_map4)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "stores different maps keys" do
|
69
|
+
# key 1
|
70
|
+
jat_class::Map.call(exposed: :all)
|
71
|
+
jat_class::Map.call(exposed: :all)
|
72
|
+
|
73
|
+
# key 2
|
74
|
+
jat_class::Map.call(exposed: :default)
|
75
|
+
jat_class::Map.call(exposed: :default)
|
76
|
+
|
77
|
+
# key 3
|
78
|
+
jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
|
79
|
+
jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel1"})
|
80
|
+
|
81
|
+
# key 4
|
82
|
+
jat_class::Map.call(include: "rel1", fields: {foo: "attr1,rel1"})
|
83
|
+
|
84
|
+
# key 5
|
85
|
+
jat_class::Map.call(include: "rel2", fields: {foo: "attr1,rel2"})
|
86
|
+
|
87
|
+
assert_equal 5, jat_class::Map.maps_cache.keys.count
|
88
|
+
end
|
89
|
+
|
90
|
+
it "clears old results when there are too many cache keys" do
|
91
|
+
jat_class.config[:cached_maps_count] = 1
|
92
|
+
|
93
|
+
full_map1 = jat_class::Map.call(exposed: :all)
|
94
|
+
full_map2 = jat_class::Map.call(exposed: :all)
|
95
|
+
|
96
|
+
# ensure maps refer to same object
|
97
|
+
assert_same(full_map1, full_map2)
|
98
|
+
|
99
|
+
# replace single possible cache key with another `exposed` map
|
100
|
+
jat_class::Map.call(exposed: :default)
|
101
|
+
|
102
|
+
# calculate full map again, it should not match
|
103
|
+
full_map3 = jat_class::Map.call(exposed: :all)
|
104
|
+
assert !full_map1.equal?(full_map3)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApiPreloads" do
|
6
|
+
before do
|
7
|
+
@plugin = Jat::Plugins.find_plugin(:json_api_preloads)
|
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
|
+
|
20
|
+
jat_class.expects(:plugin).with(:base_preloads, foo: :bar)
|
21
|
+
|
22
|
+
@plugin.before_load(jat_class, foo: :bar)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "InstanceMethods" do
|
26
|
+
it "adds #preloads method as a delegator to #{@plugin}::Preloads" do
|
27
|
+
jat_class = Class.new(Jat)
|
28
|
+
jat_class.plugin :json_api
|
29
|
+
jat_class.plugin @plugin
|
30
|
+
jat = jat_class.allocate
|
31
|
+
|
32
|
+
@plugin::Preloads.expects(:call).with(jat).returns("RES")
|
33
|
+
|
34
|
+
assert_equal "RES", jat.preloads
|
35
|
+
end
|
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 :json_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
|
51
|
+
end
|
@@ -2,8 +2,13 @@
|
|
2
2
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
|
-
describe "Jat::Plugins::
|
6
|
-
let(:base)
|
5
|
+
describe "Jat::Plugins::JsonApiPreloads::Preloads" do
|
6
|
+
let(:base) do
|
7
|
+
Class.new(Jat) do
|
8
|
+
plugin :json_api
|
9
|
+
plugin :json_api_preloads
|
10
|
+
end
|
11
|
+
end
|
7
12
|
|
8
13
|
let(:user_serializer) { Class.new(base) { type(:user) } }
|
9
14
|
let(:profile_serializer) { Class.new(base) { type(:profile) } }
|
@@ -13,10 +18,10 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
13
18
|
let(:jat_profile) { profile_serializer.allocate }
|
14
19
|
let(:jat_email) { email_serializer.allocate }
|
15
20
|
|
16
|
-
let(:described_class) { Jat::Plugins::
|
21
|
+
let(:described_class) { Jat::Plugins::JsonApiPreloads::Preloads }
|
17
22
|
|
18
23
|
def define_map(map)
|
19
|
-
jat_user.
|
24
|
+
jat_user.expects(:map).returns(map)
|
20
25
|
end
|
21
26
|
|
22
27
|
it "returns empty hash when no attributes requested" do
|
@@ -129,7 +134,7 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
129
134
|
assert_equal({company: {profile: {}, confirmed_email: {}, unconfirmed_email: {}}}, result)
|
130
135
|
end
|
131
136
|
|
132
|
-
it "
|
137
|
+
it "does not raise error if 2 serializers with recursive preloads specified" do
|
133
138
|
user_serializer.relationship :profile, serializer: profile_serializer, preload: :profile
|
134
139
|
profile_serializer.relationship :user, serializer: user_serializer, preload: :user
|
135
140
|
|
@@ -138,11 +143,11 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
138
143
|
profile: {attributes: [], relationships: %i[user]}
|
139
144
|
)
|
140
145
|
|
141
|
-
|
142
|
-
|
146
|
+
expected_result = {profile: {user: {}}}
|
147
|
+
assert_equal(expected_result, described_class.call(jat_user))
|
143
148
|
end
|
144
149
|
|
145
|
-
it "
|
150
|
+
it "does not raise error if 3 serializers with recursive preloads specified" do
|
146
151
|
user_serializer.relationship :profile, serializer: profile_serializer, preload: :profile
|
147
152
|
profile_serializer.relationship :email, serializer: email_serializer, preload: :email
|
148
153
|
email_serializer.relationship :user, serializer: user_serializer, preload: :user
|
@@ -153,8 +158,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
153
158
|
email: {attributes: [], relationships: %i[user]}
|
154
159
|
)
|
155
160
|
|
156
|
-
|
157
|
-
|
161
|
+
expected_result = {profile: {email: {user: {}}}}
|
162
|
+
assert_equal(expected_result, described_class.call(jat_user))
|
158
163
|
end
|
159
164
|
|
160
165
|
it "does not raises error if 2 serializers preloads same preloads" do
|
@@ -168,7 +173,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
168
173
|
email: {attributes: [], relationships: %i[]}
|
169
174
|
)
|
170
175
|
|
171
|
-
|
176
|
+
expected_result = {profile: {email: {}}, email: {}}
|
177
|
+
assert_equal(expected_result, described_class.call(jat_user))
|
172
178
|
end
|
173
179
|
|
174
180
|
it "merges preloads the same way regardless of order of preloads" do
|
@@ -179,8 +185,8 @@ describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
|
|
179
185
|
jat_a1 = a.allocate
|
180
186
|
jat_a2 = a.allocate
|
181
187
|
|
182
|
-
jat_a1.
|
183
|
-
jat_a2.
|
188
|
+
jat_a1.expects(:map).returns(a: {attributes: %i[a1 a2], relationships: %i[]})
|
189
|
+
jat_a2.expects(:map).returns(a: {attributes: %i[a2 a1], relationships: %i[]})
|
184
190
|
|
185
191
|
result1 = described_class.call(jat_a1)
|
186
192
|
result2 = described_class.call(jat_a2)
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApiValidateParams" do
|
6
|
+
before do
|
7
|
+
@plugin = Jat::Plugins.find_plugin(:json_api_validate_params)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "checks json_api plugin loaded before" do
|
11
|
+
jat_class = Class.new(Jat)
|
12
|
+
error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
|
13
|
+
assert_match(/json_api/, error.message)
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:base) do
|
17
|
+
jat_class = Class.new(Jat)
|
18
|
+
jat_class.plugin :json_api
|
19
|
+
jat_class
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:serializer) do
|
23
|
+
jat_class = Class.new(base)
|
24
|
+
jat_class.plugin @plugin
|
25
|
+
|
26
|
+
jat_class.type "foo"
|
27
|
+
jat_class.attribute :foo_bar
|
28
|
+
jat_class.relationship :foo_bazz, serializer: foo_bazz_serializer
|
29
|
+
jat_class
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:foo_bazz_serializer) do
|
33
|
+
jat_class = Class.new(base)
|
34
|
+
jat_class.type "foo_bazz"
|
35
|
+
jat_class.attribute :bazz
|
36
|
+
jat_class
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:serializer_lower_camel_case) do
|
40
|
+
jat_class = Class.new(base)
|
41
|
+
jat_class.plugin :json_api_lower_camel_case
|
42
|
+
jat_class.plugin @plugin
|
43
|
+
|
44
|
+
jat_class.type "foo"
|
45
|
+
jat_class.attribute :foo_bar
|
46
|
+
jat_class.relationship :foo_bazz, serializer: foo_bazz_serializer
|
47
|
+
jat_class
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns true when provided fields present" do
|
51
|
+
jat = serializer.new(fields: {foo: "foo_bar,foo_bazz", foo_bazz: "bazz"})
|
52
|
+
assert jat.validate
|
53
|
+
end
|
54
|
+
|
55
|
+
it "validates fields types" do
|
56
|
+
jat = serializer.new(fields: {bar: "any"})
|
57
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
58
|
+
|
59
|
+
expected_message = "Response does not have resources with type 'bar'. Existing types are: 'foo', 'foo_bazz'"
|
60
|
+
assert_equal(expected_message, error.message)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "validates attributes" do
|
64
|
+
jat = serializer.new(fields: {foo: "foo_bar,foo_baz"})
|
65
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
66
|
+
|
67
|
+
expected_message = "No attribute 'foo_baz' in resource type 'foo'. Existing attributes are: 'foo_bar', 'foo_bazz'"
|
68
|
+
assert_equal(expected_message, error.message)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "validates attributes correctly when lower_camel_case plugin loaded" do
|
72
|
+
jat = serializer_lower_camel_case.new(fields: {foo: "fooBar,fooBaz"})
|
73
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
74
|
+
|
75
|
+
expected_message = "No attribute 'fooBaz' in resource type 'foo'. Existing attributes are: 'fooBar', 'fooBazz'"
|
76
|
+
assert_equal(expected_message, error.message)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "validates includes" do
|
80
|
+
jat = serializer.new(include: "extra")
|
81
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
82
|
+
expected_message = "Type 'foo' has no included 'extra' relationship. Existing relationships are: 'foo_bazz'"
|
83
|
+
assert_equal(expected_message, error.message)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "validates includes when parameter named as existing attribute (not relationship)" do
|
87
|
+
jat = serializer.new(include: "foo_bar")
|
88
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
89
|
+
expected_message = "Type 'foo' has no included 'foo_bar' relationship. Existing relationships are: 'foo_bazz'"
|
90
|
+
assert_equal(expected_message, error.message)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "validates includes correctly when lower_camel_case plugin loaded" do
|
94
|
+
jat = serializer_lower_camel_case.new(include: "extra")
|
95
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
96
|
+
|
97
|
+
expected_message = "Type 'foo' has no included 'extra' relationship. Existing relationships are: 'fooBazz'"
|
98
|
+
assert_equal(expected_message, error.message)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "validates nested includes" do
|
102
|
+
jat = serializer.new(include: "foo_bazz.extra")
|
103
|
+
error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
|
104
|
+
|
105
|
+
expected_message = "Type 'foo_bazz' has no included 'extra' relationship. Existing relationships are: ''"
|
106
|
+
assert_equal(expected_message, error.message)
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::LowerCamelCase" 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 :lower_camel_case }
|
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 :lower_camel_case
|
16
|
+
|
17
|
+
assert new_class.plugin_used?(:simple_api_lower_camel_case)
|
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 :lower_camel_case
|
24
|
+
|
25
|
+
assert new_class.plugin_used?(:json_api_lower_camel_case)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::MapsCache" 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 :maps_cache }
|
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 :maps_cache
|
16
|
+
|
17
|
+
assert new_class.plugin_used?(:simple_api_maps_cache)
|
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 :maps_cache
|
24
|
+
|
25
|
+
assert new_class.plugin_used?(:json_api_maps_cache)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Preloads" 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 :preloads }
|
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 :preloads
|
16
|
+
|
17
|
+
assert new_class.plugin_used?(:simple_api_preloads)
|
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 :preloads
|
24
|
+
|
25
|
+
assert new_class.plugin_used?(:json_api_preloads)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::SimpleApi::FieldsParamParser" do
|
6
|
+
before do
|
7
|
+
Jat::Plugins.find_plugin :simple_api
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:jat_class) do
|
11
|
+
ser = Class.new(Jat)
|
12
|
+
ser.plugin :simple_api
|
13
|
+
ser
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:described_class) { jat_class::FieldsParamParser }
|
17
|
+
|
18
|
+
def parse(str)
|
19
|
+
jat_class::FieldsParamParser.parse(str)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe ".inspect" do
|
23
|
+
it "returns self name" do
|
24
|
+
assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".parse" do
|
29
|
+
it "returns empty hash when nil provided" do
|
30
|
+
assert_equal({}, parse(nil))
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns empty hash when empty string provided" do
|
34
|
+
assert_equal({}, parse(""))
|
35
|
+
end
|
36
|
+
|
37
|
+
it "parses single field" do
|
38
|
+
assert_equal({id: {}}, parse("id"))
|
39
|
+
end
|
40
|
+
|
41
|
+
it "parses multiple fields" do
|
42
|
+
assert_equal({id: {}, name: {}}, parse("id, name"))
|
43
|
+
end
|
44
|
+
|
45
|
+
it "parses single resource with single field" do
|
46
|
+
assert_equal({users: {id: {}}}, parse("users(id)"))
|
47
|
+
end
|
48
|
+
|
49
|
+
it "parses fields started with open PAREN" do
|
50
|
+
assert_equal({users: {id: {}}}, parse("(users(id))"))
|
51
|
+
end
|
52
|
+
|
53
|
+
it "parses single resource with multiple fields" do
|
54
|
+
assert_equal({users: {id: {}, name: {}}}, parse("users(id,name)"))
|
55
|
+
end
|
56
|
+
|
57
|
+
it "parses multiple resources with fields" do
|
58
|
+
fields = "id,posts(title,text),news(title,text)"
|
59
|
+
resp = {
|
60
|
+
id: {},
|
61
|
+
posts: {title: {}, text: {}},
|
62
|
+
news: {title: {}, text: {}}
|
63
|
+
}
|
64
|
+
|
65
|
+
assert_equal(resp, parse(fields))
|
66
|
+
end
|
67
|
+
|
68
|
+
it "parses included resources" do
|
69
|
+
fields = "id,posts(title,text,comments(author(name),comment))"
|
70
|
+
resp = {
|
71
|
+
id: {},
|
72
|
+
posts: {
|
73
|
+
title: {},
|
74
|
+
text: {},
|
75
|
+
comments: {
|
76
|
+
author: {
|
77
|
+
name: {}
|
78
|
+
},
|
79
|
+
comment: {}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
assert_equal(resp, parse(fields))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|