jat 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jat/attribute.rb +4 -5
- data/lib/jat/plugins/{common/_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +3 -3
- data/lib/jat/plugins/{common/_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +3 -1
- data/lib/jat/plugins/{common/_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +3 -3
- data/lib/jat/plugins/{common/_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +3 -3
- data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
- data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
- data/lib/jat/plugins/json_api/lib/response_piece.rb +2 -2
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +1 -1
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +1 -1
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +1 -1
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +1 -1
- data/lib/jat/plugins/simple_api/simple_api.rb +1 -1
- data/lib/jat/plugins.rb +37 -25
- data/lib/jat.rb +8 -6
- data/test/lib/jat/attribute_test.rb +8 -0
- data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
- data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
- data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
- data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
- data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -1
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -9
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
- data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -0
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +1 -1
- 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
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +16 -2
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
- data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
- data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
- data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +53 -43
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
- data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +10 -2
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +36 -1
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +1 -1
- 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
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +16 -2
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
- data/test/lib/jat/plugins/types/types_test.rb +5 -0
- data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
- data/test/lib/jat/plugins_test.rb +88 -0
- data/test/test_helper.rb +7 -3
- metadata +40 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c266d5784440b62adbb20bb09902f0134ad5e6a06245974f9b15a0557733528d
|
4
|
+
data.tar.gz: 989344e5918b193a2db72a863337c7209aa34ee9e1b28b7bf68f8bd9a9749b9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
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
|
-
|
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
|
7
|
+
module BaseActiverecordPreloads
|
8
8
|
def self.plugin_name
|
9
|
-
:
|
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(
|
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
|
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
|
5
|
+
module BaseLowerCamelCase
|
6
6
|
def self.plugin_name
|
7
|
-
:
|
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(
|
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
|
9
|
+
module BasePreloads
|
10
10
|
def self.plugin_name
|
11
|
-
:
|
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(
|
61
|
+
register_plugin(BasePreloads.plugin_name, BasePreloads)
|
62
62
|
end
|
63
63
|
end
|
@@ -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
|
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
|
151
|
+
return FROZEN_EMPTY_HASH if meta.empty?
|
152
152
|
|
153
153
|
context[:parent_object] = object
|
154
154
|
|
data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb
CHANGED
@@ -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 :
|
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 :
|
15
|
+
jat_class.plugin :base_preloads, **opts
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.load(jat_class, **_opts)
|
data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb
CHANGED
@@ -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 :
|
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 :
|
15
|
+
jat_class.plugin :base_preloads, **opts
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.load(jat_class, **_opts)
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
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
|
-
|
13
|
-
|
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::
|
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(:
|
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::
|
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(:
|
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::
|
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::
|
5
|
+
describe "Jat::Plugins::BaseLowerCamelCase" do
|
6
6
|
let(:jat_class) do
|
7
7
|
new_class = Class.new(Jat)
|
8
|
-
new_class.plugin(:
|
8
|
+
new_class.plugin(:base_lower_camel_case)
|
9
9
|
new_class
|
10
10
|
end
|
11
11
|
|
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
|
-
describe "Jat::Plugins::
|
6
|
-
before { Jat::Plugins.find_plugin(:
|
5
|
+
describe "Jat::Plugins::BasePreloads::FormatUserPreloads" do
|
6
|
+
before { Jat::Plugins.find_plugin(:base_preloads) }
|
7
7
|
|
8
|
-
let(:format) { Jat::Plugins::
|
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(:
|
6
|
+
before { Jat::Plugins.find_plugin(:base_preloads) }
|
7
7
|
|
8
|
-
let(:described_class) { Jat::Plugins::
|
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.
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
28
|
-
|
27
|
+
describe ".parse" do
|
28
|
+
it "returns empty hash when parameters not provided" do
|
29
|
+
result = described_class.parse(nil)
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
assert_equal({}, result)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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:
|
26
|
-
ser.relationship :b3, serializer:
|
27
|
-
ser.relationship :b4, 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
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|