acfs 0.42.0 → 0.43.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +8 -9
- data/acfs.gemspec +3 -3
- data/lib/acfs.rb +0 -2
- data/lib/acfs/adapter/base.rb +0 -2
- data/lib/acfs/adapter/typhoeus.rb +6 -9
- data/lib/acfs/collection.rb +3 -3
- data/lib/acfs/collections/paginatable.rb +16 -16
- data/lib/acfs/configuration.rb +3 -5
- data/lib/acfs/errors.rb +8 -7
- data/lib/acfs/global.rb +1 -1
- data/lib/acfs/location.rb +11 -11
- data/lib/acfs/middleware/base.rb +1 -2
- data/lib/acfs/middleware/json.rb +0 -1
- data/lib/acfs/middleware/logger.rb +0 -2
- data/lib/acfs/middleware/print.rb +0 -2
- data/lib/acfs/middleware/serializer.rb +3 -6
- data/lib/acfs/operation.rb +3 -4
- data/lib/acfs/request.rb +2 -3
- data/lib/acfs/request/callbacks.rb +2 -3
- data/lib/acfs/resource.rb +34 -5
- data/lib/acfs/{model → resource}/attributes.rb +70 -46
- data/lib/acfs/{model → resource}/attributes/base.rb +10 -6
- data/lib/acfs/resource/attributes/boolean.rb +33 -0
- data/lib/acfs/resource/attributes/date_time.rb +32 -0
- data/lib/acfs/{model → resource}/attributes/dict.rb +1 -3
- data/lib/acfs/{model → resource}/attributes/float.rb +3 -6
- data/lib/acfs/{model → resource}/attributes/integer.rb +3 -6
- data/lib/acfs/{model → resource}/attributes/list.rb +2 -5
- data/lib/acfs/{model → resource}/attributes/string.rb +4 -6
- data/lib/acfs/{model → resource}/attributes/uuid.rb +18 -8
- data/lib/acfs/resource/dirty.rb +47 -0
- data/lib/acfs/{model → resource}/initialization.rb +8 -10
- data/lib/acfs/{model → resource}/loadable.rb +3 -4
- data/lib/acfs/{model → resource}/locatable.rb +22 -23
- data/lib/acfs/{model → resource}/operational.rb +2 -3
- data/lib/acfs/resource/persistence.rb +257 -0
- data/lib/acfs/{model → resource}/query_methods.rb +81 -66
- data/lib/acfs/{model → resource}/service.rb +10 -9
- data/lib/acfs/resource/validation.rb +28 -0
- data/lib/acfs/response.rb +1 -2
- data/lib/acfs/response/formats.rb +1 -2
- data/lib/acfs/response/status.rb +3 -5
- data/lib/acfs/runner.rb +4 -5
- data/lib/acfs/service.rb +4 -6
- data/lib/acfs/service/middleware.rb +1 -3
- data/lib/acfs/singleton_resource.rb +11 -24
- data/lib/acfs/stub.rb +30 -22
- data/lib/acfs/util.rb +1 -1
- data/lib/acfs/version.rb +4 -2
- data/spec/acfs/adapter/typhoeus_spec.rb +4 -4
- data/spec/acfs/collection_spec.rb +33 -33
- data/spec/acfs/configuration_spec.rb +0 -1
- data/spec/acfs/global_spec.rb +3 -3
- data/spec/acfs/middleware/json_spec.rb +2 -2
- data/spec/acfs/middleware/msgpack_spec.rb +4 -4
- data/spec/acfs/request/callbacks_spec.rb +8 -8
- data/spec/acfs/request_spec.rb +5 -5
- data/spec/acfs/{model → resource}/attributes/boolean_spec.rb +2 -2
- data/spec/acfs/{model → resource}/attributes/date_time_spec.rb +7 -7
- data/spec/acfs/{model → resource}/attributes/dict_spec.rb +6 -6
- data/spec/acfs/{model → resource}/attributes/float_spec.rb +3 -3
- data/spec/acfs/{model → resource}/attributes/list_spec.rb +5 -5
- data/spec/acfs/{model → resource}/attributes/uuid_spec.rb +6 -6
- data/spec/acfs/{model → resource}/attributes_spec.rb +31 -17
- data/spec/acfs/{model → resource}/dirty_spec.rb +7 -5
- data/spec/acfs/{model → resource}/initialization_spec.rb +7 -7
- data/spec/acfs/{model → resource}/loadable_spec.rb +4 -3
- data/spec/acfs/{model → resource}/locatable_spec.rb +24 -14
- data/spec/acfs/{model → resource}/persistance_spec.rb +34 -34
- data/spec/acfs/{model → resource}/query_methods_spec.rb +171 -130
- data/spec/acfs/{model → resource}/validation_spec.rb +5 -6
- data/spec/acfs/response/formats_spec.rb +1 -1
- data/spec/acfs/response/status_spec.rb +1 -1
- data/spec/acfs/runner_spec.rb +2 -3
- data/spec/acfs/service/middleware_spec.rb +1 -1
- data/spec/acfs/service_spec.rb +3 -5
- data/spec/acfs/singleton_resource_spec.rb +3 -3
- data/spec/acfs/stub_spec.rb +52 -24
- data/spec/acfs_spec.rb +22 -19
- data/spec/spec_helper.rb +1 -1
- data/spec/support/hash.rb +9 -0
- data/spec/support/service.rb +4 -7
- data/spec/support/shared/find_callbacks.rb +7 -7
- metadata +52 -52
- data/lib/acfs/model.rb +0 -43
- data/lib/acfs/model/attributes/boolean.rb +0 -38
- data/lib/acfs/model/attributes/date_time.rb +0 -30
- data/lib/acfs/model/dirty.rb +0 -49
- data/lib/acfs/model/persistence.rb +0 -243
- data/lib/acfs/model/relations.rb +0 -10
- data/lib/acfs/model/validation.rb +0 -30
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Acfs::
|
3
|
+
describe Acfs::Resource::Dirty do
|
4
4
|
let(:model) { MyUser.new }
|
5
5
|
before do
|
6
|
-
stub_request(:get,
|
7
|
-
|
6
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
7
|
+
.to_return response id: 1, name: 'Anon', age: 12
|
8
|
+
stub_request(:post, 'http://users.example.org/users')
|
9
|
+
.to_return response id: 5, name: 'dhh', age: 12
|
8
10
|
end
|
9
11
|
|
10
12
|
it 'includes ActiveModel::Dirty' do
|
@@ -25,7 +27,7 @@ describe Acfs::Model::Dirty do
|
|
25
27
|
|
26
28
|
context 'after model load' do
|
27
29
|
let(:user) { MyUser.find 1 }
|
28
|
-
before { user
|
30
|
+
before { user && Acfs.run }
|
29
31
|
|
30
32
|
it { expect(user).to_not be_changed }
|
31
33
|
end
|
@@ -37,7 +39,7 @@ describe Acfs::Model::Dirty do
|
|
37
39
|
end
|
38
40
|
|
39
41
|
context 'after model new with attrs' do
|
40
|
-
let(:user) { MyUser.new name:
|
42
|
+
let(:user) { MyUser.new name: 'Uschi' }
|
41
43
|
|
42
44
|
it { expect(user).to be_changed }
|
43
45
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'Acfs::
|
3
|
+
describe 'Acfs::Resource::Initialization' do
|
4
4
|
let(:model) do
|
5
|
-
Class.new.tap do |c|
|
5
|
+
Class.new(Acfs::Resource).tap do |c|
|
6
6
|
c.class_eval do
|
7
|
-
include Acfs::Model
|
8
7
|
attr_accessor :name, :age
|
9
8
|
private :age=
|
10
9
|
end
|
@@ -13,18 +12,19 @@ describe 'Acfs::Model::Initialization' do
|
|
13
12
|
|
14
13
|
describe '#initialize' do
|
15
14
|
it 'should allow to set attributes with initializer' do
|
16
|
-
m = model.new name:
|
17
|
-
expect(m.name).to
|
15
|
+
m = model.new name: 'John'
|
16
|
+
expect(m.name).to eq 'John'
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'should raise error when attributes with private setters are given' do
|
21
|
-
expect { model.new
|
20
|
+
expect { model.new age: 25 }.to raise_error(NoMethodError)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
24
|
describe '#persisted?' do
|
25
|
+
subject { model.new.persisted? }
|
26
26
|
it 'should be false' do
|
27
|
-
|
27
|
+
should be false
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Acfs::
|
3
|
+
describe Acfs::Resource::Loadable do
|
4
4
|
let(:model) { MyUser.find 1 }
|
5
5
|
before do
|
6
|
-
stub_request(:get,
|
6
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
7
|
+
.to_return response id: 1, name: 'Anon', age: 12
|
7
8
|
end
|
8
9
|
|
9
10
|
describe '#loaded?' do
|
@@ -12,7 +13,7 @@ describe Acfs::Model::Loadable do
|
|
12
13
|
end
|
13
14
|
|
14
15
|
context 'afer Acfs#run' do
|
15
|
-
before { model
|
16
|
+
before { model && Acfs.run }
|
16
17
|
it { expect(model).to be_loaded }
|
17
18
|
end
|
18
19
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Acfs::
|
3
|
+
describe Acfs::Resource::Locatable do
|
4
4
|
let(:model) { MyUser }
|
5
5
|
before do
|
6
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
7
|
-
|
6
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
7
|
+
.to_return response id: 1, name: 'Anon', age: 12
|
8
|
+
stub_request(:get, 'http://users.example.org/users/1/profile')
|
9
|
+
.to_return response user_id: 2, twitter_handle: '@anon'
|
8
10
|
end
|
9
11
|
|
10
12
|
describe '.url' do
|
@@ -20,12 +22,13 @@ describe Acfs::Model::Locatable do
|
|
20
22
|
let(:model) { Profile }
|
21
23
|
|
22
24
|
it 'should replace placeholder' do
|
23
|
-
expect(model.url(user_id: 1))
|
25
|
+
expect(model.url(user_id: 1))
|
26
|
+
.to eq 'http://users.example.org/users/1/profile'
|
24
27
|
end
|
25
28
|
|
26
29
|
context 'without attributes' do
|
27
30
|
it 'should raise an error if attribute is missing' do
|
28
|
-
expect{ model.url }.to raise_error ArgumentError
|
31
|
+
expect { model.url }.to raise_error ArgumentError
|
29
32
|
end
|
30
33
|
end
|
31
34
|
end
|
@@ -37,7 +40,10 @@ describe Acfs::Model::Locatable do
|
|
37
40
|
|
38
41
|
context ':list location' do
|
39
42
|
let(:action) { :list }
|
40
|
-
|
43
|
+
|
44
|
+
its(:raw_uri) do
|
45
|
+
should eq 'http://users.example.org/users/:user_id/sessions'
|
46
|
+
end
|
41
47
|
end
|
42
48
|
|
43
49
|
context ':create location' do
|
@@ -52,12 +58,16 @@ describe Acfs::Model::Locatable do
|
|
52
58
|
|
53
59
|
context ':update location' do
|
54
60
|
let(:action) { :update }
|
55
|
-
its(:raw_uri)
|
61
|
+
its(:raw_uri) do
|
62
|
+
expect { subject }.to raise_error ArgumentError, /update.*disabled/
|
63
|
+
end
|
56
64
|
end
|
57
65
|
|
58
66
|
context ':delete location' do
|
59
67
|
let(:action) { :delete }
|
60
|
-
its(:raw_uri)
|
68
|
+
its(:raw_uri) do
|
69
|
+
should eq 'http://users.example.org/users/:user_id/sessions/del/:id'
|
70
|
+
end
|
61
71
|
end
|
62
72
|
end
|
63
73
|
end
|
@@ -67,30 +77,30 @@ describe Acfs::Model::Locatable do
|
|
67
77
|
let(:m) { model.new }
|
68
78
|
|
69
79
|
it 'should return nil' do
|
70
|
-
expect(m.url).to
|
80
|
+
expect(m.url).to be_nil
|
71
81
|
end
|
72
82
|
|
73
83
|
context 'new resource with id' do
|
74
84
|
let(:m) { model.new id: 475 }
|
75
85
|
|
76
86
|
it 'should return resource URL' do
|
77
|
-
expect(m.url).to
|
87
|
+
expect(m.url).to eq 'http://users.example.org/users/475'
|
78
88
|
end
|
79
89
|
end
|
80
90
|
|
81
91
|
context 'with attribute in path' do
|
82
92
|
it 'should return nil' do
|
83
|
-
expect(m.url).to
|
93
|
+
expect(m.url).to be_nil
|
84
94
|
end
|
85
95
|
end
|
86
96
|
end
|
87
97
|
|
88
98
|
context 'loaded resource' do
|
89
99
|
let(:m) { model.find 1 }
|
90
|
-
before { m
|
100
|
+
before { m && Acfs.run }
|
91
101
|
|
92
102
|
it "should return resource's URL" do
|
93
|
-
expect(m.url).to
|
103
|
+
expect(m.url).to eq 'http://users.example.org/users/1'
|
94
104
|
end
|
95
105
|
|
96
106
|
context 'with attribute in path' do
|
@@ -98,7 +108,7 @@ describe Acfs::Model::Locatable do
|
|
98
108
|
let(:m) { model.find user_id: 1 }
|
99
109
|
|
100
110
|
it "should return resource's URL" do
|
101
|
-
expect(m.url).to
|
111
|
+
expect(m.url).to eq 'http://users.example.org/users/2/profile'
|
102
112
|
end
|
103
113
|
end
|
104
114
|
end
|
@@ -1,33 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Acfs::
|
3
|
+
describe Acfs::Resource::Persistence do
|
4
4
|
let(:model_class) { MyUser }
|
5
5
|
before do
|
6
|
-
@get_stub = stub_request(:get, 'http://users.example.org/users/1').to_return response(
|
6
|
+
@get_stub = stub_request(:get, 'http://users.example.org/users/1').to_return response(id: 1, name: 'Anon', age: 12)
|
7
7
|
|
8
8
|
@patch_stub = stub_request(:put, 'http://users.example.org/users/1')
|
9
|
-
|
10
|
-
|
9
|
+
.with(body: '{"id":1,"name":"Idefix","age":12}')
|
10
|
+
.to_return response(id: 1, name: 'Idefix', age: 12)
|
11
11
|
|
12
12
|
@post_stub = stub_request(:post, 'http://users.example.org/users')
|
13
|
-
|
14
|
-
|
13
|
+
.with(body: '{"id":null,"name":"Idefix","age":12}')
|
14
|
+
.to_return response(id: 5, name: 'Idefix', age: 12)
|
15
15
|
|
16
16
|
stub_request(:post, 'http://users.example.org/users')
|
17
17
|
.with(body: '{"id":null,"name":"Anon","age":null}')
|
18
|
-
.to_return response(
|
18
|
+
.to_return response(id: 5, name: 'Anon', age: 12)
|
19
19
|
|
20
20
|
stub_request(:post, 'http://users.example.org/users')
|
21
21
|
.with(body: '{id:null,"name":"Idefix","age":12}')
|
22
|
-
.to_return response(
|
22
|
+
.to_return response(id: 5, name: 'Idefix', age: 12)
|
23
23
|
|
24
24
|
stub_request(:post, 'http://users.example.org/users')
|
25
25
|
.with(body: '{"id":null,"name":null,"age":12}')
|
26
|
-
.to_return response({
|
26
|
+
.to_return response({errors: {name: ['required']}}, {status: 422})
|
27
27
|
|
28
28
|
@del = stub_request(:delete, 'http://users.example.org/users/1')
|
29
|
-
|
30
|
-
|
29
|
+
.with(body: '{}')
|
30
|
+
.to_return response({id: 1, name: 'Idefix', age: 12}, {status: 200})
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'new model' do
|
@@ -65,7 +65,7 @@ describe Acfs::Model::Persistence do
|
|
65
65
|
let!(:req) do
|
66
66
|
stub_request(:post, 'http://users.example.org/users')
|
67
67
|
.with(body: '{"id":null,"name":"Idefix","age":null,"born_at":"Berlin"}')
|
68
|
-
.to_return response(
|
68
|
+
.to_return response(id: 5, name: 'Idefix', age: 12, wuff: 'woa')
|
69
69
|
end
|
70
70
|
let(:model) { model_class.new name: 'Idefix', born_at: 'Berlin' }
|
71
71
|
|
@@ -100,12 +100,12 @@ describe Acfs::Model::Persistence do
|
|
100
100
|
|
101
101
|
describe '#update_attributes' do
|
102
102
|
subject { -> { model.update_attributes name: 'John' } }
|
103
|
-
it { expect{ subject.call }.to raise_error Acfs::ResourceNotLoaded }
|
103
|
+
it { expect { subject.call }.to raise_error Acfs::ResourceNotLoaded }
|
104
104
|
end
|
105
105
|
|
106
106
|
describe '#update_attributes!' do
|
107
107
|
subject { -> { model.update_attributes! name: 'John' } }
|
108
|
-
it { expect{ subject.call }.to raise_error Acfs::ResourceNotLoaded }
|
108
|
+
it { expect { subject.call }.to raise_error Acfs::ResourceNotLoaded }
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
@@ -145,24 +145,24 @@ describe Acfs::Model::Persistence do
|
|
145
145
|
end
|
146
146
|
|
147
147
|
describe 'correct URL generation' do
|
148
|
-
let(:model_class) {PathArguments}
|
149
|
-
let(:model) {model_class.find 1, params: {required_arg: 'some_value'}}
|
148
|
+
let(:model_class) { PathArguments }
|
149
|
+
let(:model) { model_class.find 1, params: {required_arg: 'some_value'} }
|
150
150
|
|
151
151
|
before :each do
|
152
152
|
resource_url = 'http://users.example.org/some_value/users/1'
|
153
153
|
@my_get_stub = stub_request(:get, resource_url)
|
154
|
-
|
154
|
+
.to_return response(id: 1, required_arg: 'some_value')
|
155
155
|
@my_delete_stub = stub_request(:delete, resource_url)
|
156
|
-
|
157
|
-
|
156
|
+
.with(body: '{}')
|
157
|
+
.to_return response({id: 1, required_arg: 'some_value'}, {status: 200})
|
158
158
|
model
|
159
159
|
Acfs.run
|
160
160
|
end
|
161
161
|
|
162
162
|
it 'should not raise an error on URL generation' do
|
163
|
-
expect
|
163
|
+
expect do
|
164
164
|
model.delete!
|
165
|
-
|
165
|
+
end.not_to raise_error
|
166
166
|
end
|
167
167
|
|
168
168
|
it 'should request the delete' do
|
@@ -187,8 +187,8 @@ describe Acfs::Model::Persistence do
|
|
187
187
|
end
|
188
188
|
|
189
189
|
it 'should pass second hash to save' do
|
190
|
-
expect(model.__getobj__).to receive(:save).with(
|
191
|
-
model.update_attributes({
|
190
|
+
expect(model.__getobj__).to receive(:save).with(bla: 'blub')
|
191
|
+
model.update_attributes({name: 'Idefix'}, {bla: 'blub'})
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
@@ -207,8 +207,8 @@ describe Acfs::Model::Persistence do
|
|
207
207
|
end
|
208
208
|
|
209
209
|
it 'should pass second hash to save' do
|
210
|
-
expect(model.__getobj__).to receive(:save!).with(
|
211
|
-
model.update_attributes!({
|
210
|
+
expect(model.__getobj__).to receive(:save!).with(bla: 'blub')
|
211
|
+
model.update_attributes!({name: 'Idefix'}, {bla: 'blub'})
|
212
212
|
end
|
213
213
|
end
|
214
214
|
end
|
@@ -221,13 +221,13 @@ describe Acfs::Model::Persistence do
|
|
221
221
|
before do
|
222
222
|
stub_request(:put, 'http://users.example.org/users/1')
|
223
223
|
.with(body: '{"id":1,"name":"","age":12}')
|
224
|
-
.to_return response({
|
224
|
+
.to_return response({errors: {name: ['required']}}, {status: 422})
|
225
225
|
end
|
226
226
|
|
227
227
|
it 'should set local errors hash' do
|
228
228
|
model.name = ''
|
229
229
|
model.save! rescue nil
|
230
|
-
expect(model.errors.to_hash).to be == {
|
230
|
+
expect(model.errors.to_hash).to be == {name: %w(required)}
|
231
231
|
end
|
232
232
|
end
|
233
233
|
|
@@ -237,15 +237,15 @@ describe Acfs::Model::Persistence do
|
|
237
237
|
|
238
238
|
before do
|
239
239
|
stub_request(:put, 'http://users.example.org/users/1')
|
240
|
-
|
241
|
-
|
240
|
+
.with(body: '{"id":1,"name":"","age":12}')
|
241
|
+
.to_return response({errors: {name: ['required']}}, {status: 422})
|
242
242
|
end
|
243
243
|
end
|
244
244
|
end
|
245
245
|
|
246
246
|
describe '.create!' do
|
247
247
|
context 'with valid data' do
|
248
|
-
let(:data) { {
|
248
|
+
let(:data) { {name: 'Idefix', age: 12} }
|
249
249
|
|
250
250
|
it 'should create new resource' do
|
251
251
|
model = model_class.create! data
|
@@ -263,9 +263,9 @@ describe Acfs::Model::Persistence do
|
|
263
263
|
let(:data) { {name: nil, age: 12} }
|
264
264
|
|
265
265
|
it 'should raise an error' do
|
266
|
-
expect{ model_class.create! data }.to \
|
266
|
+
expect { model_class.create! data }.to \
|
267
267
|
raise_error(::Acfs::InvalidResource) do |error|
|
268
|
-
expect(error.errors).to be == {
|
268
|
+
expect(error.errors).to be == {'name' => %w(required)}
|
269
269
|
end
|
270
270
|
end
|
271
271
|
end
|
@@ -303,13 +303,13 @@ describe Acfs::Model::Persistence do
|
|
303
303
|
let!(:req) do
|
304
304
|
stub_request(:post, 'http://users.example.org/users')
|
305
305
|
.with(body: '{"id":null,"name":"Anon","age":9,"born_at":"today"}')
|
306
|
-
.to_return response(
|
306
|
+
.to_return response(id: 5, name: 'Anon', age: 9)
|
307
307
|
end
|
308
308
|
let(:data) { {age: 9, born_at: 'today'} }
|
309
309
|
|
310
310
|
it 'should store them in attributes' do
|
311
311
|
expect(subject.attributes).to eq 'id' => 5, 'name' => 'Anon',
|
312
|
-
|
312
|
+
'age' => 9, 'born_at' => 'today'
|
313
313
|
end
|
314
314
|
end
|
315
315
|
end
|
@@ -1,25 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Acfs::
|
3
|
+
describe Acfs::Resource::QueryMethods do
|
4
4
|
let(:model) { MyUser }
|
5
5
|
|
6
6
|
describe '.find' do
|
7
7
|
context 'with single id' do
|
8
8
|
context 'with successful response' do
|
9
9
|
before do
|
10
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
11
|
-
|
10
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
11
|
+
.to_return response id: 1, name: 'Anon', age: 12, born_at: 'Berlin'
|
12
|
+
stub_request(:get, 'http://users.example.org/users/2')
|
13
|
+
.to_return response id: 2, type: 'Customer',
|
14
|
+
name: 'Clare Customer', age: 24
|
12
15
|
end
|
13
16
|
|
14
|
-
let(:action) {
|
17
|
+
let(:action) { ->(cb = nil) { model.find(1, &cb) } }
|
15
18
|
it_behaves_like 'a query method with multi-callback support'
|
16
19
|
|
17
20
|
it 'should load a single remote resource' do
|
18
21
|
user = action.call
|
19
22
|
Acfs.run
|
20
23
|
|
21
|
-
expect(user.attributes).to eq
|
22
|
-
|
24
|
+
expect(user.attributes).to eq id: 1, name: 'Anon',
|
25
|
+
age: 12, born_at: 'Berlin'
|
23
26
|
end
|
24
27
|
|
25
28
|
context 'with resource type inheritance' do
|
@@ -40,7 +43,8 @@ describe Acfs::Model::QueryMethods do
|
|
40
43
|
|
41
44
|
context 'with 404 response' do
|
42
45
|
before do
|
43
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
46
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
47
|
+
.to_return response({error: 'not found'}, {status: 404})
|
44
48
|
end
|
45
49
|
|
46
50
|
it 'should raise a NotFound error' do
|
@@ -54,7 +58,8 @@ describe Acfs::Model::QueryMethods do
|
|
54
58
|
|
55
59
|
context 'with 500 response' do
|
56
60
|
before do
|
57
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
61
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
62
|
+
.to_return response(nil, status: 500)
|
58
63
|
end
|
59
64
|
|
60
65
|
it 'should raise a response error' do
|
@@ -69,55 +74,59 @@ describe Acfs::Model::QueryMethods do
|
|
69
74
|
|
70
75
|
context 'with multiple ids' do
|
71
76
|
before do
|
72
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
73
|
-
|
74
|
-
stub_request(:get, 'http://users.example.org/users/
|
75
|
-
|
77
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
78
|
+
.to_return response id: 1, name: 'Anon', age: 12
|
79
|
+
stub_request(:get, 'http://users.example.org/users/2')
|
80
|
+
.to_return response id: 2, name: 'Johnny', age: 42
|
81
|
+
stub_request(:get, 'http://users.example.org/users/3')
|
82
|
+
.to_return response id: 3, type: 'Customer', name: 'Anon', age: 12
|
83
|
+
stub_request(:get, 'http://users.example.org/users/4')
|
84
|
+
.to_return response id: 4, name: 'Johnny', age: 42
|
76
85
|
end
|
77
86
|
|
78
87
|
context 'with successful response' do
|
79
88
|
it 'should load a multiple remote resources' do
|
80
|
-
users = model.find
|
89
|
+
users = model.find([1, 2])
|
81
90
|
Acfs.run
|
82
91
|
|
83
92
|
expect(users.size).to be == 2
|
84
|
-
expect(users[0].attributes).to
|
85
|
-
expect(users[1].attributes).to
|
93
|
+
expect(users[0].attributes).to eq id: 1, name: 'Anon', age: 12
|
94
|
+
expect(users[1].attributes).to eq id: 2, name: 'Johnny', age: 42
|
86
95
|
end
|
87
96
|
|
88
97
|
it 'should invoke callback after all models are loaded' do
|
89
|
-
|
90
|
-
expect(
|
91
|
-
expect(users).to
|
98
|
+
block = proc {}
|
99
|
+
expect(block).to receive(:call) do |users|
|
100
|
+
expect(users).to equal @users
|
92
101
|
expect(users.size).to be == 2
|
93
102
|
expect(users).to be_loaded
|
94
103
|
end
|
95
104
|
|
96
|
-
@users = model.find
|
105
|
+
@users = model.find([1, 2], &block)
|
97
106
|
Acfs.run
|
98
107
|
end
|
99
108
|
|
100
109
|
it 'should invoke multiple callback after all models are loaded' do
|
101
|
-
proc1 =
|
102
|
-
proc2 =
|
110
|
+
proc1 = proc {}
|
111
|
+
proc2 = proc {}
|
103
112
|
expect(proc1).to receive(:call) do |users|
|
104
|
-
expect(users).to
|
113
|
+
expect(users).to equal @users
|
105
114
|
expect(users.size).to be == 2
|
106
115
|
expect(users).to be_loaded
|
107
116
|
end
|
108
117
|
expect(proc2).to receive(:call) do |users|
|
109
|
-
expect(users).to
|
118
|
+
expect(users).to equal @users
|
110
119
|
expect(users.size).to be == 2
|
111
120
|
expect(users).to be_loaded
|
112
121
|
end
|
113
122
|
|
114
|
-
@users = model.find
|
123
|
+
@users = model.find([1, 2], &proc1)
|
115
124
|
Acfs.add_callback(@users, &proc2)
|
116
125
|
Acfs.run
|
117
126
|
end
|
118
127
|
|
119
128
|
it 'should respect resource type inheritance' do
|
120
|
-
customers = MyUser.find 3, 4
|
129
|
+
customers = MyUser.find [3, 4]
|
121
130
|
Acfs.run
|
122
131
|
|
123
132
|
expect(customers[0]).to be_a Customer
|
@@ -127,11 +136,12 @@ describe Acfs::Model::QueryMethods do
|
|
127
136
|
|
128
137
|
context 'with one 404 response' do
|
129
138
|
before do
|
130
|
-
stub_request(:get, 'http://users.example.org/users/1')
|
139
|
+
stub_request(:get, 'http://users.example.org/users/1')
|
140
|
+
.to_return response({error: 'not found'}, {status: 404})
|
131
141
|
end
|
132
142
|
|
133
143
|
it 'should raise resource not found error' do
|
134
|
-
model.find 1, 2
|
144
|
+
model.find [1, 2]
|
135
145
|
|
136
146
|
expect { Acfs.run }.to raise_error(Acfs::ResourceNotFound)
|
137
147
|
end
|
@@ -144,24 +154,28 @@ describe Acfs::Model::QueryMethods do
|
|
144
154
|
let(:pc) { PC }
|
145
155
|
let(:mac) { Mac }
|
146
156
|
before do
|
147
|
-
stub_request(:get, 'http://computers.example.org/computers')
|
157
|
+
stub_request(:get, 'http://computers.example.org/computers')
|
158
|
+
.to_return response [
|
159
|
+
{id: 1, type: 'PC'},
|
160
|
+
{id: 2, type: 'Computer'},
|
161
|
+
{id: 3, type: 'Mac'}]
|
148
162
|
end
|
149
163
|
|
150
164
|
it 'should invoke multiple callback after all models are loaded' do
|
151
|
-
proc1 =
|
152
|
-
proc2 =
|
165
|
+
proc1 = proc {}
|
166
|
+
proc2 = proc {}
|
153
167
|
expect(proc1).to receive(:call) do |computers|
|
154
|
-
expect(computers).to
|
168
|
+
expect(computers).to equal @computers
|
155
169
|
expect(computers.size).to be == 3
|
156
170
|
expect(computers).to be_loaded
|
157
171
|
end
|
158
172
|
expect(proc2).to receive(:call) do |computers|
|
159
|
-
expect(computers).to
|
173
|
+
expect(computers).to equal @computers
|
160
174
|
expect(computers.size).to be == 3
|
161
175
|
expect(computers).to be_loaded
|
162
176
|
end
|
163
177
|
|
164
|
-
@computers = computer.all
|
178
|
+
@computers = computer.all(&proc1)
|
165
179
|
Acfs.add_callback(@computers, &proc2)
|
166
180
|
Acfs.run
|
167
181
|
end
|
@@ -191,21 +205,33 @@ describe Acfs::Model::QueryMethods do
|
|
191
205
|
|
192
206
|
context 'with another resource as type instead' do
|
193
207
|
before do
|
194
|
-
stub_request(:get, 'http://computers.example.org/computers')
|
208
|
+
stub_request(:get, 'http://computers.example.org/computers')
|
209
|
+
.to_return response [
|
210
|
+
{id: 1, type: 'MyUser'},
|
211
|
+
{id: 2, type: 'Computer'},
|
212
|
+
{id: 3, type: 'Mac'}]
|
195
213
|
end
|
196
214
|
it_behaves_like 'with invalid type'
|
197
215
|
end
|
198
216
|
|
199
217
|
context 'with a random string as type instead' do
|
200
218
|
before do
|
201
|
-
stub_request(:get, 'http://computers.example.org/computers')
|
219
|
+
stub_request(:get, 'http://computers.example.org/computers')
|
220
|
+
.to_return response [
|
221
|
+
{id: 1, type: 'PC'},
|
222
|
+
{id: 2, type: 'noValidType'},
|
223
|
+
{id: 3, type: 'Mac'}]
|
202
224
|
end
|
203
225
|
it_behaves_like 'with invalid type'
|
204
226
|
end
|
205
227
|
|
206
228
|
context 'with a non-string as type instead' do
|
207
229
|
before do
|
208
|
-
stub_request(:get, 'http://computers.example.org/computers')
|
230
|
+
stub_request(:get, 'http://computers.example.org/computers')
|
231
|
+
.to_return response [
|
232
|
+
{id: 1, type: 'PC'},
|
233
|
+
{id: 2, type: 'Computer'},
|
234
|
+
{id: 3, type: 42}]
|
209
235
|
end
|
210
236
|
it_behaves_like 'with invalid type'
|
211
237
|
end
|
@@ -217,7 +243,7 @@ describe Acfs::Model::QueryMethods do
|
|
217
243
|
context 'standard resource' do
|
218
244
|
let(:model) { MyUser }
|
219
245
|
let!(:user) { model.send described_method, age: 24 }
|
220
|
-
subject { Acfs.run
|
246
|
+
subject { Acfs.run && user }
|
221
247
|
|
222
248
|
context 'return value' do
|
223
249
|
subject { user }
|
@@ -227,7 +253,10 @@ describe Acfs::Model::QueryMethods do
|
|
227
253
|
end
|
228
254
|
|
229
255
|
context 'with params' do
|
230
|
-
let!(:request)
|
256
|
+
let!(:request) do
|
257
|
+
stub_request(:get, 'http://users.example.org/users?age=24')
|
258
|
+
.to_return response([{id: 1, name: 'Mike', age: 24}])
|
259
|
+
end
|
231
260
|
|
232
261
|
it 'should include params in URI to index action' do
|
233
262
|
subject
|
@@ -236,24 +265,30 @@ describe Acfs::Model::QueryMethods do
|
|
236
265
|
end
|
237
266
|
|
238
267
|
context 'with non-empty response' do
|
239
|
-
before
|
268
|
+
before do
|
269
|
+
stub_request(:get, 'http://users.example.org/users?age=24')
|
270
|
+
.to_return response [
|
271
|
+
{id: 1, name: 'Mike', age: 24},
|
272
|
+
{id: 4, type: 'Maria', age: 24},
|
273
|
+
{id: 7, type: 'James', age: 24}]
|
274
|
+
end
|
240
275
|
|
241
276
|
it 'should invoke callback after model is loaded' do
|
242
|
-
|
277
|
+
block = proc {}
|
243
278
|
|
244
|
-
expect(
|
279
|
+
expect(block).to receive(:call) do |user|
|
245
280
|
expect(user).to eql @user.__getobj__
|
246
281
|
expect(user).to be_a MyUser
|
247
282
|
expect(user).to be_loaded
|
248
283
|
end
|
249
284
|
|
250
|
-
@user = model.send described_method, age: 24, &
|
285
|
+
@user = model.send described_method, age: 24, &block
|
251
286
|
Acfs.run
|
252
287
|
end
|
253
288
|
|
254
289
|
it 'should invoke multiple callbacks after model is loaded' do
|
255
|
-
proc1 =
|
256
|
-
proc2 =
|
290
|
+
proc1 = proc {}
|
291
|
+
proc2 = proc {}
|
257
292
|
|
258
293
|
expect(proc1).to receive(:call) do |user|
|
259
294
|
expect(user).to eql @user.__getobj__
|
@@ -282,7 +317,7 @@ describe Acfs::Model::QueryMethods do
|
|
282
317
|
let(:model) { Single }
|
283
318
|
|
284
319
|
it '.find_by should not be defined' do
|
285
|
-
expect { model.find_by }.to raise_error
|
320
|
+
expect { model.find_by }.to raise_error ::Acfs::UnsupportedOperation
|
286
321
|
end
|
287
322
|
end
|
288
323
|
end
|
@@ -294,28 +329,31 @@ describe Acfs::Model::QueryMethods do
|
|
294
329
|
context 'standard resource' do
|
295
330
|
let(:model) { MyUser }
|
296
331
|
let!(:user) { model.send described_method, age: 24 }
|
297
|
-
subject { Acfs.run
|
332
|
+
subject { Acfs.run && user }
|
298
333
|
|
299
334
|
context 'with empty response' do
|
300
|
-
before
|
335
|
+
before do
|
336
|
+
stub_request(:get, 'http://users.example.org/users?age=24')
|
337
|
+
.to_return response []
|
338
|
+
end
|
301
339
|
|
302
340
|
it { should be_nil }
|
303
341
|
|
304
342
|
it 'should invoke callback after model is loaded' do
|
305
|
-
|
343
|
+
block = proc {}
|
306
344
|
|
307
|
-
expect(
|
345
|
+
expect(block).to receive(:call) do |user|
|
308
346
|
expect(user).to eql @user.__getobj__
|
309
347
|
expect(user).to be_a NilClass
|
310
348
|
end
|
311
349
|
|
312
|
-
@user = model.find_by age: 24, &
|
350
|
+
@user = model.find_by age: 24, &block
|
313
351
|
Acfs.run
|
314
352
|
end
|
315
353
|
|
316
354
|
it 'should invoke multiple callbacks after model is loaded' do
|
317
|
-
proc1 =
|
318
|
-
proc2 =
|
355
|
+
proc1 = proc {}
|
356
|
+
proc2 = proc {}
|
319
357
|
|
320
358
|
expect(proc1).to receive(:call) do |user|
|
321
359
|
expect(user).to eql @user.__getobj__
|
@@ -341,10 +379,13 @@ describe Acfs::Model::QueryMethods do
|
|
341
379
|
context 'standard resource' do
|
342
380
|
let(:model) { MyUser }
|
343
381
|
let!(:user) { model.send described_method, age: 24 }
|
344
|
-
subject { Acfs.run
|
382
|
+
subject { Acfs.run && user }
|
345
383
|
|
346
384
|
context 'with empty response' do
|
347
|
-
before
|
385
|
+
before do
|
386
|
+
stub_request(:get, 'http://users.example.org/users?age=24')
|
387
|
+
.to_return response []
|
388
|
+
end
|
348
389
|
|
349
390
|
it 'should raise an ResourceNotFound error' do
|
350
391
|
model.find_by! age: 24
|
@@ -352,11 +393,11 @@ describe Acfs::Model::QueryMethods do
|
|
352
393
|
end
|
353
394
|
|
354
395
|
it 'should not invoke callback after model could not be loaded' do
|
355
|
-
|
396
|
+
block = proc {}
|
356
397
|
|
357
|
-
expect(
|
398
|
+
expect(block).not_to receive(:call)
|
358
399
|
|
359
|
-
model.find_by! age: 24, &
|
400
|
+
model.find_by! age: 24, &block
|
360
401
|
expect { Acfs.run }.to raise_error
|
361
402
|
end
|
362
403
|
end
|
@@ -365,30 +406,30 @@ describe Acfs::Model::QueryMethods do
|
|
365
406
|
describe '#each_page' do
|
366
407
|
context 'without parameters' do
|
367
408
|
before do
|
368
|
-
stub_request(:get, 'http://users.example.org/users')
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
stub_request(:get, 'http://users.example.org/users?page=2')
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
stub_request(:get, 'http://users.example.org/users?page=3')
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
stub_request(:get, 'http://users.example.org/users?page=4')
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
409
|
+
stub_request(:get, 'http://users.example.org/users')
|
410
|
+
.to_return response([{id: 1, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
411
|
+
headers: {
|
412
|
+
'X-Total-Pages' => '4',
|
413
|
+
'Link' => '<http://users.example.org/users?page=2>; rel="next"'
|
414
|
+
})
|
415
|
+
stub_request(:get, 'http://users.example.org/users?page=2')
|
416
|
+
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
417
|
+
headers: {
|
418
|
+
'X-Total-Pages' => '4',
|
419
|
+
'Link' => '<http://users.example.org/users?page=3>; rel="next"'
|
420
|
+
})
|
421
|
+
stub_request(:get, 'http://users.example.org/users?page=3')
|
422
|
+
.to_return response([{id: 3, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
423
|
+
headers: {
|
424
|
+
'X-Total-Pages' => '4',
|
425
|
+
'Link' => '<http://users.example.org/users?page=4>; rel="next"'
|
426
|
+
})
|
427
|
+
stub_request(:get, 'http://users.example.org/users?page=4')
|
428
|
+
.to_return response([{id: 4, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
429
|
+
headers: {
|
430
|
+
'X-Total-Pages' => '4',
|
431
|
+
'Link' => ''
|
432
|
+
})
|
392
433
|
end
|
393
434
|
|
394
435
|
it 'should iterate all pages' do
|
@@ -407,30 +448,30 @@ describe Acfs::Model::QueryMethods do
|
|
407
448
|
|
408
449
|
context 'with parameters' do
|
409
450
|
before do
|
410
|
-
stub_request(:get, 'http://users.example.org/users?param=bla')
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
stub_request(:get, 'http://users.example.org/users?where=fuu&page=2')
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
stub_request(:get, 'http://users.example.org/users?page=3')
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
stub_request(:get, 'http://users.example.org/users?page=4')
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
451
|
+
stub_request(:get, 'http://users.example.org/users?param=bla')
|
452
|
+
.to_return response([{id: 1, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
453
|
+
headers: {
|
454
|
+
'X-Total-Pages' => '4',
|
455
|
+
'Link' => '<http://users.example.org/users?where=fuu&page=2>; rel="next"'
|
456
|
+
})
|
457
|
+
stub_request(:get, 'http://users.example.org/users?where=fuu&page=2')
|
458
|
+
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
459
|
+
headers: {
|
460
|
+
'X-Total-Pages' => '4',
|
461
|
+
'Link' => '<http://users.example.org/users?page=3>; rel="next"'
|
462
|
+
})
|
463
|
+
stub_request(:get, 'http://users.example.org/users?page=3')
|
464
|
+
.to_return response([{id: 3, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
465
|
+
headers: {
|
466
|
+
'X-Total-Pages' => '4',
|
467
|
+
'Link' => '<http://users.example.org/users?page=4>; rel="next"'
|
468
|
+
})
|
469
|
+
stub_request(:get, 'http://users.example.org/users?page=4')
|
470
|
+
.to_return response([{id: 4, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
471
|
+
headers: {
|
472
|
+
'X-Total-Pages' => '4',
|
473
|
+
'Link' => ''
|
474
|
+
})
|
434
475
|
end
|
435
476
|
|
436
477
|
it 'should call first page with params and follow relations' do
|
@@ -451,30 +492,30 @@ describe Acfs::Model::QueryMethods do
|
|
451
492
|
describe '#each_item' do
|
452
493
|
context 'without parameters' do
|
453
494
|
before do
|
454
|
-
stub_request(:get, 'http://users.example.org/users')
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
stub_request(:get, 'http://users.example.org/users?page=2')
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
stub_request(:get, 'http://users.example.org/users?page=3')
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
stub_request(:get, 'http://users.example.org/users?page=4')
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
495
|
+
stub_request(:get, 'http://users.example.org/users')
|
496
|
+
.to_return response([{id: 1, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
497
|
+
headers: {
|
498
|
+
'X-Total-Pages' => '4',
|
499
|
+
'Link' => '<http://users.example.org/users?page=2>; rel="next"'
|
500
|
+
})
|
501
|
+
stub_request(:get, 'http://users.example.org/users?page=2')
|
502
|
+
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
503
|
+
headers: {
|
504
|
+
'X-Total-Pages' => '4',
|
505
|
+
'Link' => '<http://users.example.org/users?page=3>; rel="next"'
|
506
|
+
})
|
507
|
+
stub_request(:get, 'http://users.example.org/users?page=3')
|
508
|
+
.to_return response([{id: 3, name: 'Anno', age: 1604, born_at: 'Santa Maria'}, {id: 4, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
509
|
+
headers: {
|
510
|
+
'X-Total-Pages' => '4',
|
511
|
+
'Link' => '<http://users.example.org/users?page=4>; rel="next"'
|
512
|
+
})
|
513
|
+
stub_request(:get, 'http://users.example.org/users?page=4')
|
514
|
+
.to_return response([{id: 5, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
515
|
+
headers: {
|
516
|
+
'X-Total-Pages' => '4',
|
517
|
+
'Link' => ''
|
518
|
+
})
|
478
519
|
end
|
479
520
|
|
480
521
|
it 'should iterate all pages' do
|
@@ -485,7 +526,7 @@ describe Acfs::Model::QueryMethods do
|
|
485
526
|
end
|
486
527
|
Acfs.run
|
487
528
|
|
488
|
-
expect(indecies).to eq [1,2,3,4,5]
|
529
|
+
expect(indecies).to eq [1, 2, 3, 4, 5]
|
489
530
|
end
|
490
531
|
end
|
491
532
|
end
|