class TestTapiocaClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_fill_url_template(self): expected_url = 'https://api.test.com/user/123/' resource = self.wrapper.user(id='123') self.assertEqual(resource.data(), expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) self.assertEqual(len(client), 3) def test_iterated_client_items_should_be_tapioca_instances(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) for item in client: self.assertTrue(isinstance(item, TapiocaClient)) def test_iterated_client_items_should_contain_list_items(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) for i, item in enumerate(client): self.assertEqual(item().data(), i)
def test_call_conversion_with_no_serializer(self): wrapper = TesterClient() responses.add(responses.GET, wrapper.test().data, body='{"any_data": "%#ˆ$&"}', status=200, content_type='application/json') response = wrapper.test().get() with self.assertRaises(NotImplementedError): response.any_data().to_datetime()
def test_not_token_refresh_client_propagates_client_error(self): no_refresh_client = TesterClient() responses.add_callback( responses.POST, no_refresh_client.test().data, callback=lambda *a, **k: (401, {}, ''), content_type='application/json', ) with self.assertRaises(ClientError): no_refresh_client.test().post()
def test_not_token_refresh_ready_client_call_raises_not_implemented(self): no_refresh_client = TesterClient() responses.add_callback( responses.POST, no_refresh_client.test().data, callback=lambda *a, **k: (401, {}, ''), content_type='application/json', ) with self.assertRaises(NotImplementedError): no_refresh_client.test().post(refresh_auth=True)
def test_external_serializer_is_passed_along_clients(self): serializer_wrapper = TesterClient(serializer_class=SimpleSerializer) responses.add(responses.GET, serializer_wrapper.test().data, body='{"date": "2014-11-13T14:53:18.694072+00:00"}', status=200, content_type='application/json') response = serializer_wrapper.test().get() self.assertTrue(response._api.serializer.__class__, SimpleSerializer)
def test_added_custom_resources(self): wrapper = TesterClient(resource_mapping=[ Resource("myresource", "http://url.ru/myresource"), Resource("myresource2", "https://url.ru/myresource2"), ]) responses.add(responses.GET, url=wrapper.myresource().data, body='[]', status=200, content_type='application/json') response = self.wrapper.myresource().get() assert response.data == []
class TestTapiocaClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_fill_url_template(self): expected_url = 'https://api.test.com/user/123/' resource = self.wrapper.user(id='123') self.assertEqual(resource.data, expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) self.assertEqual(len(client), 3) def test_iterated_client_items_should_be_tapioca_instances(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for item in client: self.assertTrue(isinstance(item, TapiocaClient)) def test_iterated_client_items_should_contain_list_items(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for i, item in enumerate(client): self.assertEqual(item().data, i) @responses.activate def test_in_operator(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": 1, "other": 2}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertIn('data', response) self.assertIn('other', response) self.assertNotIn('wat', response) @responses.activate def test_trasnform_camelCase_in_snake_case(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body= '{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response.data.key_snake().data, 'value') self.assertEqual(response.data.camel_case().data, 'data in camel case') self.assertEqual(response.data.normal_camel_case().data, 'data in camel case')
class TestExceptions(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_adapter_raises_response_process_exception_on_400s(self): responses.add(responses.GET, self.wrapper.test().data, body='{"erros": "Server Error"}', status=400, content_type='application/json') response = requests.get(self.wrapper.test().data) with self.assertRaises(ResponseProcessException): TesterClientAdapter().process_response(response, {}) @responses.activate def test_adapter_raises_response_process_exception_on_500s(self): responses.add(responses.GET, self.wrapper.test().data, body='{"erros": "Server Error"}', status=500, content_type='application/json') response = requests.get(self.wrapper.test().data) with self.assertRaises(ResponseProcessException): TesterClientAdapter().process_response(response, {}) @responses.activate def test_raises_request_error(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') with self.assertRaises(ClientError): self.wrapper.test().get() @responses.activate def test_raises_server_error(self): responses.add(responses.GET, self.wrapper.test().data, status=500, content_type='application/json') with self.assertRaises(ServerError): self.wrapper.test().get()
class TestExceptions(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_adapter_raises_response_process_exception_on_400s(self): responses.add(responses.GET, self.wrapper.test().data, body='{"erros": "Server Error"}', status=400, content_type='application/json') response = requests.get(self.wrapper.test().data) with self.assertRaises(ResponseProcessException): TesterClientAdapter().process_response(response) @responses.activate def test_adapter_raises_response_process_exception_on_500s(self): responses.add(responses.GET, self.wrapper.test().data, body='{"erros": "Server Error"}', status=500, content_type='application/json') response = requests.get(self.wrapper.test().data) with self.assertRaises(ResponseProcessException): TesterClientAdapter().process_response(response) @responses.activate def test_raises_request_error(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') with self.assertRaises(ClientError): self.wrapper.test().get() @responses.activate def test_raises_server_error(self): responses.add(responses.GET, self.wrapper.test().data, status=500, content_type='application/json') with self.assertRaises(ServerError): self.wrapper.test().get()
class TestTapiocaClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_fill_url_template(self): expected_url = 'https://api.test.com/user/123/' resource = self.wrapper.user(id='123') self.assertEqual(resource.data, expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) self.assertEqual(len(client), 3) def test_iterated_client_items_should_be_tapioca_instances(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for item in client: self.assertTrue(isinstance(item, TapiocaClient)) def test_iterated_client_items_should_contain_list_items(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for i, item in enumerate(client): self.assertEqual(item().data, i) @responses.activate def test_in_operator(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": 1, "other": 2}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertIn('data', response) self.assertIn('other', response) self.assertNotIn('wat', response) @responses.activate def test_trasnform_camelCase_in_snake_case(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response.data.key_snake().data, 'value') self.assertEqual(response.data.camel_case().data, 'data in camel case') self.assertEqual(response.data.normal_camel_case().data, 'data in camel case')
class TestTapiocaExecutor(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_resource_executor_data_should_be_composed_url(self): expected_url = 'https://api.test.com/test/' resource = self.wrapper.test() self.assertEqual(resource.data(), expected_url) def test_docs(self): self.assertEqual('\n'.join(self.wrapper.resource.__doc__.split('\n')[1:]), 'Resource: ' + self.wrapper.resource._resource['resource'] + '\n' 'Docs: ' + self.wrapper.resource._resource['docs'] + '\n' 'Foo: ' + self.wrapper.resource._resource['foo'] + '\n' 'Spam: ' + self.wrapper.resource._resource['spam']) def test_access_data_attributres_through_executor(self): client = self.wrapper._wrap_in_tapioca({'test': 'value'}) items = client().items() self.assertTrue(isinstance(items, TapiocaClient)) data = dict(items().data()) self.assertEqual(data, {'test': 'value'}) def test_is_possible_to_reverse_a_list_through_executor(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) client().reverse() self.assertEqual(client().data(), [2,1,0]) def test_cannot__getittem__(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) with self.assertRaises(Exception): client()[0] def test_cannot_iterate(self): client = self.wrapper._wrap_in_tapioca([0,1,2]) with self.assertRaises(Exception): for item in client(): pass
class TestTapiException(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_exception_contain_tapi_client(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiException as e: exception = e self.assertIs(exception.client.__class__, TapiClient) @responses.activate def test_exception_contain_status_code(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiException as e: exception = e self.assertIs(exception.status_code, 400) @responses.activate def test_exception_message(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiException as e: exception = e self.assertEqual(str(exception), 'response status code: 400')
class TestTapiocaException(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_exception_contain_tapioca_client(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiocaException as e: exception = e self.assertIs(exception.client.__class__, TapiocaClient) @responses.activate def test_exception_contain_status_code(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiocaException as e: exception = e self.assertIs(exception.status_code, 400) @responses.activate def test_exception_message(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=400, content_type='application/json') try: self.wrapper.test().get() except TapiocaException as e: exception = e self.assertEqual(str(exception), 'response status code: 400')
def setUp(self): self.wrapper = TesterClient()
class TestTapiClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_added_custom_resources(self): wrapper = TesterClient(resource_mapping=[ Resource("myresource", "http://url.ru/myresource"), Resource("myresource2", "https://url.ru/myresource2"), ]) responses.add(responses.GET, url=wrapper.myresource().data, body='[]', status=200, content_type='application/json') response = self.wrapper.myresource().get() assert response.data == [] def test_fill_url_template(self): expected_url = 'https://api.test.com/user/123/' resource = self.wrapper.user(id='123') self.assertEqual(resource.data, expected_url) def test_fill_another_root_url_template(self): expected_url = 'https://api.another.com/another-root/' resource = self.wrapper.another_root() self.assertEqual(resource.data, expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapi([0, 1, 2]) self.assertEqual(len(client), 3) def test_iterated_client_items(self): client = self.wrapper._wrap_in_tapi([0, 1, 2]) for i, item in enumerate(client): self.assertEqual(item, i) def test_client_data_dict(self): client = self.wrapper._wrap_in_tapi({"data": 0}) assert client["data"] == 0 assert client.data == {"data": 0} def test_client_data_list(self): client = self.wrapper._wrap_in_tapi([0, 1, 2]) assert client[0] == 0 assert client[1] == 1 @responses.activate def test_in_operator(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": 1, "other": 2}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertIn('data', response) self.assertIn('other', response) self.assertNotIn('wat', response) @responses.activate def test_accessing_index_out_of_bounds_should_raise_index_error(self): responses.add(responses.GET, self.wrapper.test().data, body='["a", "b", "c"]', status=200, content_type='application/json') response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3] @responses.activate def test_accessing_empty_list_should_raise_index_error(self): responses.add(responses.GET, self.wrapper.test().data, body='[]', status=200, content_type='application/json') response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3] def test_fill_url_from_default_params(self): wrapper = TesterClient(default_url_params={'id': 123}) self.assertEqual(wrapper.user().data, 'https://api.test.com/user/123/')
def test_fill_url_from_default_params(self): wrapper = TesterClient(default_url_params={'id': 123}) self.assertEqual(wrapper.user().data, 'https://api.test.com/user/123/')
def test_passing_serializer_on_instatiation(self): wrapper = TesterClient(serializer_class=SimpleSerializer) serializer = wrapper._api.serializer self.assertTrue(isinstance(serializer, BaseSerializer))
class TestTapiocaExecutor(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_resource_executor_data_should_be_composed_url(self): expected_url = 'https://api.test.com/test/' resource = self.wrapper.test() self.assertEqual(resource.data, expected_url) def test_docs(self): self.assertEqual( '\n'.join(self.wrapper.resource.__doc__.split('\n')[1:]), 'Resource: ' + self.wrapper.resource._resource['resource'] + '\n' 'Docs: ' + self.wrapper.resource._resource['docs'] + '\n' 'Foo: ' + self.wrapper.resource._resource['foo'] + '\n' 'Spam: ' + self.wrapper.resource._resource['spam']) def test_access_data_attributres_through_executor(self): client = self.wrapper._wrap_in_tapioca({'test': 'value'}) items = client().items() self.assertTrue(isinstance(items, TapiocaClient)) data = dict(items().data) self.assertEqual(data, {'test': 'value'}) def test_is_possible_to_reverse_a_list_through_executor(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) client().reverse() self.assertEqual(client().data, [2, 1, 0]) def test_cannot__getittem__(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): client()[0] def test_cannot_iterate(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): for item in client(): pass def test_dir_call_returns_executor_methods(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) e_dir = dir(client()) self.assertIn('data', e_dir) self.assertIn('response', e_dir) self.assertIn('get', e_dir) self.assertIn('post', e_dir) self.assertIn('pages', e_dir) self.assertIn('open_docs', e_dir) self.assertIn('open_in_browser', e_dir) @responses.activate def test_response_executor_object_has_a_response(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() executor = response() executor.response executor._response = None def test_raises_error_if_executor_does_not_have_a_response_object(self): client = self.wrapper with self.assertRaises(Exception): client().response @responses.activate def test_response_executor_has_a_status_code(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().status_code, 200)
class TestTapiocaExecutorRequests(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_when_executor_has_no_response(self): with self.assertRaises(Exception) as context: self.wrapper.test().response exception = context.exception self.assertIn("has no response", str(exception)) @responses.activate def test_get_request(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_access_response_field(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() response_data = response.data() self.assertEqual(response_data.data, {'key': 'value'}) @responses.activate def test_post_request(self): responses.add(responses.POST, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().post() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_token_expired_and_not_refresh_flag(self): responses.add(responses.POST, self.wrapper.test().data, body='{"error": "Token expired"}', status=401, content_type='application/json') with self.assertRaises(ClientError) as context: response = self.wrapper.test().post() @responses.activate def test_token_expired_and_refresh_flag(self): self.first_call = True responses.add_callback( responses.POST, self.wrapper.test().data, callback=self.request_callback, content_type='application/json', ) response = self.wrapper.test().post(refresh_auth=True) def request_callback(self, request): if self.first_call: self.first_call = False return (401, {'content_type':'application/json'}, json.dumps('{"error": "Token expired"}')) else: self.first_call = None return (201, {'content_type':'application/json'}, json.dumps('{"error": "Token expired"}')) @responses.activate def test_put_request(self): responses.add(responses.PUT, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().put() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_patch_request(self): responses.add(responses.PATCH, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().patch() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_delete_request(self): responses.add(responses.DELETE, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().delete() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_simple_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 2) @responses.activate def test_simple_pages_with_max_items_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=3, max_pages=2): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 3) @responses.activate def test_simple_pages_with_max_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=3): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 7) @responses.activate def test_simple_pages_max_page_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, 'value') iterations_count += 1 @responses.activate def test_data_serialization(self): wrapper = SerializerClient() responses.add(responses.POST, self.wrapper.test().data, body='{}', status=200, content_type='application/json') string_date = '2014-11-13T14:53:18.694072+00:00' string_decimal = '1.45' data = { 'date': arrow.get(string_date).datetime, 'decimal': Decimal(string_decimal), } wrapper.test().post(data=data) request_body = responses.calls[0].request.body self.assertEqual( json.loads(request_body), {'date': string_date, 'decimal': string_decimal})
class TestTapiocaExecutorRequests(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_when_executor_has_no_response(self): with self.assertRaises(Exception) as context: self.wrapper.test().response exception = context.exception self.assertIn("has no response", str(exception)) @responses.activate def test_get_request(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_access_response_field(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() response_data = response.data() self.assertEqual(response_data.data, {'key': 'value'}) @responses.activate def test_post_request(self): responses.add(responses.POST, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().post() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_put_request(self): responses.add(responses.PUT, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().put() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_patch_request(self): responses.add(responses.PATCH, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().patch() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_delete_request(self): responses.add(responses.DELETE, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().delete() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_simple_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 2) @responses.activate def test_simple_pages_with_max_items_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=3, max_pages=2): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 3) @responses.activate def test_simple_pages_with_max_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=3): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 7) @responses.activate def test_simple_pages_max_page_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, 'value') iterations_count += 1 @responses.activate def test_data_serialization(self): wrapper = SerializerClient() responses.add(responses.POST, self.wrapper.test().data, body='{}', status=200, content_type='application/json') string_date = '2014-11-13T14:53:18.694072+00:00' string_decimal = '1.45' data = { 'date': arrow.get(string_date).datetime, 'decimal': Decimal(string_decimal), } wrapper.test().post(data=data) request_body = responses.calls[0].request.body self.assertEqual(json.loads(request_body), { 'date': string_date, 'decimal': string_decimal })
class TestIteratorFeatures(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_simple_pages_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(): self.assertIn(item.key().data, "value") iterations_count += 1 self.assertEqual(iterations_count, 2) @responses.activate def test_simple_pages_with_max_items_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=3, max_pages=2): self.assertIn(item.key().data, "value") iterations_count += 1 self.assertEqual(iterations_count, 3) @responses.activate def test_simple_pages_with_max_pages_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=3): self.assertIn(item.key().data, "value") iterations_count += 1 self.assertEqual(iterations_count, 7) @responses.activate def test_simple_pages_max_page_zero_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=0): self.assertIn(item.key().data, "value") iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, "value") iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, "value") iterations_count += 1
class TestTapiocaExecutorRequests(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_when_executor_has_no_response(self): with self.assertRaises(Exception) as context: self.wrapper.test().response exception = context.exception self.assertIn("has no response", str(exception)) @responses.activate def test_get_request(self): responses.add( responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() self.assertEqual(response().data, {"data": {"key": "value"}}) @responses.activate def test_access_response_field(self): responses.add( responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() response_data = response.data() self.assertEqual(response_data.data, {"key": "value"}) @responses.activate def test_post_request(self): responses.add( responses.POST, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type="application/json", ) response = self.wrapper.test().post() self.assertEqual(response().data, {"data": {"key": "value"}}) @responses.activate def test_put_request(self): responses.add( responses.PUT, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type="application/json", ) response = self.wrapper.test().put() self.assertEqual(response().data, {"data": {"key": "value"}}) @responses.activate def test_patch_request(self): responses.add( responses.PATCH, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type="application/json", ) response = self.wrapper.test().patch() self.assertEqual(response().data, {"data": {"key": "value"}}) @responses.activate def test_delete_request(self): responses.add( responses.DELETE, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type="application/json", ) response = self.wrapper.test().delete() self.assertEqual(response().data, {"data": {"key": "value"}})
class TestIteratorFeatures(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() @responses.activate def test_simple_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 2) @responses.activate def test_simple_pages_with_max_items_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=3, max_pages=2): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 3) @responses.activate def test_simple_pages_with_max_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body= '{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=3): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 7) @responses.activate def test_simple_pages_max_page_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data, 'value') iterations_count += 1 self.assertEqual(iterations_count, 0)
class TestTapiocaClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_fill_url_template(self): expected_url = 'https://api.test.com/user/123/' resource = self.wrapper.user(id='123') self.assertEqual(resource.data, expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) self.assertEqual(len(client), 3) def test_iterated_client_items_should_be_tapioca_instances(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for item in client: self.assertTrue(isinstance(item, TapiocaClient)) def test_iterated_client_items_should_contain_list_items(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for i, item in enumerate(client): self.assertEqual(item().data, i) @responses.activate def test_in_operator(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": 1, "other": 2}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertIn('data', response) self.assertIn('other', response) self.assertNotIn('wat', response) @responses.activate def test_transform_camelCase_in_snake_case(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body= '{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response.data.key_snake().data, 'value') self.assertEqual(response.data.camel_case().data, 'data in camel case') self.assertEqual(response.data.normal_camel_case().data, 'data in camel case') @responses.activate def test_should_be_able_to_access_by_index(self): responses.add(responses.GET, self.wrapper.test().data, body='["a", "b", "c"]', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response[0]().data, 'a') self.assertEqual(response[1]().data, 'b') self.assertEqual(response[2]().data, 'c') @responses.activate def test_accessing_index_out_of_bounds_should_raise_index_error(self): responses.add(responses.GET, self.wrapper.test().data, body='["a", "b", "c"]', status=200, content_type='application/json') response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3] @responses.activate def test_accessing_empty_list_should_raise_index_error(self): responses.add(responses.GET, self.wrapper.test().data, body='[]', status=200, content_type='application/json') response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3] def test_fill_url_from_default_params(self): wrapper = TesterClient(default_url_params={'id': 123}) self.assertEqual(wrapper.user().data, 'https://api.test.com/user/123/')
class TestTapiocaExecutor(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_resource_executor_data_should_be_composed_url(self): expected_url = 'https://api.test.com/test/' resource = self.wrapper.test() self.assertEqual(resource.data, expected_url) def test_docs(self): self.assertEqual( '\n'.join(self.wrapper.resource.__doc__.split('\n')[1:]), 'Resource: ' + self.wrapper.resource._resource['resource'] + '\n' 'Docs: ' + self.wrapper.resource._resource['docs'] + '\n' 'Foo: ' + self.wrapper.resource._resource['foo'] + '\n' 'Spam: ' + self.wrapper.resource._resource['spam']) def test_access_data_attributres_through_executor(self): client = self.wrapper._wrap_in_tapioca({'test': 'value'}) items = client().items() self.assertTrue(isinstance(items, TapiocaClient)) data = dict(items().data) self.assertEqual(data, {'test': 'value'}) def test_is_possible_to_reverse_a_list_through_executor(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) client().reverse() self.assertEqual(client().data, [2, 1, 0]) def test_cannot__getittem__(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): client()[0] def test_cannot_iterate(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): for item in client(): pass def test_dir_call_returns_executor_methods(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) e_dir = dir(client()) self.assertIn('data', e_dir) self.assertIn('response', e_dir) self.assertIn('get', e_dir) self.assertIn('post', e_dir) self.assertIn('pages', e_dir) self.assertIn('open_docs', e_dir) self.assertIn('open_in_browser', e_dir) @responses.activate def test_response_executor_object_has_a_response(self): next_url = 'http://api.teste.com/next_batch' responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() executor = response() executor.response executor._response = None def test_raises_error_if_executor_does_not_have_a_response_object(self): client = self.wrapper with self.assertRaises(Exception): client().response @responses.activate def test_response_executor_has_a_status_code(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().status_code, 200)
class TestTapiocaExecutorRequests(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_when_executor_has_no_response(self): with self.assertRaisesRegexp(Exception, "has no response"): self.wrapper.test().response() @responses.activate def test_get_request(self): responses.add(responses.GET, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().data(), {'data': {'key': 'value'}}) @responses.activate def test_access_response_field(self): responses.add(responses.GET, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() response_data = response.data() self.assertEqual(response_data.data(), {'key': 'value'}) @responses.activate def test_post_request(self): responses.add(responses.POST, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().post() self.assertEqual(response().data(), {'data': {'key': 'value'}}) @responses.activate def test_put_request(self): responses.add(responses.PUT, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().put() self.assertEqual(response().data(), {'data': {'key': 'value'}}) @responses.activate def test_patch_request(self): responses.add(responses.PATCH, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().patch() self.assertEqual(response().data(), {'data': {'key': 'value'}}) @responses.activate def test_delete_request(self): responses.add(responses.DELETE, self.wrapper.test().data(), body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().delete() self.assertEqual(response().data(), {'data': {'key': 'value'}}) @responses.activate def test_simple_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(): self.assertIn(item.key().data(), 'value') iterations_count += 1 self.assertEqual(iterations_count, 2) @responses.activate def test_simple_pages_with_max_items_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=3, max_pages=2): self.assertIn(item.key().data(), 'value') iterations_count += 1 self.assertEqual(iterations_count, 3) @responses.activate def test_simple_pages_with_max_pages_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}'% next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": "%s"}}'% next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}, {"key": "value"}, {"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=3): self.assertIn(item.key().data(), 'value') iterations_count += 1 self.assertEqual(iterations_count, 7) @responses.activate def test_simple_pages_max_page_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_pages=0): self.assertIn(item.key().data(), 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_simple_pages_max_item_zero_iterator(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() iterations_count = 0 for item in response().pages(max_items=0): self.assertIn(item.key().data(), 'value') iterations_count += 1 self.assertEqual(iterations_count, 0) @responses.activate def test_response_executor_object_has_a_response(self): next_url = 'http://api.teste.com/next_batch' responses.add(responses.GET, self.wrapper.test().data(), body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type='application/json') responses.add(responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type='application/json') response = self.wrapper.test().get() executor = response() executor.response() executor._response = None with self.assertRaises(Exception): executor.response()
class TestTapiocaExecutorRequests(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_when_executor_has_no_response(self): with self.assertRaises(Exception) as context: self.wrapper.test().response exception = context.exception self.assertIn("has no response", str(exception)) @responses.activate def test_get_request(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_access_response_field(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() response_data = response.data() self.assertEqual(response_data.data, {'key': 'value'}) @responses.activate def test_post_request(self): responses.add(responses.POST, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().post() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_put_request(self): responses.add(responses.PUT, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().put() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_patch_request(self): responses.add(responses.PATCH, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().patch() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_delete_request(self): responses.add(responses.DELETE, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=201, content_type='application/json') response = self.wrapper.test().delete() self.assertEqual(response().data, {'data': {'key': 'value'}}) @responses.activate def test_carries_request_kwargs_over_calls(self): responses.add(responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type='application/json') response = self.wrapper.test().get() request_kwargs = response.data.key()._request_kwargs self.assertIn('url', request_kwargs) self.assertIn('data', request_kwargs) self.assertIn('headers', request_kwargs)
class TestTapiocaExecutor(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_resource_executor_data_should_be_composed_url(self): expected_url = "https://api.test.com/test/" resource = self.wrapper.test() self.assertEqual(resource.data, expected_url) def test_docs(self): self.assertEqual( "\n".join(self.wrapper.resource.__doc__.split("\n")[1:]), "Resource: " + self.wrapper.resource._resource["resource"] + "\n" "Docs: " + self.wrapper.resource._resource["docs"] + "\n" "Foo: " + self.wrapper.resource._resource["foo"] + "\n" "Spam: " + self.wrapper.resource._resource["spam"], ) def test_access_data_attributres_through_executor(self): client = self.wrapper._wrap_in_tapioca({"test": "value"}) items = client().items() self.assertTrue(isinstance(items, TapiocaClient)) data = dict(items().data) self.assertEqual(data, {"test": "value"}) def test_is_possible_to_reverse_a_list_through_executor(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) client().reverse() self.assertEqual(client().data, [2, 1, 0]) def test_cannot__getittem__(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): client()[0] def test_cannot_iterate(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) with self.assertRaises(Exception): for item in client(): pass def test_dir_call_returns_executor_methods(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) e_dir = dir(client()) self.assertIn("data", e_dir) self.assertIn("response", e_dir) self.assertIn("get", e_dir) self.assertIn("post", e_dir) self.assertIn("pages", e_dir) self.assertIn("open_docs", e_dir) self.assertIn("open_in_browser", e_dir) @responses.activate def test_response_executor_object_has_a_response(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) responses.add( responses.GET, next_url, body='{"data": [{"key": "value"}], "paging": {"next": ""}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() executor = response() executor.response executor._response = None def test_raises_error_if_executor_does_not_have_a_response_object(self): client = self.wrapper with self.assertRaises(Exception): client().response @responses.activate def test_response_executor_has_a_status_code(self): responses.add( responses.GET, self.wrapper.test().data, body='{"data": {"key": "value"}}', status=200, content_type="application/json", ) response = self.wrapper.test().get() self.assertEqual(response().status_code, 200)
class TestTapiocaClient(unittest.TestCase): def setUp(self): self.wrapper = TesterClient() def test_fill_url_template(self): expected_url = "https://api.test.com/user/123/" resource = self.wrapper.user(id="123") self.assertEqual(resource.data, expected_url) def test_calling_len_on_tapioca_list(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) self.assertEqual(len(client), 3) def test_iterated_client_items_should_be_tapioca_instances(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for item in client: self.assertTrue(isinstance(item, TapiocaClient)) def test_iterated_client_items_should_contain_list_items(self): client = self.wrapper._wrap_in_tapioca([0, 1, 2]) for i, item in enumerate(client): self.assertEqual(item().data, i) @responses.activate def test_in_operator(self): responses.add( responses.GET, self.wrapper.test().data, body='{"data": 1, "other": 2}', status=200, content_type="application/json", ) response = self.wrapper.test().get() self.assertIn("data", response) self.assertIn("other", response) self.assertNotIn("wat", response) @responses.activate def test_transform_camelCase_in_snake_case(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}' % next_url, status=200, content_type="application/json", ) response = self.wrapper.test().get() self.assertEqual(response.data.key_snake().data, "value") self.assertEqual(response.data.camel_case().data, "data in camel case") self.assertEqual(response.data.normal_camel_case().data, "data in camel case") @responses.activate def test_should_be_able_to_access_by_index(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='["a", "b", "c"]', status=200, content_type="application/json" ) response = self.wrapper.test().get() self.assertEqual(response[0]().data, "a") self.assertEqual(response[1]().data, "b") self.assertEqual(response[2]().data, "c") @responses.activate def test_accessing_index_out_of_bounds_should_raise_index_error(self): next_url = "http://api.teste.com/next_batch" responses.add( responses.GET, self.wrapper.test().data, body='["a", "b", "c"]', status=200, content_type="application/json" ) response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3] @responses.activate def test_accessing_empty_list_should_raise_index_error(self): next_url = "http://api.teste.com/next_batch" responses.add(responses.GET, self.wrapper.test().data, body="[]", status=200, content_type="application/json") response = self.wrapper.test().get() with self.assertRaises(IndexError): response[3]