acfs 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. metadata +8 -141
  3. data/CHANGELOG.md +0 -325
  4. data/LICENSE +0 -22
  5. data/README.md +0 -334
  6. data/acfs.gemspec +0 -37
  7. data/lib/acfs.rb +0 -49
  8. data/lib/acfs/adapter/base.rb +0 -26
  9. data/lib/acfs/adapter/typhoeus.rb +0 -60
  10. data/lib/acfs/collection.rb +0 -27
  11. data/lib/acfs/collections/paginatable.rb +0 -75
  12. data/lib/acfs/configuration.rb +0 -113
  13. data/lib/acfs/errors.rb +0 -106
  14. data/lib/acfs/global.rb +0 -99
  15. data/lib/acfs/location.rb +0 -78
  16. data/lib/acfs/middleware/base.rb +0 -20
  17. data/lib/acfs/middleware/json.rb +0 -27
  18. data/lib/acfs/middleware/logger.rb +0 -23
  19. data/lib/acfs/middleware/msgpack.rb +0 -30
  20. data/lib/acfs/middleware/print.rb +0 -21
  21. data/lib/acfs/middleware/serializer.rb +0 -39
  22. data/lib/acfs/operation.rb +0 -81
  23. data/lib/acfs/request.rb +0 -36
  24. data/lib/acfs/request/callbacks.rb +0 -50
  25. data/lib/acfs/resource.rb +0 -37
  26. data/lib/acfs/resource/attributes.rb +0 -268
  27. data/lib/acfs/resource/attributes/base.rb +0 -28
  28. data/lib/acfs/resource/attributes/boolean.rb +0 -37
  29. data/lib/acfs/resource/attributes/date_time.rb +0 -31
  30. data/lib/acfs/resource/attributes/dict.rb +0 -37
  31. data/lib/acfs/resource/attributes/float.rb +0 -31
  32. data/lib/acfs/resource/attributes/integer.rb +0 -27
  33. data/lib/acfs/resource/attributes/list.rb +0 -34
  34. data/lib/acfs/resource/attributes/string.rb +0 -24
  35. data/lib/acfs/resource/attributes/uuid.rb +0 -47
  36. data/lib/acfs/resource/dirty.rb +0 -35
  37. data/lib/acfs/resource/initialization.rb +0 -29
  38. data/lib/acfs/resource/loadable.rb +0 -33
  39. data/lib/acfs/resource/locatable.rb +0 -128
  40. data/lib/acfs/resource/operational.rb +0 -22
  41. data/lib/acfs/resource/persistence.rb +0 -257
  42. data/lib/acfs/resource/query_methods.rb +0 -264
  43. data/lib/acfs/resource/service.rb +0 -42
  44. data/lib/acfs/resource/validation.rb +0 -37
  45. data/lib/acfs/response.rb +0 -28
  46. data/lib/acfs/response/formats.rb +0 -25
  47. data/lib/acfs/response/status.rb +0 -31
  48. data/lib/acfs/rspec.rb +0 -11
  49. data/lib/acfs/runner.rb +0 -97
  50. data/lib/acfs/service.rb +0 -91
  51. data/lib/acfs/service/middleware.rb +0 -56
  52. data/lib/acfs/service/middleware/stack.rb +0 -63
  53. data/lib/acfs/singleton_resource.rb +0 -83
  54. data/lib/acfs/stub.rb +0 -172
  55. data/lib/acfs/util.rb +0 -20
  56. data/lib/acfs/version.rb +0 -14
  57. data/lib/acfs/yard.rb +0 -5
  58. data/spec/acfs/adapter/typhoeus_spec.rb +0 -28
  59. data/spec/acfs/collection_spec.rb +0 -155
  60. data/spec/acfs/configuration_spec.rb +0 -51
  61. data/spec/acfs/global_spec.rb +0 -137
  62. data/spec/acfs/location_spec.rb +0 -23
  63. data/spec/acfs/middleware/json_spec.rb +0 -63
  64. data/spec/acfs/middleware/msgpack_spec.rb +0 -60
  65. data/spec/acfs/operation_spec.rb +0 -10
  66. data/spec/acfs/request/callbacks_spec.rb +0 -46
  67. data/spec/acfs/request_spec.rb +0 -77
  68. data/spec/acfs/resource/attributes/boolean_spec.rb +0 -56
  69. data/spec/acfs/resource/attributes/date_time_spec.rb +0 -49
  70. data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
  71. data/spec/acfs/resource/attributes/float_spec.rb +0 -59
  72. data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
  73. data/spec/acfs/resource/attributes/list_spec.rb +0 -58
  74. data/spec/acfs/resource/attributes/uuid_spec.rb +0 -40
  75. data/spec/acfs/resource/attributes_spec.rb +0 -179
  76. data/spec/acfs/resource/dirty_spec.rb +0 -47
  77. data/spec/acfs/resource/initialization_spec.rb +0 -30
  78. data/spec/acfs/resource/loadable_spec.rb +0 -20
  79. data/spec/acfs/resource/locatable_spec.rb +0 -116
  80. data/spec/acfs/resource/persistance_spec.rb +0 -316
  81. data/spec/acfs/resource/query_methods_spec.rb +0 -541
  82. data/spec/acfs/resource/validation_spec.rb +0 -127
  83. data/spec/acfs/response/formats_spec.rb +0 -50
  84. data/spec/acfs/response/status_spec.rb +0 -69
  85. data/spec/acfs/runner_spec.rb +0 -97
  86. data/spec/acfs/service/middleware_spec.rb +0 -33
  87. data/spec/acfs/service_spec.rb +0 -46
  88. data/spec/acfs/singleton_resource_spec.rb +0 -15
  89. data/spec/acfs/stub_spec.rb +0 -343
  90. data/spec/acfs_spec.rb +0 -203
  91. data/spec/fixtures/config.yml +0 -14
  92. data/spec/spec_helper.rb +0 -41
  93. data/spec/support/hash.rb +0 -9
  94. data/spec/support/response.rb +0 -10
  95. data/spec/support/service.rb +0 -91
  96. data/spec/support/shared/find_callbacks.rb +0 -48
@@ -1,316 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Resource::Persistence do
4
- let(:model_class) { MyUser }
5
- before do
6
- @get_stub = stub_request(:get, 'http://users.example.org/users/1').to_return response(id: 1, name: 'Anon', age: 12)
7
-
8
- @patch_stub = stub_request(:put, 'http://users.example.org/users/1')
9
- .with(body: '{"id":1,"name":"Idefix","age":12}')
10
- .to_return response(id: 1, name: 'Idefix', age: 12)
11
-
12
- @post_stub = stub_request(:post, 'http://users.example.org/users')
13
- .with(body: '{"id":null,"name":"Idefix","age":12}')
14
- .to_return response(id: 5, name: 'Idefix', age: 12)
15
-
16
- stub_request(:post, 'http://users.example.org/users')
17
- .with(body: '{"id":null,"name":"Anon","age":null}')
18
- .to_return response(id: 5, name: 'Anon', age: 12)
19
-
20
- stub_request(:post, 'http://users.example.org/users')
21
- .with(body: '{id:null,"name":"Idefix","age":12}')
22
- .to_return response(id: 5, name: 'Idefix', age: 12)
23
-
24
- stub_request(:post, 'http://users.example.org/users')
25
- .with(body: '{"id":null,"name":null,"age":12}')
26
- .to_return response({errors: {name: ['required']}}, {status: 422})
27
-
28
- @del = stub_request(:delete, 'http://users.example.org/users/1')
29
- .with(body: '{}')
30
- .to_return response({id: 1, name: 'Idefix', age: 12}, {status: 200})
31
- end
32
-
33
- context 'new model' do
34
- let(:model) { model_class.new }
35
-
36
- it { expect(model).to_not be_persisted }
37
- it { expect(model).to be_new }
38
-
39
- describe '#save!' do
40
- context 'when modified' do
41
- let(:model) { model_class.find 1 }
42
- before do
43
- model
44
- Acfs.run
45
- model.name = 'Idefix'
46
- end
47
-
48
- it 'should PUT to model URL' do
49
- model.save!
50
-
51
- expect(@patch_stub).to have_been_requested
52
- end
53
- end
54
-
55
- context 'when new' do
56
- let(:model) { model_class.new name: 'Idefix', age: 12 }
57
-
58
- it 'should POST to collection URL' do
59
- model.save!
60
-
61
- expect(@post_stub).to have_been_requested
62
- end
63
-
64
- context 'with unknown attributes' do
65
- let!(:req) do
66
- stub_request(:post, 'http://users.example.org/users')
67
- .with(body: '{"id":null,"name":"Idefix","age":null,"born_at":"Berlin"}')
68
- .to_return response(id: 5, name: 'Idefix', age: 12, wuff: 'woa')
69
- end
70
- let(:model) { model_class.new name: 'Idefix', born_at: 'Berlin' }
71
-
72
- it 'should POST to collection URL' do
73
- model.save!
74
- expect(req).to have_been_requested
75
- end
76
-
77
- it 'should still have unknown attribute' do
78
- model.save!
79
- expect(model.attributes).to include 'born_at' => 'Berlin'
80
- end
81
-
82
- it 'should include server send unknown attribute' do
83
- model.save!
84
- expect(model.attributes).to include 'wuff' => 'woa'
85
- end
86
- end
87
- end
88
- end
89
-
90
- context 'after save' do
91
- before { model.save! }
92
-
93
- it { expect(model).to be_persisted }
94
- it { expect(model).to_not be_new }
95
- end
96
- end
97
-
98
- context 'unloaded model' do
99
- let!(:model) { model_class.find 1 }
100
-
101
- describe '#update_attributes' do
102
- subject { -> { model.update_attributes name: 'John' } }
103
- it { expect { subject.call }.to raise_error Acfs::ResourceNotLoaded }
104
- end
105
-
106
- describe '#update_attributes!' do
107
- subject { -> { model.update_attributes! name: 'John' } }
108
- it { expect { subject.call }.to raise_error Acfs::ResourceNotLoaded }
109
- end
110
- end
111
-
112
- context 'loaded model' do
113
- context 'without changes' do
114
- let(:model) { model_class.find 1 }
115
- before { model; Acfs.run }
116
-
117
- it { expect(model).to be_persisted }
118
- it { expect(model).to_not be_new }
119
- end
120
-
121
- context 'with changes' do
122
- let(:model) { model_class.find 1 }
123
- before { model; Acfs.run; model.name = 'dhh' }
124
-
125
- it { expect(model).to be_persisted }
126
- it { expect(model).to_not be_new }
127
- end
128
-
129
- describe '#delete!' do
130
- let(:model) { model_class.find 1 }
131
- let(:before_acfs_run) {}
132
-
133
- describe 'normal delete actions' do
134
- before { model; Acfs.run }
135
-
136
- it 'should trigger DELETE request' do
137
- model.delete!
138
- expect(@del).to have_been_requested
139
- end
140
-
141
- it 'should be frozen after DELETE' do
142
- model.delete!
143
- expect(model.__getobj__).to be_frozen
144
- end
145
- end
146
-
147
- describe 'correct URL generation' do
148
- let(:model_class) { PathArguments }
149
- let(:model) { model_class.find 1, params: {required_arg: 'some_value'} }
150
-
151
- before :each do
152
- resource_url = 'http://users.example.org/some_value/users/1'
153
- @my_get_stub = stub_request(:get, resource_url)
154
- .to_return response(id: 1, required_arg: 'some_value')
155
- @my_delete_stub = stub_request(:delete, resource_url)
156
- .with(body: '{}')
157
- .to_return response({id: 1, required_arg: 'some_value'}, {status: 200})
158
- model
159
- Acfs.run
160
- end
161
-
162
- it 'should not raise an error on URL generation' do
163
- expect do
164
- model.delete!
165
- end.not_to raise_error
166
- end
167
-
168
- it 'should request the delete' do
169
- model.delete!
170
- expect(@my_delete_stub).to have_been_requested
171
- end
172
- end
173
- end
174
-
175
- describe '#update_atributes!' do
176
- let(:model) { model_class.find 1 }
177
- before { model; Acfs.run }
178
-
179
- it 'should set attributes' do
180
- model.update_attributes name: 'Idefix'
181
- expect(model.attributes.symbolize_keys).to eq id: 1, name: 'Idefix', age: 12
182
- end
183
-
184
- it 'should save resource' do
185
- expect(model.__getobj__).to receive(:save).with({})
186
- model.update_attributes name: 'Idefix'
187
- end
188
-
189
- it 'should pass second hash to save' do
190
- expect(model.__getobj__).to receive(:save).with(bla: 'blub')
191
- model.update_attributes({name: 'Idefix'}, {bla: 'blub'})
192
- end
193
- end
194
-
195
- describe '#update_atributes' do
196
- let(:model) { model_class.find 1 }
197
- before { model; Acfs.run }
198
-
199
- it 'should set attributes' do
200
- model.update_attributes! name: 'Idefix'
201
- expect(model.attributes.symbolize_keys).to eq id: 1, name: 'Idefix', age: 12
202
- end
203
-
204
- it 'should save resource' do
205
- expect(model.__getobj__).to receive(:save!).with({})
206
- model.update_attributes! name: 'Idefix'
207
- end
208
-
209
- it 'should pass second hash to save' do
210
- expect(model.__getobj__).to receive(:save!).with(bla: 'blub')
211
- model.update_attributes!({name: 'Idefix'}, {bla: 'blub'})
212
- end
213
- end
214
- end
215
-
216
- describe '.save!' do
217
- context 'with invalid data validated on server side' do
218
- let(:model) { model_class.find 1 }
219
- before { model; Acfs.run }
220
-
221
- before do
222
- stub_request(:put, 'http://users.example.org/users/1')
223
- .with(body: '{"id":1,"name":"","age":12}')
224
- .to_return response({errors: {name: ['required']}}, {status: 422})
225
- end
226
-
227
- it 'should set local errors hash' do
228
- model.name = ''
229
- model.save! rescue nil
230
- expect(model.errors.to_hash).to be == {name: %w(required)}
231
- end
232
- end
233
-
234
- context 'hash modification on iteration in ActiveModel when errors on field is nil' do
235
- let(:model) { model_class.find 1 }
236
- before { model; Acfs.run }
237
-
238
- before do
239
- stub_request(:put, 'http://users.example.org/users/1')
240
- .with(body: '{"id":1,"name":"","age":12}')
241
- .to_return response({errors: {name: ['required']}}, {status: 422})
242
- end
243
- end
244
- end
245
-
246
- describe '.create!' do
247
- context 'with valid data' do
248
- let(:data) { {name: 'Idefix', age: 12} }
249
-
250
- it 'should create new resource' do
251
- model = model_class.create! data
252
- expect(model.name).to be == 'Idefix'
253
- expect(model.age).to be == 12
254
- end
255
-
256
- it 'should be persisted' do
257
- model = model_class.create! data
258
- expect(model).to be_persisted
259
- end
260
- end
261
-
262
- context 'with invalid data' do
263
- let(:data) { {name: nil, age: 12} }
264
-
265
- it 'should raise an error' do
266
- expect { model_class.create! data }.to \
267
- raise_error(::Acfs::InvalidResource) do |error|
268
- expect(error.errors).to be == {'name' => %w(required)}
269
- end
270
- end
271
- end
272
- end
273
-
274
- describe '.create' do
275
- subject { model_class.create data }
276
-
277
- context 'with valid data' do
278
- let(:data) { {name: 'Idefix', age: 12} }
279
-
280
- it 'should create new resource' do
281
- expect(subject.name).to be == 'Idefix'
282
- expect(subject.age).to be == 12
283
- end
284
-
285
- it 'should be persisted' do
286
- expect(subject).to be_persisted
287
- end
288
- end
289
-
290
- context 'with invalid data' do
291
- let(:data) { {name: nil, age: 12} }
292
-
293
- it 'should return not persisted resource' do
294
- expect(subject).to_not be_persisted
295
- end
296
-
297
- it 'should contain error hash' do
298
- expect(subject.errors.to_hash).to eq name: %w(required)
299
- end
300
- end
301
-
302
- context 'with additional data' do
303
- let!(:req) do
304
- stub_request(:post, 'http://users.example.org/users')
305
- .with(body: '{"id":null,"name":"Anon","age":9,"born_at":"today"}')
306
- .to_return response(id: 5, name: 'Anon', age: 9)
307
- end
308
- let(:data) { {age: 9, born_at: 'today'} }
309
-
310
- it 'should store them in attributes' do
311
- expect(subject.attributes).to eq 'id' => 5, 'name' => 'Anon',
312
- 'age' => 9, 'born_at' => 'today'
313
- end
314
- end
315
- end
316
- end
@@ -1,541 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Resource::QueryMethods do
4
- let(:model) { MyUser }
5
-
6
- describe '.find' do
7
- context 'with single id' do
8
- context 'with successful response' do
9
- before do
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
15
- end
16
-
17
- let(:action) { ->(cb = nil) { model.find(1, &cb) } }
18
- it_behaves_like 'a query method with multi-callback support'
19
-
20
- it 'should load a single remote resource' do
21
- user = action.call
22
- Acfs.run
23
-
24
- expect(user.attributes).to eq id: 1, name: 'Anon',
25
- age: 12, born_at: 'Berlin'
26
- end
27
-
28
- context 'with resource type inheritance' do
29
- let!(:user) { MyUser.find 2 }
30
- subject { user }
31
- before { Acfs.run }
32
-
33
- it 'should respect resource type inheritance' do
34
- expect(subject).to be_a Customer
35
- end
36
-
37
- it 'should implement ActiveModel class interface' do
38
- expect(subject.class).to be_a ActiveModel::Naming
39
- expect(subject.class).to be_a ActiveModel::Translation
40
- end
41
- end
42
- end
43
-
44
- context 'with 404 response' do
45
- before do
46
- stub_request(:get, 'http://users.example.org/users/1')
47
- .to_return response({error: 'not found'}, {status: 404})
48
- end
49
-
50
- it 'should raise a NotFound error' do
51
- @user = model.find 1
52
-
53
- expect { Acfs.run }.to raise_error(Acfs::ResourceNotFound)
54
-
55
- expect(@user).to_not be_loaded
56
- end
57
- end
58
-
59
- context 'with 500 response' do
60
- before do
61
- stub_request(:get, 'http://users.example.org/users/1')
62
- .to_return response(nil, status: 500)
63
- end
64
-
65
- it 'should raise a response error' do
66
- @user = model.find 1
67
-
68
- expect { Acfs.run }.to raise_error(Acfs::ErroneousResponse)
69
-
70
- expect(@user).to_not be_loaded
71
- end
72
- end
73
- end
74
-
75
- context 'with multiple ids' do
76
- before do
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
85
- end
86
-
87
- context 'with successful response' do
88
- it 'should load a multiple remote resources' do
89
- users = model.find([1, 2])
90
- Acfs.run
91
-
92
- expect(users.size).to be == 2
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
95
- end
96
-
97
- it 'should invoke callback after all models are loaded' do
98
- block = proc {}
99
- expect(block).to receive(:call) do |users|
100
- expect(users).to equal @users
101
- expect(users.size).to be == 2
102
- expect(users).to be_loaded
103
- end
104
-
105
- @users = model.find([1, 2], &block)
106
- Acfs.run
107
- end
108
-
109
- it 'should invoke multiple callback after all models are loaded' do
110
- proc1 = proc {}
111
- proc2 = proc {}
112
- expect(proc1).to receive(:call) do |users|
113
- expect(users).to equal @users
114
- expect(users.size).to be == 2
115
- expect(users).to be_loaded
116
- end
117
- expect(proc2).to receive(:call) do |users|
118
- expect(users).to equal @users
119
- expect(users.size).to be == 2
120
- expect(users).to be_loaded
121
- end
122
-
123
- @users = model.find([1, 2], &proc1)
124
- Acfs.add_callback(@users, &proc2)
125
- Acfs.run
126
- end
127
-
128
- it 'should respect resource type inheritance' do
129
- customers = MyUser.find [3, 4]
130
- Acfs.run
131
-
132
- expect(customers[0]).to be_a Customer
133
- expect(customers[1]).to be_a MyUser
134
- end
135
- end
136
-
137
- context 'with one 404 response' do
138
- before do
139
- stub_request(:get, 'http://users.example.org/users/1')
140
- .to_return response({error: 'not found'}, {status: 404})
141
- end
142
-
143
- it 'should raise resource not found error' do
144
- model.find [1, 2]
145
-
146
- expect { Acfs.run }.to raise_error(Acfs::ResourceNotFound)
147
- end
148
- end
149
- end
150
- end
151
-
152
- describe '.all' do
153
- let(:computer) { Computer }
154
- let(:pc) { PC }
155
- let(:mac) { Mac }
156
- before do
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'}]
162
- end
163
-
164
- it 'should invoke multiple callback after all models are loaded' do
165
- proc1 = proc {}
166
- proc2 = proc {}
167
- expect(proc1).to receive(:call) do |computers|
168
- expect(computers).to equal @computers
169
- expect(computers.size).to be == 3
170
- expect(computers).to be_loaded
171
- end
172
- expect(proc2).to receive(:call) do |computers|
173
- expect(computers).to equal @computers
174
- expect(computers.size).to be == 3
175
- expect(computers).to be_loaded
176
- end
177
-
178
- @computers = computer.all(&proc1)
179
- Acfs.add_callback(@computers, &proc2)
180
- Acfs.run
181
- end
182
-
183
- context 'with resource type inheritance' do
184
- it 'should create appropriate subclass resources' do
185
- @computers = Computer.all
186
-
187
- expect(@computers).to_not be_loaded
188
-
189
- Acfs.run
190
-
191
- expect(@computers).to be_loaded
192
- expect(@computers).to have(3).items
193
- expect(@computers[0]).to be_a PC
194
- expect(@computers[1]).to be_a Computer
195
- expect(@computers[2]).to be_a Mac
196
- end
197
-
198
- context 'with invalid type set' do
199
- shared_examples 'with invalid type' do
200
- it 'should raise error if type is no subclass' do
201
- Computer.all
202
- expect { Acfs.run }.to raise_error(Acfs::ResourceTypeError)
203
- end
204
- end
205
-
206
- context 'with another resource as type instead' do
207
- before do
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'}]
213
- end
214
- it_behaves_like 'with invalid type'
215
- end
216
-
217
- context 'with a random string as type instead' do
218
- before do
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'}]
224
- end
225
- it_behaves_like 'with invalid type'
226
- end
227
-
228
- context 'with a non-string as type instead' do
229
- before do
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}]
235
- end
236
- it_behaves_like 'with invalid type'
237
- end
238
- end
239
- end
240
- end
241
-
242
- shared_examples 'find_by' do
243
- context 'standard resource' do
244
- let(:model) { MyUser }
245
- let!(:user) { model.send described_method, age: 24 }
246
- subject { Acfs.run && user }
247
-
248
- context 'return value' do
249
- subject { user }
250
-
251
- it { should be_a MyUser }
252
- it { should_not be_loaded }
253
- end
254
-
255
- context 'with params' do
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
260
-
261
- it 'should include params in URI to index action' do
262
- subject
263
- expect(request).to have_been_requested
264
- end
265
- end
266
-
267
- context 'with non-empty response' do
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
275
-
276
- it 'should invoke callback after model is loaded' do
277
- block = proc {}
278
-
279
- expect(block).to receive(:call) do |user|
280
- expect(user).to eql @user.__getobj__
281
- expect(user).to be_a MyUser
282
- expect(user).to be_loaded
283
- end
284
-
285
- @user = model.send described_method, age: 24, &block
286
- Acfs.run
287
- end
288
-
289
- it 'should invoke multiple callbacks after model is loaded' do
290
- proc1 = proc {}
291
- proc2 = proc {}
292
-
293
- expect(proc1).to receive(:call) do |user|
294
- expect(user).to eql @user.__getobj__
295
- expect(user).to be_a MyUser
296
- expect(user).to be_loaded
297
- end
298
-
299
- expect(proc2).to receive(:call) do |user|
300
- expect(user).to eql @user.__getobj__
301
- expect(user).to be_a MyUser
302
- expect(user).to be_loaded
303
- end
304
-
305
- @user = model.send described_method, age: 24, &proc1
306
- Acfs.add_callback @user, &proc2
307
- Acfs.run
308
- end
309
-
310
- it 'should load a single MyUser object' do
311
- expect(subject).to be_a MyUser
312
- end
313
- end
314
- end
315
-
316
- context 'singleton resource' do
317
- let(:model) { Single }
318
-
319
- it '.find_by should not be defined' do
320
- expect { model.find_by }.to raise_error ::Acfs::UnsupportedOperation
321
- end
322
- end
323
- end
324
-
325
- describe '.find_by' do
326
- let(:described_method) { :find_by }
327
- it_behaves_like 'find_by'
328
-
329
- context 'standard resource' do
330
- let(:model) { MyUser }
331
- let!(:user) { model.send described_method, age: 24 }
332
- subject { Acfs.run && user }
333
-
334
- context 'with empty response' do
335
- before do
336
- stub_request(:get, 'http://users.example.org/users?age=24')
337
- .to_return response []
338
- end
339
-
340
- it { should be_nil }
341
-
342
- it 'should invoke callback after model is loaded' do
343
- block = proc {}
344
-
345
- expect(block).to receive(:call) do |user|
346
- expect(user).to eql @user.__getobj__
347
- expect(user).to be_a NilClass
348
- end
349
-
350
- @user = model.find_by age: 24, &block
351
- Acfs.run
352
- end
353
-
354
- it 'should invoke multiple callbacks after model is loaded' do
355
- proc1 = proc {}
356
- proc2 = proc {}
357
-
358
- expect(proc1).to receive(:call) do |user|
359
- expect(user).to eql @user.__getobj__
360
- expect(user).to be_a NilClass
361
- end
362
- expect(proc2).to receive(:call) do |user|
363
- expect(user).to eql @user.__getobj__
364
- expect(user).to be_a NilClass
365
- end
366
-
367
- @user = model.find_by age: 24, &proc1
368
- Acfs.add_callback @user, &proc2
369
- Acfs.run
370
- end
371
- end
372
- end
373
- end
374
-
375
- describe '.find_by!' do
376
- let(:described_method) { :find_by! }
377
- it_behaves_like 'find_by'
378
-
379
- context 'standard resource' do
380
- let(:model) { MyUser }
381
- let!(:user) { model.send described_method, age: 24 }
382
- subject { Acfs.run && user }
383
-
384
- context 'with empty response' do
385
- before do
386
- stub_request(:get, 'http://users.example.org/users?age=24')
387
- .to_return response []
388
- end
389
-
390
- it 'should raise an ResourceNotFound error' do
391
- model.find_by! age: 24
392
- expect { Acfs.run }.to raise_error Acfs::ResourceNotFound
393
- end
394
-
395
- it 'should not invoke callback after model could not be loaded' do
396
- block = proc {}
397
-
398
- expect(block).not_to receive(:call)
399
-
400
- model.find_by! age: 24, &block
401
- expect { Acfs.run }.to raise_error Acfs::ResourceNotFound
402
- end
403
- end
404
- end
405
-
406
- describe '#each_page' do
407
- context 'without parameters' do
408
- before do
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
- })
433
- end
434
-
435
- it 'should iterate all pages' do
436
- index = 0
437
- model.each_page do |page|
438
- expect(page).to be_a Acfs::Collection
439
-
440
- index += 1
441
- expect(page.first.id).to eq index
442
- end
443
- Acfs.run
444
-
445
- expect(index).to eq 4
446
- end
447
- end
448
-
449
- context 'with parameters' do
450
- before do
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
- })
475
- end
476
-
477
- it 'should call first page with params and follow relations' do
478
- index = 0
479
- model.each_page(param: 'bla') do |page|
480
- expect(page).to be_a Acfs::Collection
481
-
482
- index += 1
483
- expect(page.first.id).to eq index
484
- end
485
- Acfs.run
486
-
487
- expect(index).to eq 4
488
- end
489
- end
490
- end
491
-
492
- describe '#each_item' do
493
- context 'without parameters' do
494
- before do
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
- })
519
- end
520
-
521
- it 'should iterate all pages' do
522
- indices = []
523
- model.each_item do |item|
524
- expect(item).to be_a MyUser
525
- indices << item.id
526
- end
527
- Acfs.run
528
-
529
- expect(indices).to eq [1, 2, 3, 4, 5]
530
- end
531
-
532
- it 'should pass the collection to the provided block' do
533
- model.each_item do |_item, collection|
534
- expect(collection).to be_a Acfs::Collection
535
- end
536
- Acfs.run
537
- end
538
- end
539
- end
540
- end
541
- end