jat 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +36 -4
  3. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +7 -3
  4. data/lib/jat/plugins/_activerecord_preloads/lib/preloader.rb +7 -13
  5. data/lib/jat/plugins/_lower_camel_case/_lower_camel_case.rb +32 -0
  6. data/lib/jat/plugins/_preloads/_preloads.rb +8 -2
  7. data/lib/jat/plugins/cache/cache.rb +9 -5
  8. data/lib/jat/plugins/json_api/json_api.rb +145 -105
  9. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  10. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  11. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  12. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  13. data/lib/jat/plugins/json_api/lib/response.rb +84 -196
  14. data/lib/jat/plugins/json_api/lib/response_piece.rb +166 -0
  15. data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +31 -0
  16. data/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads.rb +16 -24
  17. data/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +30 -0
  18. data/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache.rb +54 -0
  19. data/lib/jat/plugins/json_api_validate_params/json_api_validate_params.rb +57 -0
  20. data/lib/jat/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  21. data/lib/jat/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  22. data/lib/jat/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  23. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  24. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  25. data/lib/jat/plugins/simple_api/lib/response.rb +78 -89
  26. data/lib/jat/plugins/simple_api/lib/response_piece.rb +84 -0
  27. data/lib/jat/plugins/simple_api/simple_api.rb +83 -24
  28. data/lib/jat/plugins/simple_api_activerecord/lib/preloads.rb +55 -0
  29. data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +31 -0
  30. data/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +30 -0
  31. data/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +48 -0
  32. data/lib/jat/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  33. data/lib/jat/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  34. data/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params.rb +45 -0
  35. data/lib/jat/plugins/to_str/to_str.rb +10 -4
  36. data/lib/jat/plugins.rb +3 -16
  37. data/lib/jat.rb +28 -30
  38. data/test/lib/jat/attribute_test.rb +15 -5
  39. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +34 -15
  40. data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +10 -24
  41. data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
  42. data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +1 -1
  43. data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +1 -1
  44. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  45. data/test/lib/jat/plugins/json_api/json_api_test.rb +63 -47
  46. data/test/lib/jat/plugins/json_api/lib/{params/fields_test.rb → fields_param_parser_test.rb} +7 -6
  47. data/test/lib/jat/plugins/json_api/lib/{params/include_test.rb → include_param_parser_test.rb} +4 -4
  48. data/test/lib/jat/plugins/json_api/lib/map_test.rb +150 -79
  49. data/test/lib/jat/plugins/json_api/lib/response_test.rb +32 -32
  50. data/test/lib/jat/plugins/{_json_api_activerecord/_json_api_activerecord_test.rb → json_api_activerecord/json_api_activerecord_test.rb} +14 -5
  51. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads_test.rb +11 -10
  52. data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
  53. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  54. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
  55. data/test/lib/jat/plugins/simple_api/lib/{params/parse_test.rb → fields_param_parser_test.rb} +10 -4
  56. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +111 -34
  57. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +80 -74
  58. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +91 -25
  59. data/test/lib/jat/plugins/simple_api_activerecord/lib/preloads_test.rb +135 -0
  60. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +38 -0
  61. data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
  62. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  63. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
  64. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  65. data/test/lib/jat_test.rb +47 -24
  66. data/test/lib/plugin_test.rb +3 -3
  67. data/test/test_helper.rb +0 -3
  68. data/test/test_plugin.rb +9 -12
  69. metadata +60 -71
  70. data/CHANGELOG.md +0 -7
  71. data/README.md +0 -21
  72. data/jat.gemspec +0 -37
  73. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  74. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  75. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  76. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  77. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  78. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  79. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  80. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  81. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  82. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  83. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  84. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  85. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  86. data/lib/jat/presenter.rb +0 -51
  87. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  88. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  89. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  90. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  91. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  92. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  93. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  94. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  95. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  96. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  97. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  98. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  99. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  100. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  101. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  102. data/test/lib/jat/presenter_test.rb +0 -61
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiActiverecord" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_activerecord)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ it "loads other plugins in after_load" do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :simple_api
19
+
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
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiLowerCamelCase" do
6
+ let(:jat_class) do
7
+ new_class = Class.new(Jat)
8
+ new_class.plugin(:simple_api)
9
+ new_class.plugin(:simple_api_lower_camel_case)
10
+ new_class
11
+ end
12
+
13
+ it "loads _lower_camel_case plugin" do
14
+ assert jat_class.plugin_used?(:_lower_camel_case)
15
+ end
16
+
17
+ it "returns attributes in lowerCamelCase case" do
18
+ jat_class.attribute(:foo_bar) { 1 }
19
+
20
+ response = jat_class.to_h(true)
21
+ assert_equal({fooBar: 1}, response)
22
+ end
23
+
24
+ it "accepts fields in lowerCamelCase format" do
25
+ jat_class.attribute(:foo_bar, exposed: false) { 1 }
26
+
27
+ response = jat_class.to_h(true, fields: "fooBar")
28
+ assert_equal({fooBar: 1}, response)
29
+ end
30
+
31
+ it "returns meta keys in lowerCamelCase format" do
32
+ jat_class.meta(:user_agent) { "Firefox" }
33
+
34
+ response = jat_class.to_h(nil)
35
+ assert_equal({meta: {userAgent: "Firefox"}}, response)
36
+ end
37
+
38
+ it "returns context meta keys in lowerCamelCase format" do
39
+ response = jat_class.to_h(nil, meta: {user_agent: "Firefox"})
40
+ assert_equal({meta: {userAgent: "Firefox"}}, response)
41
+ end
42
+
43
+ it "joins meta in lowerCamelCase format" do
44
+ jat_class.meta(:userAgent) { "Firefox" }
45
+ response = jat_class.to_h(nil, meta: {user_agent: "Firefox 2.0"})
46
+ assert_equal({meta: {userAgent: "Firefox 2.0"}}, response)
47
+ end
48
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiMapsCache" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_maps_cache)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ it "adds config variable how many maps to store per serializer" do
17
+ jat_class = Class.new(Jat)
18
+
19
+ @plugin.after_load(jat_class)
20
+ assert_equal(100, jat_class.config[:cached_maps_count]) # default 100
21
+
22
+ @plugin.after_load(jat_class, cached_maps_count: 10) # change value via opts
23
+ assert_equal(10, jat_class.config[:cached_maps_count])
24
+ end
25
+
26
+ describe "Test maps responses" do
27
+ let(:jat_class) do
28
+ Class.new(Jat) do
29
+ plugin(:simple_api)
30
+ plugin(:simple_api_maps_cache)
31
+
32
+ attribute(:attr1)
33
+ attribute(:attr2)
34
+ end
35
+ end
36
+
37
+ it "returns same maps when requested with same params" do
38
+ full_map1 = jat_class::Map.call(exposed: :all)
39
+ full_map2 = jat_class::Map.call(exposed: :all)
40
+
41
+ exposed_map1 = jat_class::Map.call(exposed: :default)
42
+ exposed_map2 = jat_class::Map.call(exposed: :default)
43
+
44
+ current_map1 = jat_class::Map.call(fields: "attr1")
45
+ current_map2 = jat_class::Map.call(fields: "attr1")
46
+ current_map3 = jat_class::Map.call(fields: "attr2")
47
+
48
+ assert_same(full_map1, full_map2)
49
+ assert_same(exposed_map1, exposed_map2)
50
+ assert_same(current_map1, current_map2)
51
+
52
+ # should not match, fields are not same
53
+ assert !current_map1.equal?(current_map3)
54
+ end
55
+
56
+ it "stores different maps keys" do
57
+ # key 1
58
+ jat_class::Map.call(exposed: :all)
59
+ jat_class::Map.call(exposed: :all)
60
+
61
+ # key 2
62
+ jat_class::Map.call(exposed: :default)
63
+ jat_class::Map.call(exposed: :default)
64
+
65
+ # key 3
66
+ jat_class::Map.call(fields: "attr1")
67
+ jat_class::Map.call(fields: "attr1")
68
+
69
+ # key 4
70
+ jat_class::Map.call(fields: "attr2")
71
+
72
+ # key 5
73
+ jat_class::Map.call(fields: "attr1,attr2")
74
+
75
+ assert_equal 5, jat_class::Map.maps_cache.keys.count
76
+ end
77
+
78
+ it "clears old results when there are too many cache keys" do
79
+ jat_class.config[:cached_maps_count] = 1
80
+
81
+ full_map1 = jat_class::Map.call(exposed: :all)
82
+ full_map2 = jat_class::Map.call(exposed: :all)
83
+
84
+ # ensure maps refer to same object
85
+ assert_same(full_map1, full_map2)
86
+
87
+ # replace single possible cache key with another `exposed` map
88
+ jat_class::Map.call(exposed: :default)
89
+
90
+ # calculate full map again, it should not match
91
+ full_map3 = jat_class::Map.call(exposed: :all)
92
+ assert !full_map1.equal?(full_map3)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiValidateParams" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_validate_params)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ let(:base_serializer) do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :simple_api
19
+ jat_class.plugin @plugin
20
+ jat_class
21
+ end
22
+
23
+ let(:serializer) do
24
+ jat_class = Class.new(base_serializer)
25
+ jat_class.attribute :foo_bar
26
+ jat_class.relationship :foo_bazz, serializer: jat_class
27
+ jat_class
28
+ end
29
+
30
+ let(:serializer_lower_camel_case) do
31
+ jat_class = Class.new(base_serializer)
32
+ jat_class.plugin :lower_camel_case
33
+
34
+ jat_class.attribute :foo_bar
35
+ jat_class.relationship :foo_bazz, serializer: jat_class
36
+ jat_class
37
+ end
38
+
39
+ it "validates fields" do
40
+ jat = serializer.new(fields: "foo_bar,extra")
41
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
42
+ expected_message = "Field 'extra' not exists"
43
+ assert_equal(expected_message, error.message)
44
+ end
45
+
46
+ it "validates deeply nested fields" do
47
+ c = Class.new(base_serializer)
48
+ c.attribute :c1
49
+ c.attribute :c2
50
+
51
+ b = Class.new(base_serializer)
52
+ b.attribute :b1
53
+ b.attribute :b2
54
+ b.relationship :c, serializer: c
55
+
56
+ a = Class.new(base_serializer)
57
+ a.attribute :a1
58
+ a.attribute :a2
59
+ a.relationship :b, serializer: b
60
+ a.relationship :c, serializer: c
61
+
62
+ jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2,c3)")
63
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
64
+ expected_message = "Field 'c3' ('b.c.c3') not exists"
65
+ assert_equal(expected_message, error.message)
66
+ end
67
+
68
+ it "validates deeply nested fields about not existing relationship" do
69
+ c = Class.new(base_serializer)
70
+ c.attribute :c1
71
+ c.attribute :c2
72
+
73
+ b = Class.new(base_serializer)
74
+ b.attribute :b1
75
+ b.attribute :b2
76
+ b.relationship :c, serializer: c
77
+
78
+ a = Class.new(base_serializer)
79
+ a.attribute :a1
80
+ a.attribute :a2
81
+ a.relationship :b, serializer: b
82
+ a.relationship :c, serializer: c
83
+
84
+ jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2(c3))")
85
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
86
+ expected_message = "Field 'c2' ('b.c.c2') is not a relationship to add 'c3' attribute"
87
+ assert_equal(expected_message, error.message)
88
+ end
89
+ end
@@ -7,7 +7,7 @@ describe "Jat::Plugins::ToStr" do
7
7
  new_class = Class.new(Jat)
8
8
  new_class.plugin(:to_str)
9
9
  new_class.class_exec do
10
- def to_h
10
+ def to_h(object)
11
11
  {object => context}
12
12
  end
13
13
  end
@@ -34,7 +34,7 @@ describe "Jat::Plugins::ToStr" do
34
34
  new_class = Class.new(Jat)
35
35
  new_class.plugin(:to_str, to_str: ->(data) { data.inspect })
36
36
  new_class.class_exec do
37
- def to_h
37
+ def to_h(object)
38
38
  {object => context}
39
39
  end
40
40
  end
@@ -45,7 +45,7 @@ describe "Jat::Plugins::ToStr" do
45
45
 
46
46
  describe "#to_str" do
47
47
  it "returns json string of to_h" do
48
- assert_equal '{"obj":"ctx"}', jat_class.new("obj", "ctx").to_str
48
+ assert_equal '{"obj":"ctx"}', jat_class.new("ctx").to_str("obj")
49
49
  end
50
50
  end
51
51
  end
data/test/lib/jat_test.rb CHANGED
@@ -6,19 +6,36 @@ describe Jat do
6
6
  let(:jat_class) { Class.new(Jat) }
7
7
 
8
8
  describe ".plugin" do
9
- it "raises error when trying to load not a symbol or module" do
10
- plugin = "TEST_PLUGIN"
11
- error = assert_raises(Jat::Error) { jat_class.plugin(plugin) }
9
+ it "loads same plugin only once" do
10
+ plugin = Module.new
11
+ jat_class.plugin plugin
12
+ jat_class.plugin plugin
12
13
 
13
- error_message = "Plugin class must be a Symbol or a Module, #{plugin.inspect} given"
14
- assert_equal error_message, error.message
14
+ assert_equal [plugin], jat_class.config[:plugins]
15
+ end
16
+ end
17
+
18
+ describe ".plugin_used?" do
19
+ it "tells if plugin has been already used in current serializer" do
20
+ assert_equal false, jat_class.plugin_used?(:json_api)
21
+ jat_class.plugin(:json_api)
22
+ assert_equal true, jat_class.plugin_used?(:json_api)
23
+ end
24
+
25
+ it "accepts Module" do
26
+ plugin = Module.new
27
+
28
+ assert_equal false, jat_class.plugin_used?(plugin)
29
+ jat_class.plugin(plugin)
30
+ assert_equal true, jat_class.plugin_used?(plugin)
15
31
  end
16
32
  end
17
33
 
18
34
  describe ".config" do
19
- it "returns config object" do
35
+ it "returns config object with default values" do
20
36
  config = jat_class.config
21
37
  assert_equal jat_class::Config, config.class
38
+ assert_equal({plugins: []}, config.opts)
22
39
  end
23
40
  end
24
41
 
@@ -40,11 +57,6 @@ describe Jat do
40
57
  assert_equal parent::Attribute, child::Attribute.superclass
41
58
  assert child.attributes[:foo]
42
59
  end
43
-
44
- it "inherits presenter" do
45
- child = Class.new(parent)
46
- assert_equal parent::Presenter, child::Presenter.superclass
47
- end
48
60
  end
49
61
 
50
62
  describe ".call" do
@@ -54,6 +66,8 @@ describe Jat do
54
66
  end
55
67
 
56
68
  describe ".to_h" do
69
+ let(:jat) { jat_class.allocate }
70
+
57
71
  it "raises error that we should add some response generation plugin" do
58
72
  error = assert_raises(Jat::Error) { jat_class.to_h(nil) }
59
73
 
@@ -61,13 +75,15 @@ describe Jat do
61
75
  assert_equal error_message, error.message
62
76
  end
63
77
 
64
- it "initializes serializer instance with provided attributes" do
65
- jat_class.expects(:new).with("obj", "context")
78
+ it "initializes serializer instance with provided context" do
79
+ jat.expects(:to_h).with("obj")
80
+ jat_class.expects(:new).with("context").returns(jat)
66
81
  jat_class.to_h("obj", "context")
67
82
  end
68
83
 
69
84
  it "adds empty hash context when context not provided" do
70
- jat_class.expects(:new).with("obj", {})
85
+ jat.expects(:to_h).with("obj")
86
+ jat_class.expects(:new).with({}).returns(jat)
71
87
  jat_class.to_h("obj")
72
88
  end
73
89
  end
@@ -84,27 +100,34 @@ describe Jat do
84
100
  attribute = jat_class.attribute :foo
85
101
  assert_equal jat_class.attributes[:foo], attribute
86
102
  end
103
+ end
104
+
105
+ describe ".relationship" do
106
+ it "forces using of :serializer option" do
107
+ error = assert_raises(ArgumentError) { jat_class.relationship(:foo) }
108
+ assert_match "serializer", error.message
109
+ end
87
110
 
88
- it "adds method to Presenter class" do
89
- jat_class.attribute :foo
90
- assert jat_class::Presenter.method_defined?(:foo)
111
+ it "adds new attribute" do
112
+ jat_class.relationship(:foo, serializer: jat_class)
113
+
114
+ attribute = jat_class.attributes[:foo]
115
+ assert_equal jat_class, attribute.serializer
91
116
  end
92
117
  end
93
118
 
94
119
  describe "#initialize" do
95
- it "initializes object and context attributes" do
96
- jat = jat_class.new("obj", "context")
97
-
98
- assert_equal "obj", jat.object
120
+ it "initializes context" do
121
+ jat = jat_class.new("context")
99
122
  assert_equal "context", jat.context
100
123
  end
101
124
  end
102
125
 
103
126
  describe "#to_h" do
104
127
  it "raises error" do
105
- jat = jat_class.new(nil, nil)
128
+ jat = jat_class.new
106
129
 
107
- error = assert_raises(Jat::Error) { jat.to_h }
130
+ error = assert_raises(Jat::Error) { jat.to_h(nil) }
108
131
  error_message = "Method #to_h must be implemented by plugin"
109
132
  assert_equal error_message, error.message
110
133
  end
@@ -112,7 +135,7 @@ describe Jat do
112
135
 
113
136
  describe "#config" do
114
137
  it "returns self class config" do
115
- jat = jat_class.new(nil, nil)
138
+ jat = jat_class.new
116
139
 
117
140
  assert_same jat.config, jat_class.config
118
141
  end
@@ -6,7 +6,7 @@ require "test_plugin"
6
6
  describe "plugin system" do
7
7
  describe "Jat.plugin" do
8
8
  let(:jat_class) { Class.new(Jat) }
9
- let(:components) { [jat_class, jat_class::Attribute, jat_class::Config, jat_class::Presenter] }
9
+ let(:components) { [jat_class, jat_class::Attribute, jat_class::Config] }
10
10
 
11
11
  before do
12
12
  components.each do |component|
@@ -36,12 +36,12 @@ describe "plugin system" do
36
36
  end
37
37
  end
38
38
 
39
- it "loads dependencies before loading plugin" do
39
+ it "calls before_load before loading plugin" do
40
40
  jat_class.plugin(TestPlugin)
41
41
  assert_equal :foo, jat_class.config[:before_load]
42
42
  end
43
43
 
44
- it "after_loads after loading plugin" do
44
+ it "calls after_load after loading plugin" do
45
45
  jat_class.plugin(TestPlugin)
46
46
  assert_equal :plugin_foo, jat_class.config[:after_load]
47
47
  end
data/test/test_helper.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "simplecov"
4
- SimpleCov.start
5
-
6
3
  if RUBY_VERSION >= "2.7"
7
4
  Warning[:deprecated] = true
8
5
  Warning[:experimental] = true
data/test/test_plugin.rb CHANGED
@@ -5,6 +5,15 @@ module TestPlugin
5
5
  jat_class.config[:before_load] = jat_class.foo
6
6
  end
7
7
 
8
+ def self.load(jat_class, **_opts)
9
+ jat_class.extend(ClassMethods)
10
+ jat_class.include(InstanceMethods)
11
+ jat_class::Attribute.extend(AttributeClassMethods)
12
+ jat_class::Attribute.include(AttributeMethods)
13
+ jat_class::Config.extend(ConfigClassMethods)
14
+ jat_class::Config.include(ConfigMethods)
15
+ end
16
+
8
17
  def self.after_load(jat_class, *)
9
18
  jat_class.config[:after_load] = jat_class.foo
10
19
  end
@@ -44,16 +53,4 @@ module TestPlugin
44
53
  :plugin_foo
45
54
  end
46
55
  end
47
-
48
- module PresenterClassMethods
49
- def foo
50
- :plugin_foo
51
- end
52
- end
53
-
54
- module PresenterMethods
55
- def foo
56
- :plugin_foo
57
- end
58
- end
59
56
  end