|
| 1 | +import pytest |
| 2 | +import mock |
| 3 | +from amadeus import Client, Response, Request |
| 4 | +from amadeus.mixins.pagination import Pagination |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def self(): |
| 9 | + # Initialize the client and pagination object |
| 10 | + self.next_response = mock.MagicMock(Response) |
| 11 | + client = mock.MagicMock(Client) |
| 12 | + client.request = self.next_response |
| 13 | + self.client = client |
| 14 | + self.pagination = Pagination() |
| 15 | + self.pagination.request = self.client.request |
| 16 | + |
| 17 | + # Initialize the previous request made and response received |
| 18 | + self.request = mock.MagicMock(Request) |
| 19 | + self.request.verb = self.verb = 'GET' |
| 20 | + self.request.path = self.path = '/a' |
| 21 | + self.request.params = self.params = {'a': 'b'} |
| 22 | + self.response = Response(mock.MagicMock('http_response'), self.request) |
| 23 | + return self |
| 24 | + |
| 25 | + |
| 26 | +def test_create_new_request_when_previous(self): |
| 27 | + self.response.result = { |
| 28 | + 'meta': {'links': {'previous': 'https://f.co?page%5Boffset%5D=1'}} |
| 29 | + } |
| 30 | + self.pagination.previous(self.response) |
| 31 | + self.client.request.call_args.assert_called_with( |
| 32 | + 'GET', '/a', {'a': 'b', 'page': {'offset': '1'}}) |
| 33 | + |
| 34 | + |
| 35 | +def test_should_return_nil_page_not_found_with_previous(self): |
| 36 | + self.response.result = {'meta': {'links': {}}} |
| 37 | + next_response = self.pagination.previous(self.response) |
| 38 | + assert next_response is None |
| 39 | + |
| 40 | + |
| 41 | +def test_create_new_request_when_next(self): |
| 42 | + self.response.result = { |
| 43 | + 'meta': {'links': {'next': 'https://f.co?page%5Boffset%5D=1'}} |
| 44 | + } |
| 45 | + self.pagination.next(self.response) |
| 46 | + self.client.request.call_args.assert_called_with( |
| 47 | + 'GET', '/a', {'a': 'b', 'page': {'offset': '1'}}) |
| 48 | + |
| 49 | + |
| 50 | +def test_should_return_nil_page_not_found_with_next(self): |
| 51 | + self.response.result = {'meta': {'links': {}}} |
| 52 | + next_response = self.pagination.next(self.response) |
| 53 | + assert next_response is None |
| 54 | + |
| 55 | + |
| 56 | +def test_create_new_request_when_first(self): |
| 57 | + self.response.result = { |
| 58 | + 'meta': {'links': {'first': 'https://f.co?page%5Boffset%5D=1'}} |
| 59 | + } |
| 60 | + self.pagination.first(self.response) |
| 61 | + self.client.request.call_args.assert_called_with( |
| 62 | + 'GET', '/a', {'a': 'b', 'page': {'offset': '1'}}) |
| 63 | + |
| 64 | + |
| 65 | +def test_should_return_nil_page_not_found_with_first(self): |
| 66 | + self.response.result = {'meta': {'links': {}}} |
| 67 | + next_response = self.pagination.first(self.response) |
| 68 | + assert next_response is None |
| 69 | + |
| 70 | + |
| 71 | +def test_create_new_request_when_last(self): |
| 72 | + self.response.result = { |
| 73 | + 'meta': {'links': {'last': 'https://f.co?page%5Boffset%5D=1'}} |
| 74 | + } |
| 75 | + self.pagination.last(self.response) |
| 76 | + self.client.request.call_args.assert_called_with( |
| 77 | + 'GET', '/a', {'a': 'b', 'page': {'offset': '1'}}) |
| 78 | + |
| 79 | + |
| 80 | +def test_should_return_nil_page_not_found_with_last(self): |
| 81 | + self.response.result = {'meta': {'links': {}}} |
| 82 | + next_response = self.pagination.last(self.response) |
| 83 | + assert next_response is None |
0 commit comments