diff --git a/cookie_api/app.py b/cookie_api/app.py index 3753ed0..ae96349 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -21,15 +21,6 @@ def get_state(injector: Injector, auth: Auth): def get_cookies(session: Session): cookies = session.query(Cookie).all() - # result = [{"id": cookie.id, - # "created_date": cookie.created_date, - # "modified_date": cookie.modified_date, - # "name": cookie.name, - # "recipe_url": cookie.recipe_url, - # "sku": cookie.sku, - # "qoh": cookie.qoh, - # "unit_cost": cookie.unit_cost} - # for cookie in cookies] result = [cookie.to_dict() for cookie in cookies] return result @@ -71,7 +62,7 @@ commands = sqlalchemy_backend.commands + commands components = sqlalchemy_backend.components + auth_components -def application_factory(**settings): +def application_factory(settings={}): """Returns an instance of Cookie API""" return App(settings=settings, commands=commands, diff --git a/cookie_api/models.py b/cookie_api/models.py index 18058e2..c04cd8c 100644 --- a/cookie_api/models.py +++ b/cookie_api/models.py @@ -34,8 +34,9 @@ class DBMixin: modified_date = Column(DateTime, server_default=utcnow(), onupdate=utcnow()) def to_dict(self): - d = self.__dict__ - d.pop('_sa_instance_state') + d = self.__dict__.copy() + if '_sa_instance_state' in d: + d.pop('_sa_instance_state') return d diff --git a/cookie_api/renders.py b/cookie_api/renders.py index 7b01f58..5b6572e 100644 --- a/cookie_api/renders.py +++ b/cookie_api/renders.py @@ -21,3 +21,5 @@ class JSONRenderer(Renderer): def render(self, data: http.ResponseData) -> bytes: return json.dumps(data, default=extended_encoder).encode('utf-8') + +# TODO add an XML render diff --git a/cookie_api/types.py b/cookie_api/types.py index 9967254..dd23fcc 100644 --- a/cookie_api/types.py +++ b/cookie_api/types.py @@ -15,3 +15,5 @@ class Datetime(datetime.datetime): except ValueError: raise TypeSystemError(cls=cls, code='type') from None return cls.native_type(*args, **kwargs) + + diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..55e7cb2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,53 @@ +from apistar.frameworks.wsgi import WSGIApp +from apistar.backends.sqlalchemy_backend import SQLAlchemyBackend, get_session + +import pytest + +from cookie_api.models import Base +from cookie_api.renders import JSONRenderer +from cookie_api.app import commands, routes, components + +settings = { + 'DATABASE': { + 'URL': 'postgresql://apistar:local@localhost/test_cookie_api', + 'METADATA': Base.metadata + }, + 'RENDERERS': [JSONRenderer()], + 'JWT': { + 'SECRET': 'thisisasecret' + } +} + +backend = SQLAlchemyBackend(settings) + + +@pytest.fixture(autouse=True) +def create_db(): + Base.metadata.create_all(backend.engine) + + yield + + Base.metadata.drop_all(backend.engine) + + +@pytest.fixture(name='rb_session') +def db_session_fixure(): + "Returns a SQLAlchemy session that autorolls back" + session = backend.Session() + try: + yield session + session.rollback() + except: + session.rollback() + raise + finally: + session.close() + + +@pytest.fixture(name='app', scope='session') +def apistar_app_fixture(): + """Returns a session scoped WSGIApp instance""" + return WSGIApp(settings=settings, + commands=commands, + components=components, + routes=routes) diff --git a/tests/test_cookies.py b/tests/test_cookies.py index e4dd9ca..ab5b42d 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -1,29 +1,73 @@ # CRUD Cookies +import pytest +from apistar import TestClient +from cookie_api.models import Cookie +from cookie_api.app import get_cookies, get_cookie -def test_get_cookies(): - pass +def test_get_cookies_empty(app): + client = TestClient(app) + response = client.get('/cookies') + assert response.status_code == 200 + assert response.json() == [] -def test_get_cookie(): - pass + +def test_get_empty_cookies(rb_session): + assert [] == get_cookies(rb_session) + + +def test_get_cookies(rb_session): + cookie = Cookie(name='sugar', + recipe_url='http://cookie.com/sugar', + sku='SC', + qoh=14, + unit_cost=1.50) + rb_session.add(cookie) + rb_session.flush() + cookies = rb_session.query(Cookie).all() + assert [cookie.to_dict() for cookie in cookies] == get_cookies(rb_session) + + +def test_get_cookie(rb_session): + cookie = Cookie(name='sugar', + recipe_url='http://cookie.com/sugar', + sku='SC', + qoh=14, + unit_cost=1.50) + + rb_session.add(cookie) + rb_session.flush() + cookie = rb_session.query(Cookie).filter_by(id=cookie.id).one() + assert cookie.to_dict() == get_cookie(rb_session, 1) + + +def test_get_cookie_that_doesnt_exist(rb_session): + response = get_cookie(rb_session, 100) + assert {"error": "404 Not Found"} == response.content + assert 404 == response.status +@pytest.mark.skip() def test_valid_create_cookie(): pass +@pytest.mark.skip() def test_invalid_create_cookie(): pass +@pytest.mark.skip() def test_valid_update_cookie(): pass +@pytest.mark.skip() def test_invalid_update_cookie(): pass +@pytest.mark.skip() def test_delete_cookie(): pass diff --git a/wsgi.py b/wsgi.py index 9de23e7..2f46235 100644 --- a/wsgi.py +++ b/wsgi.py @@ -13,5 +13,5 @@ settings = { } if __name__ == "__main__": - app = application_factory(**settings) + app = application_factory(settings) app.main()