fun_with_json_api 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/config/locales/fun_with_json_api.en.yml +7 -0
- data/lib/fun_with_json_api/collection_manager.rb +116 -0
- data/lib/fun_with_json_api/exceptions/invalid_resource.rb +15 -0
- data/lib/fun_with_json_api/exceptions/relationship_method_not_supported.rb +16 -0
- data/lib/fun_with_json_api/version.rb +1 -1
- data/lib/fun_with_json_api.rb +1 -0
- data/spec/dummy/log/test.log +23939 -0
- data/spec/fun_with_json_api/collection_manager_spec.rb +177 -0
- metadata +7 -2
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FunWithJsonApi::CollectionManager do
|
4
|
+
subject(:instance) { described_class.new(collection, deserializer_class, deserializer_options) }
|
5
|
+
let(:collection) { double('collection') }
|
6
|
+
let(:deserializer_class) { class_double('FunWithJsonApi::Deserializer', type: 'examples') }
|
7
|
+
let(:deserializer_options) { double('deserializer_options') }
|
8
|
+
|
9
|
+
describe '#load_collection' do
|
10
|
+
it 'calls FunWithJsonApi.load_collection with the document and the deserializers' do
|
11
|
+
document = double('document')
|
12
|
+
|
13
|
+
parsed_collection = double('parsed_collection')
|
14
|
+
expect(FunWithJsonApi).to receive(:find_collection)
|
15
|
+
.with(document, deserializer_class, deserializer_options)
|
16
|
+
.and_return(parsed_collection)
|
17
|
+
|
18
|
+
expect(instance.load_collection(document)).to eq parsed_collection
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#report_invalid_resources_at_index!' do
|
23
|
+
context 'with a resource index' do
|
24
|
+
let(:resource_index) { 42 }
|
25
|
+
|
26
|
+
context 'with a reason message as a string' do
|
27
|
+
let(:reason_message) { Faker::Lorem.sentence }
|
28
|
+
|
29
|
+
it 'raises a InvalidResource exception with the reason message as a detail' do
|
30
|
+
expect do
|
31
|
+
instance.report_invalid_resources_at_index! resource_index, reason_message
|
32
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
33
|
+
expect(e.payload.size).to eq 1
|
34
|
+
|
35
|
+
payload = e.payload.first
|
36
|
+
expect(payload.code).to eq 'invalid_resource'
|
37
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
38
|
+
expect(payload.detail).to eq reason_message
|
39
|
+
expect(payload.pointer).to eq '/data/42/id'
|
40
|
+
expect(payload.status).to eq '422'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with a reason message as a callable' do
|
46
|
+
let(:reason_message) { Faker::Lorem.sentence }
|
47
|
+
|
48
|
+
it 'raises a InvalidResource exception by invoking call with the resource index' do
|
49
|
+
expect do
|
50
|
+
instance.report_invalid_resources_at_index!(
|
51
|
+
resource_index, ->(index) { "#{index}-#{reason_message}" }
|
52
|
+
)
|
53
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
54
|
+
expect(e.payload.size).to eq 1
|
55
|
+
|
56
|
+
payload = e.payload.first
|
57
|
+
expect(payload.code).to eq 'invalid_resource'
|
58
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
59
|
+
expect(payload.detail).to eq "42-#{reason_message}"
|
60
|
+
expect(payload.pointer).to eq '/data/42/id'
|
61
|
+
expect(payload.status).to eq '422'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'with a reason message not included' do
|
67
|
+
it 'raises a InvalidResource exception with a default reason message' do
|
68
|
+
expect do
|
69
|
+
instance.report_invalid_resources_at_index! resource_index
|
70
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
71
|
+
expect(e.payload.size).to eq 1
|
72
|
+
|
73
|
+
payload = e.payload.first
|
74
|
+
expect(payload.code).to eq 'invalid_resource'
|
75
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
76
|
+
expect(payload.detail).to eq "Unable to update the relationship with this 'examples'"
|
77
|
+
expect(payload.pointer).to eq '/data/42/id'
|
78
|
+
expect(payload.status).to eq '422'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with multiple resource indexes' do
|
85
|
+
let(:resource_index) { [1, 3] }
|
86
|
+
|
87
|
+
context 'with a reason message as a string' do
|
88
|
+
let(:reason_message) { Faker::Lorem.sentence }
|
89
|
+
|
90
|
+
it 'raises a InvalidResource exception with a payload for each index' do
|
91
|
+
expect do
|
92
|
+
instance.report_invalid_resources_at_index! resource_index, reason_message
|
93
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidResource) do |e|
|
94
|
+
expect(e.payload.size).to eq 2
|
95
|
+
|
96
|
+
payload = e.payload.first
|
97
|
+
expect(payload.code).to eq 'invalid_resource'
|
98
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
99
|
+
expect(payload.detail).to eq reason_message
|
100
|
+
expect(payload.pointer).to eq '/data/1/id'
|
101
|
+
expect(payload.status).to eq '422'
|
102
|
+
|
103
|
+
payload = e.payload.second
|
104
|
+
expect(payload.code).to eq 'invalid_resource'
|
105
|
+
expect(payload.title).to eq 'Unable to update the relationship with this resource'
|
106
|
+
expect(payload.detail).to eq reason_message
|
107
|
+
expect(payload.pointer).to eq '/data/3/id'
|
108
|
+
expect(payload.status).to eq '422'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
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
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fun_with_json_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Morrall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -160,6 +160,7 @@ files:
|
|
160
160
|
- lib/fun_with_json_api/attributes/relationship_collection.rb
|
161
161
|
- lib/fun_with_json_api/attributes/string_attribute.rb
|
162
162
|
- lib/fun_with_json_api/attributes/uuid_v4_attribute.rb
|
163
|
+
- lib/fun_with_json_api/collection_manager.rb
|
163
164
|
- lib/fun_with_json_api/controller_methods.rb
|
164
165
|
- lib/fun_with_json_api/deserializer.rb
|
165
166
|
- lib/fun_with_json_api/deserializer_class_methods.rb
|
@@ -176,8 +177,10 @@ files:
|
|
176
177
|
- lib/fun_with_json_api/exceptions/invalid_document_type.rb
|
177
178
|
- lib/fun_with_json_api/exceptions/invalid_relationship.rb
|
178
179
|
- lib/fun_with_json_api/exceptions/invalid_relationship_type.rb
|
180
|
+
- lib/fun_with_json_api/exceptions/invalid_resource.rb
|
179
181
|
- lib/fun_with_json_api/exceptions/missing_relationship.rb
|
180
182
|
- lib/fun_with_json_api/exceptions/missing_resource.rb
|
183
|
+
- lib/fun_with_json_api/exceptions/relationship_method_not_supported.rb
|
181
184
|
- lib/fun_with_json_api/exceptions/unauthorized_resource.rb
|
182
185
|
- lib/fun_with_json_api/exceptions/unknown_attribute.rb
|
183
186
|
- lib/fun_with_json_api/exceptions/unknown_relationship.rb
|
@@ -231,6 +234,7 @@ files:
|
|
231
234
|
- spec/dummy/public/500.html
|
232
235
|
- spec/dummy/public/favicon.ico
|
233
236
|
- spec/fixtures/active_record.rb
|
237
|
+
- spec/fun_with_json_api/collection_manager_spec.rb
|
234
238
|
- spec/fun_with_json_api/controller_methods_spec.rb
|
235
239
|
- spec/fun_with_json_api/deserializer_class_methods_spec.rb
|
236
240
|
- spec/fun_with_json_api/deserializer_spec.rb
|
@@ -307,6 +311,7 @@ test_files:
|
|
307
311
|
- spec/dummy/Rakefile
|
308
312
|
- spec/dummy/README.rdoc
|
309
313
|
- spec/fixtures/active_record.rb
|
314
|
+
- spec/fun_with_json_api/collection_manager_spec.rb
|
310
315
|
- spec/fun_with_json_api/controller_methods_spec.rb
|
311
316
|
- spec/fun_with_json_api/deserializer_class_methods_spec.rb
|
312
317
|
- spec/fun_with_json_api/deserializer_spec.rb
|