jat 0.0.5 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +5 -16
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +7 -2
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +16 -4
  7. data/lib/jat/plugins/{_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +6 -2
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +6 -2
  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 +5 -1
  12. data/lib/jat/plugins/json_api/json_api.rb +7 -3
  13. data/lib/jat/plugins/json_api/lib/map.rb +4 -5
  14. data/lib/jat/plugins/json_api/lib/response.rb +1 -5
  15. data/lib/jat/plugins/json_api/lib/response_piece.rb +19 -10
  16. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  17. data/lib/jat/plugins/{json_api_lower_camel_case → json_api/plugins/json_api_lower_camel_case}/json_api_lower_camel_case.rb +6 -2
  18. data/lib/jat/plugins/{json_api_maps_cache → json_api/plugins/json_api_maps_cache}/json_api_maps_cache.rb +5 -1
  19. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  20. data/lib/jat/plugins/{json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +1 -1
  21. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/json_api_validate_params.rb +5 -1
  22. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/params_error.rb +0 -0
  23. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/validate_fields_param.rb +0 -0
  24. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/validate_include_param.rb +1 -3
  25. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  26. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  27. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  28. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +20 -36
  29. data/lib/jat/plugins/simple_api/lib/map.rb +4 -5
  30. data/lib/jat/plugins/simple_api/lib/response.rb +1 -5
  31. data/lib/jat/plugins/simple_api/lib/response_piece.rb +1 -5
  32. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  33. data/lib/jat/plugins/{simple_api_lower_camel_case → simple_api/plugins/simple_api_lower_camel_case}/simple_api_lower_camel_case.rb +6 -2
  34. data/lib/jat/plugins/{simple_api_maps_cache → simple_api/plugins/simple_api_maps_cache}/simple_api_maps_cache.rb +6 -4
  35. data/lib/jat/plugins/{simple_api_activerecord → simple_api/plugins/simple_api_preloads}/lib/preloads.rb +1 -1
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  37. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/lib/fields_error.rb +0 -0
  38. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/lib/validate_fields_param.rb +0 -0
  39. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/simple_api_validate_params.rb +6 -2
  40. data/lib/jat/plugins/simple_api/simple_api.rb +8 -7
  41. data/lib/jat/plugins/to_str/to_str.rb +5 -1
  42. data/lib/jat/plugins/types/types.rb +55 -0
  43. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  44. data/lib/jat/plugins.rb +41 -15
  45. data/lib/jat.rb +24 -17
  46. data/test/lib/jat/attribute_test.rb +8 -0
  47. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  48. data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
  49. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
  50. data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
  51. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  52. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  53. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  54. data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -9
  55. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
  56. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -11
  57. data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
  58. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  59. data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -1
  60. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +4 -18
  61. 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
  62. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  63. data/test/lib/jat/plugins/{json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +8 -6
  64. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
  65. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  66. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  67. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  68. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +57 -43
  69. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
  70. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  71. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +18 -12
  72. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +33 -6
  73. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +4 -18
  74. 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
  75. data/test/lib/jat/plugins/{simple_api_activerecord → simple_api_preloads}/lib/preloads_test.rb +8 -8
  76. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  77. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
  78. data/test/lib/jat/plugins/types/types_test.rb +83 -0
  79. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  80. data/test/lib/jat/plugins_test.rb +88 -0
  81. data/test/test_helper.rb +8 -4
  82. metadata +98 -82
  83. data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +0 -31
  84. data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +0 -31
@@ -2,10 +2,10 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::LowerCamelCase" do
5
+ describe "Jat::Plugins::BaseLowerCamelCase" do
6
6
  let(:jat_class) do
7
7
  new_class = Class.new(Jat)
8
- new_class.plugin(:_lower_camel_case)
8
+ new_class.plugin(:base_lower_camel_case)
9
9
  new_class
10
10
  end
11
11
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::Preloads" do
5
+ describe "Jat::Plugins::BasePreloads" do
6
6
  let(:jat_class) do
7
- Class.new(Jat) { plugin(:_preloads) }
7
+ Class.new(Jat) { plugin(:base_preloads) }
8
8
  end
9
9
 
10
10
  def attribute(name, opts)
@@ -2,10 +2,10 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::Preloads::FormatUserPreloads" do
6
- before { Jat::Plugins.find_plugin(:_preloads) }
5
+ describe "Jat::Plugins::BasePreloads::FormatUserPreloads" do
6
+ before { Jat::Plugins.find_plugin(:base_preloads) }
7
7
 
8
- let(:format) { Jat::Plugins::Preloads::FormatUserPreloads }
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.find_plugin(:_preloads) }
6
+ before { Jat::Plugins.find_plugin(:base_preloads) }
7
7
 
8
- let(:described_class) { Jat::Plugins::Preloads::PreloadsWithPath }
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: {}}
@@ -23,14 +23,6 @@ describe "Jat::Plugins::JsonApi" do
23
23
  end
24
24
 
25
25
  describe ".after_load" do
26
- it "loads json_api_activerecord plugin if activerecord option provided" do
27
- jat_class = Class.new(Jat)
28
- jat_class.expects(:plugin).with(:json_api_activerecord, activerecord: true)
29
-
30
- plugin.load(jat_class)
31
- plugin.after_load(jat_class, activerecord: true)
32
- end
33
-
34
26
  it "adds config variable with name of response plugin that was loaded" do
35
27
  jat_class = Class.new(Jat)
36
28
  jat_class.plugin(:json_api)
@@ -54,7 +46,7 @@ describe "Jat::Plugins::JsonApi" do
54
46
  describe "#map" do
55
47
  it "returns map for provided context" do
56
48
  jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
57
- assert_equal "MAP", jat_class.map("CONTEXT")
49
+ assert_equal "MAP", jat_class.new("CONTEXT").map
58
50
  end
59
51
  end
60
52
 
@@ -18,21 +18,29 @@ describe "Jat::Plugins::JsonApi::FieldsParamParser" do
18
18
 
19
19
  let(:described_class) { jat_class::FieldsParamParser }
20
20
 
21
- it "returns empty hash when parameters not provided" do
22
- result = described_class.parse(nil)
23
-
24
- assert_equal({}, result)
21
+ describe ".inspect" do
22
+ it "returns self name" do
23
+ assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
24
+ end
25
25
  end
26
26
 
27
- it "returns parsed attributes" do
28
- result = described_class.parse(a: "a1,a2")
27
+ describe ".parse" do
28
+ it "returns empty hash when parameters not provided" do
29
+ result = described_class.parse(nil)
29
30
 
30
- assert_equal({a: %i[a1 a2]}, result)
31
- end
31
+ assert_equal({}, result)
32
+ end
32
33
 
33
- it "validates provided attributes" do
34
- jat_class.plugin :json_api_validate_params
35
- error = assert_raises(Jat::Error) { described_class.parse(a: "a1,a2,a3,a4") }
36
- assert_match(/a4/, error.message)
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
37
45
  end
38
46
  end
@@ -3,11 +3,10 @@
3
3
  require "test_helper"
4
4
 
5
5
  describe "Jat::Plugins::JsonApi::Params::Include" do
6
- before { Jat::Plugins.find_plugin(:json_api) }
7
-
8
6
  let(:base_class) { Class.new(Jat) { plugin :json_api } }
9
7
  let(:a_serializer) { Class.new(base_class) }
10
8
  let(:b_serializer) { Class.new(base_class) }
9
+ let(:c_serializer) { Class.new(base_class) }
11
10
 
12
11
  before do
13
12
  ser = a_serializer
@@ -22,20 +21,35 @@ describe "Jat::Plugins::JsonApi::Params::Include" do
22
21
  ser = b_serializer
23
22
  ser.type :b
24
23
  ser.attribute :b1
25
- ser.relationship :b2, serializer: a_serializer
26
- ser.relationship :b3, serializer: a_serializer
27
- ser.relationship :b4, serializer: a_serializer
24
+ ser.relationship :b2, serializer: c_serializer
25
+ ser.relationship :b3, serializer: c_serializer
26
+ ser.relationship :b4, serializer: c_serializer
27
+
28
+ ser = c_serializer
29
+ ser.type :c
30
+ ser.attribute :c1
31
+ ser.relationship :c2, serializer: a_serializer
32
+ ser.relationship :c3, serializer: a_serializer
33
+ ser.relationship :c4, serializer: a_serializer
28
34
  end
29
35
 
30
36
  let(:described_class) { a_serializer::IncludeParamParser }
31
37
 
32
- it "returns empty hash when param not provided" do
33
- result = described_class.parse(nil)
34
- assert_equal({}, result)
38
+ describe ".inspect" do
39
+ it "returns self name" do
40
+ assert_equal "#{a_serializer}::IncludeParamParser", described_class.inspect
41
+ end
35
42
  end
36
43
 
37
- it "returns typed keys" do
38
- result = described_class.parse("a2.b2,a2.b3,a3.b2.a5,a4")
39
- assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2 b3]}, result)
44
+ describe ".parse" do
45
+ it "returns empty hash when param not provided" do
46
+ result = described_class.parse(nil)
47
+ assert_equal({}, result)
48
+ end
49
+
50
+ it "returns typed keys" do
51
+ result = described_class.parse("a2.b2.c2,a2.b2.c3,a3.b2.c4.a5,a4")
52
+ assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2], c: %i[c2 c3 c4]}, result)
53
+ end
40
54
  end
41
55
  end
@@ -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
@@ -76,7 +76,6 @@ 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
- id { |obj| obj }
80
79
  end
81
80
 
82
81
  str_serializer = Class.new(base_class) do
@@ -486,4 +485,10 @@ describe "Jat::Plugins::JsonApi::Response" do
486
485
  assert_equal({self: "/self", related: "/foo/self"}, links)
487
486
  end
488
487
  end
488
+
489
+ describe ".inspect" do
490
+ it "returns self name" do
491
+ assert_equal "#{base_class}::Response", base_class::Response.inspect
492
+ end
493
+ end
489
494
  end
@@ -13,26 +13,12 @@ describe "Jat::Plugins::JsonApiActiverecord" do
13
13
  assert_match(/json_api/, error.message)
14
14
  end
15
15
 
16
- it "loads other plugins in after_load" do
16
+ it "loads other plugins" do
17
17
  jat_class = Class.new(Jat)
18
18
  jat_class.plugin :json_api
19
+ jat_class.plugin :json_api_activerecord
19
20
 
20
- jat_class.expects(:plugin).with(:_preloads, foo: :bar)
21
- jat_class.expects(:plugin).with(:_activerecord_preloads, foo: :bar)
22
-
23
- @plugin.after_load(jat_class, foo: :bar)
24
- end
25
-
26
- describe "InstanceMethods" do
27
- it "add .preloads method as a delegator to #{@plugin}::Preloads" do
28
- jat_class = Class.new(Jat)
29
- jat_class.plugin :json_api
30
- jat_class.plugin @plugin
31
- jat = jat_class.allocate
32
-
33
- @plugin::Preloads.expects(:call).with(jat).returns("RES")
34
-
35
- assert_equal "RES", jat.preloads
36
- end
21
+ assert jat_class.plugin_used?(:json_api_preloads)
22
+ assert jat_class.plugin_used?(:base_activerecord_preloads)
37
23
  end
38
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
 
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApiPreloads" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:json_api_preloads)
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
+
20
+ jat_class.expects(:plugin).with(:base_preloads, foo: :bar)
21
+
22
+ @plugin.before_load(jat_class, foo: :bar)
23
+ end
24
+
25
+ describe "InstanceMethods" do
26
+ it "adds #preloads method as a delegator to #{@plugin}::Preloads" do
27
+ jat_class = Class.new(Jat)
28
+ jat_class.plugin :json_api
29
+ jat_class.plugin @plugin
30
+ jat = jat_class.allocate
31
+
32
+ @plugin::Preloads.expects(:call).with(jat).returns("RES")
33
+
34
+ assert_equal "RES", jat.preloads
35
+ end
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
51
+ end
@@ -2,18 +2,20 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::JsonApiActiverecord::Preloads" do
6
- let(:base) { Class.new(Jat) { plugin :json_api, activerecord: true } }
5
+ describe "Jat::Plugins::JsonApiPreloads::Preloads" do
6
+ let(:base) do
7
+ Class.new(Jat) do
8
+ plugin :json_api
9
+ plugin :json_api_preloads
10
+ end
11
+ end
7
12
 
8
13
  let(:user_serializer) { Class.new(base) { type(:user) } }
9
14
  let(:profile_serializer) { Class.new(base) { type(:profile) } }
10
15
  let(:email_serializer) { Class.new(base) { type(:email) } }
11
16
 
12
17
  let(:jat_user) { user_serializer.allocate }
13
- let(:jat_profile) { profile_serializer.allocate }
14
- let(:jat_email) { email_serializer.allocate }
15
-
16
- let(:described_class) { Jat::Plugins::JsonApiActiverecord::Preloads }
18
+ let(:described_class) { Jat::Plugins::JsonApiPreloads::Preloads }
17
19
 
18
20
  def define_map(map)
19
21
  jat_user.expects(:map).returns(map)