jat 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +26 -5
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
  7. data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
  9. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  10. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  11. data/lib/jat/plugins/cache/cache.rb +14 -6
  12. data/lib/jat/plugins/json_api/json_api.rb +150 -106
  13. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  14. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  15. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  16. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  17. data/lib/jat/plugins/json_api/lib/response.rb +81 -197
  18. data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
  19. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  20. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
  21. data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
  22. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  23. data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
  24. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
  25. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  26. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  27. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  28. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  29. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  30. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  31. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  32. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  33. data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
  34. data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
  35. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
  37. data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
  38. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
  39. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  40. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  41. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  42. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
  43. data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
  44. data/lib/jat/plugins/to_str/to_str.rb +15 -5
  45. data/lib/jat/plugins/types/types.rb +54 -0
  46. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  47. data/lib/jat/plugins.rb +42 -30
  48. data/lib/jat.rb +40 -35
  49. data/test/lib/jat/attribute_test.rb +22 -4
  50. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  51. data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
  52. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
  53. data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
  54. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  55. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  56. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  57. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  58. data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
  59. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
  60. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
  61. data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
  62. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  63. data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
  64. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
  65. data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
  66. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  67. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  68. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
  69. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
  70. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  71. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  72. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  73. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
  74. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
  75. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  76. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
  77. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
  78. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
  79. data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
  80. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  81. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
  82. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  83. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
  84. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  85. data/test/lib/jat/plugins/types/types_test.rb +84 -0
  86. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  87. data/test/lib/jat/plugins_test.rb +88 -0
  88. data/test/lib/jat_test.rb +47 -24
  89. data/test/lib/plugin_test.rb +3 -3
  90. data/test/test_helper.rb +7 -6
  91. data/test/test_plugin.rb +9 -12
  92. metadata +107 -88
  93. data/CHANGELOG.md +0 -7
  94. data/README.md +0 -21
  95. data/jat.gemspec +0 -37
  96. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
  97. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  98. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  99. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  100. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  101. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  102. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  103. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  104. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  105. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  106. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  107. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  108. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  109. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  110. data/lib/jat/presenter.rb +0 -51
  111. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
  112. data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
  113. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  114. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  115. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  116. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  117. data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
  118. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  119. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  120. data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
  121. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  122. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  123. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  124. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  125. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  126. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  127. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  128. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  129. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  130. data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
  131. data/test/lib/jat/presenter_test.rb +0 -61
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApiActiverecord" do
6
- before do
7
- @plugin = Jat::Plugins.load_plugin(:_json_api_activerecord)
8
- end
9
-
10
- it "loads other plugins in after_load" do
11
- jat_class = Class.new(Jat)
12
- jat_class.expects(:plugin).with(:_preloads, foo: :bar)
13
- jat_class.expects(:plugin).with(:_activerecord_preloads, foo: :bar)
14
-
15
- Jat::Plugins.after_load(@plugin, jat_class, foo: :bar)
16
- end
17
-
18
- describe "ClassMethods" do
19
- it "add .jat_preloads method as a delegator to #{@plugin}::Preloads" do
20
- jat_class = Class.new(Jat)
21
- jat_class.plugin @plugin
22
- jat = jat_class.allocate
23
-
24
- @plugin::Preloads.expects(:call).with(jat).returns("RES")
25
-
26
- assert_equal "RES", jat_class.jat_preloads(jat)
27
- end
28
- end
29
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::CamelLower" do
6
- let(:jat_class) do
7
- new_class = Class.new(Jat)
8
- new_class.plugin(:camel_lower)
9
- new_class
10
- end
11
-
12
- describe "Attribute" do
13
- describe "#name" do
14
- it "returns name in camel_lower 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
-
27
- describe "Test responses with simple_api plugin" do
28
- before { jat_class.plugin(:simple_api) }
29
-
30
- it "returns attributes in camelLower case" do
31
- jat_class.attribute(:foo_bar) { 1 }
32
-
33
- response = jat_class.to_h(true)
34
- assert_equal({fooBar: 1}, response)
35
- end
36
-
37
- it "accepts fields in camelLower format" do
38
- jat_class.attribute(:foo_bar, exposed: false) { 1 }
39
-
40
- response = jat_class.to_h(true, params: {fields: "fooBar"})
41
- assert_equal({fooBar: 1}, response)
42
- end
43
- end
44
-
45
- describe "Test responses with json_api plugin" do
46
- before do
47
- jat_class.plugin(:json_api)
48
- jat_class.type :foo
49
- jat_class.attribute(:id) { object }
50
- end
51
-
52
- it "returns attributes in camelLower case" do
53
- jat_class.attribute(:foo_bar) { 1 }
54
-
55
- assert_equal({fooBar: 1}, jat_class.to_h(true).dig(:data, :attributes))
56
- end
57
-
58
- it "accepts `fields` in camelLower format" do
59
- jat_class.attribute(:foo_bar, exposed: false) { 1 }
60
-
61
- response = jat_class.to_h(true, params: {fields: {foo: "fooBar"}})
62
- assert_equal({fooBar: 1}, response.dig(:data, :attributes))
63
- end
64
-
65
- it "accepts `include` in camelLower format" do
66
- new_serializer = Class.new(Jat)
67
- new_serializer.plugin(:json_api)
68
- new_serializer.type :new
69
- new_serializer.attribute(:id) { object }
70
-
71
- jat_class.relationship(:foo_bar, serializer: new_serializer) { 1 }
72
-
73
- response = jat_class.to_h(true, params: {include: "fooBar"})
74
- response_relationships = response.dig(:data, :relationships).keys
75
- assert_includes(response_relationships, :fooBar)
76
- end
77
- end
78
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::ConstructTraversalMap" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::ConstructTraversalMap }
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
20
- ser.relationship :c, serializer: c
21
- ser.relationship :d, serializer: d, exposed: true
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
- ser
32
- end
33
-
34
- let(:c) do
35
- ser = Class.new(base_class)
36
- ser.type :c
37
- ser.attribute :c1
38
- ser.attribute :c2
39
- ser.attribute :c3, exposed: false
40
- ser
41
- end
42
-
43
- let(:d) do
44
- ser = Class.new(base_class)
45
- ser.type :d
46
- ser.attribute :d1
47
- ser.attribute :d2
48
- ser.attribute :d3, exposed: false
49
- ser
50
- end
51
-
52
- it "returns all attributes" do
53
- result = described_class.new(a, :all).to_h
54
- expected_result = {
55
- a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
56
- b: {serializer: b, attributes: %i[b1 b2 b3], relationships: []},
57
- c: {serializer: c, attributes: %i[c1 c2 c3], relationships: []},
58
- d: {serializer: d, attributes: %i[d1 d2 d3], relationships: []}
59
- }
60
-
61
- assert_equal expected_result, result
62
- end
63
-
64
- it "returns exposed attributes" do
65
- result = described_class.new(a, :exposed).to_h
66
- expected_result = {
67
- a: {serializer: a, attributes: %i[a1 a2], relationships: %i[d]},
68
- d: {serializer: d, attributes: %i[d1 d2], relationships: []}
69
- }
70
-
71
- assert_equal expected_result, result
72
- end
73
-
74
- it "returns only manually exposed per-type attributes or exposed by default when no manual type provided" do
75
- exposed = {
76
- a: %i[a2 a3 c d],
77
- c: %i[c2 c3],
78
- d: %i[d2 d3]
79
- }
80
- result = described_class.new(a, :manual, manually_exposed: exposed).to_h
81
- expected_result = {
82
- a: {serializer: a, attributes: %i[a2 a3], relationships: %i[c d]},
83
- c: {serializer: c, attributes: %i[c2 c3], relationships: []},
84
- d: {serializer: d, attributes: %i[d2 d3], relationships: []}
85
- }
86
-
87
- assert_equal expected_result, result
88
- end
89
-
90
- it "returns manually exposed per-type attributes or exposed by default when no manual type provided" do
91
- exposed = {
92
- a: %i[a2 a3 b c],
93
- c: %i[c2 c3]
94
- }
95
- result = described_class.new(a, :manual, manually_exposed: exposed).to_h
96
- expected_result = {
97
- a: {serializer: a, attributes: %i[a2 a3], relationships: %i[b c]},
98
- b: {serializer: b, attributes: %i[b1 b2], relationships: []},
99
- c: {serializer: c, attributes: %i[c2 c3], relationships: []}
100
- }
101
-
102
- assert_equal expected_result, result
103
- end
104
-
105
- it "returns combined auto-exposed and manualy exposed attributes" do
106
- exposed = {
107
- a: %i[c],
108
- c: %i[c3]
109
- }
110
- result = described_class.new(a, :exposed, manually_exposed: exposed).to_h
111
- expected_result = {
112
- a: {serializer: a, attributes: %i[a1 a2], relationships: %i[c d]},
113
- c: {serializer: c, attributes: %i[c1 c2 c3], relationships: []},
114
- d: {serializer: d, attributes: %i[d1 d2], relationships: []}
115
- }
116
-
117
- assert_equal expected_result, result
118
- end
119
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Fields::Parse" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Fields::Parse }
9
-
10
- it "returns empty hash when param not provided" do
11
- result = described_class.call(nil)
12
- assert_equal({}, result)
13
- end
14
-
15
- it "returns hash with parsed keys" do
16
- result = described_class.call(a: "a1,a2", b: "b1")
17
- assert_equal({a: %i[a1 a2], b: %i[b1]}, result)
18
- end
19
-
20
- it "symbolizes types" do
21
- result = described_class.call("a" => "a1,a2", "b" => "b1")
22
- assert_equal({a: %i[a1 a2], b: %i[b1]}, result)
23
- end
24
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Fields::Validate" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Fields::Validate }
9
- let(:base_class) { Class.new(Jat) { plugin :json_api } }
10
- let(:a_serializer) { Class.new(base_class) }
11
- let(:b_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
-
19
- ser = b_serializer
20
- ser.type :b
21
- ser.attribute :b1
22
- ser.relationship :b2, serializer: a_serializer
23
- end
24
-
25
- it "does not raises when serializer has all requested keys" do
26
- assert described_class.call(a_serializer.allocate, a: %i[a1 a2], b: %i[b1 b2])
27
- end
28
-
29
- it "does not raises when requested only fields for nested serializer" do
30
- assert described_class.call(a_serializer.allocate, b: %i[b1 b2])
31
- end
32
-
33
- it "raises error when some type can not be in response" do
34
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[a1 a2], foo: %i[b1 b2]) }
35
- assert_equal "#{a_serializer} and its children have no requested type `foo`", error.message
36
- end
37
-
38
- it "raises error when some key is not present in main serializer" do
39
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[b1]) }
40
- assert_equal "#{a_serializer} has no requested attribute or relationship `b1`", error.message
41
- end
42
-
43
- it "raises error when some key is not present in nested serializer" do
44
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[a1], b: %i[a1]) }
45
- assert_equal "#{b_serializer} has no requested attribute or relationship `a1`", error.message
46
- end
47
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Fields" do
6
- before { Jat::Plugins.load_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::Plugins::JsonApi::Params::Fields }
20
-
21
- it "returns empty hash when parameters not provided" do
22
- result = described_class.call(jat_class.allocate, nil)
23
-
24
- assert_equal({}, result)
25
- end
26
-
27
- it "returns parsed attributes" do
28
- result = described_class.call(jat_class.allocate, a: "a1,a2")
29
-
30
- assert_equal({a: %i[a1 a2]}, result)
31
- end
32
-
33
- it "validates provided attributes" do
34
- error = assert_raises(Jat::Error) { described_class.call(jat_class.allocate, a: "a1,a2,a3,a4") }
35
- assert_match(/a4/, error.message)
36
- end
37
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Include::Parse" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Include::Parse }
9
-
10
- it "returns empty hash when param not provided" do
11
- result = described_class.call(nil)
12
-
13
- assert_equal({}, result)
14
- end
15
-
16
- it "returns hash when single element" do
17
- result = described_class.call("foo")
18
-
19
- assert_equal({foo: {}}, result)
20
- end
21
-
22
- it "returns hash when multiple elements" do
23
- result = described_class.call("foo,bar,bazz")
24
-
25
- assert_equal({foo: {}, bar: {}, bazz: {}}, result)
26
- end
27
-
28
- it "returns hash when nested elements" do
29
- result = described_class.call("foo.bar.bazz")
30
-
31
- assert_equal({foo: {bar: {bazz: {}}}}, result)
32
- end
33
-
34
- it "returns hash when multiple nested elements" do
35
- result = described_class.call("foo,bar.bazz,bar.bazzz,test.test1.test2,test.test1.test3")
36
-
37
- assert_equal(
38
- {
39
- foo: {},
40
- bar: {bazz: {}, bazzz: {}},
41
- test: {test1: {test2: {}, test3: {}}}
42
- },
43
- result
44
- )
45
- end
46
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Include::Validate" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Include::Validate }
9
-
10
- let(:base_class) { Class.new(Jat) { plugin :json_api } }
11
- let(:a_serializer) { Class.new(base_class) }
12
- let(:b_serializer) { Class.new(base_class) }
13
-
14
- before do
15
- ser = a_serializer
16
- ser.type :a
17
- ser.attribute :a1
18
- ser.relationship :a2, serializer: b_serializer
19
- ser.relationship :a3, serializer: b_serializer
20
-
21
- ser = b_serializer
22
- ser.type :b
23
- ser.attribute :b1
24
- ser.relationship :b2, serializer: a_serializer
25
- end
26
-
27
- it "does not raises error when all `include` values are valid" do
28
- include_param = {a2: {b2: {a2: {b2: {a2: {}}}}}, a3: {b2: {a2: {b2: {a2: {}}}}}}
29
- assert described_class.call(a_serializer, include_param)
30
- end
31
-
32
- it "raises error when some key is invalid" do
33
- error = assert_raises(Jat::Error) { described_class.call(a_serializer, foo: {}) }
34
- assert_equal "#{a_serializer} has no `foo` relationship", error.message
35
- end
36
-
37
- it "raises error when some children key is invalid" do
38
- error = assert_raises(Jat::Error) { described_class.call(a_serializer, a2: {foo: {}}) }
39
- assert_equal "#{b_serializer} has no `foo` relationship", error.message
40
- end
41
-
42
- it "raises error when trying to include attribute, not relationship" do
43
- error = assert_raises(Jat::Error) { described_class.call(a_serializer, a1: {}) }
44
- assert_equal "#{a_serializer} has no `a1` relationship", error.message
45
- end
46
-
47
- it "raises error when trying to include not existing nested attribute" do
48
- error = assert_raises(Jat::Error) { described_class.call(a_serializer, a2: {b1: {}}) }
49
- assert_equal "#{b_serializer} has no `b1` relationship", error.message
50
- end
51
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Include" do
6
- before { Jat::Plugins.load_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) { Jat::Plugins::JsonApi::Params::Include }
31
-
32
- it "returns empty hash when param not provided" do
33
- result = described_class.call(a_serializer.allocate, nil)
34
- assert_equal({}, result)
35
- end
36
-
37
- it "returns typed keys" do
38
- result = described_class.call(a_serializer.allocate, "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
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Presenters::DocumentLinksPresenter" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
- let(:presenter_class) { jat_class::DocumentLinksPresenter }
10
-
11
- describe ".jat_class=" do
12
- it "assigns @jat_class" do
13
- presenter_class.jat_class = :foo
14
- assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
- end
16
- end
17
-
18
- describe ".jat_class" do
19
- it "returns self @jat_class" do
20
- assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
- assert_same jat_class, presenter_class.jat_class
22
- end
23
- end
24
-
25
- describe ".inspect" do
26
- it "returns self name" do
27
- assert_equal "#{jat_class}::Presenters::DocumentLinksPresenter", presenter_class.inspect
28
- end
29
- end
30
-
31
- describe ".add_method" do
32
- let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
-
34
- it "adds method by providing block without variables" do
35
- presenter_class.add_method(:foo, proc { [object, context] })
36
- assert_equal %w[OBJECT CONTEXT], presenter.foo
37
- end
38
-
39
- it "adds method by providing block with one variables" do
40
- presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
- assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
- end
43
-
44
- it "adds method by providing block with two variables" do
45
- presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
- assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
- end
48
-
49
- it "raises error when block has more than two variables" do
50
- error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
- assert_equal "Invalid block arguments count", error.message
52
- end
53
-
54
- it "redefines_method" do
55
- presenter_class.add_method(:foo, proc { "OLD" })
56
- presenter_class.add_method(:foo, proc { "NEW" })
57
- assert_equal "NEW", presenter.foo
58
- end
59
- end
60
-
61
- describe "#initialize" do
62
- it "initializes object presenter with provided object and context" do
63
- presenter = presenter_class.new("OBJ", "CTX")
64
-
65
- assert_equal "OBJ", presenter.object
66
- assert_equal "CTX", presenter.context
67
- end
68
- end
69
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Presenters::DocumentMetaPresenter" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
- let(:presenter_class) { jat_class::DocumentMetaPresenter }
10
-
11
- describe ".jat_class=" do
12
- it "assigns @jat_class" do
13
- presenter_class.jat_class = :foo
14
- assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
- end
16
- end
17
-
18
- describe ".jat_class" do
19
- it "returns self @jat_class" do
20
- assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
- assert_same jat_class, presenter_class.jat_class
22
- end
23
- end
24
-
25
- describe ".inspect" do
26
- it "returns self name" do
27
- assert_equal "#{jat_class}::Presenters::DocumentMetaPresenter", presenter_class.inspect
28
- end
29
- end
30
-
31
- describe ".add_method" do
32
- let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
-
34
- it "adds method by providing block without variables" do
35
- presenter_class.add_method(:foo, proc { [object, context] })
36
- assert_equal %w[OBJECT CONTEXT], presenter.foo
37
- end
38
-
39
- it "adds method by providing block with one variables" do
40
- presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
- assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
- end
43
-
44
- it "adds method by providing block with two variables" do
45
- presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
- assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
- end
48
-
49
- it "raises error when block has more than two variables" do
50
- error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
- assert_equal "Invalid block arguments count", error.message
52
- end
53
-
54
- it "redefines_method" do
55
- presenter_class.add_method(:foo, proc { "OLD" })
56
- presenter_class.add_method(:foo, proc { "NEW" })
57
- assert_equal "NEW", presenter.foo
58
- end
59
- end
60
-
61
- describe "#initialize" do
62
- it "initializes object presenter with provided object and context" do
63
- presenter = presenter_class.new("OBJ", "CTX")
64
-
65
- assert_equal "OBJ", presenter.object
66
- assert_equal "CTX", presenter.context
67
- end
68
- end
69
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Presenters::JsonapiPresenter" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
- let(:presenter_class) { jat_class::JsonapiPresenter }
10
-
11
- describe ".jat_class=" do
12
- it "assigns @jat_class" do
13
- presenter_class.jat_class = :foo
14
- assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
- end
16
- end
17
-
18
- describe ".jat_class" do
19
- it "returns self @jat_class" do
20
- assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
- assert_same jat_class, presenter_class.jat_class
22
- end
23
- end
24
-
25
- describe ".inspect" do
26
- it "returns self name" do
27
- assert_equal "#{jat_class}::Presenters::JsonapiPresenter", presenter_class.inspect
28
- end
29
- end
30
-
31
- describe ".add_method" do
32
- let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
-
34
- it "adds method by providing block without variables" do
35
- presenter_class.add_method(:foo, proc { [object, context] })
36
- assert_equal %w[OBJECT CONTEXT], presenter.foo
37
- end
38
-
39
- it "adds method by providing block with one variables" do
40
- presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
- assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
- end
43
-
44
- it "adds method by providing block with two variables" do
45
- presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
- assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
- end
48
-
49
- it "raises error when block has more than two variables" do
50
- error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
- assert_equal "Invalid block arguments count", error.message
52
- end
53
-
54
- it "redefines_method" do
55
- presenter_class.add_method(:foo, proc { "OLD" })
56
- presenter_class.add_method(:foo, proc { "NEW" })
57
- assert_equal "NEW", presenter.foo
58
- end
59
- end
60
-
61
- describe "#initialize" do
62
- it "initializes object presenter with provided object and context" do
63
- presenter = presenter_class.new("OBJ", "CTX")
64
-
65
- assert_equal "OBJ", presenter.object
66
- assert_equal "CTX", presenter.context
67
- end
68
- end
69
- end