Skip to content

Commit

Permalink
100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuykendall committed Nov 7, 2016
1 parent 9dc8a07 commit 77c42a3
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
include =
marvelous/*
25 changes: 12 additions & 13 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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`


Expand Down
2 changes: 1 addition & 1 deletion marvelous/comic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions marvelous/comics_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 10 additions & 4 deletions marvelous/series.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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

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))

Expand All @@ -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]
Expand Down
10 changes: 2 additions & 8 deletions marvelous/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions tests/cache_test.py
Original file line number Diff line number Diff line change
@@ -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()
35 changes: 35 additions & 0 deletions tests/series_test.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file modified tests/testing_mock.sqlite
Binary file not shown.

0 comments on commit 77c42a3

Please sign in to comment.