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
@@ -3,115 +3,194 @@
3
3
  require "test_helper"
4
4
 
5
5
  describe "Jat::Plugins::JsonApi::Map" do
6
- subject { Jat::Plugins::JsonApi::Map.call(jat) }
6
+ before { Jat::Plugins.find_plugin(:json_api) }
7
7
 
8
- let(:jat_class) do
9
- Class.new(Jat) do
10
- plugin :json_api
11
- type :jat
12
- end
13
- end
14
- let(:jat) { jat_class.new(nil, params: {fields: param_fields, include: param_includes}) }
15
-
16
- let(:default_map) { {a: :a1, b: :b1, c: :c1} }
17
- let(:includes_map) { {b: :b2, c: :c2} }
18
- let(:fields_map) { {c: :c3} }
8
+ let(:described_class) { a::Map }
9
+ let(:base_class) { Class.new(Jat) { plugin :json_api } }
19
10
 
20
- before do
21
- jat.traversal_map.expects(:exposed).returns(default_map)
22
- end
11
+ let(:a) do
12
+ ser = Class.new(base_class)
13
+ ser.type :a
23
14
 
24
- describe "when no params given" do
25
- let(:param_includes) { nil }
26
- let(:param_fields) { nil }
15
+ ser.attribute :a1
16
+ ser.attribute :a2
17
+ ser.attribute :a3, exposed: false
27
18
 
28
- it "returns map of exposed by default fields" do
29
- assert_equal default_map, subject
30
- end
19
+ ser.relationship :b, serializer: b, exposed: true
20
+ ser.relationship :c, serializer: c
21
+ ser.relationship :d, serializer: d
22
+ ser
31
23
  end
32
24
 
33
- describe "when fields given" do
34
- let(:param_includes) { nil }
35
- let(:param_fields) { "FIELDS" }
36
-
37
- before do
38
- constructor = Jat::Plugins::JsonApi::ConstructTraversalMap.allocate
39
- constructor.expects(:to_h).returns(fields_map)
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
40
31
 
41
- Jat::Plugins::JsonApi::Params::Fields
42
- .expects(:call)
43
- .with(jat, "FIELDS")
44
- .returns("PARSED_FIELDS")
45
-
46
- Jat::Plugins::JsonApi::ConstructTraversalMap
47
- .expects(:new)
48
- .with(jat_class, :manual, manually_exposed: "PARSED_FIELDS")
49
- .returns(constructor)
50
- end
51
-
52
- it "constructs map with default and provided fields" do
53
- assert_equal({a: :a1, b: :b1, c: :c3}, subject)
54
- end
32
+ ser.relationship :c, serializer: c
33
+ ser.relationship :d, serializer: d
34
+ ser
55
35
  end
56
36
 
57
- describe "when includes given" do
58
- let(:param_includes) { "INCLUDES" }
59
- let(:param_fields) { nil }
60
-
61
- before do
62
- constructor = Jat::Plugins::JsonApi::ConstructTraversalMap.allocate
63
- constructor.expects(:to_h).returns(includes_map)
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
64
45
 
65
- Jat::Plugins::JsonApi::Params::Include
66
- .expects(:call)
67
- .with(jat, "INCLUDES")
68
- .returns("PARSED_INCLUDES")
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
69
54
 
70
- Jat::Plugins::JsonApi::ConstructTraversalMap
71
- .expects(:new)
72
- .with(jat_class, :exposed, manually_exposed: "PARSED_INCLUDES")
73
- .returns(constructor)
55
+ describe ".call" do
56
+ describe "with default :exposed option" do
57
+ it "returns exposed by default attributes" do
58
+ result = described_class.call(exposed: :default)
59
+ expected_result = {
60
+ a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
61
+ b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
62
+ }
63
+
64
+ assert_equal expected_result, result
65
+ end
66
+
67
+ it "returns exposed by default attributes when no :exposed param provided" do
68
+ result = described_class.call({})
69
+ expected_result = {
70
+ a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
71
+ b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]}
72
+ }
73
+
74
+ assert_equal expected_result, result
75
+ end
76
+
77
+ it "returns additionally included fields specified in :includes option" do
78
+ includes = "c,b.d"
79
+ result = described_class.call(include: includes)
80
+ expected_result = {
81
+ a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
82
+ b: {serializer: b, attributes: %i[b1 b2], relationships: %i[d]},
83
+ c: {serializer: c, attributes: %i[c1 c2], relationships: %i[]},
84
+ d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
85
+ }
86
+
87
+ assert_equal expected_result, result
88
+ end
89
+
90
+ it "returns fields specified in :fields option (only specified fields for specified type)" do
91
+ fields = {b: "b1,d"}
92
+ result = described_class.call(fields: fields)
93
+ expected_result = {
94
+ a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b]},
95
+ b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
96
+ d: {serializer: d, attributes: %i[d1 d2], relationships: %i[]}
97
+ }
98
+
99
+ assert_equal expected_result, result
100
+ end
101
+
102
+ it "returns fields specified in :includes and :fields options" do
103
+ includes = "c"
104
+ fields = {c: "c1"}
105
+ result = described_class.call(include: includes, fields: fields)
106
+ expected_result = {
107
+ a: {serializer: a, attributes: %i[a1 a2], relationships: %i[b c]},
108
+ b: {serializer: b, attributes: %i[b1 b2], relationships: %i[]},
109
+ c: {serializer: c, attributes: %i[c1], relationships: %i[]}
110
+ }
111
+
112
+ assert_equal expected_result, result
113
+ end
74
114
  end
75
115
 
76
- it "constructs map with default and included fields" do
77
- assert_equal({a: :a1, b: :b2, c: :c2}, subject)
116
+ describe "with exposed: :none option" do
117
+ it "returns no attributes" do
118
+ result = described_class.call(exposed: :none)
119
+ expected_result = {
120
+ a: {serializer: a, attributes: %i[], relationships: %i[]}
121
+ }
122
+
123
+ assert_equal expected_result, result
124
+ end
125
+
126
+ it "returns additionally included fields specified in :includes option" do
127
+ includes = "b.c"
128
+ result = described_class.call(exposed: :none, include: includes)
129
+ expected_result = {
130
+ a: {serializer: a, attributes: %i[], relationships: %i[b]},
131
+ b: {serializer: b, attributes: %i[], relationships: %i[c]},
132
+ c: {serializer: c, attributes: %i[], relationships: %i[]}
133
+ }
134
+
135
+ assert_equal expected_result, result
136
+ end
137
+
138
+ it "returns fields specified in :fields option (only specified fields for specified type)" do
139
+ fields = {a: "a1,b", b: "b1,d"}
140
+ result = described_class.call(exposed: :none, fields: fields)
141
+ expected_result = {
142
+ a: {serializer: a, attributes: %i[a1], relationships: %i[b]},
143
+ b: {serializer: b, attributes: %i[b1], relationships: %i[d]},
144
+ d: {serializer: d, attributes: %i[], relationships: %i[]}
145
+ }
146
+
147
+ assert_equal expected_result, result
148
+ end
149
+
150
+ it "returns fields specified in :includes and :fields options" do
151
+ includes = "c"
152
+ fields = {c: "c1"}
153
+ result = described_class.call(exposed: :none, include: includes, fields: fields)
154
+ expected_result = {
155
+ a: {serializer: a, attributes: %i[], relationships: %i[c]},
156
+ c: {serializer: c, attributes: %i[c1], relationships: %i[]}
157
+ }
158
+
159
+ assert_equal expected_result, result
160
+ end
78
161
  end
79
- end
80
-
81
- describe "when fields and includes given" do
82
- let(:param_fields) { "FIELDS" }
83
- let(:param_includes) { "INCLUDES" }
84
-
85
- before do
86
- constructor1 = Jat::Plugins::JsonApi::ConstructTraversalMap.allocate
87
- constructor1.expects(:to_h).returns(fields_map)
88
162
 
89
- Jat::Plugins::JsonApi::Params::Fields
90
- .expects(:call)
91
- .with(jat, "FIELDS")
92
- .returns("PARSED_FIELDS")
93
-
94
- Jat::Plugins::JsonApi::ConstructTraversalMap
95
- .expects(:new)
96
- .with(jat_class, :manual, manually_exposed: "PARSED_FIELDS")
97
- .returns(constructor1)
98
-
99
- constructor2 = Jat::Plugins::JsonApi::ConstructTraversalMap.allocate
100
- constructor2.expects(:to_h).returns(includes_map)
101
-
102
- Jat::Plugins::JsonApi::Params::Include
103
- .expects(:call)
104
- .with(jat, "INCLUDES")
105
- .returns("PARSED_INCLUDES")
106
-
107
- Jat::Plugins::JsonApi::ConstructTraversalMap
108
- .expects(:new)
109
- .with(jat_class, :exposed, manually_exposed: "PARSED_INCLUDES")
110
- .returns(constructor2)
163
+ describe "with exposed: :all option" do
164
+ it "returns all attributes" do
165
+ result = described_class.call(exposed: :all)
166
+ expected_result = {
167
+ a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
168
+ b: {serializer: b, attributes: %i[b1 b2 b3], relationships: %i[c d]},
169
+ c: {serializer: c, attributes: %i[c1 c2 c3], relationships: %i[]},
170
+ d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
171
+ }
172
+
173
+ assert_equal expected_result, result
174
+ end
175
+
176
+ it "returns only specified fields for specified fields types" do
177
+ fields = {b: "b1", c: "c2"}
178
+ result = described_class.call(exposed: :all, fields: fields)
179
+ expected_result = {
180
+ a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
181
+ b: {serializer: b, attributes: %i[b1], relationships: %i[]},
182
+ c: {serializer: c, attributes: %i[c2], relationships: %i[]},
183
+ d: {serializer: d, attributes: %i[d1 d2 d3], relationships: %i[]}
184
+ }
185
+
186
+ assert_equal expected_result, result
187
+ end
111
188
  end
189
+ end
112
190
 
113
- it "constructs map with using everything: defaults, includes, fields" do
114
- assert_equal({a: :a1, b: :b2, c: :c3}, subject)
191
+ describe ".inspect" do
192
+ it "returns self name" do
193
+ assert_equal "#{a}::Map", described_class.inspect
115
194
  end
116
195
  end
117
196
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::ResponsePiece" do
6
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
7
+
8
+ describe ".inspect" do
9
+ it "returns self name" do
10
+ assert_equal "#{jat_class}::ResponsePiece", jat_class::ResponsePiece.inspect
11
+ end
12
+ end
13
+ end
@@ -14,7 +14,7 @@ describe "Jat::Plugins::JsonApi::Response" do
14
14
  it "returns correct structure with data" do
15
15
  str_serializer = Class.new(base_class) do
16
16
  type "str"
17
- attribute(:id) { |_| "STRING" }
17
+ id { |_| "STRING" }
18
18
  end
19
19
 
20
20
  assert_equal({data: {type: :str, id: "STRING"}}, str_serializer.to_h("STRING"))
@@ -23,7 +23,7 @@ describe "Jat::Plugins::JsonApi::Response" do
23
23
  it "returns correct structure with array data" do
24
24
  str_serializer = Class.new(base_class) do
25
25
  type "str"
26
- attribute(:id) { |obj| obj }
26
+ id { |obj| obj }
27
27
  end
28
28
 
29
29
  assert_equal(
@@ -35,7 +35,7 @@ describe "Jat::Plugins::JsonApi::Response" do
35
35
  it "returns correct structure with data with attributes" do
36
36
  str_serializer = Class.new(base_class) do
37
37
  type "str"
38
- attribute(:id) { |obj| obj[0] }
38
+ id { |obj| obj[0] }
39
39
  attribute :length
40
40
  end
41
41
 
@@ -48,12 +48,12 @@ describe "Jat::Plugins::JsonApi::Response" do
48
48
  it "returns correct structure with has-one relationship" do
49
49
  int_serializer = Class.new(base_class) do
50
50
  type "int"
51
- attribute(:id) { |obj| obj }
51
+ id { |obj| obj }
52
52
  end
53
53
 
54
54
  str_serializer = Class.new(base_class) do
55
55
  type "str"
56
- attribute(:id) { |obj| obj[0] }
56
+ id { |obj| obj[0] }
57
57
  relationship :length, serializer: int_serializer, exposed: true
58
58
  end
59
59
 
@@ -76,12 +76,12 @@ describe "Jat::Plugins::JsonApi::Response" do
76
76
  it "does not return has-one relationship when not exposed" do
77
77
  int_serializer = Class.new(base_class) do
78
78
  type "int"
79
- attribute(:id) { |obj| obj }
79
+ id { |obj| obj }
80
80
  end
81
81
 
82
82
  str_serializer = Class.new(base_class) do
83
83
  type "str"
84
- attribute(:id) { |obj| obj[0] }
84
+ id { |obj| obj[0] }
85
85
  relationship :length, serializer: int_serializer # relationships are not exposed by default
86
86
  end
87
87
 
@@ -91,7 +91,7 @@ describe "Jat::Plugins::JsonApi::Response" do
91
91
  it "returns correct structure with empty has-one relationship" do
92
92
  str_serializer = Class.new(base_class) do
93
93
  type "str"
94
- attribute(:id) { |obj| obj[0] }
94
+ id { |obj| obj[0] }
95
95
  relationship(:length, serializer: self, exposed: true) { |_obj| nil }
96
96
  end
97
97
 
@@ -109,13 +109,13 @@ describe "Jat::Plugins::JsonApi::Response" do
109
109
  it "returns correct structure with has-one relationship with attributes" do
110
110
  int_serializer = Class.new(base_class) do
111
111
  type "int"
112
- attribute(:id) { |obj| obj }
112
+ id { |obj| obj }
113
113
  attribute(:next) { |obj| obj + 1 }
114
114
  end
115
115
 
116
116
  str_serializer = Class.new(base_class) do
117
117
  type "str"
118
- attribute(:id) { |obj| obj[0] }
118
+ id { |obj| obj[0] }
119
119
  relationship :length, serializer: int_serializer, exposed: true
120
120
  end
121
121
 
@@ -142,7 +142,7 @@ describe "Jat::Plugins::JsonApi::Response" do
142
142
 
143
143
  str_serializer = Class.new(base_class) do
144
144
  type "str"
145
- attribute(:id) { |_obj| "id" }
145
+ id { |_obj| "id" }
146
146
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
147
147
  end
148
148
 
@@ -160,12 +160,12 @@ describe "Jat::Plugins::JsonApi::Response" do
160
160
  it "returns correct structure with has-many relationship" do
161
161
  chr_serializer = Class.new(base_class) do
162
162
  type "chr"
163
- attribute(:id) { |obj| obj }
163
+ id { |obj| obj }
164
164
  end
165
165
 
166
166
  str_serializer = Class.new(base_class) do
167
167
  type "str"
168
- attribute(:id) { |obj| obj[0] }
168
+ id { |obj| obj[0] }
169
169
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
170
170
  end
171
171
 
@@ -188,13 +188,13 @@ describe "Jat::Plugins::JsonApi::Response" do
188
188
  it "returns correct structure with has-many relationship with attributes" do
189
189
  chr_serializer = Class.new(base_class) do
190
190
  type "chr"
191
- attribute(:id) { |obj| obj }
191
+ id { |obj| obj }
192
192
  attribute :next
193
193
  end
194
194
 
195
195
  str_serializer = Class.new(base_class) do
196
196
  type "str"
197
- attribute(:id) { |obj| obj[0] }
197
+ id { |obj| obj[0] }
198
198
  relationship :chars, serializer: chr_serializer, many: true, exposed: true
199
199
  end
200
200
 
@@ -218,12 +218,12 @@ describe "Jat::Plugins::JsonApi::Response" do
218
218
  it "accepts includes param" do
219
219
  chr_serializer = Class.new(base_class) do
220
220
  type "chr"
221
- attribute(:id) { |obj| obj }
221
+ id { |obj| obj }
222
222
  end
223
223
 
224
224
  str_serializer = Class.new(base_class) do
225
225
  type "str"
226
- attribute(:id) { |obj| obj[0] }
226
+ id { |obj| obj[0] }
227
227
  relationship :chars, serializer: chr_serializer, many: true, exposed: false
228
228
  end
229
229
 
@@ -239,19 +239,19 @@ describe "Jat::Plugins::JsonApi::Response" do
239
239
  {type: :chr, id: "a"}, {type: :chr, id: "b"}
240
240
  ]
241
241
  },
242
- str_serializer.to_h("ab", params: {include: "chars"})
242
+ str_serializer.to_h("ab", include: "chars")
243
243
  )
244
244
  end
245
245
 
246
246
  it "accepts sparse_fieldset" do
247
247
  chr_serializer = Class.new(base_class) do
248
248
  type "chr"
249
- attribute(:id) { |obj| obj }
249
+ id { |obj| obj }
250
250
  end
251
251
 
252
252
  str_serializer = Class.new(base_class) do
253
253
  type "str"
254
- attribute(:id) { |obj| obj[0] }
254
+ id { |obj| obj[0] }
255
255
  relationship :chars, serializer: chr_serializer, many: true, exposed: false
256
256
  end
257
257
 
@@ -267,7 +267,7 @@ describe "Jat::Plugins::JsonApi::Response" do
267
267
  {type: :chr, id: "a"}, {type: :chr, id: "b"}
268
268
  ]
269
269
  },
270
- str_serializer.to_h("ab", params: {fields: {str: "chars"}})
270
+ str_serializer.to_h("ab", fields: {str: "chars"})
271
271
  )
272
272
  end
273
273
 
@@ -275,7 +275,7 @@ describe "Jat::Plugins::JsonApi::Response" do
275
275
  let(:serializer) do
276
276
  Class.new(base_class) do
277
277
  type :foo
278
- attribute(:id) { |obj| obj }
278
+ id { |obj| obj }
279
279
  end
280
280
  end
281
281
 
@@ -311,7 +311,7 @@ describe "Jat::Plugins::JsonApi::Response" do
311
311
  let(:serializer) do
312
312
  Class.new(base_class) do
313
313
  type :foo
314
- attribute(:id) { |obj| obj }
314
+ id { |obj| obj }
315
315
  end
316
316
  end
317
317
 
@@ -347,7 +347,7 @@ describe "Jat::Plugins::JsonApi::Response" do
347
347
  let(:serializer) do
348
348
  Class.new(base_class) do
349
349
  type :foo
350
- attribute(:id) { |obj| obj }
350
+ id { |obj| obj }
351
351
  end
352
352
  end
353
353
 
@@ -374,21 +374,21 @@ describe "Jat::Plugins::JsonApi::Response" do
374
374
  let(:bar_serializer) do
375
375
  Class.new(base_class) do
376
376
  type :bar
377
- attribute(:id) { |obj| obj }
377
+ id { |obj| obj }
378
378
  end
379
379
  end
380
380
 
381
381
  let(:foo_serializer) do
382
382
  Class.new(base_class) do
383
383
  type :foo
384
- attribute(:id) { |obj| obj }
384
+ id { |obj| obj }
385
385
  end
386
386
  end
387
387
 
388
388
  it "adds relationship meta defined in serializer" do
389
389
  foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
390
390
  bar_serializer.relationship_meta(:version) { "1.2.3" }
391
- bar_serializer.relationship_meta(:uid) { |parent, obj, context| [parent, obj, context[:time]] }
391
+ bar_serializer.relationship_meta(:uid) { |obj, context| [context[:parent_object], obj, context[:time]] }
392
392
  bar_serializer.relationship_meta(:null) {}
393
393
 
394
394
  response = foo_serializer.to_h("foo", time: "12:00")
@@ -401,7 +401,7 @@ describe "Jat::Plugins::JsonApi::Response" do
401
401
  let(:serializer) do
402
402
  Class.new(base_class) do
403
403
  type :foo
404
- attribute(:id) { |obj| obj }
404
+ id { |obj| obj }
405
405
  end
406
406
  end
407
407
 
@@ -437,7 +437,7 @@ describe "Jat::Plugins::JsonApi::Response" do
437
437
  let(:serializer) do
438
438
  Class.new(base_class) do
439
439
  type :foo
440
- attribute(:id) { |obj| obj }
440
+ id { |obj| obj }
441
441
  end
442
442
  end
443
443
 
@@ -464,21 +464,21 @@ describe "Jat::Plugins::JsonApi::Response" do
464
464
  let(:bar_serializer) do
465
465
  Class.new(base_class) do
466
466
  type :bar
467
- attribute(:id) { |obj| obj }
467
+ id { |obj| obj }
468
468
  end
469
469
  end
470
470
 
471
471
  let(:foo_serializer) do
472
472
  Class.new(base_class) do
473
473
  type :foo
474
- attribute(:id) { |obj| obj }
474
+ id { |obj| obj }
475
475
  end
476
476
  end
477
477
 
478
478
  it "adds relationship link defined in serializer" do
479
479
  foo_serializer.relationship(:bar, serializer: -> { bar_serializer }, exposed: true) { "bar" }
480
480
  bar_serializer.relationship_link(:self) { "/self" }
481
- bar_serializer.relationship_link(:related) { |parent_obj| "/#{parent_obj}/self" }
481
+ bar_serializer.relationship_link(:related) { |_obj, ctx| "/#{ctx[:parent_object]}/self" }
482
482
  bar_serializer.relationship_link(:null) {}
483
483
 
484
484
  response = foo_serializer.to_h("foo")
@@ -486,4 +486,10 @@ describe "Jat::Plugins::JsonApi::Response" do
486
486
  assert_equal({self: "/self", related: "/foo/self"}, links)
487
487
  end
488
488
  end
489
+
490
+ describe ".inspect" do
491
+ it "returns self name" do
492
+ assert_equal "#{base_class}::Response", base_class::Response.inspect
493
+ end
494
+ end
489
495
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiActiverecord" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:json_api_activerecord)
8
+ end
9
+
10
+ it "checks json_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/json_api/, error.message)
14
+ end
15
+
16
+ it "loads other plugins" do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :json_api
19
+ jat_class.plugin :json_api_activerecord
20
+
21
+ assert jat_class.plugin_used?(:json_api_preloads)
22
+ assert jat_class.plugin_used?(:base_activerecord_preloads)
23
+ end
24
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiLowerCamelCase" do
6
+ let(:jat_class) do
7
+ new_class = Class.new(Jat)
8
+ new_class.plugin(:json_api)
9
+ new_class.plugin(:json_api_lower_camel_case)
10
+ new_class
11
+ end
12
+
13
+ before do
14
+ jat_class.type :foo
15
+ jat_class.id { |object| object }
16
+ end
17
+
18
+ it "checks json_api plugin loaded before" do
19
+ jat_class = Class.new(Jat)
20
+ error = assert_raises(Jat::Error) { jat_class.plugin :json_api_lower_camel_case }
21
+ assert_match(/json_api/, error.message)
22
+ end
23
+
24
+ it "returns attributes in lowerCamelCase case" do
25
+ jat_class.attribute(:foo_bar) { 1 }
26
+
27
+ assert_equal({fooBar: 1}, jat_class.to_h(true).dig(:data, :attributes))
28
+ end
29
+
30
+ it "accepts `fields` in lowerCamelCase format" do
31
+ jat_class.attribute(:foo_bar, exposed: false) { 1 }
32
+
33
+ response = jat_class.to_h(true, fields: {foo: "fooBar"})
34
+ assert_equal({fooBar: 1}, response.dig(:data, :attributes))
35
+ end
36
+
37
+ it "accepts `include` in lowerCamelCase format" do
38
+ new_serializer = Class.new(Jat)
39
+ new_serializer.plugin(:json_api)
40
+ new_serializer.type :new
41
+ new_serializer.id { |object| object }
42
+
43
+ jat_class.relationship(:foo_bar, serializer: new_serializer) { 1 }
44
+
45
+ response = jat_class.to_h(true, include: "fooBar")
46
+ response_relationships = response.dig(:data, :relationships).keys
47
+ assert_includes(response_relationships, :fooBar)
48
+ end
49
+
50
+ it "returns document meta keys in lowerCamelCase format" do
51
+ jat_class.document_meta(:user_agent) { "Firefox" }
52
+
53
+ response = jat_class.to_h(nil)
54
+ assert_equal({meta: {userAgent: "Firefox"}}, response)
55
+ end
56
+
57
+ it "returns document jsonapi keys in lowerCamelCase format" do
58
+ jat_class.jsonapi(:user_agent) { "Firefox" }
59
+
60
+ response = jat_class.to_h(nil)
61
+ assert_equal({jsonapi: {userAgent: "Firefox"}}, response)
62
+ end
63
+
64
+ it "returns document links keys in lowerCamelCase format" do
65
+ jat_class.document_link(:user_agent) { "Firefox" }
66
+
67
+ response = jat_class.to_h(nil)
68
+ assert_equal({links: {userAgent: "Firefox"}}, response)
69
+ end
70
+
71
+ it "returns context meta keys in lowerCamelCase format" do
72
+ response = jat_class.to_h(nil, meta: {user_agent: "Chrome"})
73
+ assert_equal({meta: {userAgent: "Chrome"}}, response)
74
+ end
75
+
76
+ it "returns context jsonapi keys in lowerCamelCase format" do
77
+ response = jat_class.to_h(nil, jsonapi: {user_agent: "Chrome"})
78
+ assert_equal({jsonapi: {userAgent: "Chrome"}}, response)
79
+ end
80
+
81
+ it "returns context links keys in lowerCamelCase format" do
82
+ response = jat_class.to_h(nil, links: {user_agent: "Chrome"})
83
+ assert_equal({links: {userAgent: "Chrome"}}, response)
84
+ end
85
+ end