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,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::BaseLowerCamelCase" do
|
6
|
+
let(:jat_class) do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
new_class.plugin(:base_lower_camel_case)
|
9
|
+
new_class
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Attribute" do
|
13
|
+
describe "#name" do
|
14
|
+
it "returns name in lower_camel_case case" do
|
15
|
+
attribute = jat_class.attribute(:foo)
|
16
|
+
assert :foo, attribute.name
|
17
|
+
|
18
|
+
attribute = jat_class.attribute(:foo_bar)
|
19
|
+
assert :fooBar, attribute.name
|
20
|
+
|
21
|
+
attribute = jat_class.attribute(:foo_bar_bazz)
|
22
|
+
assert :fooBarBazz, attribute.name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
|
-
describe "Jat::Plugins::
|
6
|
-
before { Jat::Plugins.
|
5
|
+
describe "Jat::Plugins::BasePreloads::FormatUserPreloads" do
|
6
|
+
before { Jat::Plugins.find_plugin(:base_preloads) }
|
7
7
|
|
8
|
-
let(:format) { Jat::Plugins::
|
8
|
+
let(:format) { Jat::Plugins::BasePreloads::FormatUserPreloads }
|
9
9
|
|
10
10
|
it "transforms nil to empty hash" do
|
11
11
|
preloads = nil
|
@@ -3,9 +3,9 @@
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
5
|
describe "Jat::Plugins::Preloads::PreloadsWithPath" do
|
6
|
-
before { Jat::Plugins.
|
6
|
+
before { Jat::Plugins.find_plugin(:base_preloads) }
|
7
7
|
|
8
|
-
let(:described_class) { Jat::Plugins::
|
8
|
+
let(:described_class) { Jat::Plugins::BasePreloads::PreloadsWithPath }
|
9
9
|
|
10
10
|
it "returns provided preloads and path to last value" do
|
11
11
|
preloads = {a: {b: {c: {}}, d: {}}, e: {}}
|
@@ -6,7 +6,7 @@ describe "Jat::Plugins::Cache" do
|
|
6
6
|
let(:jat_class) do
|
7
7
|
new_class = Class.new(Jat)
|
8
8
|
to_h_mod = Module.new do
|
9
|
-
def to_h
|
9
|
+
def to_h(object)
|
10
10
|
"RES_#{object}"
|
11
11
|
end
|
12
12
|
end
|
@@ -28,8 +28,8 @@ describe "Jat::Plugins::Cache" do
|
|
28
28
|
describe "InstanceMethods" do
|
29
29
|
describe "#to_h" do
|
30
30
|
it "takes result from cache" do
|
31
|
-
result1 = jat_class.new("OBJECT"
|
32
|
-
result2 = jat_class.new("OBJECT"
|
31
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
32
|
+
result2 = jat_class.new(context).to_h("OBJECT")
|
33
33
|
|
34
34
|
assert_equal "RES_OBJECT", result1
|
35
35
|
assert_equal "RES_OBJECT", result2
|
@@ -37,8 +37,8 @@ describe "Jat::Plugins::Cache" do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "does not take cached result when cache keys are different" do
|
40
|
-
result1 = jat_class.new("OBJECT"
|
41
|
-
result2 = jat_class.new(
|
40
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
41
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_h("OBJECT")
|
42
42
|
|
43
43
|
assert_equal "RES_OBJECT", result1
|
44
44
|
assert_equal "RES_OBJECT", result2
|
@@ -46,7 +46,7 @@ describe "Jat::Plugins::Cache" do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it "does not saves cache when no context[:cache] provided" do
|
49
|
-
jat_class.new(
|
49
|
+
jat_class.new({}).to_str("OBJECT")
|
50
50
|
|
51
51
|
assert_equal [], hash_storage.keys
|
52
52
|
end
|
@@ -54,8 +54,8 @@ describe "Jat::Plugins::Cache" do
|
|
54
54
|
|
55
55
|
describe "#to_str" do
|
56
56
|
it "takes result from cache" do
|
57
|
-
result1 = jat_class.new("OBJECT"
|
58
|
-
result2 = jat_class.new("OBJECT"
|
57
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
58
|
+
result2 = jat_class.new(context).to_str("OBJECT")
|
59
59
|
|
60
60
|
assert_equal '"RES_OBJECT"', result1
|
61
61
|
assert_equal '"RES_OBJECT"', result2
|
@@ -63,8 +63,8 @@ describe "Jat::Plugins::Cache" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "does not take cached result when cache keys are different" do
|
66
|
-
result1 = jat_class.new("OBJECT"
|
67
|
-
result2 = jat_class.new(
|
66
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
67
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_str("OBJECT")
|
68
68
|
|
69
69
|
assert_equal '"RES_OBJECT"', result1
|
70
70
|
assert_equal '"RES_OBJECT"', result2
|
@@ -73,7 +73,7 @@ describe "Jat::Plugins::Cache" do
|
|
73
73
|
|
74
74
|
it "does not saves cache for #to_h" do
|
75
75
|
context[:foo] = :bar
|
76
|
-
jat_class.new("OBJECT"
|
76
|
+
jat_class.new(context).to_str("OBJECT")
|
77
77
|
|
78
78
|
assert_equal ["OBJECT.bar.to_str"], hash_storage.keys
|
79
79
|
end
|
@@ -7,77 +7,85 @@ describe "Jat::Plugins::JsonApi" do
|
|
7
7
|
new_class = Class.new(Jat)
|
8
8
|
new_class.plugin(plugin)
|
9
9
|
new_class.type :jat
|
10
|
-
new_class.
|
10
|
+
new_class.id key: :itself
|
11
11
|
new_class
|
12
12
|
end
|
13
13
|
|
14
|
-
let(:plugin) { Jat::Plugins.
|
14
|
+
let(:plugin) { Jat::Plugins.find_plugin(:json_api) }
|
15
15
|
|
16
|
-
describe ".
|
17
|
-
it "
|
16
|
+
describe ".before_load" do
|
17
|
+
it "checks some response plugin was already loaded" do
|
18
18
|
jat_class = Class.new(Jat)
|
19
|
-
jat_class.
|
20
|
-
|
21
|
-
|
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
22
|
end
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
+
describe ".after_load" do
|
26
|
+
it "adds config variable with name of response plugin that was loaded" do
|
25
27
|
jat_class = Class.new(Jat)
|
26
|
-
|
27
|
-
|
28
|
-
assert_equal jat_class
|
29
|
-
assert_equal jat_class::LinksPresenter.jat_class, jat_class
|
30
|
-
assert_equal jat_class::DocumentLinksPresenter.jat_class, jat_class
|
31
|
-
assert_equal jat_class::RelationshipLinksPresenter.jat_class, jat_class
|
32
|
-
assert_equal jat_class::MetaPresenter.jat_class, jat_class
|
33
|
-
assert_equal jat_class::DocumentMetaPresenter.jat_class, jat_class
|
34
|
-
assert_equal jat_class::RelationshipMetaPresenter.jat_class, jat_class
|
28
|
+
jat_class.plugin(:json_api)
|
29
|
+
|
30
|
+
assert_equal(:json_api, jat_class.config[:response_plugin_loaded])
|
35
31
|
end
|
36
32
|
end
|
37
33
|
|
38
34
|
describe "InstanceMethods" do
|
39
35
|
let(:jat) do
|
40
|
-
jat_class.new
|
36
|
+
jat_class.new
|
41
37
|
end
|
42
38
|
|
43
39
|
describe "#to_h" do
|
44
40
|
it "returns response in json-api format" do
|
45
41
|
expected_result = {data: {type: :jat, id: "JAT"}}
|
46
|
-
assert_equal expected_result, jat.to_h
|
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.new("CONTEXT").map
|
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
|
47
58
|
end
|
48
59
|
end
|
49
60
|
|
50
|
-
describe "#
|
51
|
-
it "returns memorized
|
52
|
-
|
53
|
-
|
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
|
54
66
|
end
|
55
67
|
end
|
56
68
|
end
|
57
69
|
|
58
70
|
describe "ClassMethods" do
|
59
|
-
describe ".
|
60
|
-
it "adds new
|
61
|
-
jat_class.
|
62
|
-
|
63
|
-
atribute = jat_class.attributes[:foo]
|
64
|
-
assert_equal jat_class, atribute.serializer
|
65
|
-
assert_equal true, atribute.exposed?
|
66
|
-
assert_equal "block", jat_class::Presenter.new(nil, nil).foo
|
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}
|
67
75
|
end
|
68
76
|
end
|
69
77
|
|
70
78
|
describe ".inherited" do
|
71
79
|
it "inherits type" do
|
72
80
|
child = Class.new(jat_class)
|
73
|
-
assert_equal :jat, child.
|
81
|
+
assert_equal :jat, child.get_type
|
74
82
|
end
|
75
83
|
|
76
84
|
it "inherits `jsonapi` data" do
|
77
85
|
jat_class.jsonapi(:version) { "1.0" }
|
78
86
|
|
79
87
|
child = Class.new(jat_class)
|
80
|
-
assert_equal("1.0", child.jsonapi_data[:version].
|
88
|
+
assert_equal("1.0", child.jsonapi_data[:version].value(nil, nil))
|
81
89
|
end
|
82
90
|
|
83
91
|
it "inherits `object_links`, `relationship_links`, `document_links`" do
|
@@ -86,9 +94,9 @@ describe "Jat::Plugins::JsonApi" do
|
|
86
94
|
jat_class.document_link(:last) { "/articles/3" }
|
87
95
|
child = Class.new(jat_class)
|
88
96
|
|
89
|
-
assert_equal("/articles/1", child.object_links[:self].
|
90
|
-
assert_equal("/articles/2", child.relationship_links[:related].
|
91
|
-
assert_equal("/articles/3", child.document_links[:last].
|
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))
|
92
100
|
end
|
93
101
|
|
94
102
|
it "does not change parents links when children links are changed" do
|
@@ -101,9 +109,9 @@ describe "Jat::Plugins::JsonApi" do
|
|
101
109
|
child.relationship_links.delete(:related)
|
102
110
|
child.document_links.delete(:last)
|
103
111
|
|
104
|
-
assert_equal("/articles/1", jat_class.object_links[:self].
|
105
|
-
assert_equal("/articles/2", jat_class.relationship_links[:related].
|
106
|
-
assert_equal("/articles/3", jat_class.document_links[:last].
|
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))
|
107
115
|
end
|
108
116
|
|
109
117
|
it "inherits `object_meta`, `relationship_meta`, `document_meta`" do
|
@@ -112,9 +120,9 @@ describe "Jat::Plugins::JsonApi" do
|
|
112
120
|
jat_class.document_meta(:last) { "foo/3" }
|
113
121
|
child = Class.new(jat_class)
|
114
122
|
|
115
|
-
assert_equal("foo/1", child.added_object_meta[:self].
|
116
|
-
assert_equal("foo/2", child.added_relationship_meta[:related].
|
117
|
-
assert_equal("foo/3", child.added_document_meta[:last].
|
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))
|
118
126
|
end
|
119
127
|
|
120
128
|
it "does not change parents meta when children meta changed" do
|
@@ -127,27 +135,27 @@ describe "Jat::Plugins::JsonApi" do
|
|
127
135
|
child.added_relationship_meta.delete(:related)
|
128
136
|
child.added_document_meta.delete(:last)
|
129
137
|
|
130
|
-
assert_equal("foo/1", jat_class.added_object_meta[:self].
|
131
|
-
assert_equal("foo/2", jat_class.added_relationship_meta[:related].
|
132
|
-
assert_equal("foo/3", jat_class.added_document_meta[:last].
|
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))
|
133
141
|
end
|
134
142
|
end
|
135
143
|
|
136
|
-
describe ".type" do
|
144
|
+
describe ".type, .get_type" do
|
137
145
|
it "does not allows to ask for type before type is defined" do
|
138
146
|
new_class = Class.new(Jat) { plugin(:json_api) }
|
139
147
|
|
140
|
-
error = assert_raises(Jat::Error) { new_class.
|
148
|
+
error = assert_raises(Jat::Error) { new_class.get_type }
|
141
149
|
assert_equal "#{new_class} has no defined type", error.message
|
142
150
|
end
|
143
151
|
|
144
152
|
it "saves and returns current type" do
|
145
|
-
assert_equal :jat, jat_class.
|
153
|
+
assert_equal :jat, jat_class.get_type
|
146
154
|
end
|
147
155
|
|
148
156
|
it "symbolizes type" do
|
149
157
|
jat_class.type "users"
|
150
|
-
assert_equal :users, jat_class.
|
158
|
+
assert_equal :users, jat_class.get_type
|
151
159
|
end
|
152
160
|
end
|
153
161
|
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
describe ".inspect" do
|
22
|
+
it "returns self name" do
|
23
|
+
assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".parse" do
|
28
|
+
it "returns empty hash when parameters not provided" do
|
29
|
+
result = described_class.parse(nil)
|
30
|
+
|
31
|
+
assert_equal({}, result)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns parsed attributes" do
|
35
|
+
result = described_class.parse(a: "a1,a2")
|
36
|
+
|
37
|
+
assert_equal({a: %i[a1 a2]}, result)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "validates provided attributes" do
|
41
|
+
jat_class.plugin :json_api_validate_params
|
42
|
+
error = assert_raises(Jat::Error) { described_class.parse(a: "a1,a2,a3,a4") }
|
43
|
+
assert_match(/a4/, error.message)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,57 @@
|
|
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
|
+
let(:c_serializer) { Class.new(base_class) }
|
12
|
+
|
13
|
+
before do
|
14
|
+
ser = a_serializer
|
15
|
+
ser.type :a
|
16
|
+
ser.attribute :a1
|
17
|
+
ser.relationship :a2, serializer: b_serializer
|
18
|
+
ser.relationship :a3, serializer: b_serializer
|
19
|
+
ser.relationship :a4, serializer: b_serializer
|
20
|
+
ser.relationship :a5, serializer: b_serializer
|
21
|
+
ser.relationship :a6, serializer: b_serializer
|
22
|
+
|
23
|
+
ser = b_serializer
|
24
|
+
ser.type :b
|
25
|
+
ser.attribute :b1
|
26
|
+
ser.relationship :b2, serializer: c_serializer
|
27
|
+
ser.relationship :b3, serializer: c_serializer
|
28
|
+
ser.relationship :b4, serializer: c_serializer
|
29
|
+
|
30
|
+
ser = c_serializer
|
31
|
+
ser.type :c
|
32
|
+
ser.attribute :c1
|
33
|
+
ser.relationship :c2, serializer: a_serializer
|
34
|
+
ser.relationship :c3, serializer: a_serializer
|
35
|
+
ser.relationship :c4, serializer: a_serializer
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:described_class) { a_serializer::IncludeParamParser }
|
39
|
+
|
40
|
+
describe ".inspect" do
|
41
|
+
it "returns self name" do
|
42
|
+
assert_equal "#{a_serializer}::IncludeParamParser", described_class.inspect
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ".parse" do
|
47
|
+
it "returns empty hash when param not provided" do
|
48
|
+
result = described_class.parse(nil)
|
49
|
+
assert_equal({}, result)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns typed keys" do
|
53
|
+
result = described_class.parse("a2.b2.c2,a2.b2.c3,a3.b2.c4.a5,a4")
|
54
|
+
assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2], c: %i[c2 c3 c4]}, result)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|