class ClientTestCase(unittest.TestCase): def setUp(self): self.gapi = Client() def test_resource_querysets_available(self): for resource in [r._resource_name for r in get_available_resource_classes()]: self.assertTrue(hasattr(self.gapi, resource)) self.assertIsInstance(getattr(self.gapi, resource), Query) def test_query_interface(self): resource_name = get_available_resource_classes()[0]._resource_name self.assertTrue(isinstance(self.gapi.query(resource_name), Query)) def test_unavailable_resource_query(self): with self.assertRaises(AttributeError): self.gapi.query("fake_resource") def test_build_interface(self): class MockResource(Resource): _as_is_fields = ['id', 'foo'] _resource_name = 'foo' self.gapi.foo = Query(self.gapi, MockResource) resource = self.gapi.build('foo', {'id': 1, 'foo': 'bar'}) self.assertEqual(resource.id, 1) self.assertEqual(resource.foo, 'bar') @patch('gapipy.request.APIRequestor._request') def test_create_interface(self, mock_request): class MockResource(Resource): _as_is_fields = ['id', 'foo'] _resource_name = 'foo' self.gapi.foo = Query(self.gapi, MockResource) # On create, a representation of the resource is created. mock_request.return_value = { 'id': 1, 'foo': 'bar', } # Create allows arbitrary data to be sent, even if it's not part of the # final resource. resource = self.gapi.create('foo', {'id': 1, 'foo': 'bar', 'context': 'abc'}) self.assertEqual(resource.id, 1) @patch('gapipy.query.Query.get_resource_data') def test_correct_client_is_associated_with_resources(self, mock_get_data): mock_get_data.return_value = { 'id': 123 } en_client = Client(api_language='en') de_client = Client(api_language='de') en_itin = en_client.itineraries.get(123) de_itin = de_client.itineraries.get(123) self.assertEqual(en_itin._client, en_client) self.assertEqual(de_itin._client, de_client)
def test_correct_client_is_associated_with_resources(self, mock_get_data): mock_get_data.return_value = {'id': 123} en_client = Client(api_language='en') de_client = Client(api_language='de') en_itin = en_client.itineraries.get(123) de_itin = de_client.itineraries.get(123) self.assertEqual(en_itin._client, en_client) self.assertEqual(de_itin._client, de_client)
def test_product_type_is_set_properly(self, mock_request): data = { 'products': [ { 'type': 'departures', 'sub_type': 'Tour', 'id': 1, } ], } promotion = Promotion(data, client=Client()) product = promotion.products[0] self.assertEqual(product.type, 'departures') self.assertEqual(product.to_dict(), { 'id': 1, 'type': 'departures', 'sub_type': 'Tour', }) # Ensure when the product implicitly fetched it makes a request, and # retains the type fields. self.assertEqual(mock_request.call_count, 0) mock_request.return_value = { 'finish_address': { 'country': { 'name': 'Australia', } } } self.assertEqual(product.finish_address.country.name, 'Australia') self.assertEqual(product.type, 'departures')
def test_retries_no_connection_pooling(self): """Should initialize the client's requestor with the passed number of retries.""" expected_retries = 42 client_with_retries = Client(max_retries=expected_retries) # Connection pooling defaults to https only https_retries = client_with_retries.requestor.adapters[ 'https://'].max_retries.total self.assertEqual(https_retries, expected_retries)
def test_fetch_resource(self, resource, resource_id): # Test if a resource can be parsed properly. This is useful to catch # changes in the API api = Client() query = getattr(api, resource._resource_name) instance = query.get(resource_id) # Test that we take into account all of the fields of the json data # returned by the API (this only checks for the top-level fields, not # those of nested objects) self.assertEqual(sorted(instance._allowed_fields()), sorted(instance._raw_data.keys()))
def test_query_key_with_language(self): self.client = Client(application_key='') self.client.api_language = 'de' query = Query(self.client, self.resource) key = query.query_key(1) expected = '{}:1:de'.format(self.resource_name) self.assertEqual(key, expected) # Unsetting the language should also remove it from the key self.client.api_language = None key = query.query_key(1) expected = '{}:1'.format(self.resource_name) self.assertEqual(key, expected)
def test_departure_addon_product_types(self): departure = Departure(DUMMY_DEPARTURE, client=Client()) for addon in departure.addons: # Product should use a resource class that matches with its type self.assertEqual(addon.product._resource_name, addon.product.type) # Each addon instance should have only one resource class listed # for it's "product" field product_resource_fields = [ (field, resource_class) for (field, resource_class) in addon._resource_fields if field == 'product'] self.assertEqual(len(product_resource_fields), 1)
def test_query_key_with_env_with_one_id(self): client = Client(application_key='test_abcd') output = client.tour_dossiers.query_key(1) expected = 'tour_dossiers:1:test' self.assertEqual(output, expected)
def test_query_key_with_env_with_language(self): self.client = Client(application_key='test_abcd') self.client.api_language = 'de' output = self.client.tour_dossiers.query_key(123) expected = 'tour_dossiers:123:de:test' self.assertEqual(output, expected)
def test_query_key_with_underscore_live(self): client = Client(application_key='live_abs') output = client.tour_dossiers.query_key(123) expected = 'tour_dossiers:123' self.assertEqual(output, expected)
def test_query_key_with_no_underscore(self): client = Client(application_key='somestringnounderscore') output = client.tour_dossiers.query_key(123) expected = 'tour_dossiers:123' self.assertEqual(output, expected)
def setUp(self): self.client = Client()
def setUpClass(cls): cls.dossier = Client().tour_dossiers.get(21715)
def setUp(self): self.client = Client(cache_backend='gapipy.cache.SimpleCache') self.cache = self.client._cache self.cache.clear()
def setUpClass(cls): cls.tour = Client().tours.get(21715)
def setUp(self): self.client = Client() self.cache = self.client._cache self.cache.clear()
def setUp(self): self.client = Client() self.resources = Resources(_resource_name='resources', _uri=None)
def test_fake_amount_is_set_properly(self): departure = Departure(DUMMY_DEPARTURE, client=Client()) prices = departure.rooms[0].price_bands[0].prices for price in prices: promotion = price.promotions[0].to_dict() self.assertTrue('amount' in promotion)
def setUp(self): self.gapi = Client()
def test_query_key_test(self): client = Client(application_key='test') output = client.tour_dossiers.query_key(123) expected = 'tour_dossiers:123' self.assertEqual(output, expected)
def setUp(self): # Any ol' resource will do. self.client = Client(application_key='test_abcd') self.resource = get_available_resource_classes()[0] self.resource_name = self.resource._resource_name
class ClientTestCase(unittest.TestCase): def setUp(self): self.gapi = Client() def test_resource_querysets_available(self): for resource in [ r._resource_name for r in get_available_resource_classes() ]: self.assertTrue(hasattr(self.gapi, resource)) self.assertIsInstance(getattr(self.gapi, resource), Query) def test_query_interface(self): resource_name = get_available_resource_classes()[0]._resource_name self.assertTrue(isinstance(self.gapi.query(resource_name), Query)) def test_unavailable_resource_query(self): with self.assertRaises(AttributeError): self.gapi.query("fake_resource") def test_build_interface(self): class MockResource(Resource): _as_is_fields = ['id', 'foo'] _resource_name = 'foo' self.gapi.foo = Query(self.gapi, MockResource) resource = self.gapi.build('foo', {'id': 1, 'foo': 'bar'}) self.assertEqual(resource.id, 1) self.assertEqual(resource.foo, 'bar') @patch('gapipy.request.APIRequestor._request') def test_create_interface(self, mock_request): class MockResource(Resource): _as_is_fields = ['id', 'foo'] _resource_name = 'foo' self.gapi.foo = Query(self.gapi, MockResource) # On create, a representation of the resource is created. mock_request.return_value = { 'id': 1, 'foo': 'bar', } # Create allows arbitrary data to be sent, even if it's not part of the # final resource. resource = self.gapi.create('foo', { 'id': 1, 'foo': 'bar', 'context': 'abc' }) self.assertEqual(resource.id, 1) @patch('gapipy.request.APIRequestor._request') def test_create_extra_headers(self, mock_request): """ Test that extra HTTP headers can be passed through the `.create` method on a resource """ class MockResource(Resource): _as_is_fields = ['id', 'foo'] _resource_name = 'foo' self.gapi.foo = Query(self.gapi, MockResource) resource_data = { 'id': 1, 'foo': 'bar' } # content doesn't really matter for this test mock_request.return_value = resource_data # Create a `foo` while passing extra headers extra_headers = {'X-Bender': 'I\'m not allowed to sing. Court order.'} self.gapi.create('foo', resource_data, headers=extra_headers) # Did those headers make it all the way to the requestor? mock_request.assert_called_once_with( '/foo', 'POST', data=json.dumps(resource_data), additional_headers=extra_headers, ) @patch('gapipy.query.Query.get_resource_data') def test_correct_client_is_associated_with_resources(self, mock_get_data): mock_get_data.return_value = {'id': 123} en_client = Client(api_language='en') de_client = Client(api_language='de') en_itin = en_client.itineraries.get(123) de_itin = de_client.itineraries.get(123) self.assertEqual(en_itin._client, en_client) self.assertEqual(de_itin._client, de_client) def test_default_retries(self): """Should not set any retries on the client's requestor.""" http_retries = self.gapi.requestor.adapters[ 'http://'].max_retries.total https_retries = self.gapi.requestor.adapters[ 'https://'].max_retries.total self.assertEqual(http_retries, 0) self.assertEqual(https_retries, 0) def test_retries_no_connection_pooling(self): """Should initialize the client's requestor with the passed number of retries.""" expected_retries = 42 client_with_retries = Client(max_retries=expected_retries) # Connection pooling defaults to https only https_retries = client_with_retries.requestor.adapters[ 'https://'].max_retries.total self.assertEqual(https_retries, expected_retries) def test_retries_with_connection_pooling(self): """Should initialize the client's requestor with the passed number of retries.""" expected_retries = 84 connection_pool_options = {"enable": True} client_with_retries = Client( max_retries=expected_retries, connection_pool_options=connection_pool_options) # Connection pooling defaults to https only https_retries = client_with_retries.requestor.adapters[ 'https://'].max_retries.total self.assertEqual(https_retries, expected_retries)