fun_with_json_api 0.0.8 → 0.0.8.1
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/config/locales/fun_with_json_api.en.yml +1 -1
- data/lib/fun_with_json_api/collection_manager.rb +69 -41
- data/lib/fun_with_json_api/deserializer.rb +5 -1
- data/lib/fun_with_json_api/version.rb +1 -1
- data/spec/dummy/log/test.log +27124 -0
- data/spec/fun_with_json_api/collection_manager_spec.rb +198 -84
- data/spec/fun_with_json_api_spec.rb +1 -0
- metadata +1 -1
@@ -5,30 +5,200 @@ describe FunWithJsonApi::CollectionManager do
|
|
5
5
|
let(:collection) { double('collection') }
|
6
6
|
let(:deserializer_class) { class_double('FunWithJsonApi::Deserializer', type: 'examples') }
|
7
7
|
let(:deserializer_options) { double('deserializer_options') }
|
8
|
+
let(:deserializer) { instance_double('FunWithJsonApi::Deserializer', type: 'examples') }
|
9
|
+
before do
|
10
|
+
allow(deserializer_class).to receive(:create)
|
11
|
+
.with(deserializer_options)
|
12
|
+
.and_return(deserializer)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#insert_records' do
|
16
|
+
context 'when a block is provided' do
|
17
|
+
context 'when the block returns true for each item' do
|
18
|
+
it 'loads a collection from the document and invokes the block with each collection item' do
|
19
|
+
document = double('document')
|
20
|
+
collection = [double('collection_a'), double('collection_b')]
|
21
|
+
expect(
|
22
|
+
FunWithJsonApi::FindCollectionFromDocument
|
23
|
+
).to receive(:find).with(document, deserializer).and_return(collection)
|
8
24
|
|
9
|
-
|
10
|
-
|
11
|
-
|
25
|
+
received_items = []
|
26
|
+
instance.insert_records(document) do |item|
|
27
|
+
received_items << item
|
28
|
+
true
|
29
|
+
end
|
30
|
+
expect(received_items).to eq collection
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context 'when the block returns false for an item' do
|
34
|
+
it 'raises a FunWithJsonApi::Exceptions::InvalidResource with a payload for each item' do
|
35
|
+
document = double('document')
|
36
|
+
collection = [
|
37
|
+
double('collection_a', success?: true),
|
38
|
+
double('collection_b', success?: false)
|
39
|
+
]
|
40
|
+
allow(
|
41
|
+
FunWithJsonApi::FindCollectionFromDocument
|
42
|
+
).to receive(:find).with(document, deserializer).and_return(collection)
|
43
|
+
allow(deserializer).to receive(:format_resource_id).with(collection[0]).and_return('id_a')
|
44
|
+
allow(deserializer).to receive(:format_resource_id).with(collection[1]).and_return('id_b')
|
12
45
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
46
|
+
expect do
|
47
|
+
instance.insert_records(
|
48
|
+
document,
|
49
|
+
->(index) { "Record '#{index}' is invalid" },
|
50
|
+
&:success? # Call success on each collection items
|
51
|
+
)
|
52
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
53
|
+
expect(e.payload.size).to eq 1
|
17
54
|
|
18
|
-
|
55
|
+
payload = e.payload.first
|
56
|
+
expect(payload.code).to eq 'invalid_resource'
|
57
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
58
|
+
expect(payload.detail).to eq "Record 'id_b' is invalid"
|
59
|
+
expect(payload.pointer).to eq '/data/1/id'
|
60
|
+
expect(payload.status).to eq '422'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when no block is provided' do
|
67
|
+
it 'raises a RelationshipNotSupported exception' do
|
68
|
+
document = double('document')
|
69
|
+
expect do
|
70
|
+
instance.insert_records(document)
|
71
|
+
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
72
|
+
expect(e.message).to eq(
|
73
|
+
'Override FunWithJsonApi::CollectionManager#insert_records or supply a block'
|
74
|
+
)
|
75
|
+
expect(e.payload.size).to eq 1
|
76
|
+
|
77
|
+
payload = e.payload.first
|
78
|
+
expect(payload.code).to eq 'collection_method_not_supported'
|
79
|
+
expect(payload.title).to eq 'The current relationship does not support this action'
|
80
|
+
expect(payload.detail).to eq "Unable to insert 'examples' items from this endpoint"
|
81
|
+
expect(payload.pointer).to eq nil
|
82
|
+
expect(payload.status).to eq '403'
|
83
|
+
end
|
84
|
+
end
|
19
85
|
end
|
20
86
|
end
|
21
87
|
|
22
|
-
describe '#
|
23
|
-
context '
|
24
|
-
|
88
|
+
describe '#remove_records' do
|
89
|
+
context 'when a block is provided' do
|
90
|
+
context 'when the block returns true for each item' do
|
91
|
+
it 'loads a collection from the document and invokes the block with each collection item' do
|
92
|
+
document = double('document')
|
93
|
+
collection = [double('collection_a'), double('collection_b')]
|
94
|
+
expect(
|
95
|
+
FunWithJsonApi::FindCollectionFromDocument
|
96
|
+
).to receive(:find).with(document, deserializer).and_return(collection)
|
97
|
+
|
98
|
+
received_items = []
|
99
|
+
instance.remove_records(document) do |item|
|
100
|
+
received_items << item
|
101
|
+
true
|
102
|
+
end
|
103
|
+
expect(received_items).to eq collection
|
104
|
+
end
|
105
|
+
end
|
106
|
+
context 'when the block returns false for an item' do
|
107
|
+
it 'raises a FunWithJsonApi::Exceptions::InvalidResource with a payload for each item' do
|
108
|
+
document = double('document')
|
109
|
+
collection = [
|
110
|
+
double('collection_a', success?: true),
|
111
|
+
double('collection_b', success?: false)
|
112
|
+
]
|
113
|
+
allow(
|
114
|
+
FunWithJsonApi::FindCollectionFromDocument
|
115
|
+
).to receive(:find).with(document, deserializer).and_return(collection)
|
116
|
+
allow(deserializer).to receive(:format_resource_id).with(collection[0]).and_return('id_a')
|
117
|
+
allow(deserializer).to receive(:format_resource_id).with(collection[1]).and_return('id_b')
|
118
|
+
|
119
|
+
expect do
|
120
|
+
instance.remove_records(
|
121
|
+
document,
|
122
|
+
->(index) { "Record '#{index}' is invalid" },
|
123
|
+
&:success? # Call success on each collection items
|
124
|
+
)
|
125
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
126
|
+
expect(e.payload.size).to eq 1
|
127
|
+
|
128
|
+
payload = e.payload.first
|
129
|
+
expect(payload.code).to eq 'invalid_resource'
|
130
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
131
|
+
expect(payload.detail).to eq "Record 'id_b' is invalid"
|
132
|
+
expect(payload.pointer).to eq '/data/1/id'
|
133
|
+
expect(payload.status).to eq '422'
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when no block is provided' do
|
140
|
+
it 'raises a RelationshipNotSupported exception' do
|
141
|
+
document = double('document')
|
142
|
+
expect do
|
143
|
+
instance.remove_records(document)
|
144
|
+
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
145
|
+
expect(e.message).to eq(
|
146
|
+
'Override FunWithJsonApi::CollectionManager#remove_records or supply a block'
|
147
|
+
)
|
148
|
+
expect(e.payload.size).to eq 1
|
149
|
+
|
150
|
+
payload = e.payload.first
|
151
|
+
expect(payload.code).to eq 'collection_method_not_supported'
|
152
|
+
expect(payload.title).to eq 'The current relationship does not support this action'
|
153
|
+
expect(payload.detail).to eq "Unable to remove 'examples' items from this endpoint"
|
154
|
+
expect(payload.pointer).to eq nil
|
155
|
+
expect(payload.status).to eq '403'
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '#replace_all_records' do
|
162
|
+
context 'when no block is provided' do
|
163
|
+
it 'raises a RelationshipNotSupported exception' do
|
164
|
+
document = double('document')
|
165
|
+
expect do
|
166
|
+
instance.replace_all_records(document)
|
167
|
+
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
168
|
+
expect(e.message).to eq(
|
169
|
+
'Override FunWithJsonApi::CollectionManager#replace_all_records'\
|
170
|
+
' to implement replace all'
|
171
|
+
)
|
172
|
+
expect(e.payload.size).to eq 1
|
173
|
+
|
174
|
+
payload = e.payload.first
|
175
|
+
expect(payload.code).to eq 'collection_method_not_supported'
|
176
|
+
expect(payload.title).to eq 'The current relationship does not support this action'
|
177
|
+
expect(payload.detail).to eq "Unable to replace all 'examples' items from this endpoint"
|
178
|
+
expect(payload.pointer).to eq nil
|
179
|
+
expect(payload.status).to eq '403'
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe '#raise_invalid_resource_exception' do
|
186
|
+
before do
|
187
|
+
allow(deserializer).to receive(:format_resource_id, &:id_value)
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'with a invalid resource at an index' do
|
191
|
+
let(:resource_index) { '42' }
|
192
|
+
let(:invalid_resource) { double('invalid_resource', id_value: 'id_1') }
|
25
193
|
|
26
194
|
context 'with a reason message as a string' do
|
27
195
|
let(:reason_message) { Faker::Lorem.sentence }
|
28
196
|
|
29
197
|
it 'raises a InvalidResource exception with the reason message as a detail' do
|
30
198
|
expect do
|
31
|
-
instance.
|
199
|
+
instance.send :raise_invalid_resource_exception,
|
200
|
+
{ 42 => invalid_resource },
|
201
|
+
reason_message
|
32
202
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
33
203
|
expect(e.payload.size).to eq 1
|
34
204
|
|
@@ -47,33 +217,35 @@ describe FunWithJsonApi::CollectionManager do
|
|
47
217
|
|
48
218
|
it 'raises a InvalidResource exception by invoking call with the resource index' do
|
49
219
|
expect do
|
50
|
-
instance.
|
51
|
-
|
52
|
-
|
220
|
+
instance.send :raise_invalid_resource_exception,
|
221
|
+
{ 42 => invalid_resource },
|
222
|
+
->(index) { "#{index}-#{reason_message}" }
|
53
223
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
54
224
|
expect(e.payload.size).to eq 1
|
55
225
|
|
56
226
|
payload = e.payload.first
|
57
227
|
expect(payload.code).to eq 'invalid_resource'
|
58
228
|
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
59
|
-
expect(payload.detail).to eq "
|
229
|
+
expect(payload.detail).to eq "id_1-#{reason_message}"
|
60
230
|
expect(payload.pointer).to eq '/data/42/id'
|
61
231
|
expect(payload.status).to eq '422'
|
62
232
|
end
|
63
233
|
end
|
64
234
|
end
|
65
235
|
|
66
|
-
context 'with a reason message
|
236
|
+
context 'with a nil reason message' do
|
67
237
|
it 'raises a InvalidResource exception with a default reason message' do
|
68
238
|
expect do
|
69
|
-
instance.
|
239
|
+
instance.send :raise_invalid_resource_exception, { 42 => invalid_resource }, nil
|
70
240
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
71
241
|
expect(e.payload.size).to eq 1
|
72
242
|
|
73
243
|
payload = e.payload.first
|
74
244
|
expect(payload.code).to eq 'invalid_resource'
|
75
245
|
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
76
|
-
expect(payload.detail).to eq
|
246
|
+
expect(payload.detail).to eq(
|
247
|
+
"Unable to update relationship with 'examples' resource: 'id_1'"
|
248
|
+
)
|
77
249
|
expect(payload.pointer).to eq '/data/42/id'
|
78
250
|
expect(payload.status).to eq '422'
|
79
251
|
end
|
@@ -82,14 +254,19 @@ describe FunWithJsonApi::CollectionManager do
|
|
82
254
|
end
|
83
255
|
|
84
256
|
context 'with multiple resource indexes' do
|
85
|
-
let(:
|
257
|
+
let(:resource_hash) do
|
258
|
+
{
|
259
|
+
1 => double('resource_a', id_value: 'id_a'),
|
260
|
+
3 => double('resource_b', id_value: 'id_b')
|
261
|
+
}
|
262
|
+
end
|
86
263
|
|
87
264
|
context 'with a reason message as a string' do
|
88
265
|
let(:reason_message) { Faker::Lorem.sentence }
|
89
266
|
|
90
267
|
it 'raises a InvalidResource exception with a payload for each index' do
|
91
268
|
expect do
|
92
|
-
instance.
|
269
|
+
instance.send :raise_invalid_resource_exception, resource_hash, reason_message
|
93
270
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
94
271
|
expect(e.payload.size).to eq 2
|
95
272
|
|
@@ -111,67 +288,4 @@ describe FunWithJsonApi::CollectionManager do
|
|
111
288
|
end
|
112
289
|
end
|
113
290
|
end
|
114
|
-
|
115
|
-
describe '#insert_records' do
|
116
|
-
it 'raises a RelationshipNotSupported exception' do
|
117
|
-
document = double('document')
|
118
|
-
expect do
|
119
|
-
instance.insert_records(document)
|
120
|
-
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
121
|
-
expect(e.message).to eq(
|
122
|
-
'Override FunWithJsonApi::CollectionManager#insert_records to implement insert'
|
123
|
-
)
|
124
|
-
expect(e.payload.size).to eq 1
|
125
|
-
|
126
|
-
payload = e.payload.first
|
127
|
-
expect(payload.code).to eq 'collection_method_not_supported'
|
128
|
-
expect(payload.title).to eq 'The current relationship does not support this action'
|
129
|
-
expect(payload.detail).to eq "Unable to insert 'examples' items from this endpoint"
|
130
|
-
expect(payload.pointer).to eq nil
|
131
|
-
expect(payload.status).to eq '403'
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
describe '#remove_records' do
|
137
|
-
it 'raises a RelationshipNotSupported exception' do
|
138
|
-
document = double('document')
|
139
|
-
expect do
|
140
|
-
instance.remove_records(document)
|
141
|
-
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
142
|
-
expect(e.message).to eq(
|
143
|
-
'Override FunWithJsonApi::CollectionManager#remove_records to implement remove'
|
144
|
-
)
|
145
|
-
expect(e.payload.size).to eq 1
|
146
|
-
|
147
|
-
payload = e.payload.first
|
148
|
-
expect(payload.code).to eq 'collection_method_not_supported'
|
149
|
-
expect(payload.title).to eq 'The current relationship does not support this action'
|
150
|
-
expect(payload.detail).to eq "Unable to remove 'examples' items from this endpoint"
|
151
|
-
expect(payload.pointer).to eq nil
|
152
|
-
expect(payload.status).to eq '403'
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
describe '#replace_all_records' do
|
158
|
-
it 'raises a RelationshipNotSupported exception' do
|
159
|
-
document = double('document')
|
160
|
-
expect do
|
161
|
-
instance.replace_all_records(document)
|
162
|
-
end.to raise_error(FunWithJsonApi::Exceptions::RelationshipMethodNotSupported) do |e|
|
163
|
-
expect(e.message).to eq(
|
164
|
-
'Override FunWithJsonApi::CollectionManager#replace_all_records to implement replace all'
|
165
|
-
)
|
166
|
-
expect(e.payload.size).to eq 1
|
167
|
-
|
168
|
-
payload = e.payload.first
|
169
|
-
expect(payload.code).to eq 'collection_method_not_supported'
|
170
|
-
expect(payload.title).to eq 'The current relationship does not support this action'
|
171
|
-
expect(payload.detail).to eq "Unable to replace all 'examples' items from this endpoint"
|
172
|
-
expect(payload.pointer).to eq nil
|
173
|
-
expect(payload.status).to eq '403'
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
291
|
end
|