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
@@ -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,79 @@ 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 fields started with extra close PAREN" do
54
+ assert_equal({users: {}}, parse(")users)"))
55
+ end
55
56
 
56
- assert_equal(resp, parse(fields))
57
- end
57
+ it "parses single resource with multiple fields" do
58
+ assert_equal({users: {id: {}, name: {}}}, parse("users(id,name)"))
59
+ end
60
+
61
+ it "parses multiple resources with fields" do
62
+ fields = "id,posts(title,text),news(title,text)"
63
+ resp = {
64
+ id: {},
65
+ posts: {title: {}, text: {}},
66
+ news: {title: {}, text: {}}
67
+ }
68
+
69
+ assert_equal(resp, parse(fields))
70
+ end
58
71
 
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: {}
72
+ it "parses included resources" do
73
+ fields = "id,posts(title,text,comments(author(name),comment))"
74
+ resp = {
75
+ id: {},
76
+ posts: {
77
+ title: {},
78
+ text: {},
79
+ comments: {
80
+ author: {
81
+ name: {}
82
+ },
83
+ comment: {}
84
+ }
71
85
  }
72
86
  }
73
- }
74
87
 
75
- assert_equal(resp, parse(fields))
88
+ assert_equal(resp, parse(fields))
89
+ end
76
90
  end
77
91
  end
@@ -45,89 +45,97 @@ describe "Jat::Plugins::SimpleApi::Map" do
45
45
  ser
46
46
  end
47
47
 
48
- it "returns all attributes when {exposed: :all} provided " do
49
- result = described_class.call(exposed: :all)
50
- expected_result = {
51
- a1: {},
52
- a2: {},
53
- a3: {},
54
- b: {b1: {}, b2: {}, b3: {}},
55
- c: {c1: {}, c2: {}, c3: {}},
56
- d: {d1: {}, d2: {}, d3: {}}
57
- }
58
-
59
- assert_equal expected_result, result
48
+ describe ".call" do
49
+ it "returns all attributes when {exposed: :all} provided " do
50
+ result = described_class.call(exposed: :all)
51
+ expected_result = {
52
+ a1: {},
53
+ a2: {},
54
+ a3: {},
55
+ b: {b1: {}, b2: {}, b3: {}},
56
+ c: {c1: {}, c2: {}, c3: {}},
57
+ d: {d1: {}, d2: {}, d3: {}}
58
+ }
59
+
60
+ assert_equal expected_result, result
61
+ end
62
+
63
+ it "returns exposed attributes when {exposed: :default} provided" do
64
+ result = described_class.call(exposed: :default)
65
+ expected_result = {
66
+ a1: {},
67
+ a2: {},
68
+ d: {d1: {}, d2: {}}
69
+ }
70
+
71
+ assert_equal expected_result, result
72
+ end
73
+
74
+ it "returns exposed attributes when no :exposed param provided" do
75
+ result = described_class.call({})
76
+ expected_result = {
77
+ a1: {},
78
+ a2: {},
79
+ d: {d1: {}, d2: {}}
80
+ }
81
+
82
+ assert_equal expected_result, result
83
+ end
84
+
85
+ it "returns no attributes when `{exposed: :none}` provided" do
86
+ result = described_class.call(exposed: :none)
87
+ assert_equal({}, result)
88
+ end
89
+
90
+ it "returns only manually exposed attributes when `{exposed: :none}` type provided" do
91
+ fields = "a2,a3,c(c2,c3),d(d2,d3)"
92
+ result = described_class.call(exposed: :none, fields: fields)
93
+
94
+ expected_result = {
95
+ a2: {},
96
+ a3: {},
97
+ c: {c2: {}, c3: {}},
98
+ d: {d2: {}, d3: {}}
99
+ }
100
+
101
+ assert_equal expected_result, result
102
+ end
103
+
104
+ it "returns combined auto-exposed and manually exposed attributes when `default` type provided" do
105
+ fields = "b(b3),c"
106
+ result = described_class.call(exposed: :default, fields: fields)
107
+ expected_result = {
108
+ a1: {},
109
+ a2: {},
110
+ b: {b1: {}, b2: {}, b3: {}},
111
+ c: {c1: {}, c2: {}},
112
+ d: {d1: {}, d2: {}}
113
+ }
114
+
115
+ assert_equal expected_result, result
116
+ end
117
+
118
+ it "raises error with informative message about recursive serialization" do
119
+ a.relationship :b, serializer: -> { b }, exposed: true
120
+ b.relationship :a, serializer: -> { a }, exposed: true
121
+
122
+ error = assert_raises(Jat::Error) { described_class.call({}) }
123
+ assert_includes "Recursive serialization: b -> a -> b", error.message
124
+ end
125
+
126
+ it "raises error with informative message about recursive serialization through 3 serializers" do
127
+ a.relationship :b, serializer: -> { b }, exposed: true
128
+ b.relationship :c, serializer: -> { c }, exposed: true
129
+ c.relationship :a, serializer: -> { a }, exposed: true
130
+
131
+ error = assert_raises(Jat::Error) { described_class.call({}) }
132
+ assert_equal "Recursive serialization: b -> c -> a -> b", error.message
133
+ end
60
134
  end
61
135
 
62
- it "returns exposed attributes when {exposed: :default} provided" do
63
- result = described_class.call(exposed: :default)
64
- expected_result = {
65
- a1: {},
66
- a2: {},
67
- d: {d1: {}, d2: {}}
68
- }
69
-
70
- assert_equal expected_result, result
71
- end
72
-
73
- it "returns exposed attributes when no :exposed param provided" do
74
- result = described_class.call({})
75
- expected_result = {
76
- a1: {},
77
- a2: {},
78
- d: {d1: {}, d2: {}}
79
- }
80
-
81
- assert_equal expected_result, result
82
- end
83
-
84
- it "returns no attributes when `{exposed: :none}` provided" do
85
- result = described_class.call(exposed: :none)
86
- assert_equal({}, result)
87
- end
88
-
89
- it "returns only manually exposed attributes when `{exposed: :none}` type provided" do
90
- fields = "a2,a3,c(c2,c3),d(d2,d3)"
91
- result = described_class.call(exposed: :none, fields: fields)
92
-
93
- expected_result = {
94
- a2: {},
95
- a3: {},
96
- c: {c2: {}, c3: {}},
97
- d: {d2: {}, d3: {}}
98
- }
99
-
100
- assert_equal expected_result, result
101
- end
102
-
103
- it "returns combined auto-exposed and manually exposed attributes when `default` type provided" do
104
- fields = "b(b3),c"
105
- result = described_class.call(exposed: :default, fields: fields)
106
- expected_result = {
107
- a1: {},
108
- a2: {},
109
- b: {b1: {}, b2: {}, b3: {}},
110
- c: {c1: {}, c2: {}},
111
- d: {d1: {}, d2: {}}
112
- }
113
-
114
- assert_equal expected_result, result
115
- end
116
-
117
- it "raises error with informative message about recursive serialization" do
118
- a.relationship :b, serializer: -> { b }, exposed: true
119
- b.relationship :a, serializer: -> { a }, exposed: true
120
-
121
- error = assert_raises(Jat::Error) { described_class.call({}) }
122
- assert_includes "Recursive serialization: b -> a -> b", error.message
123
- end
124
-
125
- it "raises error with informative message about recursive serialization through 3 serializers" do
126
- a.relationship :b, serializer: -> { b }, exposed: true
127
- b.relationship :c, serializer: -> { c }, exposed: true
128
- c.relationship :a, serializer: -> { a }, exposed: true
129
-
130
- error = assert_raises(Jat::Error) { described_class.call({}) }
131
- assert_equal "Recursive serialization: b -> c -> a -> b", error.message
136
+ describe ".inspect" do
137
+ it "returns self name" do
138
+ assert_equal "#{a}::Map", described_class.inspect
139
+ end
132
140
  end
133
141
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApi::ResponsePiece" do
6
+ let(:jat_class) { Class.new(Jat) { plugin :simple_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
@@ -13,7 +13,7 @@ describe "Jat::Plugins::SimpleApi::Response" do
13
13
 
14
14
  it "returns correct structure with data" do
15
15
  str_serializer = Class.new(base_class) do
16
- attribute(:id) { |_| "STRING" }
16
+ attribute(:id) { |obj| obj }
17
17
  end
18
18
 
19
19
  assert_equal({id: "STRING"}, str_serializer.to_h("STRING"))
@@ -92,7 +92,8 @@ describe "Jat::Plugins::SimpleApi::Response" do
92
92
  end
93
93
 
94
94
  assert_equal({foo: {id: "1"}}, str_serializer.to_h("1", root: :foo))
95
- assert_equal({foo: [{id: "1"}]}, str_serializer.to_h(["1"], root: :foo))
95
+ assert_equal({foo: {id: "1"}}, str_serializer.to_h("1", root: "foo"))
96
+ assert_equal({id: "1"}, str_serializer.to_h("1", root: nil))
96
97
  end
97
98
 
98
99
  it "returns correct structure with data multiple attributes" do
@@ -131,13 +132,11 @@ describe "Jat::Plugins::SimpleApi::Response" do
131
132
  end
132
133
 
133
134
  it "does not return has-one relationship when not exposed" do
134
- int_serializer = Class.new(base_class) do
135
- attribute(:id) { |obj| obj }
136
- end
135
+ nested_serializer = Class.new(base_class)
137
136
 
138
137
  str_serializer = Class.new(base_class) do
139
138
  attribute(:id) { |obj| obj[0] }
140
- attribute :length, serializer: int_serializer, exposed: false
139
+ attribute :length, serializer: nested_serializer, exposed: false
141
140
  end
142
141
 
143
142
  assert_equal({id: "S"}, str_serializer.to_h("STRING"))
@@ -242,12 +241,12 @@ describe "Jat::Plugins::SimpleApi::Response" do
242
241
  # All fields are not exposed in this serializers,
243
242
  # We will show only attributes provided in `fields` param
244
243
  chr_serializer = Class.new(base_class) do
245
- attribute(:id, exposed: false) { |obj| obj }
244
+ attribute(:id, exposed: false)
246
245
  attribute :next, exposed: false
247
246
  end
248
247
 
249
248
  str_serializer = Class.new(base_class) do
250
- attribute(:id, exposed: false) { |obj| obj }
249
+ attribute(:id, exposed: false)
251
250
  attribute :chars, serializer: chr_serializer, many: true, exposed: false
252
251
  end
253
252
 
@@ -269,7 +268,8 @@ describe "Jat::Plugins::SimpleApi::Response" do
269
268
  empty_serializer.meta_key :metadata
270
269
  empty_serializer.meta(:foo) { :bar }
271
270
 
272
- assert_equal({metadata: {foo: :bar, any: :thing}}, empty_serializer.to_h(nil, meta: {any: :thing}))
271
+ assert_equal({metadata: {foo: :bar}}, empty_serializer.to_h(nil))
272
+ assert_equal({new_meta: {foo: :bar}}, empty_serializer.to_h(nil, meta_key: "new_meta"))
273
273
  end
274
274
 
275
275
  it "returns correct structure with data and meta" do
@@ -306,14 +306,14 @@ describe "Jat::Plugins::SimpleApi::Response" do
306
306
  it "does not overwrite manually added meta" do
307
307
  str_serializer = Class.new(base_class) do
308
308
  root(:root)
309
- meta(:version) { "1.2.3" }
309
+ meta(:length)
310
310
 
311
311
  attribute(:id) { |obj| obj }
312
312
  end
313
313
 
314
314
  assert_equal(
315
- {root: {id: "1"}, meta: {version: "2.0.0"}},
316
- str_serializer.to_h("1", meta: {version: "2.0.0"})
315
+ {root: {id: "1"}, meta: {length: "33"}},
316
+ str_serializer.to_h("1", meta: {length: "33"})
317
317
  )
318
318
  end
319
319
 
@@ -345,4 +345,10 @@ describe "Jat::Plugins::SimpleApi::Response" do
345
345
  assert_equal({root: {id: "1"}}, str_serializer.to_h("1", bazz: nil))
346
346
  end
347
347
  end
348
+
349
+ describe ".inspect" do
350
+ it "returns self name" do
351
+ assert_equal "#{base_class}::Response", base_class::Response.inspect
352
+ end
353
+ end
348
354
  end
@@ -15,15 +15,17 @@ describe "Jat::Plugins::SimpleApi" do
15
15
 
16
16
  let(:plugin) { @plugin }
17
17
 
18
- describe ".after_load" do
19
- it "loads simple_api_activerecord plugin if activerecord option provided" do
18
+ describe ".before_load" do
19
+ it "raises error if response plugin was already loaded" do
20
20
  jat_class = Class.new(Jat)
21
- jat_class.expects(:plugin).with(:simple_api_activerecord, activerecord: true)
21
+ jat_class.config[:response_plugin_loaded] = :foobar
22
22
 
23
- plugin.load(jat_class)
24
- plugin.after_load(jat_class, activerecord: true)
23
+ err = assert_raises(Jat::Error) { jat_class.plugin(:simple_api) }
24
+ assert_equal("Response plugin `foobar` was already loaded before", err.message)
25
25
  end
26
+ end
26
27
 
28
+ describe ".after_load" do
27
29
  it "adds default `:meta` meta_key config option" do
28
30
  jat_class = Class.new(Jat)
29
31
  assert_nil jat_class.config[:meta_key]
@@ -54,7 +56,7 @@ describe "Jat::Plugins::SimpleApi" do
54
56
  describe "#map" do
55
57
  it "returns map for provided context" do
56
58
  jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
57
- assert_equal "MAP", jat_class.map("CONTEXT")
59
+ assert_equal "MAP", jat_class.new("CONTEXT").map
58
60
  end
59
61
  end
60
62
 
@@ -143,5 +145,30 @@ describe "Jat::Plugins::SimpleApi" do
143
145
  assert_equal :metadata, jat_class.config[:meta_key]
144
146
  end
145
147
  end
148
+
149
+ describe ".inherited" do
150
+ it "inherits root" do
151
+ jat_class.root(:foo)
152
+ child = Class.new(jat_class)
153
+ assert_equal :foo, child.config[:root_one]
154
+ assert_equal :foo, child.config[:root_many]
155
+ end
156
+
157
+ it "inherits meta" do
158
+ jat_class.meta(:foo) { "bar" }
159
+ child = Class.new(jat_class)
160
+
161
+ assert_equal "bar", child.added_meta[:foo].value(nil, nil)
162
+ end
163
+
164
+ it "does not change parents meta when children meta changed" do
165
+ jat_class.meta(:foo) { "foo" }
166
+ child = Class.new(jat_class)
167
+ child.meta(:foo) { "bazz" }
168
+
169
+ assert_equal("foo", jat_class.added_meta[:foo].value(nil, nil))
170
+ assert_equal("bazz", child.added_meta[:foo].value(nil, nil))
171
+ end
172
+ end
146
173
  end
147
174
  end
@@ -13,26 +13,12 @@ describe "Jat::Plugins::SimpleApiActiverecord" do
13
13
  assert_match(/simple_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 :simple_api
19
+ jat_class.plugin :simple_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 :simple_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?(:simple_api_preloads)
22
+ assert jat_class.plugin_used?(:base_activerecord_preloads)
37
23
  end
38
24
  end
@@ -10,8 +10,14 @@ describe "Jat::Plugins::SimpleApiLowerCamelCase" do
10
10
  new_class
11
11
  end
12
12
 
13
+ it "checks simple_api plugin loaded before" do
14
+ jat_class = Class.new(Jat)
15
+ error = assert_raises(Jat::Error) { jat_class.plugin :simple_api_lower_camel_case }
16
+ assert_match(/simple_api/, error.message)
17
+ end
18
+
13
19
  it "loads _lower_camel_case plugin" do
14
- assert jat_class.plugin_used?(:_lower_camel_case)
20
+ assert jat_class.plugin_used?(:base_lower_camel_case)
15
21
  end
16
22
 
17
23
  it "returns attributes in lowerCamelCase case" do