Skip to content

Commit 019c26c

Browse files
committed
update errors tests
1 parent 6935ac7 commit 019c26c

File tree

3 files changed

+86
-80
lines changed

3 files changed

+86
-80
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ tox==3.20.0
88
sphinx==3.4.1
99
sphinx-rtd-theme==0.5.2
1010
pytest==7.2.0
11+
pytest-cov==4.0.0

specs/mixins/errors_spec.py

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,88 @@
1-
from mamba import description, context, it, before
2-
from expects import expect, equal
3-
from doublex import Stub, Spy
4-
from doublex_expects import have_been_called_with, have_been_called
1+
from mock import MagicMock
2+
from unittest.mock import Mock
53

64
from amadeus import NetworkError, Response, Client
75

8-
with description('ResponseError') as self:
9-
with context('str(error)'):
10-
with it('should be undefine if no response is present'):
11-
error = NetworkError(None)
12-
expect(str(error)).to(equal('[---]'))
13-
14-
with it('should just return the code if no data is present'):
15-
response = Stub(Response)
16-
response.parsed = True
17-
response.result = {}
18-
response.status_code = 400
19-
20-
error = NetworkError(response)
21-
expect(str(error)).to(equal('[400]'))
22-
23-
with it('should be rich if errors are present'):
24-
response = Stub(Response)
25-
response.parsed = True
26-
response.result = {
27-
'errors': [
28-
{
29-
'detail': 'This field must be filled.',
30-
'source': {'parameter': 'departureDate'}
31-
},
32-
{
33-
'detail': 'This field must be filled.',
34-
'source': {'parameter': 'origin'}
35-
},
36-
{
37-
'detail': 'This field must be filled.',
38-
'source': {'parameter': 'destination'}
39-
}
40-
]
41-
}
42-
response.status_code = 401
43-
44-
error = NetworkError(response)
45-
expect(str(error)).to(equal(
46-
('''[401]
6+
7+
def test_ResponseError_str_representation():
8+
# Test str(error) with no response present
9+
error = NetworkError(None)
10+
assert str(error) == '[---]'
11+
12+
# Test str(error) with no data present
13+
response = Mock(Response)
14+
response.parsed = True
15+
response.result = {}
16+
response.status_code = 400
17+
18+
error = NetworkError(response)
19+
assert str(error) == '[400]'
20+
21+
# Test str(error) with errors present
22+
response = Mock(Response)
23+
response.parsed = True
24+
response.result = {
25+
'errors': [
26+
{
27+
'detail': 'This field must be filled.',
28+
'source': {'parameter': 'departureDate'},
29+
},
30+
{
31+
'detail': 'This field must be filled.',
32+
'source': {'parameter': 'origin'},
33+
},
34+
{
35+
'detail': 'This field must be filled.',
36+
'source': {'parameter': 'destination'},
37+
},
38+
]
39+
}
40+
response.status_code = 401
41+
42+
error = NetworkError(response)
43+
error = NetworkError(response)
44+
assert (
45+
('''[401]
4746
[departureDate] This field must be filled.
4847
[origin] This field must be filled.
4948
[destination] This field must be filled.''')
50-
))
51-
52-
with it('should be rich if an error_description is present'):
53-
response = Stub(Response)
54-
response.parsed = True
55-
response.result = {'error_description': 'error'}
56-
response.status_code = 401
57-
58-
error = NetworkError(response)
59-
expect(str(error)).to(equal('[401]\nerror'))
60-
61-
with context('.code'):
62-
with it('should determine the code off the class name'):
63-
error = NetworkError(None)
64-
expect(error.code).to(equal('NetworkError'))
65-
66-
with context('.log'):
67-
with before.all:
68-
self.client = Stub(Client)
69-
self.error = NetworkError(None)
70-
self.error.code = 'Foo'
71-
self.error.description = 'Bar'
72-
73-
with it('should log if the log level allows it'):
74-
self.client.logger = Spy()
75-
self.client.log_level = 'warn'
76-
self.error._log(self.client)
77-
expect(self.client.logger.warning).to(
78-
have_been_called_with('Amadeus %s: %s', 'Foo', "'Bar'")
79-
)
80-
81-
with it('should not log if the log level does not allow it'):
82-
self.client.logger = Spy()
83-
self.client.log_level = 'silent'
84-
self.error._log(self.client)
85-
expect(self.client.logger.warning).not_to(have_been_called)
49+
)
50+
51+
# Test str(error) with error_description present
52+
response = Mock(Response)
53+
response.parsed = True
54+
response.result = {'error_description': 'error'}
55+
response.status_code = 401
56+
57+
error = NetworkError(response)
58+
assert str(error) == '[401]\nerror'
59+
60+
61+
def test_ResponseError_code():
62+
# Test .code with no response present
63+
error = NetworkError(None)
64+
assert error.code == 'NetworkError'
65+
66+
67+
def test_ResponseError_log():
68+
# Test .log with log level set to 'warn'
69+
client = Mock(Client)
70+
client.logger = MagicMock()
71+
client.log_level = 'warn'
72+
73+
error = NetworkError(None)
74+
error.code = 'Foo'
75+
error.description = 'Bar'
76+
error._log(client)
77+
78+
assert client.logger.warning.called_with('Amadeus %s: %s', 'Foo', 'Bar')
79+
80+
# Test .log with log level set to 'silent'
81+
client = Mock(Client)
82+
client.logger = MagicMock()
83+
client.log_level = 'silent'
84+
85+
error = NetworkError(None)
86+
error._log(client)
87+
88+
assert not client.logger.warning.called

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ envlist = py38,python3.9,python3.10
44
[testenv]
55
commands =
66
flake8 amadeus specs setup.py
7-
pytest specs/namespaces/namespaces_spec.py
7+
# pytest specs/mixins/validator_spec.py --cov
8+
pytest specs/mixins/errors_spec.py
89

910
deps =
1011
expects==0.9.0
@@ -14,6 +15,7 @@ deps =
1415
flake8-quotes==2.1.1
1516
pytest==7.2.0
1617
mock==5.0.0
18+
pytest-cov==4.0.0
1719
usedevelop=True
1820

1921
[gh-actions]

0 commit comments

Comments
 (0)