|
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 |
5 | 3 |
|
6 | 4 | from amadeus import NetworkError, Response, Client |
7 | 5 |
|
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] |
47 | 46 | [departureDate] This field must be filled. |
48 | 47 | [origin] This field must be filled. |
49 | 48 | [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 |
0 commit comments