jat 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +4 -5
  3. data/lib/jat/plugins/{common/_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +3 -3
  4. data/lib/jat/plugins/{common/_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +3 -1
  5. data/lib/jat/plugins/{common/_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +3 -3
  6. data/lib/jat/plugins/{common/_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +3 -3
  7. data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  8. data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  9. data/lib/jat/plugins/json_api/lib/response_piece.rb +2 -2
  10. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +1 -1
  11. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +1 -1
  12. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +1 -1
  13. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +1 -1
  14. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +1 -1
  15. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +1 -1
  16. data/lib/jat/plugins/simple_api/simple_api.rb +1 -1
  17. data/lib/jat/plugins.rb +37 -25
  18. data/lib/jat.rb +8 -6
  19. data/test/lib/jat/attribute_test.rb +8 -0
  20. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  21. data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
  22. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
  23. data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
  24. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  25. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  26. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  27. data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -1
  28. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
  29. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -9
  30. data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
  31. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  32. data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -0
  33. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +1 -1
  34. data/test/lib/jat/plugins/{json_api_camel_lower/json_api_camel_lower_test.rb → json_api_lower_camel_case/json_api_lower_camel_case_test.rb} +6 -0
  35. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +16 -2
  36. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
  37. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  38. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  39. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  40. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +53 -43
  41. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
  42. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  43. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +10 -2
  44. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +36 -1
  45. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +1 -1
  46. data/test/lib/jat/plugins/{simple_api_camel_lower/simple_api_camel_lower_test.rb → simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb} +7 -1
  47. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +16 -2
  48. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
  49. data/test/lib/jat/plugins/types/types_test.rb +5 -0
  50. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  51. data/test/lib/jat/plugins_test.rb +88 -0
  52. data/test/test_helper.rb +7 -3
  53. metadata +40 -24
@@ -52,137 +52,145 @@ describe "Jat::Plugins::JsonApi::Map" do
52
52
  ser
53
53
  end
54
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
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
64
114
  end
65
115
 
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
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
99
161
  end
100
162
 
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
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
112
188
  end
113
189
  end
114
190
 
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
191
+ describe ".inspect" do
192
+ it "returns self name" do
193
+ assert_equal "#{a}::Map", described_class.inspect
186
194
  end
187
195
  end
188
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
@@ -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
@@ -19,6 +19,6 @@ describe "Jat::Plugins::JsonApiActiverecord" do
19
19
  jat_class.plugin :json_api_activerecord
20
20
 
21
21
  assert jat_class.plugin_used?(:json_api_preloads)
22
- assert jat_class.plugin_used?(:_activerecord_preloads)
22
+ assert jat_class.plugin_used?(:base_activerecord_preloads)
23
23
  end
24
24
  end
@@ -15,6 +15,12 @@ describe "Jat::Plugins::JsonApiLowerCamelCase" do
15
15
  jat_class.id { |object| object }
16
16
  end
17
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
+
18
24
  it "returns attributes in lowerCamelCase case" do
19
25
  jat_class.attribute(:foo_bar) { 1 }
20
26
 
@@ -17,13 +17,13 @@ describe "Jat::Plugins::JsonApiPreloads" do
17
17
  jat_class = Class.new(Jat)
18
18
  jat_class.plugin :json_api
19
19
 
20
- jat_class.expects(:plugin).with(:_preloads, foo: :bar)
20
+ jat_class.expects(:plugin).with(:base_preloads, foo: :bar)
21
21
 
22
22
  @plugin.before_load(jat_class, foo: :bar)
23
23
  end
24
24
 
25
25
  describe "InstanceMethods" do
26
- it "add .preloads method as a delegator to #{@plugin}::Preloads" do
26
+ it "adds #preloads method as a delegator to #{@plugin}::Preloads" do
27
27
  jat_class = Class.new(Jat)
28
28
  jat_class.plugin :json_api
29
29
  jat_class.plugin @plugin
@@ -34,4 +34,18 @@ describe "Jat::Plugins::JsonApiPreloads" do
34
34
  assert_equal "RES", jat.preloads
35
35
  end
36
36
  end
37
+
38
+ describe "ClassMethods" do
39
+ it "adds .preloads method as a delegator to #{@plugin}::Preloads" do
40
+ jat_class = Class.new(Jat)
41
+ jat_class.plugin :json_api
42
+ jat_class.plugin @plugin
43
+
44
+ jat = jat_class.allocate
45
+ jat_class.expects(:new).with("CONTEXT").returns(jat)
46
+ @plugin::Preloads.expects(:call).with(jat).returns("RES")
47
+
48
+ assert_equal "RES", jat_class.preloads("CONTEXT")
49
+ end
50
+ end
37
51
  end
@@ -13,34 +13,50 @@ describe "Jat::Plugins::JsonApiValidateParams" do
13
13
  assert_match(/json_api/, error.message)
14
14
  end
15
15
 
16
- let(:serializer) do
16
+ let(:base) do
17
17
  jat_class = Class.new(Jat)
18
18
  jat_class.plugin :json_api
19
+ jat_class
20
+ end
21
+
22
+ let(:serializer) do
23
+ jat_class = Class.new(base)
19
24
  jat_class.plugin @plugin
20
25
 
21
26
  jat_class.type "foo"
22
27
  jat_class.attribute :foo_bar
23
- jat_class.relationship :foo_bazz, serializer: jat_class
28
+ jat_class.relationship :foo_bazz, serializer: foo_bazz_serializer
29
+ jat_class
30
+ end
31
+
32
+ let(:foo_bazz_serializer) do
33
+ jat_class = Class.new(base)
34
+ jat_class.type "foo_bazz"
35
+ jat_class.attribute :bazz
24
36
  jat_class
25
37
  end
26
38
 
27
39
  let(:serializer_lower_camel_case) do
28
- jat_class = Class.new(Jat)
29
- jat_class.plugin :json_api
40
+ jat_class = Class.new(base)
30
41
  jat_class.plugin :json_api_lower_camel_case
31
42
  jat_class.plugin @plugin
32
43
 
33
44
  jat_class.type "foo"
34
45
  jat_class.attribute :foo_bar
35
- jat_class.relationship :foo_bazz, serializer: jat_class
46
+ jat_class.relationship :foo_bazz, serializer: foo_bazz_serializer
36
47
  jat_class
37
48
  end
38
49
 
50
+ it "returns true when provided fields present" do
51
+ jat = serializer.new(fields: {foo: "foo_bar,foo_bazz", foo_bazz: "bazz"})
52
+ assert jat.validate
53
+ end
54
+
39
55
  it "validates fields types" do
40
56
  jat = serializer.new(fields: {bar: "any"})
41
57
  error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
42
58
 
43
- expected_message = "Response does not have resources with type 'bar'. Existing types are: 'foo'"
59
+ expected_message = "Response does not have resources with type 'bar'. Existing types are: 'foo', 'foo_bazz'"
44
60
  assert_equal(expected_message, error.message)
45
61
  end
46
62
 
@@ -81,4 +97,12 @@ describe "Jat::Plugins::JsonApiValidateParams" do
81
97
  expected_message = "Type 'foo' has no included 'extra' relationship. Existing relationships are: 'fooBazz'"
82
98
  assert_equal(expected_message, error.message)
83
99
  end
100
+
101
+ it "validates nested includes" do
102
+ jat = serializer.new(include: "foo_bazz.extra")
103
+ error = assert_raises(Jat::JsonApiParamsError) { jat.validate }
104
+
105
+ expected_message = "Type 'foo_bazz' has no included 'extra' relationship. Existing relationships are: ''"
106
+ assert_equal(expected_message, error.message)
107
+ end
84
108
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::LowerCamelCase" do
6
+ it "raises error if no response plugin is loaded" do
7
+ new_class = Class.new(Jat)
8
+ err = assert_raises(Jat::Error) { new_class.plugin :lower_camel_case }
9
+ assert_equal "Please load :json_api or :simple_api plugin first", err.message
10
+ end
11
+
12
+ it "loads simple_api compatible plugin" do
13
+ new_class = Class.new(Jat)
14
+ new_class.plugin :simple_api
15
+ new_class.plugin :lower_camel_case
16
+
17
+ assert new_class.plugin_used?(:simple_api_lower_camel_case)
18
+ end
19
+
20
+ it "loads json_api compatible plugin" do
21
+ new_class = Class.new(Jat)
22
+ new_class.plugin :json_api
23
+ new_class.plugin :lower_camel_case
24
+
25
+ assert new_class.plugin_used?(:json_api_lower_camel_case)
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::MapsCache" do
6
+ it "raises error if no response plugin is loaded" do
7
+ new_class = Class.new(Jat)
8
+ err = assert_raises(Jat::Error) { new_class.plugin :maps_cache }
9
+ assert_equal "Please load :json_api or :simple_api plugin first", err.message
10
+ end
11
+
12
+ it "loads simple_api compatible plugin" do
13
+ new_class = Class.new(Jat)
14
+ new_class.plugin :simple_api
15
+ new_class.plugin :maps_cache
16
+
17
+ assert new_class.plugin_used?(:simple_api_maps_cache)
18
+ end
19
+
20
+ it "loads json_api compatible plugin" do
21
+ new_class = Class.new(Jat)
22
+ new_class.plugin :json_api
23
+ new_class.plugin :maps_cache
24
+
25
+ assert new_class.plugin_used?(:json_api_maps_cache)
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::Preloads" do
6
+ it "raises error if no response plugin is loaded" do
7
+ new_class = Class.new(Jat)
8
+ err = assert_raises(Jat::Error) { new_class.plugin :preloads }
9
+ assert_equal "Please load :json_api or :simple_api plugin first", err.message
10
+ end
11
+
12
+ it "loads simple_api compatible plugin" do
13
+ new_class = Class.new(Jat)
14
+ new_class.plugin :simple_api
15
+ new_class.plugin :preloads
16
+
17
+ assert new_class.plugin_used?(:simple_api_preloads)
18
+ end
19
+
20
+ it "loads json_api compatible plugin" do
21
+ new_class = Class.new(Jat)
22
+ new_class.plugin :json_api
23
+ new_class.plugin :preloads
24
+
25
+ assert new_class.plugin_used?(:json_api_preloads)
26
+ end
27
+ end
@@ -13,65 +13,75 @@ describe "Jat::Plugins::SimpleApi::FieldsParamParser" do
13
13
  ser
14
14
  end
15
15
 
16
+ let(:described_class) { jat_class::FieldsParamParser }
17
+
16
18
  def parse(str)
17
19
  jat_class::FieldsParamParser.parse(str)
18
20
  end
19
21
 
20
- it "returns empty hash when nil provided" do
21
- assert_equal({}, parse(nil))
22
+ describe ".inspect" do
23
+ it "returns self name" do
24
+ assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
25
+ end
22
26
  end
23
27
 
24
- it "returns empty hash when empty string provided" do
25
- assert_equal({}, parse(""))
26
- end
28
+ describe ".parse" do
29
+ it "returns empty hash when nil provided" do
30
+ assert_equal({}, parse(nil))
31
+ end
27
32
 
28
- it "parses single field" do
29
- assert_equal({id: {}}, parse("id"))
30
- end
33
+ it "returns empty hash when empty string provided" do
34
+ assert_equal({}, parse(""))
35
+ end
31
36
 
32
- it "parses multiple fields" do
33
- assert_equal({id: {}, name: {}}, parse("id, name"))
34
- end
37
+ it "parses single field" do
38
+ assert_equal({id: {}}, parse("id"))
39
+ end
35
40
 
36
- it "parses single resource with single field" do
37
- assert_equal({users: {id: {}}}, parse("users(id)"))
38
- end
41
+ it "parses multiple fields" do
42
+ assert_equal({id: {}, name: {}}, parse("id, name"))
43
+ end
39
44
 
40
- it "parses fields started with open PAREN" do
41
- assert_equal({users: {id: {}}}, parse("(users(id))"))
42
- end
45
+ it "parses single resource with single field" do
46
+ assert_equal({users: {id: {}}}, parse("users(id)"))
47
+ end
43
48
 
44
- it "parses single resource with multiple fields" do
45
- assert_equal({users: {id: {}, name: {}}}, parse("users(id,name)"))
46
- end
49
+ it "parses fields started with open PAREN" do
50
+ assert_equal({users: {id: {}}}, parse("(users(id))"))
51
+ end
47
52
 
48
- it "parses multiple resources with fields" do
49
- fields = "id,posts(title,text),news(title,text)"
50
- resp = {
51
- id: {},
52
- posts: {title: {}, text: {}},
53
- news: {title: {}, text: {}}
54
- }
53
+ it "parses single resource with multiple fields" do
54
+ assert_equal({users: {id: {}, name: {}}}, parse("users(id,name)"))
55
+ end
55
56
 
56
- assert_equal(resp, parse(fields))
57
- end
57
+ it "parses multiple resources with fields" do
58
+ fields = "id,posts(title,text),news(title,text)"
59
+ resp = {
60
+ id: {},
61
+ posts: {title: {}, text: {}},
62
+ news: {title: {}, text: {}}
63
+ }
64
+
65
+ assert_equal(resp, parse(fields))
66
+ end
58
67
 
59
- it "parses included resources" do
60
- fields = "id,posts(title,text,comments(author(name),comment))"
61
- resp = {
62
- id: {},
63
- posts: {
64
- title: {},
65
- text: {},
66
- comments: {
67
- author: {
68
- name: {}
69
- },
70
- comment: {}
68
+ it "parses included resources" do
69
+ fields = "id,posts(title,text,comments(author(name),comment))"
70
+ resp = {
71
+ id: {},
72
+ posts: {
73
+ title: {},
74
+ text: {},
75
+ comments: {
76
+ author: {
77
+ name: {}
78
+ },
79
+ comment: {}
80
+ }
71
81
  }
72
82
  }
73
- }
74
83
 
75
- assert_equal(resp, parse(fields))
84
+ assert_equal(resp, parse(fields))
85
+ end
76
86
  end
77
87
  end