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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4b14981546d524f665e1d268857bd8b2caafec9af07157a1f57fd2ca8f9c83c
4
- data.tar.gz: 1335c436d0e4ae1f70fb70a9ef5757610e09ce3b5a03b87ba356d31625af0ab3
3
+ metadata.gz: c266d5784440b62adbb20bb09902f0134ad5e6a06245974f9b15a0557733528d
4
+ data.tar.gz: 989344e5918b193a2db72a863337c7209aa34ee9e1b28b7bf68f8bd9a9749b9d
5
5
  SHA512:
6
- metadata.gz: fcaccbe3cb5e5a27cd5700e3677886fb9966ef8ce2775bf2be6638578489747b8a488acbd3f6f7f55752323c40bc92410af1d079dfeefe2d4b41d4f768fbfbfb
7
- data.tar.gz: 23a2cbda699f9e8f2a9588dc8cfb4350bbd1a1994603577bd3649a356148ad88d2bd754529bb63905aba59a993cc747c2e5ad762038fb571d5d83b1ecc6d40b4
6
+ metadata.gz: eba6f92cd51c734ef0583447b2300971fd4707e0e79cece169858d6551e75fba688ce6155dae9317184d423b519e1398d8f583b643174cf4d4450700d73bf3c9
7
+ data.tar.gz: bf14496368b37ec614f0f79b76c217df8c60a8401a54d48e30925fad6c7501fdba7533758b9b5bde24035b645aae2c204b9fc05740282a4ff23ce5e80968e529
data/lib/jat/attribute.rb CHANGED
@@ -9,6 +9,8 @@ class Jat
9
9
  attr_reader :params, :opts
10
10
 
11
11
  def initialize(name:, opts: {}, block: nil)
12
+ check_block_valid(block)
13
+
12
14
  @opts = EnumDeepDup.call(opts)
13
15
  @params = EnumDeepFreeze.call(name: name, opts: @opts, block: block)
14
16
  end
@@ -61,7 +63,7 @@ class Jat
61
63
  def block
62
64
  return @block if instance_variable_defined?(:@block)
63
65
 
64
- current_block = params.fetch(:block).tap { |bl| check_block_valid(bl) if bl }
66
+ current_block = params.fetch(:block)
65
67
  current_block ||= keyword_block
66
68
 
67
69
  @block = current_block
@@ -79,13 +81,10 @@ class Jat
79
81
  end
80
82
 
81
83
  def check_block_valid(block)
82
- raise Error, "Block must be a Proc (not lambda)" if block.lambda?
84
+ return unless block
83
85
 
84
86
  params = block.parameters
85
87
  raise Error, "Block can have 0-2 parameters" if params.count > 2
86
-
87
- valid_params_types = params.all? { |param| param[0] == :opt }
88
- raise Error, "Block parameters must be optional and no keyword parameters" unless valid_params_types
89
88
  end
90
89
  end
91
90
 
@@ -4,9 +4,9 @@ require_relative "./lib/preloader"
4
4
 
5
5
  class Jat
6
6
  module Plugins
7
- module ActiverecordPreloads
7
+ module BaseActiverecordPreloads
8
8
  def self.plugin_name
9
- :_activerecord_preloads
9
+ :base_activerecord_preloads
10
10
  end
11
11
 
12
12
  def self.load(jat_class, **_opts)
@@ -33,6 +33,6 @@ class Jat
33
33
  end
34
34
  end
35
35
 
36
- register_plugin(ActiverecordPreloads.plugin_name, ActiverecordPreloads)
36
+ register_plugin(BaseActiverecordPreloads.plugin_name, BaseActiverecordPreloads)
37
37
  end
38
38
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module ActiverecordPreloads
5
+ module BaseActiverecordPreloads
6
6
  class Preloader
7
7
  module ClassMethods
8
8
  def preload(object, preloads)
@@ -21,6 +21,7 @@ class Jat
21
21
  end
22
22
 
23
23
  class Loader
24
+ # :nocov: We can check only one version of activerecord
24
25
  def self.call(records, associations)
25
26
  if ActiveRecord::VERSION::MAJOR >= 7
26
27
  ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
@@ -28,6 +29,7 @@ class Jat
28
29
  ActiveRecord::Associations::Preloader.new.preload(records, associations)
29
30
  end
30
31
  end
32
+ # :nocov:
31
33
  end
32
34
 
33
35
  class ActiverecordObject
@@ -2,9 +2,9 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module LowerCamelCase
5
+ module BaseLowerCamelCase
6
6
  def self.plugin_name
7
- :_lower_camel_case
7
+ :base_lower_camel_case
8
8
  end
9
9
 
10
10
  def self.load(jat_class, **_opts)
@@ -18,7 +18,7 @@ class Jat
18
18
  end
19
19
  end
20
20
 
21
- register_plugin(LowerCamelCase.plugin_name, LowerCamelCase)
21
+ register_plugin(BaseLowerCamelCase.plugin_name, BaseLowerCamelCase)
22
22
  end
23
23
 
24
24
  class LowerCamelCaseTransformation
@@ -6,9 +6,9 @@ require_relative "./lib/preloads_with_path"
6
6
  # This plugin adds attribute methods #preloads, #preloads_path
7
7
  class Jat
8
8
  module Plugins
9
- module Preloads
9
+ module BasePreloads
10
10
  def self.plugin_name
11
- :_preloads
11
+ :base_preloads
12
12
  end
13
13
 
14
14
  def self.load(jat_class, **_opts)
@@ -58,6 +58,6 @@ class Jat
58
58
  end
59
59
  end
60
60
 
61
- register_plugin(Preloads.plugin_name, Preloads)
61
+ register_plugin(BasePreloads.plugin_name, BasePreloads)
62
62
  end
63
63
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class FormatUserPreloads
7
7
  METHODS = {
8
8
  Array => :array_to_hash,
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class PreloadsWithPath
7
7
  module ClassMethods
8
8
  BANG = "!"
@@ -136,7 +136,7 @@ class Jat
136
136
 
137
137
  def get_relationship_links(rel_serializer, rel_object)
138
138
  links = rel_serializer.relationship_links
139
- return FROZEN_EMPTY_HASH unless links
139
+ return FROZEN_EMPTY_HASH if links.empty?
140
140
 
141
141
  context[:parent_object] = object
142
142
 
@@ -148,7 +148,7 @@ class Jat
148
148
 
149
149
  def get_relationship_meta(rel_serializer, rel_object)
150
150
  meta = rel_serializer.added_relationship_meta
151
- return FROZEN_EMPTY_HASH unless meta
151
+ return FROZEN_EMPTY_HASH if meta.empty?
152
152
 
153
153
  context[:parent_object] = object
154
154
 
@@ -14,7 +14,7 @@ class Jat
14
14
 
15
15
  def self.load(jat_class, **opts)
16
16
  jat_class.plugin :json_api_preloads, **opts
17
- jat_class.plugin :_activerecord_preloads, **opts
17
+ jat_class.plugin :base_activerecord_preloads, **opts
18
18
  end
19
19
  end
20
20
 
@@ -10,7 +10,7 @@ class Jat
10
10
  def self.before_load(jat_class, **_opts)
11
11
  raise Error, "Please load :json_api plugin first" unless jat_class.plugin_used?(:json_api)
12
12
 
13
- jat_class.plugin :_lower_camel_case
13
+ jat_class.plugin :base_lower_camel_case
14
14
  end
15
15
 
16
16
  def self.load(jat_class, **_opts)
@@ -12,7 +12,7 @@ class Jat
12
12
  def self.before_load(jat_class, **opts)
13
13
  raise Error, "Please load :json_api plugin first" unless jat_class.plugin_used?(:json_api)
14
14
 
15
- jat_class.plugin :_preloads, **opts
15
+ jat_class.plugin :base_preloads, **opts
16
16
  end
17
17
 
18
18
  def self.load(jat_class, **_opts)
@@ -14,7 +14,7 @@ class Jat
14
14
 
15
15
  def self.load(jat_class, **opts)
16
16
  jat_class.plugin :simple_api_preloads, **opts
17
- jat_class.plugin :_activerecord_preloads, **opts
17
+ jat_class.plugin :base_activerecord_preloads, **opts
18
18
  end
19
19
  end
20
20
 
@@ -10,7 +10,7 @@ class Jat
10
10
  def self.before_load(jat_class, **_opts)
11
11
  raise Error, "Please load :simple_api plugin first" unless jat_class.plugin_used?(:simple_api)
12
12
 
13
- jat_class.plugin :_lower_camel_case
13
+ jat_class.plugin :base_lower_camel_case
14
14
  end
15
15
 
16
16
  def self.load(jat_class, **_opts)
@@ -12,7 +12,7 @@ class Jat
12
12
  def self.before_load(jat_class, **opts)
13
13
  raise Error, "Please load :simple_api plugin first" unless jat_class.plugin_used?(:simple_api)
14
14
 
15
- jat_class.plugin :_preloads, **opts
15
+ jat_class.plugin :base_preloads, **opts
16
16
  end
17
17
 
18
18
  def self.load(jat_class, **_opts)
@@ -79,7 +79,7 @@ class Jat
79
79
  # Assign same meta
80
80
  added_meta.each_value do |attribute|
81
81
  params = attribute.params
82
- subclass.attribute(params[:name], **params[:opts], &params[:block])
82
+ subclass.meta(params[:name], **params[:opts], &params[:block])
83
83
  end
84
84
  end
85
85
 
data/lib/jat/plugins.rb CHANGED
@@ -1,39 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Jat
4
+ class PluginLoadError < Error; end
5
+
4
6
  module Plugins
5
7
  @plugins = {}
6
8
 
7
- # Register the given plugin with Jat, so that it can be loaded using
8
- # `Jat.plugin` with a symbol. Should be used by plugin files. Example:
9
- #
10
- # Jat::Plugins.register_plugin(:plugin_name, PluginModule)
11
- def self.register_plugin(name, mod)
12
- @plugins[name] = mod
13
- end
9
+ class << self
10
+ # Registers given plugin to be able to load it using symbol name.
11
+ #
12
+ # Example: Jat::Plugins.register_plugin(:plugin_name, PluginModule)
13
+ def register_plugin(name, mod)
14
+ @plugins[name] = mod
15
+ end
14
16
 
15
- # If the registered plugin already exists, use it. Otherwise, require
16
- # and return it. This raises a LoadError if such a plugin doesn't exist,
17
- # or a Jat::Error if it exists but it does not register itself
18
- # correctly.
19
- def self.find_plugin(name)
20
- return name if name.is_a?(Module)
17
+ # If the registered plugin already exists, use it. Otherwise, require
18
+ # and return it. This raises a LoadError if such a plugin doesn't exist,
19
+ # or a Jat::Error if it exists but it does not register itself
20
+ # correctly.
21
+ def find_plugin(name)
22
+ return name if name.is_a?(Module)
23
+ return @plugins[name] if @plugins.key?(name)
21
24
 
22
- begin
23
- require "jat/plugins/#{name}/#{name}"
24
- rescue LoadError
25
- if name.start_with?("json_api")
26
- require "jat/plugins/json_api/plugins/#{name}/#{name}"
27
- elsif name.start_with?("simple_api")
28
- require "jat/plugins/simple_api/plugins/#{name}/#{name}"
29
- elsif name.start_with?("_")
30
- require "jat/plugins/common/#{name}/#{name}"
31
- else
32
- raise
25
+ begin
26
+ require_plugin(name)
27
+ rescue PluginLoadError
28
+ if name.start_with?("json_api")
29
+ require_plugin(name, "/json_api/plugins")
30
+ elsif name.start_with?("simple_api")
31
+ require_plugin(name, "/simple_api/plugins")
32
+ elsif name.start_with?("base")
33
+ require_plugin(name, "/base")
34
+ else
35
+ raise
36
+ end
33
37
  end
38
+
39
+ @plugins[name] || raise(PluginLoadError, "Plugin '#{name}' did not register itself correctly")
34
40
  end
35
41
 
36
- @plugins[name] || raise(Error, "plugin #{name} did not register itself correctly in Jat::Plugins")
42
+ private
43
+
44
+ def require_plugin(name, prefix = nil)
45
+ require "jat/plugins#{prefix}/#{name}/#{name}"
46
+ rescue LoadError
47
+ raise PluginLoadError, "Plugin '#{name}' does not exist"
48
+ end
37
49
  end
38
50
  end
39
51
  end
data/lib/jat.rb CHANGED
@@ -1,17 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "jat/attribute"
4
- require_relative "jat/config"
5
- require_relative "jat/plugins"
6
-
7
3
  # Main namespace
8
4
  class Jat
5
+ # A generic exception used by Jat.
6
+ class Error < StandardError; end
7
+
9
8
  FROZEN_EMPTY_HASH = {}.freeze
10
9
  FROZEN_EMPTY_ARRAY = [].freeze
10
+ end
11
11
 
12
- # A generic exception used by Jat.
13
- class Error < StandardError; end
12
+ require_relative "jat/attribute"
13
+ require_relative "jat/config"
14
+ require_relative "jat/plugins"
14
15
 
16
+ class Jat
15
17
  @config = Config.new({plugins: []})
16
18
 
17
19
  module ClassMethods
@@ -148,5 +148,13 @@ describe Jat::Attribute do
148
148
  attribute = attribute_class.new(name: "foo", opts: {key: :length})
149
149
  assert_equal 3, attribute.value([1, 2, 3], nil)
150
150
  end
151
+
152
+ it "raises error if provide block with more than 2 params" do
153
+ jat_class.attribute(:foo) {}
154
+ jat_class.attribute(:foo) { |_| }
155
+ jat_class.attribute(:foo) { |_, _| }
156
+ err = assert_raises(Jat::Error) { jat_class.attribute(:foo) { |_, _, _| } }
157
+ assert_equal "Block can have 0-2 parameters", err.message
158
+ end
151
159
  end
152
160
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::Activerecord" 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 :activerecord }
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 :activerecord
16
+
17
+ assert new_class.plugin_used?(:simple_api_activerecord)
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 :activerecord
24
+
25
+ assert new_class.plugin_used?(:json_api_activerecord)
26
+ end
27
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::ActiverecordPreloads" do
5
+ describe "Jat::Plugins::BaseActiverecordPreloads" do
6
6
  let(:jat_class) do
7
7
  api_test = api_test()
8
8
 
9
9
  Class.new(Jat) do
10
10
  include api_test
11
- plugin(:_activerecord_preloads)
11
+ plugin(:base_activerecord_preloads)
12
12
  end
13
13
  end
14
14
 
@@ -27,7 +27,7 @@ describe "Jat::Plugins::ActiverecordPreloads" do
27
27
  jat = jat_class.new
28
28
  jat.expects(:preloads).returns(preloads)
29
29
 
30
- Jat::Plugins::ActiverecordPreloads::Preloader
30
+ Jat::Plugins::BaseActiverecordPreloads::Preloader
31
31
  .expects(:preload)
32
32
  .with(object, preloads).returns("OBJ_WITH_PRELOADS")
33
33
 
@@ -4,11 +4,11 @@ require "test_helper"
4
4
  require "support/activerecord"
5
5
 
6
6
  describe "Jat::Plugins::ActiverecordPreloads" do
7
- before { Jat::Plugins.find_plugin(:_activerecord_preloads) }
7
+ before { Jat::Plugins.find_plugin(:base_activerecord_preloads) }
8
8
 
9
9
  describe "Preloader" do
10
10
  let(:described_class) { plugin::Preloader }
11
- let(:plugin) { Jat::Plugins::ActiverecordPreloads }
11
+ let(:plugin) { Jat::Plugins::BaseActiverecordPreloads }
12
12
 
13
13
  describe ".handlers" do
14
14
  it "returns memorized array of handlers" do
@@ -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: {}}
@@ -46,7 +46,7 @@ describe "Jat::Plugins::JsonApi" do
46
46
  describe "#map" do
47
47
  it "returns map for provided context" do
48
48
  jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
49
- assert_equal "MAP", jat_class.map("CONTEXT")
49
+ assert_equal "MAP", jat_class.new("CONTEXT").map
50
50
  end
51
51
  end
52
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
@@ -8,6 +8,7 @@ describe "Jat::Plugins::JsonApi::Params::Include" do
8
8
  let(:base_class) { Class.new(Jat) { plugin :json_api } }
9
9
  let(:a_serializer) { Class.new(base_class) }
10
10
  let(:b_serializer) { Class.new(base_class) }
11
+ let(:c_serializer) { Class.new(base_class) }
11
12
 
12
13
  before do
13
14
  ser = a_serializer
@@ -22,20 +23,35 @@ describe "Jat::Plugins::JsonApi::Params::Include" do
22
23
  ser = b_serializer
23
24
  ser.type :b
24
25
  ser.attribute :b1
25
- ser.relationship :b2, serializer: a_serializer
26
- ser.relationship :b3, serializer: a_serializer
27
- ser.relationship :b4, serializer: a_serializer
26
+ ser.relationship :b2, serializer: c_serializer
27
+ ser.relationship :b3, serializer: c_serializer
28
+ ser.relationship :b4, serializer: c_serializer
29
+
30
+ ser = c_serializer
31
+ ser.type :c
32
+ ser.attribute :c1
33
+ ser.relationship :c2, serializer: a_serializer
34
+ ser.relationship :c3, serializer: a_serializer
35
+ ser.relationship :c4, serializer: a_serializer
28
36
  end
29
37
 
30
38
  let(:described_class) { a_serializer::IncludeParamParser }
31
39
 
32
- it "returns empty hash when param not provided" do
33
- result = described_class.parse(nil)
34
- assert_equal({}, result)
40
+ describe ".inspect" do
41
+ it "returns self name" do
42
+ assert_equal "#{a_serializer}::IncludeParamParser", described_class.inspect
43
+ end
35
44
  end
36
45
 
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)
46
+ describe ".parse" do
47
+ it "returns empty hash when param not provided" do
48
+ result = described_class.parse(nil)
49
+ assert_equal({}, result)
50
+ end
51
+
52
+ it "returns typed keys" do
53
+ result = described_class.parse("a2.b2.c2,a2.b2.c3,a3.b2.c4.a5,a4")
54
+ assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2], c: %i[c2 c3 c4]}, result)
55
+ end
40
56
  end
41
57
  end