cuprum-collections 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +48 -0
- data/DEVELOPMENT.md +2 -2
- data/README.md +13 -11
- data/lib/cuprum/collections/association.rb +256 -0
- data/lib/cuprum/collections/associations/belongs_to.rb +32 -0
- data/lib/cuprum/collections/associations/has_many.rb +23 -0
- data/lib/cuprum/collections/associations/has_one.rb +23 -0
- data/lib/cuprum/collections/associations.rb +10 -0
- data/lib/cuprum/collections/basic/collection.rb +39 -74
- data/lib/cuprum/collections/basic/commands/find_many.rb +1 -1
- data/lib/cuprum/collections/basic/commands/find_matching.rb +1 -1
- data/lib/cuprum/collections/basic/repository.rb +9 -33
- data/lib/cuprum/collections/basic.rb +1 -0
- data/lib/cuprum/collections/collection.rb +154 -0
- data/lib/cuprum/collections/commands/associations/find_many.rb +161 -0
- data/lib/cuprum/collections/commands/associations/require_many.rb +48 -0
- data/lib/cuprum/collections/commands/associations.rb +13 -0
- data/lib/cuprum/collections/commands/find_one_matching.rb +1 -1
- data/lib/cuprum/collections/commands.rb +1 -0
- data/lib/cuprum/collections/errors/abstract_find_error.rb +1 -1
- data/lib/cuprum/collections/relation.rb +401 -0
- data/lib/cuprum/collections/repository.rb +71 -4
- data/lib/cuprum/collections/resource.rb +65 -0
- data/lib/cuprum/collections/rspec/contracts/association_contracts.rb +2137 -0
- data/lib/cuprum/collections/rspec/contracts/basic/command_contracts.rb +484 -0
- data/lib/cuprum/collections/rspec/contracts/basic.rb +11 -0
- data/lib/cuprum/collections/rspec/contracts/collection_contracts.rb +429 -0
- data/lib/cuprum/collections/rspec/contracts/command_contracts.rb +1462 -0
- data/lib/cuprum/collections/rspec/contracts/query_contracts.rb +1093 -0
- data/lib/cuprum/collections/rspec/contracts/relation_contracts.rb +1381 -0
- data/lib/cuprum/collections/rspec/contracts/repository_contracts.rb +605 -0
- data/lib/cuprum/collections/rspec/contracts.rb +23 -0
- data/lib/cuprum/collections/rspec/fixtures.rb +85 -82
- data/lib/cuprum/collections/rspec.rb +4 -1
- data/lib/cuprum/collections/version.rb +1 -1
- data/lib/cuprum/collections.rb +9 -4
- metadata +23 -19
- data/lib/cuprum/collections/base.rb +0 -11
- data/lib/cuprum/collections/basic/rspec/command_contract.rb +0 -392
- data/lib/cuprum/collections/rspec/assign_one_command_contract.rb +0 -168
- data/lib/cuprum/collections/rspec/build_one_command_contract.rb +0 -93
- data/lib/cuprum/collections/rspec/collection_contract.rb +0 -190
- data/lib/cuprum/collections/rspec/destroy_one_command_contract.rb +0 -108
- data/lib/cuprum/collections/rspec/find_many_command_contract.rb +0 -407
- data/lib/cuprum/collections/rspec/find_matching_command_contract.rb +0 -194
- data/lib/cuprum/collections/rspec/find_one_command_contract.rb +0 -157
- data/lib/cuprum/collections/rspec/insert_one_command_contract.rb +0 -84
- data/lib/cuprum/collections/rspec/query_builder_contract.rb +0 -92
- data/lib/cuprum/collections/rspec/query_contract.rb +0 -650
- data/lib/cuprum/collections/rspec/querying_contract.rb +0 -298
- data/lib/cuprum/collections/rspec/repository_contract.rb +0 -235
- data/lib/cuprum/collections/rspec/update_one_command_contract.rb +0 -80
- data/lib/cuprum/collections/rspec/validate_one_command_contract.rb +0 -96
@@ -1,190 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rspec/sleeping_king_studios/contract'
|
4
|
-
|
5
|
-
require 'cuprum/collections/rspec'
|
6
|
-
|
7
|
-
module Cuprum::Collections::RSpec
|
8
|
-
# Contract validating the behavior of a Collection.
|
9
|
-
module CollectionContract
|
10
|
-
extend RSpec::SleepingKingStudios::Contract
|
11
|
-
|
12
|
-
# @!method apply(example_group)
|
13
|
-
# Adds the contract to the example group.
|
14
|
-
#
|
15
|
-
# @param example_group [RSpec::Core::ExampleGroup] The example group to
|
16
|
-
# which the contract is applied.
|
17
|
-
|
18
|
-
contract do
|
19
|
-
shared_examples 'should define the command' \
|
20
|
-
do |command_name, command_class|
|
21
|
-
tools = SleepingKingStudios::Tools::Toolbelt.instance
|
22
|
-
class_name = tools.str.camelize(command_name)
|
23
|
-
|
24
|
-
describe "::#{class_name}" do
|
25
|
-
let(:constructor_options) { {} }
|
26
|
-
let(:command) do
|
27
|
-
collection.const_get(class_name).new(**constructor_options)
|
28
|
-
end
|
29
|
-
|
30
|
-
it { expect(collection).to define_constant(class_name) }
|
31
|
-
|
32
|
-
it { expect(collection.const_get(class_name)).to be_a Class }
|
33
|
-
|
34
|
-
it { expect(collection.const_get(class_name)).to be < command_class }
|
35
|
-
|
36
|
-
command_options.each do |option_name|
|
37
|
-
it "should set the ##{option_name}" do
|
38
|
-
expect(command.send(option_name))
|
39
|
-
.to be == collection.send(option_name)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'with options' do
|
44
|
-
let(:constructor_options) do
|
45
|
-
{
|
46
|
-
data: [],
|
47
|
-
member_name: 'tome'
|
48
|
-
}
|
49
|
-
end
|
50
|
-
|
51
|
-
command_options.each do |option_name|
|
52
|
-
it "should set the ##{option_name}" do
|
53
|
-
expect(command.send(option_name)).to(
|
54
|
-
be == constructor_options.fetch(option_name) do
|
55
|
-
collection.send(option_name)
|
56
|
-
end
|
57
|
-
)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "##{command_name}" do
|
64
|
-
let(:constructor_options) { {} }
|
65
|
-
let(:command) do
|
66
|
-
collection.send(command_name, **constructor_options)
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should define the command' do
|
70
|
-
expect(collection)
|
71
|
-
.to respond_to(command_name)
|
72
|
-
.with(0).arguments
|
73
|
-
.and_any_keywords
|
74
|
-
end
|
75
|
-
|
76
|
-
it { expect(command).to be_a collection.const_get(class_name) }
|
77
|
-
|
78
|
-
command_options.each do |option_name|
|
79
|
-
it "should set the ##{option_name}" do
|
80
|
-
expect(command.send(option_name))
|
81
|
-
.to be == collection.send(option_name)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe 'with options' do
|
86
|
-
let(:constructor_options) do
|
87
|
-
{
|
88
|
-
data: [],
|
89
|
-
member_name: 'tome'
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
command_options.each do |option_name|
|
94
|
-
it "should set the ##{option_name}" do
|
95
|
-
expect(command.send(option_name)).to(
|
96
|
-
be == constructor_options.fetch(option_name) do
|
97
|
-
collection.send(option_name)
|
98
|
-
end
|
99
|
-
)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
include_examples 'should define the command',
|
107
|
-
:assign_one,
|
108
|
-
commands_namespace::AssignOne
|
109
|
-
|
110
|
-
include_examples 'should define the command',
|
111
|
-
:build_one,
|
112
|
-
commands_namespace::BuildOne
|
113
|
-
|
114
|
-
include_examples 'should define the command',
|
115
|
-
:destroy_one,
|
116
|
-
commands_namespace::DestroyOne
|
117
|
-
|
118
|
-
include_examples 'should define the command',
|
119
|
-
:find_many,
|
120
|
-
commands_namespace::FindMany
|
121
|
-
|
122
|
-
include_examples 'should define the command',
|
123
|
-
:find_matching,
|
124
|
-
commands_namespace::FindMatching
|
125
|
-
|
126
|
-
include_examples 'should define the command',
|
127
|
-
:find_one,
|
128
|
-
commands_namespace::FindOne
|
129
|
-
|
130
|
-
include_examples 'should define the command',
|
131
|
-
:insert_one,
|
132
|
-
commands_namespace::InsertOne
|
133
|
-
|
134
|
-
include_examples 'should define the command',
|
135
|
-
:update_one,
|
136
|
-
commands_namespace::UpdateOne
|
137
|
-
|
138
|
-
include_examples 'should define the command',
|
139
|
-
:validate_one,
|
140
|
-
commands_namespace::ValidateOne
|
141
|
-
|
142
|
-
describe '#collection_name' do
|
143
|
-
include_examples 'should define reader',
|
144
|
-
:collection_name,
|
145
|
-
-> { an_instance_of(String) }
|
146
|
-
end
|
147
|
-
|
148
|
-
describe '#count' do
|
149
|
-
it { expect(collection).to respond_to(:count).with(0).arguments }
|
150
|
-
|
151
|
-
it { expect(collection).to have_aliased_method(:count).as(:size) }
|
152
|
-
|
153
|
-
it { expect(collection.count).to be 0 }
|
154
|
-
|
155
|
-
wrap_context 'when the collection has many items' do
|
156
|
-
it { expect(collection.count).to be items.count }
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
describe '#qualified_name' do
|
161
|
-
include_examples 'should define reader',
|
162
|
-
:qualified_name,
|
163
|
-
-> { an_instance_of(String) }
|
164
|
-
end
|
165
|
-
|
166
|
-
describe '#query' do
|
167
|
-
let(:default_order) { defined?(super()) ? super() : {} }
|
168
|
-
let(:query) { collection.query }
|
169
|
-
|
170
|
-
it { expect(collection).to respond_to(:query).with(0).arguments }
|
171
|
-
|
172
|
-
it { expect(collection.query).to be_a query_class }
|
173
|
-
|
174
|
-
it 'should set the query options' do
|
175
|
-
query_options.each do |option, value|
|
176
|
-
expect(collection.query.send option).to be == value
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
it { expect(query.criteria).to be == [] }
|
181
|
-
|
182
|
-
it { expect(query.limit).to be nil }
|
183
|
-
|
184
|
-
it { expect(query.offset).to be nil }
|
185
|
-
|
186
|
-
it { expect(query.order).to be == default_order }
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
@@ -1,108 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'cuprum/collections/rspec'
|
4
|
-
|
5
|
-
module Cuprum::Collections::RSpec
|
6
|
-
# Contract validating the behavior of a FindOne command implementation.
|
7
|
-
DESTROY_ONE_COMMAND_CONTRACT = lambda do
|
8
|
-
describe '#call' do
|
9
|
-
let(:mapped_data) do
|
10
|
-
defined?(super()) ? super() : data
|
11
|
-
end
|
12
|
-
let(:primary_key_name) { defined?(super()) ? super() : :id }
|
13
|
-
let(:primary_key_type) { defined?(super()) ? super() : Integer }
|
14
|
-
let(:invalid_primary_key_value) do
|
15
|
-
defined?(super()) ? super() : 100
|
16
|
-
end
|
17
|
-
let(:valid_primary_key_value) do
|
18
|
-
defined?(super()) ? super() : 0
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should validate the :primary_key keyword' do
|
22
|
-
expect(command)
|
23
|
-
.to validate_parameter(:call, :primary_key)
|
24
|
-
.using_constraint(primary_key_type)
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 'with an invalid primary key' do
|
28
|
-
let(:primary_key) { invalid_primary_key_value }
|
29
|
-
let(:expected_error) do
|
30
|
-
Cuprum::Collections::Errors::NotFound.new(
|
31
|
-
attribute_name: primary_key_name,
|
32
|
-
attribute_value: primary_key,
|
33
|
-
collection_name: collection_name,
|
34
|
-
primary_key: true
|
35
|
-
)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should return a failing result' do
|
39
|
-
expect(command.call(primary_key: primary_key))
|
40
|
-
.to be_a_failing_result
|
41
|
-
.with_error(expected_error)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should not remove an entity from the collection' do
|
45
|
-
expect { command.call(primary_key: primary_key) }
|
46
|
-
.not_to(change { query.reset.count })
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'when the collection has many items' do
|
51
|
-
let(:data) { fixtures_data }
|
52
|
-
let(:matching_data) do
|
53
|
-
mapped_data.find { |item| item[primary_key_name.to_s] == primary_key }
|
54
|
-
end
|
55
|
-
let!(:expected_data) do
|
56
|
-
defined?(super()) ? super() : matching_data
|
57
|
-
end
|
58
|
-
|
59
|
-
describe 'with an invalid primary key' do
|
60
|
-
let(:primary_key) { invalid_primary_key_value }
|
61
|
-
let(:expected_error) do
|
62
|
-
Cuprum::Collections::Errors::NotFound.new(
|
63
|
-
attribute_name: primary_key_name,
|
64
|
-
attribute_value: primary_key,
|
65
|
-
collection_name: collection_name,
|
66
|
-
primary_key: true
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should return a failing result' do
|
71
|
-
expect(command.call(primary_key: primary_key))
|
72
|
-
.to be_a_failing_result
|
73
|
-
.with_error(expected_error)
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'should not remove an entity from the collection' do
|
77
|
-
expect { command.call(primary_key: primary_key) }
|
78
|
-
.not_to(change { query.reset.count })
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe 'with a valid primary key' do
|
83
|
-
let(:primary_key) { valid_primary_key_value }
|
84
|
-
|
85
|
-
it 'should return a passing result' do
|
86
|
-
expect(command.call(primary_key: primary_key))
|
87
|
-
.to be_a_passing_result
|
88
|
-
.with_value(expected_data)
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'should remove an entity from the collection' do
|
92
|
-
expect { command.call(primary_key: primary_key) }
|
93
|
-
.to(
|
94
|
-
change { query.reset.count }.by(-1)
|
95
|
-
)
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should remove the entity from the collection' do
|
99
|
-
command.call(primary_key: primary_key)
|
100
|
-
|
101
|
-
expect(query.map { |item| item[primary_key_name.to_s] })
|
102
|
-
.not_to include primary_key
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
@@ -1,407 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'cuprum/collections/rspec'
|
4
|
-
|
5
|
-
module Cuprum::Collections::RSpec
|
6
|
-
# Contract validating the behavior of a FindMany command implementation.
|
7
|
-
FIND_MANY_COMMAND_CONTRACT = lambda do
|
8
|
-
describe '#call' do
|
9
|
-
let(:mapped_data) do
|
10
|
-
defined?(super()) ? super() : data
|
11
|
-
end
|
12
|
-
let(:primary_key_name) { defined?(super()) ? super() : :id }
|
13
|
-
let(:primary_key_type) { defined?(super()) ? super() : Integer }
|
14
|
-
let(:primary_keys_contract) do
|
15
|
-
Stannum::Constraints::Types::ArrayType.new(item_type: primary_key_type)
|
16
|
-
end
|
17
|
-
let(:invalid_primary_key_values) do
|
18
|
-
defined?(super()) ? super() : [100, 101, 102]
|
19
|
-
end
|
20
|
-
let(:valid_primary_key_values) do
|
21
|
-
defined?(super()) ? super() : [0, 1, 2]
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should validate the :allow_partial keyword' do
|
25
|
-
expect(command)
|
26
|
-
.to validate_parameter(:call, :allow_partial)
|
27
|
-
.using_constraint(Stannum::Constraints::Boolean.new)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'should validate the :envelope keyword' do
|
31
|
-
expect(command)
|
32
|
-
.to validate_parameter(:call, :envelope)
|
33
|
-
.using_constraint(Stannum::Constraints::Boolean.new)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should validate the :primary_keys keyword' do
|
37
|
-
expect(command)
|
38
|
-
.to validate_parameter(:call, :primary_keys)
|
39
|
-
.using_constraint(Array)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should validate the :primary_keys keyword items' do
|
43
|
-
expect(command)
|
44
|
-
.to validate_parameter(:call, :primary_keys)
|
45
|
-
.with_value([nil])
|
46
|
-
.using_constraint(primary_keys_contract)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should validate the :scope keyword' do
|
50
|
-
expect(command)
|
51
|
-
.to validate_parameter(:call, :scope)
|
52
|
-
.using_constraint(
|
53
|
-
Stannum::Constraints::Type.new(query.class, optional: true)
|
54
|
-
)
|
55
|
-
.with_value(Object.new.freeze)
|
56
|
-
end
|
57
|
-
|
58
|
-
describe 'with an array of invalid primary keys' do
|
59
|
-
let(:primary_keys) { invalid_primary_key_values }
|
60
|
-
let(:expected_error) do
|
61
|
-
Cuprum::Errors::MultipleErrors.new(
|
62
|
-
errors: primary_keys.map do |primary_key|
|
63
|
-
Cuprum::Collections::Errors::NotFound.new(
|
64
|
-
attribute_name: primary_key_name,
|
65
|
-
attribute_value: primary_key,
|
66
|
-
collection_name: command.collection_name,
|
67
|
-
primary_key: true
|
68
|
-
)
|
69
|
-
end
|
70
|
-
)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should return a failing result' do
|
74
|
-
expect(command.call(primary_keys: primary_keys))
|
75
|
-
.to be_a_failing_result
|
76
|
-
.with_error(expected_error)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'when the collection has many items' do
|
81
|
-
let(:data) { fixtures_data }
|
82
|
-
let(:matching_data) do
|
83
|
-
primary_keys
|
84
|
-
.map do |key|
|
85
|
-
mapped_data.find { |item| item[primary_key_name.to_s] == key }
|
86
|
-
end
|
87
|
-
end
|
88
|
-
let(:expected_data) do
|
89
|
-
defined?(super()) ? super() : matching_data
|
90
|
-
end
|
91
|
-
|
92
|
-
describe 'with an array of invalid primary keys' do
|
93
|
-
let(:primary_keys) { invalid_primary_key_values }
|
94
|
-
let(:expected_error) do
|
95
|
-
Cuprum::Errors::MultipleErrors.new(
|
96
|
-
errors: primary_keys.map do |primary_key|
|
97
|
-
Cuprum::Collections::Errors::NotFound.new(
|
98
|
-
attribute_name: primary_key_name,
|
99
|
-
attribute_value: primary_key,
|
100
|
-
collection_name: command.collection_name,
|
101
|
-
primary_key: true
|
102
|
-
)
|
103
|
-
end
|
104
|
-
)
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'should return a failing result' do
|
108
|
-
expect(command.call(primary_keys: primary_keys))
|
109
|
-
.to be_a_failing_result
|
110
|
-
.with_error(expected_error)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe 'with a partially valid array of primary keys' do
|
115
|
-
let(:primary_keys) do
|
116
|
-
invalid_primary_key_values + valid_primary_key_values
|
117
|
-
end
|
118
|
-
let(:expected_error) do
|
119
|
-
Cuprum::Errors::MultipleErrors.new(
|
120
|
-
errors: primary_keys.map do |primary_key|
|
121
|
-
next nil unless invalid_primary_key_values.include?(primary_key)
|
122
|
-
|
123
|
-
Cuprum::Collections::Errors::NotFound.new(
|
124
|
-
attribute_name: primary_key_name,
|
125
|
-
attribute_value: primary_key,
|
126
|
-
collection_name: command.collection_name,
|
127
|
-
primary_key: true
|
128
|
-
)
|
129
|
-
end
|
130
|
-
)
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'should return a failing result' do
|
134
|
-
expect(command.call(primary_keys: primary_keys))
|
135
|
-
.to be_a_failing_result
|
136
|
-
.with_error(expected_error)
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
describe 'with a valid array of primary keys' do
|
141
|
-
let(:primary_keys) { valid_primary_key_values }
|
142
|
-
|
143
|
-
it 'should return a passing result' do
|
144
|
-
expect(command.call(primary_keys: primary_keys))
|
145
|
-
.to be_a_passing_result
|
146
|
-
.with_value(expected_data)
|
147
|
-
end
|
148
|
-
|
149
|
-
describe 'with an ordered array of primary keys' do
|
150
|
-
let(:primary_keys) { valid_primary_key_values.reverse }
|
151
|
-
|
152
|
-
it 'should return a passing result' do
|
153
|
-
expect(command.call(primary_keys: primary_keys))
|
154
|
-
.to be_a_passing_result
|
155
|
-
.with_value(expected_data)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
describe 'with allow_partial: true' do
|
161
|
-
describe 'with an array of invalid primary keys' do
|
162
|
-
let(:primary_keys) { invalid_primary_key_values }
|
163
|
-
let(:expected_error) do
|
164
|
-
Cuprum::Errors::MultipleErrors.new(
|
165
|
-
errors: invalid_primary_key_values.map do |primary_key|
|
166
|
-
Cuprum::Collections::Errors::NotFound.new(
|
167
|
-
attribute_name: primary_key_name,
|
168
|
-
attribute_value: primary_key,
|
169
|
-
collection_name: command.collection_name,
|
170
|
-
primary_key: true
|
171
|
-
)
|
172
|
-
end
|
173
|
-
)
|
174
|
-
end
|
175
|
-
|
176
|
-
it 'should return a failing result' do
|
177
|
-
expect(command.call(primary_keys: primary_keys))
|
178
|
-
.to be_a_failing_result
|
179
|
-
.with_error(expected_error)
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
describe 'with a partially valid array of primary keys' do
|
184
|
-
let(:primary_keys) do
|
185
|
-
invalid_primary_key_values + valid_primary_key_values
|
186
|
-
end
|
187
|
-
let(:expected_error) do
|
188
|
-
Cuprum::Errors::MultipleErrors.new(
|
189
|
-
errors: primary_keys.map do |primary_key|
|
190
|
-
unless invalid_primary_key_values.include?(primary_key)
|
191
|
-
next nil
|
192
|
-
end
|
193
|
-
|
194
|
-
Cuprum::Collections::Errors::NotFound.new(
|
195
|
-
attribute_name: primary_key_name,
|
196
|
-
attribute_value: primary_key,
|
197
|
-
collection_name: command.collection_name,
|
198
|
-
primary_key: true
|
199
|
-
)
|
200
|
-
end
|
201
|
-
)
|
202
|
-
end
|
203
|
-
|
204
|
-
it 'should return a passing result' do
|
205
|
-
expect(
|
206
|
-
command.call(primary_keys: primary_keys, allow_partial: true)
|
207
|
-
)
|
208
|
-
.to be_a_passing_result
|
209
|
-
.with_value(expected_data)
|
210
|
-
.and_error(expected_error)
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
describe 'with a valid array of primary keys' do
|
215
|
-
let(:primary_keys) { valid_primary_key_values }
|
216
|
-
|
217
|
-
it 'should return a passing result' do
|
218
|
-
expect(
|
219
|
-
command.call(primary_keys: primary_keys, allow_partial: true)
|
220
|
-
)
|
221
|
-
.to be_a_passing_result
|
222
|
-
.with_value(expected_data)
|
223
|
-
end
|
224
|
-
|
225
|
-
describe 'with an ordered array of primary keys' do
|
226
|
-
let(:primary_keys) { valid_primary_key_values.reverse }
|
227
|
-
|
228
|
-
it 'should return a passing result' do
|
229
|
-
expect(
|
230
|
-
command.call(primary_keys: primary_keys, allow_partial: true)
|
231
|
-
)
|
232
|
-
.to be_a_passing_result
|
233
|
-
.with_value(expected_data)
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
describe 'with envelope: true' do
|
240
|
-
describe 'with a valid array of primary keys' do
|
241
|
-
let(:primary_keys) { valid_primary_key_values }
|
242
|
-
|
243
|
-
it 'should return a passing result' do
|
244
|
-
expect(command.call(primary_keys: primary_keys, envelope: true))
|
245
|
-
.to be_a_passing_result
|
246
|
-
.with_value({ collection_name => expected_data })
|
247
|
-
end
|
248
|
-
|
249
|
-
describe 'with an ordered array of primary keys' do
|
250
|
-
let(:primary_keys) { valid_primary_key_values.reverse }
|
251
|
-
|
252
|
-
it 'should return a passing result' do
|
253
|
-
expect(command.call(primary_keys: primary_keys, envelope: true))
|
254
|
-
.to be_a_passing_result
|
255
|
-
.with_value({ collection_name => expected_data })
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
describe 'with scope: query' do
|
262
|
-
let(:scope_filter) { -> { {} } }
|
263
|
-
|
264
|
-
describe 'with an array of invalid primary keys' do
|
265
|
-
let(:primary_keys) { invalid_primary_key_values }
|
266
|
-
let(:expected_error) do
|
267
|
-
Cuprum::Errors::MultipleErrors.new(
|
268
|
-
errors: primary_keys.map do |primary_key|
|
269
|
-
Cuprum::Collections::Errors::NotFound.new(
|
270
|
-
attribute_name: primary_key_name,
|
271
|
-
attribute_value: primary_key,
|
272
|
-
collection_name: command.collection_name,
|
273
|
-
primary_key: true
|
274
|
-
)
|
275
|
-
end
|
276
|
-
)
|
277
|
-
end
|
278
|
-
|
279
|
-
it 'should return a failing result' do
|
280
|
-
expect(command.call(primary_keys: primary_keys, scope: scope))
|
281
|
-
.to be_a_failing_result
|
282
|
-
.with_error(expected_error)
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
describe 'with a scope that does not match any keys' do
|
287
|
-
let(:scope_filter) { -> { { author: 'Ursula K. LeGuin' } } }
|
288
|
-
|
289
|
-
describe 'with a valid array of primary keys' do
|
290
|
-
let(:primary_keys) { valid_primary_key_values }
|
291
|
-
let(:expected_error) do
|
292
|
-
Cuprum::Errors::MultipleErrors.new(
|
293
|
-
errors: primary_keys.map do |primary_key|
|
294
|
-
Cuprum::Collections::Errors::NotFound.new(
|
295
|
-
attribute_name: primary_key_name,
|
296
|
-
attribute_value: primary_key,
|
297
|
-
collection_name: command.collection_name,
|
298
|
-
primary_key: true
|
299
|
-
)
|
300
|
-
end
|
301
|
-
)
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'should return a failing result' do
|
305
|
-
expect(command.call(primary_keys: primary_keys, scope: scope))
|
306
|
-
.to be_a_failing_result
|
307
|
-
.with_error(expected_error)
|
308
|
-
end
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
|
-
describe 'with a scope that matches some keys' do
|
313
|
-
let(:scope_filter) { -> { { series: nil } } }
|
314
|
-
let(:matching_data) do
|
315
|
-
super().map do |item|
|
316
|
-
next nil unless item['series'].nil?
|
317
|
-
|
318
|
-
item
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
describe 'with a valid array of primary keys' do
|
323
|
-
let(:primary_keys) { valid_primary_key_values }
|
324
|
-
let(:expected_error) do
|
325
|
-
found_keys =
|
326
|
-
matching_data
|
327
|
-
.compact
|
328
|
-
.map { |item| item[primary_key_name.to_s] }
|
329
|
-
|
330
|
-
Cuprum::Errors::MultipleErrors.new(
|
331
|
-
errors: primary_keys.map do |primary_key|
|
332
|
-
next if found_keys.include?(primary_key)
|
333
|
-
|
334
|
-
Cuprum::Collections::Errors::NotFound.new(
|
335
|
-
attribute_name: primary_key_name,
|
336
|
-
attribute_value: primary_key,
|
337
|
-
collection_name: command.collection_name,
|
338
|
-
primary_key: true
|
339
|
-
)
|
340
|
-
end
|
341
|
-
)
|
342
|
-
end
|
343
|
-
|
344
|
-
it 'should return a failing result' do
|
345
|
-
expect(command.call(primary_keys: primary_keys, scope: scope))
|
346
|
-
.to be_a_failing_result
|
347
|
-
.with_error(expected_error)
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
describe 'with allow_partial: true' do
|
352
|
-
describe 'with a valid array of primary keys' do
|
353
|
-
let(:primary_keys) { valid_primary_key_values }
|
354
|
-
let(:expected_error) do
|
355
|
-
found_keys =
|
356
|
-
matching_data
|
357
|
-
.compact
|
358
|
-
.map { |item| item[primary_key_name.to_s] }
|
359
|
-
|
360
|
-
Cuprum::Errors::MultipleErrors.new(
|
361
|
-
errors: primary_keys.map do |primary_key|
|
362
|
-
next if found_keys.include?(primary_key)
|
363
|
-
|
364
|
-
Cuprum::Collections::Errors::NotFound.new(
|
365
|
-
attribute_name: primary_key_name,
|
366
|
-
attribute_value: primary_key,
|
367
|
-
collection_name: command.collection_name,
|
368
|
-
primary_key: true
|
369
|
-
)
|
370
|
-
end
|
371
|
-
)
|
372
|
-
end
|
373
|
-
|
374
|
-
it 'should return a passing result' do
|
375
|
-
expect(
|
376
|
-
command.call(
|
377
|
-
allow_partial: true,
|
378
|
-
primary_keys: primary_keys,
|
379
|
-
scope: scope
|
380
|
-
)
|
381
|
-
)
|
382
|
-
.to be_a_passing_result
|
383
|
-
.with_value(expected_data)
|
384
|
-
.and_error(expected_error)
|
385
|
-
end
|
386
|
-
end
|
387
|
-
end
|
388
|
-
end
|
389
|
-
|
390
|
-
describe 'with a scope that matches all keys' do
|
391
|
-
let(:scope_filter) { -> { { author: 'J.R.R. Tolkien' } } }
|
392
|
-
|
393
|
-
describe 'with a valid array of primary keys' do
|
394
|
-
let(:primary_keys) { valid_primary_key_values }
|
395
|
-
|
396
|
-
it 'should return a passing result' do
|
397
|
-
expect(command.call(primary_keys: primary_keys))
|
398
|
-
.to be_a_passing_result
|
399
|
-
.with_value(expected_data)
|
400
|
-
end
|
401
|
-
end
|
402
|
-
end
|
403
|
-
end
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
407
|
-
end
|