Skip to content

Commit

Permalink
Tests for exception/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Jan 12, 2017
1 parent 7d0c43e commit 58580f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
24 changes: 12 additions & 12 deletions challonge/helpers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import aiohttp
import logging

from . import CHALLONGE_USE_FIELDS_DESCRIPTORS, CHALLONGE_USE_EXCEPTIONS
import challonge


DEFAULT_TIMEOUT = 30
log = logging.getLogger('challonge')


class APIException(Exception):
""" If anything goes wrong during a request to the Challonge API, this exception will be raised. """
pass


def assert_or_raise(cond, exc, msg: str = None):
def assert_or_raise(cond, exc, *args):
if not cond:
if CHALLONGE_USE_EXCEPTIONS:
raise exc(msg)
# print(challonge.CHALLONGE_USE_EXCEPTIONS)
if challonge.CHALLONGE_USE_EXCEPTIONS:
raise exc(*args)
else:
print('a silent exception `{.__name__}` has been raised `{}`'.format(exc, msg))
log.warning('a silent exception `{}` has been raised `{}`'.format(exc.__name__, args))


class FieldDescriptor:
Expand All @@ -43,7 +46,7 @@ def _find_holder(self, local_list, e_id):

def _get_from_dict(self, data):
for a in self._fields:
name = FieldHolder.private_name.format(a) if CHALLONGE_USE_FIELDS_DESCRIPTORS else a
name = FieldHolder.private_name.format(a) if challonge.CHALLONGE_USE_FIELDS_DESCRIPTORS else a
setattr(self, name, data[a] if a in data else None)

def __init__(cls, name, bases, dct):
Expand All @@ -54,7 +57,7 @@ def __init__(cls, name, bases, dct):
cls._get_from_dict = FieldHolder._get_from_dict
cls.__eq__ = lambda self, other: self._id == other._id

if CHALLONGE_USE_FIELDS_DESCRIPTORS:
if challonge.CHALLONGE_USE_FIELDS_DESCRIPTORS:
for a in cls._fields:
setattr(cls, a, FieldDescriptor(FieldHolder.private_name.format(a)))

Expand Down Expand Up @@ -88,12 +91,9 @@ async def __call__(self, method: str, uri: str, params_prefix: str =None, **para
auth = aiohttp.BasicAuth(login=self.username, password=self.api_key)
async with session.request(method, url, params=params, auth=auth) as response:
resp = await response.json()
if response.status in [401, 404, 406, 422, 500]:
raise APIException(uri, params, response.reason)
if response.status != 200:
raise ValueError
assert_or_raise(response.status not in [401, 404, 406, 422, 500], APIException, uri, params, response.reason)
assert_or_raise(response.status == 200, ValueError, 'Unknown API return code', uri, params, response.reason)
return resp
return None

@staticmethod
def _prepare_params(params, prefix=None) -> dict:
Expand Down
25 changes: 25 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ def test_cleanup(self):
yield from user.destroy_tournament(t)


# @unittest.skip('')
class SystemTestCase(unittest.TestCase):
@async_test
def setUp(self):
self.user = yield from challonge.get_user(username, api_key)

# @unittest.skip('')
@async_test
def test_a_raise(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)

with self.assertRaises(NameError):
yield from t.update(fake_argument=0)

challonge.CHALLONGE_USE_EXCEPTIONS = False

with self.assertLogs('challonge', level='WARN'):
yield from t.update(fake_argument=0)

challonge.CHALLONGE_USE_EXCEPTIONS = True

yield from self.user.destroy_tournament(t)


# @unittest.skip('')
class AAUserTestCase(unittest.TestCase):
# @unittest.skip('')
Expand Down

0 comments on commit 58580f1

Please sign in to comment.