acfs 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -0
- data/README.md +16 -19
- data/acfs.gemspec +2 -0
- data/lib/acfs/adapter/typhoeus.rb +6 -4
- data/lib/acfs/configuration.rb +13 -3
- data/lib/acfs/errors.rb +1 -1
- data/lib/acfs/global.rb +2 -2
- data/lib/acfs/location.rb +1 -1
- data/lib/acfs/operation.rb +1 -1
- data/lib/acfs/request/callbacks.rb +1 -1
- data/lib/acfs/request.rb +1 -1
- data/lib/acfs/resource/attributes/uuid.rb +1 -1
- data/lib/acfs/resource/locatable.rb +2 -2
- data/lib/acfs/resource/query_methods.rb +2 -2
- data/lib/acfs/runner.rb +15 -15
- data/lib/acfs/stub.rb +34 -29
- data/lib/acfs/version.rb +2 -2
- data/spec/acfs/adapter/typhoeus_spec.rb +1 -1
- data/spec/acfs/collection_spec.rb +66 -41
- data/spec/acfs/configuration_spec.rb +22 -12
- data/spec/acfs/global_spec.rb +9 -7
- data/spec/acfs/middleware/json_spec.rb +8 -8
- data/spec/acfs/middleware/{msgpack_spec.rb → message_pack_spec.rb} +6 -6
- data/spec/acfs/operation_spec.rb +3 -2
- data/spec/acfs/request/callbacks_spec.rb +19 -10
- data/spec/acfs/request_spec.rb +15 -19
- data/spec/acfs/resource/attributes/boolean_spec.rb +32 -32
- data/spec/acfs/resource/attributes/date_time_spec.rb +16 -8
- data/spec/acfs/resource/attributes/dict_spec.rb +15 -9
- data/spec/acfs/resource/attributes/float_spec.rb +20 -10
- data/spec/acfs/resource/attributes/integer_spec.rb +10 -5
- data/spec/acfs/resource/attributes/list_spec.rb +13 -8
- data/spec/acfs/resource/attributes/uuid_spec.rb +12 -6
- data/spec/acfs/resource/attributes_spec.rb +33 -32
- data/spec/acfs/resource/dirty_spec.rb +6 -3
- data/spec/acfs/resource/initialization_spec.rb +4 -5
- data/spec/acfs/resource/loadable_spec.rb +3 -1
- data/spec/acfs/resource/locatable_spec.rb +24 -18
- data/spec/acfs/resource/{persistance_spec.rb → persistence_spec.rb} +114 -82
- data/spec/acfs/resource/query_methods_spec.rb +143 -110
- data/spec/acfs/resource/validation_spec.rb +34 -27
- data/spec/acfs/response/formats_spec.rb +8 -8
- data/spec/acfs/response/status_spec.rb +16 -9
- data/spec/acfs/runner_spec.rb +10 -8
- data/spec/acfs/service/middleware_spec.rb +3 -3
- data/spec/acfs/service_spec.rb +5 -4
- data/spec/acfs/singleton_resource_spec.rb +2 -1
- data/spec/acfs/stub_spec.rb +57 -53
- data/spec/acfs_spec.rb +111 -93
- data/spec/spec_helper.rb +1 -1
- data/spec/support/response.rb +2 -2
- data/spec/support/service.rb +1 -1
- data/spec/support/shared/find_callbacks.rb +14 -10
- metadata +9 -8
@@ -10,61 +10,70 @@ describe Acfs::Resource::Validation do
|
|
10
10
|
context 'with valid attributes' do
|
11
11
|
subject { model }
|
12
12
|
|
13
|
-
it {
|
13
|
+
it { is_expected.to be_valid }
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'with invalid attributes' do
|
17
|
-
let(:params) { {name: 'invname'} }
|
18
17
|
subject { model }
|
19
18
|
|
20
|
-
|
19
|
+
let(:params) { {name: 'invname'} }
|
20
|
+
|
21
|
+
it { is_expected.not_to be_valid }
|
21
22
|
end
|
22
23
|
|
23
24
|
context 'on resource with service side errors' do
|
25
|
+
subject(:resource) { MyUser.create(params) }
|
26
|
+
|
24
27
|
before { Acfs::Stub.enable }
|
28
|
+
|
25
29
|
after { Acfs::Stub.disable }
|
26
30
|
|
27
31
|
before do
|
28
32
|
Acfs::Stub.resource MyUser, :create, return: {errors: {name: ['can\'t be blank']}}, raise: 422
|
29
33
|
end
|
30
34
|
|
31
|
-
let(:params)
|
32
|
-
let(:resource) { MyUser.create params }
|
33
|
-
subject { resource }
|
35
|
+
let(:params) { {} }
|
34
36
|
|
35
|
-
it {
|
37
|
+
it { is_expected.not_to be_valid }
|
36
38
|
|
37
|
-
it '
|
38
|
-
|
39
|
-
expect(
|
39
|
+
it 'does not override errors' do
|
40
|
+
resource.valid?
|
41
|
+
expect(resource.errors.to_hash).to eq(name: ['can\'t be blank'])
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
46
|
describe '#errors' do
|
45
47
|
context 'with valid attributes' do
|
48
|
+
subject { model.errors }
|
49
|
+
|
46
50
|
let(:params) { {name: 'john smith', age: 24} }
|
51
|
+
|
47
52
|
before { model.valid? }
|
48
|
-
subject { model.errors }
|
49
53
|
|
50
|
-
it {
|
54
|
+
it { is_expected.to be_empty }
|
51
55
|
end
|
52
56
|
|
53
57
|
context 'with invalid attributes' do
|
58
|
+
subject(:errors) { model.errors }
|
59
|
+
|
54
60
|
let(:params) { {name: 'john'} }
|
61
|
+
|
55
62
|
before { model.valid? }
|
56
|
-
subject { model.errors }
|
57
63
|
|
58
|
-
it {
|
59
|
-
it {
|
64
|
+
it { is_expected.not_to be_empty }
|
65
|
+
it { is_expected.to have(2).items }
|
60
66
|
|
61
|
-
it '
|
62
|
-
expect(
|
67
|
+
it 'contains a list of error messages' do
|
68
|
+
expect(errors.to_hash).to eq age: ["can't be blank"], name: ['is invalid']
|
63
69
|
end
|
64
70
|
end
|
65
71
|
|
66
72
|
context 'server side errors' do
|
73
|
+
subject { resource.errors.to_hash }
|
74
|
+
|
67
75
|
before { Acfs::Stub.enable }
|
76
|
+
|
68
77
|
after { Acfs::Stub.disable }
|
69
78
|
|
70
79
|
before do
|
@@ -74,7 +83,6 @@ describe Acfs::Resource::Validation do
|
|
74
83
|
|
75
84
|
let(:params) { {} }
|
76
85
|
let(:resource) { MyUser.create params }
|
77
|
-
subject { resource.errors.to_hash }
|
78
86
|
|
79
87
|
context 'with `field => [messages]` payload' do
|
80
88
|
let(:errors) { {name: ['cannot be blank']} }
|
@@ -97,33 +105,32 @@ describe Acfs::Resource::Validation do
|
|
97
105
|
end
|
98
106
|
|
99
107
|
describe '#save!' do
|
100
|
-
subject { -> { model.save! } }
|
108
|
+
subject(:save) { -> { model.save! } }
|
109
|
+
|
101
110
|
before { allow(model).to receive(:operation) }
|
102
111
|
|
103
112
|
context 'with invalid attributes' do
|
104
113
|
let(:params) { {name: 'john'} }
|
105
114
|
|
106
|
-
it { expect {
|
115
|
+
it { expect { save.call }.to raise_error Acfs::InvalidResource }
|
107
116
|
end
|
108
117
|
|
109
118
|
context 'on new resource' do
|
110
|
-
it '
|
119
|
+
it 'validates with `create` context' do
|
111
120
|
expect(model).to receive(:valid?).with(:create).and_call_original
|
112
|
-
|
121
|
+
save.call
|
113
122
|
end
|
114
123
|
end
|
115
124
|
|
116
125
|
context 'on changed resource' do
|
117
126
|
before { model.loaded! }
|
127
|
+
|
118
128
|
let(:model) { super().tap {|m| m.id = 1 } }
|
119
129
|
|
120
|
-
it '
|
130
|
+
it 'validates with `save` context' do
|
121
131
|
expect(model).to receive(:valid?).with(:save).and_call_original
|
122
|
-
|
132
|
+
save.call
|
123
133
|
end
|
124
134
|
end
|
125
135
|
end
|
126
|
-
|
127
|
-
describe 'validates with context' do
|
128
|
-
end
|
129
136
|
end
|
@@ -13,8 +13,8 @@ describe Acfs::Response::Formats do
|
|
13
13
|
context 'without Content-Type header' do
|
14
14
|
let(:headers) { {} }
|
15
15
|
|
16
|
-
it "
|
17
|
-
expect(response.content_type).to
|
16
|
+
it "fallbacks on 'text/plain'" do
|
17
|
+
expect(response.content_type).to eq Mime[:text]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -22,13 +22,13 @@ describe Acfs::Response::Formats do
|
|
22
22
|
let(:mime_type) { 'application/json' }
|
23
23
|
|
24
24
|
describe '#content_type' do
|
25
|
-
it '
|
26
|
-
expect(response.content_type).to
|
25
|
+
it 'returns Mime::JSON' do
|
26
|
+
expect(response.content_type).to eq Mime[:json]
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
describe '#json?' do
|
31
|
-
it '
|
31
|
+
it 'returns true' do
|
32
32
|
expect(response).to be_json
|
33
33
|
end
|
34
34
|
end
|
@@ -37,13 +37,13 @@ describe Acfs::Response::Formats do
|
|
37
37
|
let(:mime_type) { 'application/json; charset=utf8' }
|
38
38
|
|
39
39
|
describe '#content_type' do
|
40
|
-
it '
|
41
|
-
expect(response.content_type).to
|
40
|
+
it 'returns Mime::JSON' do
|
41
|
+
expect(response.content_type).to eq Mime[:json]
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe '#json?' do
|
46
|
-
it '
|
46
|
+
it 'returns true' do
|
47
47
|
expect(response).to be_json
|
48
48
|
end
|
49
49
|
end
|
@@ -14,18 +14,18 @@ describe Acfs::Response::Status do
|
|
14
14
|
context 'when given' do
|
15
15
|
let(:status) { 200 }
|
16
16
|
|
17
|
-
it '
|
18
|
-
expect(response.code).to
|
19
|
-
expect(response.status_code).to
|
17
|
+
it 'returns status code' do
|
18
|
+
expect(response.code).to eq 200
|
19
|
+
expect(response.status_code).to eq 200
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'when nil' do
|
24
24
|
let(:status) { nil }
|
25
25
|
|
26
|
-
it '
|
27
|
-
expect(response.code).to
|
28
|
-
expect(response.status_code).to
|
26
|
+
it 'returns zero' do
|
27
|
+
expect(response.code).to eq 0
|
28
|
+
expect(response.status_code).to eq 0
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -33,38 +33,45 @@ describe Acfs::Response::Status do
|
|
33
33
|
describe '#success?' do
|
34
34
|
context 'with success status code' do
|
35
35
|
let(:status) { 200 }
|
36
|
+
|
36
37
|
it { expect(response).to be_success }
|
37
38
|
end
|
38
39
|
|
39
40
|
context 'with error status code' do
|
40
41
|
let(:status) { 500 }
|
41
|
-
|
42
|
+
|
43
|
+
it { expect(response).not_to be_success }
|
42
44
|
end
|
43
45
|
|
44
46
|
context 'with zero status code' do
|
45
47
|
let(:status) { nil }
|
46
|
-
|
48
|
+
|
49
|
+
it { expect(response).not_to be_success }
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
53
|
describe '#modified?' do
|
51
54
|
context 'with success status code' do
|
52
55
|
let(:status) { 200 }
|
56
|
+
|
53
57
|
it { expect(response).to be_modified }
|
54
58
|
end
|
55
59
|
|
56
60
|
context 'with not modified status code' do
|
57
61
|
let(:status) { 304 }
|
58
|
-
|
62
|
+
|
63
|
+
it { expect(response).not_to be_modified }
|
59
64
|
end
|
60
65
|
|
61
66
|
context 'with error status code' do
|
62
67
|
let(:status) { 500 }
|
68
|
+
|
63
69
|
it { expect(response).to be_modified }
|
64
70
|
end
|
65
71
|
|
66
72
|
context 'with zero status code' do
|
67
73
|
let(:status) { nil }
|
74
|
+
|
68
75
|
it { expect(response).to be_modified }
|
69
76
|
end
|
70
77
|
end
|
data/spec/acfs/runner_spec.rb
CHANGED
@@ -48,7 +48,7 @@ describe ::Acfs::Runner do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
describe '#process' do
|
51
|
-
it '
|
51
|
+
it 'triggers event' do
|
52
52
|
runner.process ::Acfs::Operation.new MyUser, :read, params: {id: 0}
|
53
53
|
expect(collector.events).to have(1).items
|
54
54
|
expect(collector2.events).to have(1).items
|
@@ -56,7 +56,7 @@ describe ::Acfs::Runner do
|
|
56
56
|
end
|
57
57
|
|
58
58
|
describe '#run' do
|
59
|
-
it '
|
59
|
+
it 'triggers event' do
|
60
60
|
runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
|
61
61
|
expect(collector.events).to have(1).items
|
62
62
|
expect(collector2.events).to have(0).items
|
@@ -64,7 +64,7 @@ describe ::Acfs::Runner do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
describe '#enqueue' do
|
67
|
-
it '
|
67
|
+
it 'triggers event' do
|
68
68
|
runner.enqueue ::Acfs::Operation.new MyUser, :read, params: {id: 0}
|
69
69
|
expect(collector.events).to have(1).items
|
70
70
|
expect(collector2.events).to have(0).items
|
@@ -76,18 +76,20 @@ describe ::Acfs::Runner do
|
|
76
76
|
before do
|
77
77
|
expect_any_instance_of(UserService).to receive(:prepare).and_return nil
|
78
78
|
end
|
79
|
-
|
80
|
-
|
79
|
+
|
80
|
+
it 'does not do requests when a middleware aborted' do
|
81
|
+
expect(adapter).not_to receive :run
|
81
82
|
runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
|
82
83
|
end
|
83
84
|
end
|
84
85
|
|
85
86
|
describe '#enqueue' do
|
86
87
|
before do
|
87
|
-
expect_any_instance_of(UserService).to receive(:prepare).and_return
|
88
|
+
expect_any_instance_of(UserService).to receive(:prepare).and_return(nil)
|
88
89
|
end
|
89
|
-
|
90
|
-
|
90
|
+
|
91
|
+
it 'does not do requests when a middleware aborted' do
|
92
|
+
expect(adapter).not_to receive(:queue)
|
91
93
|
runner.enqueue ::Acfs::Operation.new MyUser, :read, params: {id: 0}
|
92
94
|
runner.start
|
93
95
|
end
|
@@ -13,19 +13,19 @@ describe Acfs::Service::Middleware do
|
|
13
13
|
describe '.use' do
|
14
14
|
let(:options) { {abc: 'cde'} }
|
15
15
|
|
16
|
-
it '
|
16
|
+
it 'adds middleware to list' do
|
17
17
|
srv_class.use middleware
|
18
18
|
|
19
19
|
expect(srv_class.middleware).to include(middleware)
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'adds middleware to stack' do
|
23
23
|
srv_class.use middleware
|
24
24
|
|
25
25
|
expect(srv_class.middleware.build(1)).to be_a(middleware)
|
26
26
|
end
|
27
27
|
|
28
|
-
it '
|
28
|
+
it 'instantiates middleware object' do
|
29
29
|
expect(middleware).to receive(:new).with(anything, options)
|
30
30
|
|
31
31
|
srv_class.use middleware, options
|
data/spec/acfs/service_spec.rb
CHANGED
@@ -14,23 +14,24 @@ describe Acfs::Service do
|
|
14
14
|
describe '#initialize' do
|
15
15
|
let(:options) { {path: 'abc', key: 'value'} }
|
16
16
|
|
17
|
-
it '
|
17
|
+
it 'sets options' do
|
18
18
|
expect(service.options).to eq(options)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe '#location' do
|
23
23
|
let(:resource) { Class.new }
|
24
|
+
|
24
25
|
before { allow(resource).to receive(:location_default_path, &proc {|_a, p| p }) }
|
25
26
|
|
26
|
-
it '
|
27
|
+
it 'extracts resource path name from given class' do
|
27
28
|
expect(service.location(resource).to_s).to eq('/classes')
|
28
29
|
end
|
29
30
|
|
30
31
|
context 'with path options' do
|
31
32
|
let(:options) { {path: 'abc'} }
|
32
33
|
|
33
|
-
it '
|
34
|
+
it 'has custom resource path' do
|
34
35
|
expect(service.location(resource).to_s).to eq('/abc')
|
35
36
|
end
|
36
37
|
end
|
@@ -41,7 +42,7 @@ describe Acfs::Service do
|
|
41
42
|
Acfs::Configuration.current.locate :test, 'http://abc.de/api/v1'
|
42
43
|
end
|
43
44
|
|
44
|
-
it '
|
45
|
+
it 'returns configured URI for service' do
|
45
46
|
expect(srv_class.base_url).to eq('http://abc.de/api/v1')
|
46
47
|
end
|
47
48
|
end
|
data/spec/acfs/stub_spec.rb
CHANGED
@@ -7,8 +7,9 @@ class SpecialCustomError < StandardError; end
|
|
7
7
|
describe Acfs::Stub do
|
8
8
|
let(:stub) { Class.new(Acfs::Stub) }
|
9
9
|
|
10
|
-
before(:all) { Acfs::Stub.enable }
|
11
|
-
|
10
|
+
before(:all) { Acfs::Stub.enable } # rubocop:disable RSpec/BeforeAfterAll
|
11
|
+
|
12
|
+
after(:all) { Acfs::Stub.disable } # rubocop:disable RSpec/BeforeAfterAll
|
12
13
|
|
13
14
|
before do
|
14
15
|
Acfs::Stub.allow_requests = false
|
@@ -16,16 +17,18 @@ describe Acfs::Stub do
|
|
16
17
|
|
17
18
|
describe '#called?' do
|
18
19
|
context 'without specified number' do
|
19
|
-
let!(:operation)
|
20
|
+
let!(:operation) do
|
21
|
+
Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
|
22
|
+
end
|
20
23
|
|
21
|
-
it '
|
24
|
+
it 'allows to test if stub was called' do
|
22
25
|
MyUser.find 1
|
23
26
|
Acfs.run
|
24
27
|
|
25
28
|
expect(operation).to be_called
|
26
29
|
end
|
27
30
|
|
28
|
-
it '
|
31
|
+
it 'allows to test if stub was called a specific number of times' do
|
29
32
|
MyUser.find 1
|
30
33
|
Acfs.run
|
31
34
|
|
@@ -44,7 +47,7 @@ describe Acfs::Stub do
|
|
44
47
|
Acfs::Stub.resource MyUser, :read, with: {id: 1}, raise: :not_found
|
45
48
|
end
|
46
49
|
|
47
|
-
it '
|
50
|
+
it 'raises error' do
|
48
51
|
MyUser.find 1
|
49
52
|
|
50
53
|
expect { Acfs.run }.to raise_error(Acfs::AmbiguousStubError)
|
@@ -58,23 +61,23 @@ describe Acfs::Stub do
|
|
58
61
|
Acfs::Stub.resource MyUser, :read, with: {id: 3}, raise: :not_found
|
59
62
|
end
|
60
63
|
|
61
|
-
it '
|
64
|
+
it 'allows to stub resource reads' do
|
62
65
|
user = MyUser.find 1
|
63
66
|
Acfs.run
|
64
67
|
|
65
|
-
expect(user.id).to
|
66
|
-
expect(user.name).to
|
67
|
-
expect(user.age).to
|
68
|
+
expect(user.id).to eq 1
|
69
|
+
expect(user.name).to eq 'John Smith'
|
70
|
+
expect(user.age).to eq 32
|
68
71
|
end
|
69
72
|
|
70
73
|
context 'with error' do
|
71
|
-
it '
|
74
|
+
it 'allows to raise errors' do
|
72
75
|
MyUser.find 2
|
73
76
|
|
74
77
|
expect { Acfs.run }.to raise_error(SpecialCustomError)
|
75
78
|
end
|
76
79
|
|
77
|
-
it '
|
80
|
+
it 'allows to raise symbolic errors' do
|
78
81
|
MyUser.find 3
|
79
82
|
|
80
83
|
expect { Acfs.run }.to raise_error(Acfs::ResourceNotFound)
|
@@ -87,7 +90,7 @@ describe Acfs::Stub do
|
|
87
90
|
Acfs::Stub.resource Computer, :read, with: {id: 2}, return: {id: 2, type: 'Mac'}
|
88
91
|
end
|
89
92
|
|
90
|
-
it '
|
93
|
+
it 'creates inherited type' do
|
91
94
|
pc = Computer.find 1
|
92
95
|
mac = Computer.find 2
|
93
96
|
|
@@ -101,18 +104,26 @@ describe Acfs::Stub do
|
|
101
104
|
|
102
105
|
context 'with create action' do
|
103
106
|
before do
|
104
|
-
|
105
|
-
|
107
|
+
lmbd = lambda {|op|
|
108
|
+
op.data[:ident] == 'john@exmaple.org' && op.data[:password] == 'wrong'
|
109
|
+
}
|
110
|
+
|
111
|
+
Acfs::Stub.resource Session, :create,
|
112
|
+
with: {ident: 'john@exmaple.org', password: 's3cr3t'},
|
113
|
+
return: {id: 'longhash', user: 1}
|
114
|
+
Acfs::Stub.resource Session, :create,
|
115
|
+
with: lmbd,
|
116
|
+
raise: 422
|
106
117
|
end
|
107
118
|
|
108
|
-
it '
|
119
|
+
it 'allows stub resource creation' do
|
109
120
|
session = Session.create! ident: 'john@exmaple.org', password: 's3cr3t'
|
110
121
|
|
111
|
-
expect(session.id).to
|
112
|
-
expect(session.user).to
|
122
|
+
expect(session.id).to eq 'longhash'
|
123
|
+
expect(session.user).to eq 1
|
113
124
|
end
|
114
125
|
|
115
|
-
it '
|
126
|
+
it 'allows to raise error' do
|
116
127
|
expect do
|
117
128
|
Session.create! ident: 'john@exmaple.org', password: 'wrong'
|
118
129
|
end.to raise_error(::Acfs::InvalidResource)
|
@@ -125,14 +136,14 @@ describe Acfs::Stub do
|
|
125
136
|
return: [{id: 1, name: 'John Smith', age: 32}, {id: 2, name: 'Anon', age: 12}]
|
126
137
|
end
|
127
138
|
|
128
|
-
it '
|
139
|
+
it 'returns collection' do
|
129
140
|
users = MyUser.all
|
130
141
|
Acfs.run
|
131
142
|
|
132
143
|
expect(users).to have(2).items
|
133
144
|
end
|
134
145
|
|
135
|
-
it '
|
146
|
+
it 'returns defined resources' do
|
136
147
|
users = MyUser.all
|
137
148
|
Acfs.run
|
138
149
|
|
@@ -148,7 +159,7 @@ describe Acfs::Stub do
|
|
148
159
|
return: [{id: 1, type: 'PC'}, {id: 2, type: 'Mac'}]
|
149
160
|
end
|
150
161
|
|
151
|
-
it '
|
162
|
+
it 'creates inherited type' do
|
152
163
|
computers = Computer.all
|
153
164
|
Acfs.run
|
154
165
|
|
@@ -158,6 +169,11 @@ describe Acfs::Stub do
|
|
158
169
|
end
|
159
170
|
|
160
171
|
context 'with header' do
|
172
|
+
subject do
|
173
|
+
Acfs.run
|
174
|
+
comments
|
175
|
+
end
|
176
|
+
|
161
177
|
before do
|
162
178
|
Acfs::Stub.resource Comment, :list,
|
163
179
|
return: [{id: 1, text: 'Foo'}, {id: 2, text: 'Bar'}],
|
@@ -168,40 +184,40 @@ describe Acfs::Stub do
|
|
168
184
|
let(:headers) do
|
169
185
|
{
|
170
186
|
'X-Total-Pages' => '2',
|
171
|
-
'X-Total-Count' => '10'
|
187
|
+
'X-Total-Count' => '10',
|
172
188
|
}
|
173
189
|
end
|
174
|
-
subject { Acfs.run; comments }
|
175
190
|
|
176
|
-
its(:total_pages) {
|
177
|
-
its(:total_count) {
|
191
|
+
its(:total_pages) { is_expected.to eq 2 }
|
192
|
+
its(:total_count) { is_expected.to eq 10 }
|
178
193
|
end
|
179
194
|
end
|
180
195
|
|
181
196
|
context 'with update action' do
|
182
197
|
before do
|
183
198
|
Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
|
184
|
-
Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 22},
|
199
|
+
Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 22},
|
200
|
+
return: {id: 1, name: 'John Smith', age: 23}
|
185
201
|
Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 0}, raise: 422
|
186
202
|
end
|
187
203
|
|
188
204
|
let!(:update_stub) do
|
189
205
|
Acfs::Stub.resource MyUser, :update,
|
190
206
|
with: {id: 1, name: 'Jane Smith'},
|
191
|
-
return: ->(op) {
|
207
|
+
return: ->(op) { op.data.map {|k, v| [k, v.to_s.upcase] }.to_h }
|
192
208
|
end
|
193
209
|
|
194
|
-
it '
|
210
|
+
it 'allows stub resource update' do
|
195
211
|
user = MyUser.find 1
|
196
212
|
Acfs.run
|
197
213
|
|
198
214
|
user.age = 22
|
199
215
|
user.save!
|
200
216
|
|
201
|
-
expect(user.age).to
|
217
|
+
expect(user.age).to eq 23
|
202
218
|
end
|
203
219
|
|
204
|
-
it '
|
220
|
+
it 'allows to raise error' do
|
205
221
|
user = MyUser.find 1
|
206
222
|
Acfs.run
|
207
223
|
|
@@ -213,7 +229,7 @@ describe Acfs::Stub do
|
|
213
229
|
end.to raise_error(::Acfs::InvalidResource)
|
214
230
|
end
|
215
231
|
|
216
|
-
it '
|
232
|
+
it 'matches partial :with' do
|
217
233
|
user = MyUser.find 1
|
218
234
|
Acfs.run
|
219
235
|
|
@@ -224,7 +240,7 @@ describe Acfs::Stub do
|
|
224
240
|
expect(update_stub).to be_called
|
225
241
|
end
|
226
242
|
|
227
|
-
it '
|
243
|
+
it 'processes response body' do
|
228
244
|
user = MyUser.find 1
|
229
245
|
Acfs.run
|
230
246
|
|
@@ -235,18 +251,6 @@ describe Acfs::Stub do
|
|
235
251
|
expect(user.name).to eq 'JANE SMITH'
|
236
252
|
end
|
237
253
|
end
|
238
|
-
|
239
|
-
context 'with create action' do
|
240
|
-
before do
|
241
|
-
Acfs::Stub.resource MyUser, :create, with: {name: 'John Smith', age: 0}, raise: 422
|
242
|
-
end
|
243
|
-
|
244
|
-
it 'should allow to raise error' do
|
245
|
-
expect do
|
246
|
-
MyUser.create! name: 'John Smith', age: 0
|
247
|
-
end.to raise_error(::Acfs::InvalidResource)
|
248
|
-
end
|
249
|
-
end
|
250
254
|
end
|
251
255
|
|
252
256
|
describe '.allow_requests=' do
|
@@ -256,9 +260,9 @@ describe Acfs::Stub do
|
|
256
260
|
stub_request(:get, 'http://users.example.org/users/2').to_return response(id: 2, name: 'John', age: 26)
|
257
261
|
end
|
258
262
|
|
259
|
-
it '
|
263
|
+
it 'allows real requests' do
|
260
264
|
@user = MyUser.find 2
|
261
|
-
expect { Acfs.run }.
|
265
|
+
expect { Acfs.run }.not_to raise_error
|
262
266
|
end
|
263
267
|
end
|
264
268
|
|
@@ -267,7 +271,7 @@ describe Acfs::Stub do
|
|
267
271
|
Acfs::Stub.allow_requests = false
|
268
272
|
end
|
269
273
|
|
270
|
-
it '
|
274
|
+
it 'does not allow real requests' do
|
271
275
|
@user = MyUser.find 2
|
272
276
|
expect { Acfs.run }.to raise_error(Acfs::RealRequestsNotAllowedError)
|
273
277
|
end
|
@@ -279,7 +283,7 @@ describe Acfs::Stub do
|
|
279
283
|
|
280
284
|
context 'with a match in params' do
|
281
285
|
let(:op) do
|
282
|
-
|
286
|
+
instance_double('Acfs::Operation').tap do |op|
|
283
287
|
allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
|
284
288
|
allow(op).to receive(:data).and_return({})
|
285
289
|
end
|
@@ -292,7 +296,7 @@ describe Acfs::Stub do
|
|
292
296
|
|
293
297
|
context 'with a match in data' do
|
294
298
|
let(:op) do
|
295
|
-
|
299
|
+
instance_double('Acfs::Operation').tap do |op|
|
296
300
|
allow(op).to receive(:full_params).and_return({})
|
297
301
|
allow(op).to receive(:data).and_return(id: 1337, blub: 'abc')
|
298
302
|
end
|
@@ -305,7 +309,7 @@ describe Acfs::Stub do
|
|
305
309
|
|
306
310
|
context 'with no match in params nor data' do
|
307
311
|
let(:op) do
|
308
|
-
|
312
|
+
instance_double('Acfs::Operation').tap do |op|
|
309
313
|
allow(op).to receive(:full_params).and_return(id: 1337)
|
310
314
|
allow(op).to receive(:data).and_return({})
|
311
315
|
end
|
@@ -318,7 +322,7 @@ describe Acfs::Stub do
|
|
318
322
|
|
319
323
|
context 'with a wrong match' do
|
320
324
|
let(:op) do
|
321
|
-
|
325
|
+
instance_double('Acfs::Operation').tap do |op|
|
322
326
|
allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
|
323
327
|
allow(op).to receive(:data).and_return({})
|
324
328
|
end
|
@@ -331,7 +335,7 @@ describe Acfs::Stub do
|
|
331
335
|
|
332
336
|
context 'with a missing match' do
|
333
337
|
let(:op) do
|
334
|
-
|
338
|
+
instance_double('Acfs::Operation').tap do |op|
|
335
339
|
allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
|
336
340
|
allow(op).to receive(:data).and_return({})
|
337
341
|
end
|