|
1 | | -from mamba import description, context, it, before |
2 | | -from expects import expect, be_a, raise_error, equal, be |
3 | | -from doublex_expects import have_been_called_with |
4 | | -from doublex import Spy |
| 1 | + |
5 | 2 |
|
6 | 3 | from amadeus import Client |
7 | 4 | from os import environ |
8 | 5 | from logging import Logger, getLogger |
| 6 | +import pytest |
| 7 | +from mock import MagicMock |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def valid_params(): |
| 12 | + return { |
| 13 | + 'client_id': '1234', |
| 14 | + 'client_secret': '4546' |
| 15 | + } |
| 16 | + |
| 17 | + |
| 18 | +def test_client_init(valid_params): |
| 19 | + client = Client(**valid_params) |
| 20 | + assert isinstance(client, Client) |
| 21 | + |
| 22 | + |
| 23 | +def test_client_init_with_env_vars(valid_params): |
| 24 | + environ['AMADEUS_CLIENT_ID'] = '123' |
| 25 | + environ['AMADEUS_CLIENT_SECRET'] = '234' |
| 26 | + |
| 27 | + client = Client() |
| 28 | + assert isinstance(client, Client) |
| 29 | + |
| 30 | + del environ['AMADEUS_CLIENT_ID'] |
| 31 | + del environ['AMADEUS_CLIENT_SECRET'] |
| 32 | + |
| 33 | + |
| 34 | +def test_client_init_with_missing_params(valid_params): |
| 35 | + # Test missing client_id |
| 36 | + valid_params_copy = valid_params.copy() |
| 37 | + del valid_params_copy['client_id'] |
| 38 | + with pytest.raises(ValueError): |
| 39 | + Client(**valid_params_copy) |
| 40 | + |
| 41 | + # Test missing client_secret |
| 42 | + valid_params_copy = valid_params.copy() |
| 43 | + del valid_params_copy['client_secret'] |
| 44 | + with pytest.raises(ValueError): |
| 45 | + Client(**valid_params_copy) |
| 46 | + |
| 47 | + |
| 48 | +def test_client_logging(valid_params): |
| 49 | + # Test default logger |
| 50 | + amadeus = Client(**valid_params) |
| 51 | + assert isinstance(amadeus.logger, Logger) |
| 52 | + assert amadeus.log_level == 'silent' |
| 53 | + |
| 54 | + # Test custom logger |
| 55 | + logger = getLogger('amadeus') |
| 56 | + valid_params['logger'] = logger |
| 57 | + amadeus = Client(**valid_params) |
| 58 | + assert amadeus.logger is logger |
| 59 | + assert amadeus.log_level == 'silent' |
| 60 | + |
| 61 | + # Test custom log level |
| 62 | + logger.setLevel(10) |
| 63 | + amadeus = Client(**valid_params) |
| 64 | + assert amadeus.logger is logger |
| 65 | + assert amadeus.logger.level == 10 |
| 66 | + |
| 67 | + |
| 68 | +def test_client_options(valid_params): |
| 69 | + # Test unrecognized option warning |
| 70 | + logger = MagicMock() |
| 71 | + valid_params['logger'] = logger |
| 72 | + valid_params['foobar'] = 'test' |
| 73 | + Client(**valid_params) |
| 74 | + logger.warning.assert_called_with('Unrecognized option: foobar') |
| 75 | + |
| 76 | + # Test default host |
| 77 | + amadeus = Client(**valid_params) |
| 78 | + assert amadeus.host == Client.HOSTS['test'] |
| 79 | + |
| 80 | + # Test custom hostname |
| 81 | + valid_params['hostname'] = 'production' |
| 82 | + amadeus = Client(**valid_params) |
| 83 | + assert amadeus.host == Client.HOSTS['production'] |
9 | 84 |
|
10 | | -with description('Mixins/Validator') as self: |
11 | | - with before.all: |
12 | | - self.valid_params = { |
13 | | - 'client_id': '1234', |
14 | | - 'client_secret': '4546' |
15 | | - } |
16 | | - |
17 | | - with context('Client.__init__'): |
18 | | - with it('should create a new client with direct variables'): |
19 | | - client = Client(**self.valid_params) |
20 | | - expect(client).to(be_a(Client)) |
21 | | - |
22 | | - with it('should create a new client with environment variables'): |
23 | | - environ['AMADEUS_CLIENT_ID'] = '123' |
24 | | - environ['AMADEUS_CLIENT_SECRET'] = '234' |
25 | | - |
26 | | - expect(Client()).to(be_a(Client)) |
27 | | - |
28 | | - del environ['AMADEUS_CLIENT_ID'] |
29 | | - del environ['AMADEUS_CLIENT_SECRET'] |
30 | | - |
31 | | - with context('with missing parameters'): |
32 | | - with it('should refuse to create a new client without client_id'): |
33 | | - del self.valid_params['client_id'] |
34 | | - expect(lambda: Client(**self.valid_params)).to( |
35 | | - raise_error(ValueError) |
36 | | - ) |
37 | | - |
38 | | - with it('should refuse to create a new client w/o client_secret'): |
39 | | - del self.valid_params['client_secret'] |
40 | | - expect(lambda: Client(**self.valid_params)).to( |
41 | | - raise_error(ValueError) |
42 | | - ) |
43 | | - |
44 | | - with it('should by default have a logger'): |
45 | | - amadeus = Client(**self.valid_params) |
46 | | - expect(amadeus.logger).to(be_a(Logger)) |
47 | | - expect(amadeus.log_level).to(equal('silent')) |
48 | | - |
49 | | - with it('should allow for setting a custom logger'): |
50 | | - logger = getLogger('amadeus') |
51 | | - self.valid_params['logger'] = logger |
52 | | - amadeus = Client(**self.valid_params) |
53 | | - expect(amadeus.logger).to(be(logger)) |
54 | | - expect(amadeus.log_level).to(equal('silent')) |
55 | | - |
56 | | - with it('should persist a custom log level'): |
57 | | - logger = getLogger('amadeus') |
58 | | - logger.setLevel(10) |
59 | | - self.valid_params['logger'] = logger |
60 | | - amadeus = Client(**self.valid_params) |
61 | | - expect(amadeus.logger).to(be(logger)) |
62 | | - expect(amadeus.logger.level).to(be(10)) |
63 | | - |
64 | | - with it('should warn when an unrecognized option is passed in'): |
65 | | - logger = Spy() |
66 | | - self.valid_params['logger'] = logger |
67 | | - self.valid_params['foobar'] = 'test' |
68 | | - Client(**self.valid_params) |
69 | | - expect(logger.warning).to( |
70 | | - have_been_called_with('Unrecognized option: foobar') |
71 | | - ) |
72 | | - |
73 | | - with it('should default to the test host'): |
74 | | - amadeus = Client(**self.valid_params) |
75 | | - expect(amadeus.host).to(equal(Client.HOSTS['test'])) |
76 | | - |
77 | | - with it('should allow for setting a different hostname'): |
78 | | - self.valid_params['hostname'] = 'production' |
79 | | - amadeus = Client(**self.valid_params) |
80 | | - expect(amadeus.host).to(equal(Client.HOSTS['production'])) |
81 | | - |
82 | | - with it('should allow for setting a full different host'): |
83 | | - host = 'http://foo.bar.com/' |
84 | | - self.valid_params['host'] = host |
85 | | - amadeus = Client(**self.valid_params) |
86 | | - expect(amadeus.host).to(equal(host)) |
| 85 | + # Test custom host |
| 86 | + host = 'http://foo.bar.com/' |
| 87 | + valid_params['host'] = host |
| 88 | + amadeus = Client(**valid_params) |
| 89 | + assert amadeus.host == host |
0 commit comments