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,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Preloads::FormatUserPreloads" do
|
6
|
+
before { Jat::Plugins.find_plugin(:_preloads) }
|
7
|
+
|
8
|
+
let(:format) { Jat::Plugins::Preloads::FormatUserPreloads }
|
9
|
+
|
10
|
+
it "transforms nil to empty hash" do
|
11
|
+
preloads = nil
|
12
|
+
assert_equal ({}), format.to_hash(preloads)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "transforms false to empty hash" do
|
16
|
+
preloads = false
|
17
|
+
assert_equal({}, format.to_hash(preloads))
|
18
|
+
end
|
19
|
+
|
20
|
+
it "transforms Symbol" do
|
21
|
+
preloads = :foo
|
22
|
+
assert_equal({foo: {}}, format.to_hash(preloads))
|
23
|
+
end
|
24
|
+
|
25
|
+
it "transforms String" do
|
26
|
+
preloads = "foo"
|
27
|
+
assert_equal({foo: {}}, format.to_hash(preloads))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "transforms Hash" do
|
31
|
+
preloads = {foo: :bar}
|
32
|
+
assert_equal({foo: {bar: {}}}, format.to_hash(preloads))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "transforms Array" do
|
36
|
+
preloads = %i[foo bar]
|
37
|
+
assert_equal({foo: {}, bar: {}}, format.to_hash(preloads))
|
38
|
+
end
|
39
|
+
|
40
|
+
it "transforms nested hashes and arrays" do
|
41
|
+
preloads = [:foo, {"bar" => "bazz"}, ["bazz"]]
|
42
|
+
assert_equal({foo: {}, bar: {bazz: {}}, bazz: {}}, format.to_hash(preloads))
|
43
|
+
|
44
|
+
preloads = {"bar" => "bazz", :foo => [:bar, "bazz"]}
|
45
|
+
assert_equal({bar: {bazz: {}}, foo: {bar: {}, bazz: {}}}, format.to_hash(preloads))
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Preloads::PreloadsWithPath" do
|
6
|
+
before { Jat::Plugins.find_plugin(:_preloads) }
|
7
|
+
|
8
|
+
let(:described_class) { Jat::Plugins::Preloads::PreloadsWithPath }
|
9
|
+
|
10
|
+
it "returns provided preloads and path to last value" do
|
11
|
+
preloads = {a: {b: {c: {}}, d: {}}, e: {}}
|
12
|
+
new_preloads, path = described_class.call(preloads)
|
13
|
+
|
14
|
+
assert_equal({a: {b: {c: {}}, d: {}}, e: {}}, new_preloads)
|
15
|
+
assert_equal(%i[e], path)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns provided preloads and path to marked with `!` value" do
|
19
|
+
preloads = {a: {b!: {c: {}}, d: {}}, e: {}}
|
20
|
+
new_preloads, path = described_class.call(preloads)
|
21
|
+
|
22
|
+
assert_equal({a: {b: {c: {}}, d: {}}, e: {}}, new_preloads)
|
23
|
+
assert_equal(%i[a b], path)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns empty preloads and path when empty hash provided" do
|
27
|
+
preloads = {}
|
28
|
+
new_preloads, path = described_class.call(preloads)
|
29
|
+
|
30
|
+
assert_equal({}, new_preloads)
|
31
|
+
assert_equal([], path)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Cache" do
|
6
|
+
let(:jat_class) do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
to_h_mod = Module.new do
|
9
|
+
def to_h(object)
|
10
|
+
"RES_#{object}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
new_class.include(to_h_mod)
|
14
|
+
new_class.plugin(:cache)
|
15
|
+
new_class
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:context) { {cache: hash_cache} }
|
19
|
+
let(:hash_storage) { {} }
|
20
|
+
let(:hash_cache) do
|
21
|
+
hash_storage
|
22
|
+
lambda do |object, context, &block|
|
23
|
+
key = [object, context[:foo], context[:_format]].join(".").freeze
|
24
|
+
hash_storage[key] ||= block.call
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "InstanceMethods" do
|
29
|
+
describe "#to_h" do
|
30
|
+
it "takes result from cache" do
|
31
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
32
|
+
result2 = jat_class.new(context).to_h("OBJECT")
|
33
|
+
|
34
|
+
assert_equal "RES_OBJECT", result1
|
35
|
+
assert_equal "RES_OBJECT", result2
|
36
|
+
assert_same result1, result2
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not take cached result when cache keys are different" do
|
40
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
41
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_h("OBJECT")
|
42
|
+
|
43
|
+
assert_equal "RES_OBJECT", result1
|
44
|
+
assert_equal "RES_OBJECT", result2
|
45
|
+
refute_same result1, result2
|
46
|
+
end
|
47
|
+
|
48
|
+
it "does not saves cache when no context[:cache] provided" do
|
49
|
+
jat_class.new({}).to_str("OBJECT")
|
50
|
+
|
51
|
+
assert_equal [], hash_storage.keys
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#to_str" do
|
56
|
+
it "takes result from cache" do
|
57
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
58
|
+
result2 = jat_class.new(context).to_str("OBJECT")
|
59
|
+
|
60
|
+
assert_equal '"RES_OBJECT"', result1
|
61
|
+
assert_equal '"RES_OBJECT"', result2
|
62
|
+
assert_same result1, result2
|
63
|
+
end
|
64
|
+
|
65
|
+
it "does not take cached result when cache keys are different" do
|
66
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
67
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_str("OBJECT")
|
68
|
+
|
69
|
+
assert_equal '"RES_OBJECT"', result1
|
70
|
+
assert_equal '"RES_OBJECT"', result2
|
71
|
+
refute_same result1, result2
|
72
|
+
end
|
73
|
+
|
74
|
+
it "does not saves cache for #to_h" do
|
75
|
+
context[:foo] = :bar
|
76
|
+
jat_class.new(context).to_str("OBJECT")
|
77
|
+
|
78
|
+
assert_equal ["OBJECT.bar.to_str"], hash_storage.keys
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi" do
|
6
|
+
let(:jat_class) do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
new_class.plugin(plugin)
|
9
|
+
new_class.type :jat
|
10
|
+
new_class.id key: :itself
|
11
|
+
new_class
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:plugin) { Jat::Plugins.find_plugin(:json_api) }
|
15
|
+
|
16
|
+
describe ".before_load" do
|
17
|
+
it "checks some response plugin was already loaded" do
|
18
|
+
jat_class = Class.new(Jat)
|
19
|
+
jat_class.config[:response_plugin_loaded] = :simple_api
|
20
|
+
error = assert_raises(Jat::Error) { jat_class.plugin(:json_api) }
|
21
|
+
assert_equal "Response plugin `simple_api` was already loaded before", error.message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".after_load" do
|
26
|
+
it "adds config variable with name of response plugin that was loaded" do
|
27
|
+
jat_class = Class.new(Jat)
|
28
|
+
jat_class.plugin(:json_api)
|
29
|
+
|
30
|
+
assert_equal(:json_api, jat_class.config[:response_plugin_loaded])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "InstanceMethods" do
|
35
|
+
let(:jat) do
|
36
|
+
jat_class.new
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#to_h" do
|
40
|
+
it "returns response in json-api format" do
|
41
|
+
expected_result = {data: {type: :jat, id: "JAT"}}
|
42
|
+
assert_equal expected_result, jat.to_h("JAT")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#map" do
|
47
|
+
it "returns map for provided context" do
|
48
|
+
jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
|
49
|
+
assert_equal "MAP", jat_class.map("CONTEXT")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#map_full" do
|
54
|
+
it "returns memorized map with all fields exposed" do
|
55
|
+
jat_class::Map.expects(:call).with(exposed: :all).returns "MAP"
|
56
|
+
assert_equal "MAP", jat_class.map_full
|
57
|
+
assert_same jat_class.map_full, jat_class.map_full
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#map_exposed" do
|
62
|
+
it "returns memorized map with exposed by default fields" do
|
63
|
+
jat_class::Map.expects(:call).with(exposed: :default).returns "MAP"
|
64
|
+
assert_equal "MAP", jat_class.map_exposed
|
65
|
+
assert_same jat_class.map_exposed, jat_class.map_exposed
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "ClassMethods" do
|
71
|
+
describe ".id" do
|
72
|
+
it "adds new `:id`" do
|
73
|
+
jat_class.id(key: :uuid)
|
74
|
+
assert_equal jat_class.get_id.params, {name: :id, opts: {key: :uuid}, block: nil}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe ".inherited" do
|
79
|
+
it "inherits type" do
|
80
|
+
child = Class.new(jat_class)
|
81
|
+
assert_equal :jat, child.get_type
|
82
|
+
end
|
83
|
+
|
84
|
+
it "inherits `jsonapi` data" do
|
85
|
+
jat_class.jsonapi(:version) { "1.0" }
|
86
|
+
|
87
|
+
child = Class.new(jat_class)
|
88
|
+
assert_equal("1.0", child.jsonapi_data[:version].value(nil, nil))
|
89
|
+
end
|
90
|
+
|
91
|
+
it "inherits `object_links`, `relationship_links`, `document_links`" do
|
92
|
+
jat_class.object_link(:self) { "/articles/1" }
|
93
|
+
jat_class.relationship_link(:related) { "/articles/2" }
|
94
|
+
jat_class.document_link(:last) { "/articles/3" }
|
95
|
+
child = Class.new(jat_class)
|
96
|
+
|
97
|
+
assert_equal("/articles/1", child.object_links[:self].value(nil, nil))
|
98
|
+
assert_equal("/articles/2", child.relationship_links[:related].value(nil, nil))
|
99
|
+
assert_equal("/articles/3", child.document_links[:last].value(nil, nil))
|
100
|
+
end
|
101
|
+
|
102
|
+
it "does not change parents links when children links are changed" do
|
103
|
+
jat_class.object_link(:self) { "/articles/1" }
|
104
|
+
jat_class.relationship_link(:related) { "/articles/2" }
|
105
|
+
jat_class.document_link(:last) { "/articles/3" }
|
106
|
+
|
107
|
+
child = Class.new(jat_class)
|
108
|
+
child.object_links.delete(:self)
|
109
|
+
child.relationship_links.delete(:related)
|
110
|
+
child.document_links.delete(:last)
|
111
|
+
|
112
|
+
assert_equal("/articles/1", jat_class.object_links[:self].value(nil, nil))
|
113
|
+
assert_equal("/articles/2", jat_class.relationship_links[:related].value(nil, nil))
|
114
|
+
assert_equal("/articles/3", jat_class.document_links[:last].value(nil, nil))
|
115
|
+
end
|
116
|
+
|
117
|
+
it "inherits `object_meta`, `relationship_meta`, `document_meta`" do
|
118
|
+
jat_class.object_meta(:self) { "foo/1" }
|
119
|
+
jat_class.relationship_meta(:related) { "foo/2" }
|
120
|
+
jat_class.document_meta(:last) { "foo/3" }
|
121
|
+
child = Class.new(jat_class)
|
122
|
+
|
123
|
+
assert_equal("foo/1", child.added_object_meta[:self].value(nil, nil))
|
124
|
+
assert_equal("foo/2", child.added_relationship_meta[:related].value(nil, nil))
|
125
|
+
assert_equal("foo/3", child.added_document_meta[:last].value(nil, nil))
|
126
|
+
end
|
127
|
+
|
128
|
+
it "does not change parents meta when children meta changed" do
|
129
|
+
jat_class.object_meta(:self) { "foo/1" }
|
130
|
+
jat_class.relationship_meta(:related) { "foo/2" }
|
131
|
+
jat_class.document_meta(:last) { "foo/3" }
|
132
|
+
|
133
|
+
child = Class.new(jat_class)
|
134
|
+
child.added_object_meta.delete(:self)
|
135
|
+
child.added_relationship_meta.delete(:related)
|
136
|
+
child.added_document_meta.delete(:last)
|
137
|
+
|
138
|
+
assert_equal("foo/1", jat_class.added_object_meta[:self].value(nil, nil))
|
139
|
+
assert_equal("foo/2", jat_class.added_relationship_meta[:related].value(nil, nil))
|
140
|
+
assert_equal("foo/3", jat_class.added_document_meta[:last].value(nil, nil))
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe ".type, .get_type" do
|
145
|
+
it "does not allows to ask for type before type is defined" do
|
146
|
+
new_class = Class.new(Jat) { plugin(:json_api) }
|
147
|
+
|
148
|
+
error = assert_raises(Jat::Error) { new_class.get_type }
|
149
|
+
assert_equal "#{new_class} has no defined type", error.message
|
150
|
+
end
|
151
|
+
|
152
|
+
it "saves and returns current type" do
|
153
|
+
assert_equal :jat, jat_class.get_type
|
154
|
+
end
|
155
|
+
|
156
|
+
it "symbolizes type" do
|
157
|
+
jat_class.type "users"
|
158
|
+
assert_equal :users, jat_class.get_type
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi::FieldsParamParser" do
|
6
|
+
before { Jat::Plugins.find_plugin(:json_api) }
|
7
|
+
|
8
|
+
let(:jat_class) do
|
9
|
+
Class.new(Jat) do
|
10
|
+
plugin :json_api
|
11
|
+
type :a
|
12
|
+
|
13
|
+
attribute :a1
|
14
|
+
attribute :a2
|
15
|
+
attribute :a3
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:described_class) { jat_class::FieldsParamParser }
|
20
|
+
|
21
|
+
it "returns empty hash when parameters not provided" do
|
22
|
+
result = described_class.parse(nil)
|
23
|
+
|
24
|
+
assert_equal({}, result)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns parsed attributes" do
|
28
|
+
result = described_class.parse(a: "a1,a2")
|
29
|
+
|
30
|
+
assert_equal({a: %i[a1 a2]}, result)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "validates provided attributes" do
|
34
|
+
jat_class.plugin :json_api_validate_params
|
35
|
+
error = assert_raises(Jat::Error) { described_class.parse(a: "a1,a2,a3,a4") }
|
36
|
+
assert_match(/a4/, error.message)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi::Params::Include" do
|
6
|
+
before { Jat::Plugins.find_plugin(:json_api) }
|
7
|
+
|
8
|
+
let(:base_class) { Class.new(Jat) { plugin :json_api } }
|
9
|
+
let(:a_serializer) { Class.new(base_class) }
|
10
|
+
let(:b_serializer) { Class.new(base_class) }
|
11
|
+
|
12
|
+
before do
|
13
|
+
ser = a_serializer
|
14
|
+
ser.type :a
|
15
|
+
ser.attribute :a1
|
16
|
+
ser.relationship :a2, serializer: b_serializer
|
17
|
+
ser.relationship :a3, serializer: b_serializer
|
18
|
+
ser.relationship :a4, serializer: b_serializer
|
19
|
+
ser.relationship :a5, serializer: b_serializer
|
20
|
+
ser.relationship :a6, serializer: b_serializer
|
21
|
+
|
22
|
+
ser = b_serializer
|
23
|
+
ser.type :b
|
24
|
+
ser.attribute :b1
|
25
|
+
ser.relationship :b2, serializer: a_serializer
|
26
|
+
ser.relationship :b3, serializer: a_serializer
|
27
|
+
ser.relationship :b4, serializer: a_serializer
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:described_class) { a_serializer::IncludeParamParser }
|
31
|
+
|
32
|
+
it "returns empty hash when param not provided" do
|
33
|
+
result = described_class.parse(nil)
|
34
|
+
assert_equal({}, result)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns typed keys" do
|
38
|
+
result = described_class.parse("a2.b2,a2.b3,a3.b2.a5,a4")
|
39
|
+
assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2 b3]}, result)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::JsonApi::Map" do
|
6
|
+
before { Jat::Plugins.find_plugin(:json_api) }
|
7
|
+
|
8
|
+
let(:described_class) { a::Map }
|
9
|
+
let(:base_class) { Class.new(Jat) { plugin :json_api } }
|
10
|
+
|
11
|
+
let(:a) do
|
12
|
+
ser = Class.new(base_class)
|
13
|
+
ser.type :a
|
14
|
+
|
15
|
+
ser.attribute :a1
|
16
|
+
ser.attribute :a2
|
17
|
+
ser.attribute :a3, exposed: false
|
18
|
+
|
19
|
+
ser.relationship :b, serializer: b, exposed: true
|
20
|
+
ser.relationship :c, serializer: c
|
21
|
+
ser.relationship :d, serializer: d
|
22
|
+
ser
|
23
|
+
end
|
24
|
+
|
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
|
31
|
+
|
32
|
+
ser.relationship :c, serializer: c
|
33
|
+
ser.relationship :d, serializer: d
|
34
|
+
ser
|
35
|
+
end
|
36
|
+
|
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
|
45
|
+
|
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
|
54
|
+
|
55
|
+
describe "with default :exposed option" do
|
56
|
+
it "returns exposed by default attributes" do
|
57
|
+
result = described_class.call(exposed: :default)
|
58
|
+
expected_result = {
|
59
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
60
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
|
61
|
+
}
|
62
|
+
|
63
|
+
assert_equal expected_result, result
|
64
|
+
end
|
65
|
+
|
66
|
+
it "returns exposed by default attributes when no :exposed param provided" do
|
67
|
+
result = described_class.call({})
|
68
|
+
expected_result = {
|
69
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
70
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
|
71
|
+
}
|
72
|
+
|
73
|
+
assert_equal expected_result, result
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns additionally included fields specified in :includes option" do
|
77
|
+
includes = "c,b.d"
|
78
|
+
result = described_class.call(include: includes)
|
79
|
+
expected_result = {
|
80
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
|
81
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[d]},
|
82
|
+
c: {serializer: c, attributes: %i[c1 c2], relationships: %i[]},
|
83
|
+
d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
|
84
|
+
}
|
85
|
+
|
86
|
+
assert_equal expected_result, result
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns fields specified in :fields option (only specified fields for specified type)" do
|
90
|
+
fields = {b: "b1,d"}
|
91
|
+
result = described_class.call(fields: fields)
|
92
|
+
expected_result = {
|
93
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
|
94
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
|
95
|
+
d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
|
96
|
+
}
|
97
|
+
|
98
|
+
assert_equal expected_result, result
|
99
|
+
end
|
100
|
+
|
101
|
+
it "returns fields specified in :includes and :fields options" do
|
102
|
+
includes = "c"
|
103
|
+
fields = {c: "c1"}
|
104
|
+
result = described_class.call(include: includes, fields: fields)
|
105
|
+
expected_result = {
|
106
|
+
a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
|
107
|
+
b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]},
|
108
|
+
c: {serializer: c, attributes: %i[c1], relationships: %i[]}
|
109
|
+
}
|
110
|
+
|
111
|
+
assert_equal expected_result, result
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "with exposed: :none option" do
|
116
|
+
it "returns no attributes" do
|
117
|
+
result = described_class.call(exposed: :none)
|
118
|
+
expected_result = {
|
119
|
+
a: {serializer: a, attributes: %i[], relationships: %i[]}
|
120
|
+
}
|
121
|
+
|
122
|
+
assert_equal expected_result, result
|
123
|
+
end
|
124
|
+
|
125
|
+
it "returns additionally included fields specified in :includes option" do
|
126
|
+
includes = "b.c"
|
127
|
+
result = described_class.call(exposed: :none, include: includes)
|
128
|
+
expected_result = {
|
129
|
+
a: {serializer: a, attributes: %i[], relationships: %i[b]},
|
130
|
+
b: {serializer: b, attributes: %i[], relationships: %i[c]},
|
131
|
+
c: {serializer: c, attributes: %i[], relationships: %i[]}
|
132
|
+
}
|
133
|
+
|
134
|
+
assert_equal expected_result, result
|
135
|
+
end
|
136
|
+
|
137
|
+
it "returns fields specified in :fields option (only specified fields for specified type)" do
|
138
|
+
fields = {a: "a1,b", b: "b1,d"}
|
139
|
+
result = described_class.call(exposed: :none, fields: fields)
|
140
|
+
expected_result = {
|
141
|
+
a: {serializer: a, attributes: %i[a1], relationships: %i[b]},
|
142
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
|
143
|
+
d: {serializer: d, attributes: %i[], relationships: %i[]}
|
144
|
+
}
|
145
|
+
|
146
|
+
assert_equal expected_result, result
|
147
|
+
end
|
148
|
+
|
149
|
+
it "returns fields specified in :includes and :fields options" do
|
150
|
+
includes = "c"
|
151
|
+
fields = {c: "c1"}
|
152
|
+
result = described_class.call(exposed: :none, include: includes, fields: fields)
|
153
|
+
expected_result = {
|
154
|
+
a: {serializer: a, attributes: %i[], relationships: %i[c]},
|
155
|
+
c: {serializer: c, attributes: %i[c1], relationships: %i[]}
|
156
|
+
}
|
157
|
+
|
158
|
+
assert_equal expected_result, result
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "with exposed: :all option" do
|
163
|
+
it "returns all attributes" do
|
164
|
+
result = described_class.call(exposed: :all)
|
165
|
+
expected_result = {
|
166
|
+
a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
|
167
|
+
b: {serializer: b, attributes: %i[b1 b2 b3], relationships: %i[c d]},
|
168
|
+
c: {serializer: c, attributes: %i[c1 c2 c3], relationships: %i[]},
|
169
|
+
d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
|
170
|
+
}
|
171
|
+
|
172
|
+
assert_equal expected_result, result
|
173
|
+
end
|
174
|
+
|
175
|
+
it "returns only specified fields for specified fields types" do
|
176
|
+
fields = {b: "b1", c: "c2"}
|
177
|
+
result = described_class.call(exposed: :all, fields: fields)
|
178
|
+
expected_result = {
|
179
|
+
a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
|
180
|
+
b: {serializer: b, attributes: %i[b1], relationships: %i[]},
|
181
|
+
c: {serializer: c, attributes: %i[c2], relationships: %i[]},
|
182
|
+
d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
|
183
|
+
}
|
184
|
+
|
185
|
+
assert_equal expected_result, result
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|