jat 0.0.1 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jat/attribute.rb +107 -0
- data/lib/jat/config.rb +35 -0
- data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
- data/lib/jat/plugins/cache/cache.rb +47 -0
- data/lib/jat/plugins/common/_activerecord_preloads/_activerecord_preloads.rb +38 -0
- data/lib/jat/plugins/common/_activerecord_preloads/lib/preloader.rb +93 -0
- data/lib/jat/plugins/common/_lower_camel_case/_lower_camel_case.rb +36 -0
- data/lib/jat/plugins/common/_preloads/_preloads.rb +63 -0
- data/lib/jat/plugins/common/_preloads/lib/format_user_preloads.rb +52 -0
- data/lib/jat/plugins/common/_preloads/lib/preloads_with_path.rb +78 -0
- data/lib/jat/plugins/json_api/json_api.rb +251 -0
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
- data/lib/jat/plugins/json_api/lib/map.rb +119 -0
- data/lib/jat/plugins/json_api/lib/params/fields/parse.rb +27 -0
- data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +58 -0
- data/lib/jat/plugins/json_api/lib/params/fields.rb +23 -0
- data/lib/jat/plugins/json_api/lib/params/include/parse.rb +55 -0
- data/lib/jat/plugins/json_api/lib/params/include/validate.rb +29 -0
- data/lib/jat/plugins/json_api/lib/params/include.rb +49 -0
- data/lib/jat/plugins/json_api/lib/response.rb +123 -0
- data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/lib/preloads.rb +76 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
- data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
- data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
- data/lib/jat/plugins/preloads/preloads.rb +23 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
- data/lib/jat/plugins/simple_api/lib/map.rb +99 -0
- data/lib/jat/plugins/simple_api/lib/response.rb +119 -0
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
- data/lib/jat/plugins/simple_api/simple_api.rb +125 -0
- data/lib/jat/plugins/to_str/to_str.rb +54 -0
- data/lib/jat/plugins/types/types.rb +54 -0
- data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
- data/lib/jat/plugins.rb +39 -0
- data/lib/jat/utils/enum_deep_dup.rb +29 -0
- data/lib/jat/utils/enum_deep_freeze.rb +19 -0
- data/lib/jat.rb +66 -141
- data/test/lib/jat/attribute_test.rb +152 -0
- data/test/lib/jat/config_test.rb +57 -0
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +59 -0
- data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +84 -0
- data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
- data/test/lib/jat/plugins/_preloads/_preloads_test.rb +68 -0
- data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +47 -0
- data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +33 -0
- data/test/lib/jat/plugins/cache/cache_test.rb +82 -0
- data/test/lib/jat/plugins/json_api/json_api_test.rb +162 -0
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +38 -0
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +41 -0
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +188 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +489 -0
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
- data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +37 -0
- data/test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb +197 -0
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +77 -0
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +133 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +348 -0
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +139 -0
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
- data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
- data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +37 -0
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +52 -0
- data/test/lib/jat/plugins/types/types_test.rb +79 -0
- data/test/lib/jat/utils/enum_deep_dup_test.rb +31 -0
- data/test/lib/jat/utils/enum_deep_freeze_test.rb +28 -0
- data/test/lib/jat_test.rb +143 -0
- data/test/lib/plugin_test.rb +49 -0
- data/test/support/activerecord.rb +24 -0
- data/test/test_helper.rb +13 -0
- data/test/test_plugin.rb +56 -0
- metadata +243 -11
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::SimpleApiPreloads::Preloads" do
|
6
|
+
let(:base) do
|
7
|
+
Class.new(Jat) do
|
8
|
+
plugin :simple_api
|
9
|
+
plugin :simple_api_preloads
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:user_serializer) { Class.new(base) }
|
14
|
+
let(:profile_serializer) { Class.new(base) }
|
15
|
+
let(:email_serializer) { Class.new(base) }
|
16
|
+
|
17
|
+
let(:jat_user) { user_serializer.new }
|
18
|
+
let(:jat_profile) { profile_serializer.new }
|
19
|
+
let(:jat_email) { email_serializer.new }
|
20
|
+
|
21
|
+
let(:described_class) { Jat::Plugins::SimpleApiPreloads::Preloads }
|
22
|
+
|
23
|
+
def define_map(map)
|
24
|
+
user_serializer::Map.expects(:call).returns(map)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns empty hash when no attributes requested" do
|
28
|
+
define_map({})
|
29
|
+
|
30
|
+
result = described_class.call(jat_user)
|
31
|
+
assert_equal({}, result)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns empty hash when no attributes with preloads requested" do
|
35
|
+
user_serializer.attribute :name
|
36
|
+
define_map({name: {}})
|
37
|
+
|
38
|
+
result = described_class.call(jat_user)
|
39
|
+
assert_equal({}, result)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns preloads for requested attributes" do
|
43
|
+
user_serializer.attribute :name, preload: :profile
|
44
|
+
define_map({name: {}})
|
45
|
+
|
46
|
+
result = described_class.call(jat_user)
|
47
|
+
assert_equal({profile: {}}, result)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns merged preloads for requested attributes" do
|
51
|
+
user_serializer.attribute :first_name, preload: :profile
|
52
|
+
user_serializer.attribute :phone, preload: {profile: :phones}
|
53
|
+
user_serializer.attribute :email, preload: {profile: :emails}
|
54
|
+
|
55
|
+
define_map({first_name: {}, phone: {}, email: {}})
|
56
|
+
|
57
|
+
result = described_class.call(jat_user)
|
58
|
+
assert_equal({profile: {phones: {}, emails: {}}}, result)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns no preloads and no nested preloads for relationships when specified preloads is nil" do
|
62
|
+
user_serializer.relationship :profile, serializer: profile_serializer, preload: nil
|
63
|
+
profile_serializer.attribute :email, preload: :email # should not be preloaded
|
64
|
+
define_map(profile: {email: {}})
|
65
|
+
|
66
|
+
result = described_class.call(jat_user)
|
67
|
+
assert_equal({}, result)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns preloads for nested relationships joined to root when specified preloads is {}" do
|
71
|
+
user_serializer.relationship :profile, serializer: profile_serializer, preload: {}
|
72
|
+
profile_serializer.attribute :email, preload: :email # should be preloaded to root
|
73
|
+
define_map({profile: {email: {}}})
|
74
|
+
|
75
|
+
result = described_class.call(jat_user)
|
76
|
+
assert_equal({email: {}}, result)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "returns preloads for nested relationships joined to root when specified preloads is []" do
|
80
|
+
user_serializer.relationship :profile, serializer: profile_serializer, preload: []
|
81
|
+
profile_serializer.attribute :email, preload: :email # should be preloaded to root
|
82
|
+
define_map({profile: {email: {}}})
|
83
|
+
|
84
|
+
result = described_class.call(jat_user)
|
85
|
+
assert_equal({email: {}}, result)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "returns preloads for relationships" do
|
89
|
+
user_serializer.relationship :profile, serializer: profile_serializer
|
90
|
+
define_map(profile: {})
|
91
|
+
|
92
|
+
result = described_class.call(jat_user)
|
93
|
+
assert_equal({profile: {}}, result)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns nested preloads for relationships" do
|
97
|
+
user_serializer.relationship :profile, serializer: profile_serializer
|
98
|
+
profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
|
99
|
+
define_map(profile: {email: {}})
|
100
|
+
|
101
|
+
result = described_class.call(jat_user)
|
102
|
+
assert_equal({profile: {confirmed_email: {}, unconfirmed_email: {}}}, result)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "preloads nested relationships for nested relationship" do
|
106
|
+
user_serializer.relationship :profile, serializer: profile_serializer, preload: {company: :profile}
|
107
|
+
profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
|
108
|
+
define_map(profile: {email: {}})
|
109
|
+
|
110
|
+
result = described_class.call(jat_user)
|
111
|
+
assert_equal({company: {profile: {confirmed_email: {}, unconfirmed_email: {}}}}, result)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "preloads nested relationships to main (!) resource" do
|
115
|
+
user_serializer.relationship :profile, serializer: profile_serializer, preload: {company!: :profile}
|
116
|
+
profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
|
117
|
+
define_map(profile: {email: {}})
|
118
|
+
|
119
|
+
result = described_class.call(jat_user)
|
120
|
+
assert_equal({company: {profile: {}, confirmed_email: {}, unconfirmed_email: {}}}, result)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "merges preloads the same way regardless of order of preloads" do
|
124
|
+
a = Class.new(base)
|
125
|
+
a.attribute :a1, preload: {foo: {bar: {bazz1: {}, bazz: {}}}}
|
126
|
+
a.attribute :a2, preload: {foo: {bar: {bazz2: {}, bazz: {last: {}}}}}
|
127
|
+
|
128
|
+
jat_a1 = a.allocate
|
129
|
+
jat_a2 = a.allocate
|
130
|
+
|
131
|
+
jat_a1.expects(:map).returns({a1: {}, a2: {}})
|
132
|
+
jat_a2.expects(:map).returns({a2: {}, a1: {}})
|
133
|
+
|
134
|
+
result1 = described_class.call(jat_a1)
|
135
|
+
result2 = described_class.call(jat_a2)
|
136
|
+
|
137
|
+
assert_equal(result2, result1)
|
138
|
+
assert_equal({foo: {bar: {bazz: {last: {}}, bazz1: {}, bazz2: {}}}}, result1)
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::SimpleApiPreloads" do
|
6
|
+
before do
|
7
|
+
@plugin = Jat::Plugins.find_plugin(:simple_api_preloads)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "checks simple_api plugin loaded before" do
|
11
|
+
jat_class = Class.new(Jat)
|
12
|
+
error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
|
13
|
+
assert_match(/simple_api/, error.message)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "loads other plugins in before_load" do
|
17
|
+
jat_class = Class.new(Jat)
|
18
|
+
jat_class.plugin :simple_api
|
19
|
+
|
20
|
+
jat_class.expects(:plugin).with(:_preloads, foo: :bar)
|
21
|
+
|
22
|
+
@plugin.before_load(jat_class, foo: :bar)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "InstanceMethods" do
|
26
|
+
it "add .preloads method as a delegator to #{@plugin}::Preloads" do
|
27
|
+
jat_class = Class.new(Jat)
|
28
|
+
jat_class.plugin :simple_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
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::SimpleApiValidateParams" do
|
6
|
+
before do
|
7
|
+
@plugin = Jat::Plugins.find_plugin(:simple_api_validate_params)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "checks simple_api plugin loaded before" do
|
11
|
+
jat_class = Class.new(Jat)
|
12
|
+
error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
|
13
|
+
assert_match(/simple_api/, error.message)
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:base_serializer) do
|
17
|
+
jat_class = Class.new(Jat)
|
18
|
+
jat_class.plugin :simple_api
|
19
|
+
jat_class.plugin @plugin
|
20
|
+
jat_class
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:serializer) do
|
24
|
+
jat_class = Class.new(base_serializer)
|
25
|
+
jat_class.attribute :foo_bar
|
26
|
+
jat_class.relationship :foo_bazz, serializer: jat_class
|
27
|
+
jat_class
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:serializer_lower_camel_case) do
|
31
|
+
jat_class = Class.new(base_serializer)
|
32
|
+
jat_class.plugin :lower_camel_case
|
33
|
+
|
34
|
+
jat_class.attribute :foo_bar
|
35
|
+
jat_class.relationship :foo_bazz, serializer: jat_class
|
36
|
+
jat_class
|
37
|
+
end
|
38
|
+
|
39
|
+
it "validates fields" do
|
40
|
+
jat = serializer.new(fields: "foo_bar,extra")
|
41
|
+
error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
|
42
|
+
expected_message = "Field 'extra' not exists"
|
43
|
+
assert_equal(expected_message, error.message)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "validates deeply nested fields" do
|
47
|
+
c = Class.new(base_serializer)
|
48
|
+
c.attribute :c1
|
49
|
+
c.attribute :c2
|
50
|
+
|
51
|
+
b = Class.new(base_serializer)
|
52
|
+
b.attribute :b1
|
53
|
+
b.attribute :b2
|
54
|
+
b.relationship :c, serializer: c
|
55
|
+
|
56
|
+
a = Class.new(base_serializer)
|
57
|
+
a.attribute :a1
|
58
|
+
a.attribute :a2
|
59
|
+
a.relationship :b, serializer: b
|
60
|
+
a.relationship :c, serializer: c
|
61
|
+
|
62
|
+
jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2,c3)")
|
63
|
+
error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
|
64
|
+
expected_message = "Field 'c3' ('b.c.c3') not exists"
|
65
|
+
assert_equal(expected_message, error.message)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "validates deeply nested fields about not existing relationship" do
|
69
|
+
c = Class.new(base_serializer)
|
70
|
+
c.attribute :c1
|
71
|
+
c.attribute :c2
|
72
|
+
|
73
|
+
b = Class.new(base_serializer)
|
74
|
+
b.attribute :b1
|
75
|
+
b.attribute :b2
|
76
|
+
b.relationship :c, serializer: c
|
77
|
+
|
78
|
+
a = Class.new(base_serializer)
|
79
|
+
a.attribute :a1
|
80
|
+
a.attribute :a2
|
81
|
+
a.relationship :b, serializer: b
|
82
|
+
a.relationship :c, serializer: c
|
83
|
+
|
84
|
+
jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2(c3))")
|
85
|
+
error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
|
86
|
+
expected_message = "Field 'c2' ('b.c.c2') is not a relationship to add 'c3' attribute"
|
87
|
+
assert_equal(expected_message, error.message)
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::ToStr" do
|
6
|
+
let(:jat_class) do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
new_class.plugin(:to_str)
|
9
|
+
new_class.class_exec do
|
10
|
+
def to_h(object)
|
11
|
+
{object => context}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
new_class
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "Jat" do
|
19
|
+
describe ".to_str" do
|
20
|
+
it "returns json string of to_h" do
|
21
|
+
assert_equal '{"obj":"ctx"}', jat_class.to_str("obj", "ctx")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns json string of to_h with default empty hash context" do
|
25
|
+
assert_equal '{"obj":{}}', jat_class.to_str("obj")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns result serialized with given in config serializer" do
|
29
|
+
jat_class.config[:to_str] = ->(data) { data.inspect }
|
30
|
+
assert_equal '{"obj"=>"ctx"}', jat_class.to_str("obj", "ctx")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "accepts to_str config option when loading plugin" do
|
34
|
+
new_class = Class.new(Jat)
|
35
|
+
new_class.plugin(:to_str, to_str: ->(data) { data.inspect })
|
36
|
+
new_class.class_exec do
|
37
|
+
def to_h(object)
|
38
|
+
{object => context}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal '{"obj"=>"ctx"}', new_class.to_str("obj", "ctx")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#to_str" do
|
47
|
+
it "returns json string of to_h" do
|
48
|
+
assert_equal '{"obj":"ctx"}', jat_class.new("ctx").to_str("obj")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Types" do
|
6
|
+
let(:struct) { Struct.new(:attr) }
|
7
|
+
let(:obj) { struct.new(nil) }
|
8
|
+
|
9
|
+
let(:jat_class) do
|
10
|
+
new_class = Class.new(Jat)
|
11
|
+
new_class.plugin(:types)
|
12
|
+
new_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def attr_value(original_value)
|
16
|
+
obj = struct.new(original_value)
|
17
|
+
jat_class.attributes[:attr].value(obj, nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "has predefined :int type" do
|
21
|
+
jat_class.attribute(:attr, type: :int)
|
22
|
+
|
23
|
+
assert_equal(0, attr_value("0"))
|
24
|
+
assert_equal(8, attr_value("010")) # octal system
|
25
|
+
assert_equal(10, attr_value("0xa")) # hexadecimal system
|
26
|
+
end
|
27
|
+
|
28
|
+
it "has predefined :bool type" do
|
29
|
+
jat_class.attribute(:attr, type: :bool)
|
30
|
+
|
31
|
+
assert_equal(false, attr_value(nil))
|
32
|
+
assert_equal(false, attr_value(false))
|
33
|
+
assert_equal(true, attr_value(1))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "has predefined :float type" do
|
37
|
+
jat_class.attribute(:attr, type: :float)
|
38
|
+
|
39
|
+
assert_same(0.0, attr_value("0"))
|
40
|
+
assert_same(0.001, attr_value("1e-3"))
|
41
|
+
assert_same(1.234, attr_value("1.234"))
|
42
|
+
end
|
43
|
+
|
44
|
+
it "has predefined :array type" do
|
45
|
+
jat_class.attribute(:attr, type: :array)
|
46
|
+
|
47
|
+
assert_equal([1], attr_value(1))
|
48
|
+
assert_equal([1], attr_value([1]))
|
49
|
+
end
|
50
|
+
|
51
|
+
it "has predefined :hash type" do
|
52
|
+
jat_class.attribute(:attr, type: :hash)
|
53
|
+
|
54
|
+
assert_equal({}, attr_value([]))
|
55
|
+
assert_equal({}, attr_value({}))
|
56
|
+
assert_equal({key: :value}, attr_value(key: :value))
|
57
|
+
end
|
58
|
+
|
59
|
+
it "has predefined :str type" do
|
60
|
+
jat_class.attribute(:attr, type: :str)
|
61
|
+
|
62
|
+
assert_equal("", attr_value(nil))
|
63
|
+
assert_equal("123", attr_value(123))
|
64
|
+
end
|
65
|
+
|
66
|
+
it "allows to use callable method as type" do
|
67
|
+
jat_class.attribute(:attr, type: ->(obj) { obj ? :yes : :no })
|
68
|
+
|
69
|
+
assert_equal(:no, attr_value(nil))
|
70
|
+
assert_equal(:yes, attr_value(1))
|
71
|
+
end
|
72
|
+
|
73
|
+
it "allows to configure custom type" do
|
74
|
+
jat_class.config[:types][:year] = ->(obj) { obj.strftime("%Y") }
|
75
|
+
jat_class.attribute(:attr, type: :year)
|
76
|
+
|
77
|
+
assert_equal("2020", attr_value(Time.new(2020, 10, 10)))
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe Jat::EnumDeepDup do
|
6
|
+
describe ".call" do
|
7
|
+
it "makes deep dup of hash" do
|
8
|
+
hash = {key1: {key11: {key111: :value111}}, key2: [{key22: {key222: :value222}}]}
|
9
|
+
dup = Jat::EnumDeepDup.call(hash)
|
10
|
+
|
11
|
+
assert_equal hash, dup
|
12
|
+
|
13
|
+
refute_same hash, dup
|
14
|
+
refute_same hash[:key1], dup[:key1]
|
15
|
+
refute_same hash[:key1][:key11], dup[:key1][:key11]
|
16
|
+
|
17
|
+
refute_same hash[:key2], dup[:key2]
|
18
|
+
refute_same hash[:key2][0], dup[:key2][0]
|
19
|
+
refute_same hash[:key2][0][:key22], dup[:key2][0][:key22]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not duplicates non-enumerable objects" do
|
23
|
+
hash = {key1: Jat, key2: [-> {}]}
|
24
|
+
dup = Jat::EnumDeepDup.call(hash)
|
25
|
+
|
26
|
+
assert_equal hash, dup
|
27
|
+
assert_same hash[:key1], dup[:key1]
|
28
|
+
assert_same hash[:key2][0], dup[:key2][0]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe Jat::EnumDeepFreeze do
|
6
|
+
describe ".call" do
|
7
|
+
it "deeply freezes provided hash" do
|
8
|
+
hash = {key1: {key11: {key111: :value111}}, key2: [{key22: {key222: :value222}}]}
|
9
|
+
Jat::EnumDeepFreeze.call(hash)
|
10
|
+
|
11
|
+
assert hash.frozen?
|
12
|
+
assert hash[:key1].frozen?
|
13
|
+
assert hash[:key1][:key11].frozen?
|
14
|
+
|
15
|
+
assert hash[:key2].frozen?
|
16
|
+
assert hash[:key2][0].frozen?
|
17
|
+
assert hash[:key2][0][:key22].frozen?
|
18
|
+
end
|
19
|
+
|
20
|
+
it "does not freezes non-enumerable objects" do
|
21
|
+
hash = {key1: Jat, key2: [-> {}]}
|
22
|
+
Jat::EnumDeepFreeze.call(hash)
|
23
|
+
|
24
|
+
refute hash[:key1].frozen?
|
25
|
+
refute hash[:key2][0].frozen?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe Jat do
|
6
|
+
let(:jat_class) { Class.new(Jat) }
|
7
|
+
|
8
|
+
describe ".plugin" do
|
9
|
+
it "loads same plugin only once" do
|
10
|
+
plugin = Module.new
|
11
|
+
jat_class.plugin plugin
|
12
|
+
jat_class.plugin plugin
|
13
|
+
|
14
|
+
assert_equal [plugin], jat_class.config[:plugins]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".plugin_used?" do
|
19
|
+
it "tells if plugin has been already used in current serializer" do
|
20
|
+
assert_equal false, jat_class.plugin_used?(:json_api)
|
21
|
+
jat_class.plugin(:json_api)
|
22
|
+
assert_equal true, jat_class.plugin_used?(:json_api)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "accepts Module" do
|
26
|
+
plugin = Module.new
|
27
|
+
|
28
|
+
assert_equal false, jat_class.plugin_used?(plugin)
|
29
|
+
jat_class.plugin(plugin)
|
30
|
+
assert_equal true, jat_class.plugin_used?(plugin)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".config" do
|
35
|
+
it "returns config object with default values" do
|
36
|
+
config = jat_class.config
|
37
|
+
assert_equal jat_class::Config, config.class
|
38
|
+
assert_equal({plugins: []}, config.opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe ".inherited" do
|
43
|
+
let(:parent) { jat_class }
|
44
|
+
|
45
|
+
it "inherits config class with same options" do
|
46
|
+
parent.config[:foo] = :bar
|
47
|
+
child = Class.new(parent)
|
48
|
+
|
49
|
+
assert_equal parent::Config, child::Config.superclass
|
50
|
+
assert_equal :bar, child.config[:foo]
|
51
|
+
end
|
52
|
+
|
53
|
+
it "inherits attributes" do
|
54
|
+
parent.attribute(:foo) { :bar }
|
55
|
+
child = Class.new(parent)
|
56
|
+
|
57
|
+
assert_equal parent::Attribute, child::Attribute.superclass
|
58
|
+
assert child.attributes[:foo]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe ".call" do
|
63
|
+
it "returns self" do
|
64
|
+
assert_equal jat_class, jat_class.call
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ".to_h" do
|
69
|
+
let(:jat) { jat_class.allocate }
|
70
|
+
|
71
|
+
it "raises error that we should add some response generation plugin" do
|
72
|
+
error = assert_raises(Jat::Error) { jat_class.to_h(nil) }
|
73
|
+
|
74
|
+
error_message = "Method #to_h must be implemented by plugin"
|
75
|
+
assert_equal error_message, error.message
|
76
|
+
end
|
77
|
+
|
78
|
+
it "initializes serializer instance with provided context" do
|
79
|
+
jat.expects(:to_h).with("obj")
|
80
|
+
jat_class.expects(:new).with("context").returns(jat)
|
81
|
+
jat_class.to_h("obj", "context")
|
82
|
+
end
|
83
|
+
|
84
|
+
it "adds empty hash context when context not provided" do
|
85
|
+
jat.expects(:to_h).with("obj")
|
86
|
+
jat_class.expects(:new).with({}).returns(jat)
|
87
|
+
jat_class.to_h("obj")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe ".attributes" do
|
92
|
+
it "initializes new attributes hash" do
|
93
|
+
assert_equal({}, jat_class.attributes)
|
94
|
+
assert_same jat_class.attributes, jat_class.instance_variable_get(:@attributes)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe ".attribute" do
|
99
|
+
it "adds new attribute to attributes hash" do
|
100
|
+
attribute = jat_class.attribute :foo
|
101
|
+
assert_equal jat_class.attributes[:foo], attribute
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe ".relationship" do
|
106
|
+
it "forces using of :serializer option" do
|
107
|
+
error = assert_raises(ArgumentError) { jat_class.relationship(:foo) }
|
108
|
+
assert_match "serializer", error.message
|
109
|
+
end
|
110
|
+
|
111
|
+
it "adds new attribute" do
|
112
|
+
jat_class.relationship(:foo, serializer: jat_class)
|
113
|
+
|
114
|
+
attribute = jat_class.attributes[:foo]
|
115
|
+
assert_equal jat_class, attribute.serializer
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#initialize" do
|
120
|
+
it "initializes context" do
|
121
|
+
jat = jat_class.new("context")
|
122
|
+
assert_equal "context", jat.context
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "#to_h" do
|
127
|
+
it "raises error" do
|
128
|
+
jat = jat_class.new
|
129
|
+
|
130
|
+
error = assert_raises(Jat::Error) { jat.to_h(nil) }
|
131
|
+
error_message = "Method #to_h must be implemented by plugin"
|
132
|
+
assert_equal error_message, error.message
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "#config" do
|
137
|
+
it "returns self class config" do
|
138
|
+
jat = jat_class.new
|
139
|
+
|
140
|
+
assert_same jat.config, jat_class.config
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "test_plugin"
|
5
|
+
|
6
|
+
describe "plugin system" do
|
7
|
+
describe "Jat.plugin" do
|
8
|
+
let(:jat_class) { Class.new(Jat) }
|
9
|
+
let(:components) { [jat_class, jat_class::Attribute, jat_class::Config] }
|
10
|
+
|
11
|
+
before do
|
12
|
+
components.each do |component|
|
13
|
+
component::InstanceMethods.send(:define_method, :foo) { :foo }
|
14
|
+
component::ClassMethods.send(:define_method, :foo) { :foo }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
components.each do |component|
|
20
|
+
component::InstanceMethods.send(:undef_method, :foo)
|
21
|
+
component::ClassMethods.send(:undef_method, :foo)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "allows the plugin to override base methods of core classes" do
|
26
|
+
components.each do |component|
|
27
|
+
assert_equal :foo, component.foo
|
28
|
+
assert_equal :foo, component.allocate.foo
|
29
|
+
end
|
30
|
+
|
31
|
+
jat_class.plugin(TestPlugin)
|
32
|
+
|
33
|
+
components.each do |component|
|
34
|
+
assert_equal :plugin_foo, component.foo
|
35
|
+
assert_equal :plugin_foo, component.allocate.foo
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "calls before_load before loading plugin" do
|
40
|
+
jat_class.plugin(TestPlugin)
|
41
|
+
assert_equal :foo, jat_class.config[:before_load]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "calls after_load after loading plugin" do
|
45
|
+
jat_class.plugin(TestPlugin)
|
46
|
+
assert_equal :plugin_foo, jat_class.config[:after_load]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
conn = ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:").connection
|
6
|
+
|
7
|
+
conn.create_table(:users) do |t|
|
8
|
+
t.string :name
|
9
|
+
end
|
10
|
+
|
11
|
+
conn.create_table(:comments) do |t|
|
12
|
+
t.belongs_to :user, index: false
|
13
|
+
t.text :text
|
14
|
+
end
|
15
|
+
|
16
|
+
module AR
|
17
|
+
class User < ActiveRecord::Base
|
18
|
+
has_many :comments
|
19
|
+
end
|
20
|
+
|
21
|
+
class Comment < ActiveRecord::Base
|
22
|
+
belongs_to :user
|
23
|
+
end
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
if RUBY_VERSION >= "2.7"
|
4
|
+
Warning[:deprecated] = true
|
5
|
+
Warning[:experimental] = true
|
6
|
+
end
|
7
|
+
|
8
|
+
require "bundler/setup"
|
9
|
+
require "minitest/autorun"
|
10
|
+
require "mocha/minitest"
|
11
|
+
require "pry-byebug"
|
12
|
+
|
13
|
+
require_relative "../lib/jat"
|