Skip to content

Commit

Permalink
improving tests and fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeximenes committed Feb 28, 2016
1 parent 85f81e6 commit 89d042b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 59 deletions.
2 changes: 1 addition & 1 deletion tapioca/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_iterator_next_request_kwargs(self, iterator_request_kwargs,
raise NotImplementedError()

def is_authentication_expired(self, exception, *args, **kwargs):
return False
raise NotImplementedError()

def refresh_authentication(self, api_params, *args, **kwargs):
raise NotImplementedError()
Expand Down
10 changes: 3 additions & 7 deletions tapioca/tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ def _get_client_from_name_or_fallback(self, name):
if client is not None:
return client

try:
normal_camel_case_name = camel_case_name[0].upper()
except Exception:
return None
normal_camel_case_name = camel_case_name[0].upper()
normal_camel_case_name += camel_case_name[1:]

client = self._get_client_from_name(normal_camel_case_name)
Expand All @@ -97,12 +94,11 @@ def _get_client_from_name_or_fallback(self, name):
return None

def _get_client_from_name(self, name):

if self._data and \
(isinstance(self._data, list) and isinstance(name, int) or
if (isinstance(self._data, list) and isinstance(name, int) or
hasattr(self._data, '__iter__') and name in self._data):
return self._wrap_in_tapioca(data=self._data[name])

# if could not access, falback to resource mapping
resource_mapping = self._api.resource_mapping
if name in resource_mapping:
resource = resource_mapping[name]
Expand Down
17 changes: 12 additions & 5 deletions tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ def get_iterator_next_request_kwargs(self, iterator_request_kwargs,
if url:
return {'url': url}

def is_authentication_expired(self, exception, *args, **kwargs):
return exception.status_code == 401

def refresh_authentication(self, api_params, *args, **kwargs):
pass

TesterClient = generate_wrapper_from_adapter(TesterClientAdapter)

Expand All @@ -58,3 +53,15 @@ class SerializerClientAdapter(TesterClientAdapter):


SerializerClient = generate_wrapper_from_adapter(SerializerClientAdapter)


class TokenRefreshClientAdapter(TesterClientAdapter):

def is_authentication_expired(self, exception, *args, **kwargs):
return exception.status_code == 401

def refresh_authentication(self, api_params, *args, **kwargs):
pass


TokenRefreshClient = generate_wrapper_from_adapter(TokenRefreshClientAdapter)
22 changes: 22 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import arrow
import unittest
import responses
import json
from decimal import Decimal

from tapioca.serializers import BaseSerializer, SimpleSerializer
Expand Down Expand Up @@ -40,6 +41,27 @@ def test_executor_dir_returns_serializer_methods(self):
self.assertIn('to_datetime', e_dir)
self.assertIn('to_decimal', e_dir)

@responses.activate
def test_request_with_data_serialization(self):
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),
}

self.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 TestDeserialization(unittest.TestCase):

Expand Down
135 changes: 89 additions & 46 deletions tests/test_tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tapioca.serializers import SimpleSerializer
from tapioca.exceptions import ClientError

from tests.client import TesterClient, SerializerClient
from tests.client import TesterClient, SerializerClient, TokenRefreshClient


class TestTapiocaClient(unittest.TestCase):
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_in_operator(self):
self.assertNotIn('wat', response)

@responses.activate
def test_trasnform_camelCase_in_snake_case(self):
def test_transform_camelCase_in_snake_case(self):
next_url = 'http://api.teste.com/next_batch'

responses.add(responses.GET, self.wrapper.test().data,
Expand All @@ -72,6 +72,48 @@ def test_trasnform_camelCase_in_snake_case(self):
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]


class TestTapiocaExecutor(unittest.TestCase):
Expand Down Expand Up @@ -220,34 +262,6 @@ def test_post_request(self):

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,
Expand Down Expand Up @@ -281,6 +295,12 @@ def test_delete_request(self):

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'
Expand Down Expand Up @@ -426,26 +446,49 @@ def test_simple_pages_max_item_zero_iterator(self):
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')
class TestTokenRefreshing(unittest.TestCase):

def setUp(self):
self.wrapper = TokenRefreshClient()

@responses.activate
def test_not_token_refresh_ready_client_call_raises_not_implemented(self):
no_refresh_client = TesterClient()

string_date = '2014-11-13T14:53:18.694072+00:00'
string_decimal = '1.45'
responses.add_callback(
responses.POST, no_refresh_client.test().data,
callback=lambda *a, **k: (401, {}, ''),
content_type='application/json',
)

data = {
'date': arrow.get(string_date).datetime,
'decimal': Decimal(string_decimal),
}
with self.assertRaises(NotImplementedError):
no_refresh_client.test().post(refresh_auth=True)

wrapper.test().post(data=data)
@responses.activate
def test_token_expired_and_no_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()

request_body = responses.calls[0].request.body
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"}'))

self.assertEqual(
json.loads(request_body),
{'date': string_date, 'decimal': string_decimal})
@responses.activate
def test_token_expired_with_active_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)

0 comments on commit 89d042b

Please sign in to comment.