diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..5ab8eaf --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +include = + marvelous/* diff --git a/docs/index.rst b/docs/index.rst index 12fb630..dd8b4ba 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -70,25 +70,24 @@ Instantiating API public_key = "Public key from https://developer.marvel.com/account" private_key = "Private key from https://developer.marvel.com/account" - print_calls = True # Will print URL of app API calls, for debugging - def CacheClass: - def get(self, key): - # This method should return cahed value with key - return None + # Optional + class CacheClass: + def get(self, key): + # This method should return cahed value with key + return None - def store(self, key, value): - # This method should store key value pair - return + def store(self, key, value): + # This method should store key value pair + return cache = CacheClass() # m is a session object, read about it below m = marvelous.api( - public_key, - private_key, - print_calls=print_calls, - cache=cache + public_key, + private_key, + cache=cache ) @@ -187,7 +186,7 @@ Series - ``response`` - Dictionary, raw response body - ``id`` - Int - ``resource_uri`` - String, `resourceURI` from API -- ``name`` - String +- ``title`` - String - ``comics`` - Method, Returns ``ComicsList`` object for `/v1/public/series/{seriesId}/comics` diff --git a/marvelous/comic.py b/marvelous/comic.py index 9d7ec12..07d29c3 100644 --- a/marvelous/comic.py +++ b/marvelous/comic.py @@ -47,7 +47,7 @@ def process_input(self, data): # Marvel comic 1768, and maybe others, returns a modified of # "-0001-11-30T00:00:00-0500". The best way to handle this is # probably just to ignore it, since I don't know how to fix it. - if new_data['modified'][0] == '-': + if new_data.get('modified', ' ')[0] == '-': del new_data['modified'] return new_data diff --git a/marvelous/comics_list.py b/marvelous/comics_list.py index dd15f5e..562612f 100644 --- a/marvelous/comics_list.py +++ b/marvelous/comics_list.py @@ -19,6 +19,9 @@ def __init__(self, response): def __iter__(self): return iter(self.comics) + def __len__(self): + return len(self.comics) + def __getitem__(self, index): try: return next(itertools.islice(self.comics, index, index+1)) diff --git a/marvelous/series.py b/marvelous/series.py index 74edb15..176684a 100644 --- a/marvelous/series.py +++ b/marvelous/series.py @@ -1,9 +1,9 @@ from marshmallow import Schema, fields, pre_load, post_load -from . import comics_list +from . import comics_list, exceptions -class Series(): +class Series: def __init__(self, **kwargs): if 'response' not in kwargs: kwargs['response'] = None @@ -11,7 +11,10 @@ def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) - def comics(self, params): + def comics(self, params=None): + if params is None: + params = {} + return comics_list.ComicsList( self.session.call(['series', self.id, 'comics'], params=params)) @@ -20,10 +23,13 @@ class SeriesSchema(Schema): response = fields.Raw() id = fields.Int() resourceURI = fields.Str(attribute='resource_uri') - name = fields.Str() + title = fields.Str() @pre_load def process_input(self, data): + if data.get('code', 200) != 200: + raise exceptions.ApiError(data.get('status')) + if 'status' in data: data['data']['results'][0]['response'] = data data = data['data']['results'][0] diff --git a/marvelous/session.py b/marvelous/session.py index 23c375b..601834b 100644 --- a/marvelous/session.py +++ b/marvelous/session.py @@ -8,13 +8,10 @@ class Session(): api_url = "http://gateway.marvel.com:80/v1/public/{}" - def __init__( - self, public_key, private_key, cache=None, - print_calls=False): + def __init__(self, public_key, private_key, cache=None): self.public_key = public_key self.private_key = private_key - self.print_calls = print_calls self.cache = cache def call(self, endpoint, params=None): @@ -46,13 +43,10 @@ def call(self, endpoint, params=None): response = requests.get(url, params=params) - if self.print_calls: - print(response.url) - data = response.json() if 'message' in data: - raise exceptions.ApiError(response['message']) + raise exceptions.ApiError(data['message']) if self.cache and response.status_code == 200: try: diff --git a/tests/cache_test.py b/tests/cache_test.py new file mode 100644 index 0000000..a968130 --- /dev/null +++ b/tests/cache_test.py @@ -0,0 +1,42 @@ +import os +import unittest + +import marvelous + + +class NoGet: + def store(self, key, value): + # This method should store key value pair + return + + +class NoStore: + def get(self, key): + # This method should return cahed value with key + return None + + +class TestComics(unittest.TestCase): + def setUp(self): + self.pub = os.getenv('PUBLIC_KEY', 'pub') + self.priv = os.getenv('PRIVATE_KEY', 'priv') + + def test_no_get(self): + m = marvelous.api( + public_key=self.pub, private_key=self.priv, + cache=NoGet()) + + with self.assertRaises(marvelous.exceptions.CacheError): + m.series(466) + + def test_no_store(self): + m = marvelous.api( + public_key=self.pub, private_key=self.priv, + cache=NoStore()) + + with self.assertRaises(marvelous.exceptions.CacheError): + m.series(466) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/series_test.py b/tests/series_test.py new file mode 100644 index 0000000..3a7dd7b --- /dev/null +++ b/tests/series_test.py @@ -0,0 +1,35 @@ +import os +import unittest + +import marvelous +from marvelous.comics_list import ComicsList + + +class TestComics(unittest.TestCase): + def setUp(self): + pub = os.getenv('PUBLIC_KEY', 'pub') + priv = os.getenv('PRIVATE_KEY', 'priv') + self.m = marvelous.api( + public_key=pub, private_key=priv, + cache=marvelous.SqliteCache("tests/testing_mock.sqlite")) + + def test_known_series(self): + usms = self.m.series(466) + self.assertTrue(usms.title == "Ultimate Spider-Man (2000 - 2009)") + self.assertTrue(usms.id == 466) + comics = usms.comics() + self.assertTrue(comics[0].id == 23931) + + self.assertTrue(len(comics[:5]) == 5) + self.assertTrue(len(comics) == len([x for x in comics if x.id > 3])) + + def test_bad_series(self): + with self.assertRaises(marvelous.exceptions.ApiError): + self.m.series(-1) + + def test_bad_response_data(self): + with self.assertRaises(marvelous.exceptions.ApiError): + ComicsList({'data': {'results': [{'modified': 'potato'}]}}) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/testing_mock.sqlite b/tests/testing_mock.sqlite index aab3d18..84afbe5 100644 Binary files a/tests/testing_mock.sqlite and b/tests/testing_mock.sqlite differ