added pytest conf and cookie view tests

email
androiddrew 7 years ago
parent 610b6e1056
commit e4a6ee8f83

@ -21,15 +21,6 @@ def get_state(injector: Injector, auth: Auth):
def get_cookies(session: Session): def get_cookies(session: Session):
cookies = session.query(Cookie).all() 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] result = [cookie.to_dict() for cookie in cookies]
return result return result
@ -71,7 +62,7 @@ commands = sqlalchemy_backend.commands + commands
components = sqlalchemy_backend.components + auth_components components = sqlalchemy_backend.components + auth_components
def application_factory(**settings): def application_factory(settings={}):
"""Returns an instance of Cookie API""" """Returns an instance of Cookie API"""
return App(settings=settings, return App(settings=settings,
commands=commands, commands=commands,

@ -34,8 +34,9 @@ class DBMixin:
modified_date = Column(DateTime, server_default=utcnow(), onupdate=utcnow()) modified_date = Column(DateTime, server_default=utcnow(), onupdate=utcnow())
def to_dict(self): def to_dict(self):
d = self.__dict__ d = self.__dict__.copy()
d.pop('_sa_instance_state') if '_sa_instance_state' in d:
d.pop('_sa_instance_state')
return d return d

@ -21,3 +21,5 @@ class JSONRenderer(Renderer):
def render(self, data: http.ResponseData) -> bytes: def render(self, data: http.ResponseData) -> bytes:
return json.dumps(data, default=extended_encoder).encode('utf-8') return json.dumps(data, default=extended_encoder).encode('utf-8')
# TODO add an XML render

@ -15,3 +15,5 @@ class Datetime(datetime.datetime):
except ValueError: except ValueError:
raise TypeSystemError(cls=cls, code='type') from None raise TypeSystemError(cls=cls, code='type') from None
return cls.native_type(*args, **kwargs) return cls.native_type(*args, **kwargs)

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

@ -1,29 +1,73 @@
# CRUD Cookies # 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(): def test_valid_create_cookie():
pass pass
@pytest.mark.skip()
def test_invalid_create_cookie(): def test_invalid_create_cookie():
pass pass
@pytest.mark.skip()
def test_valid_update_cookie(): def test_valid_update_cookie():
pass pass
@pytest.mark.skip()
def test_invalid_update_cookie(): def test_invalid_update_cookie():
pass pass
@pytest.mark.skip()
def test_delete_cookie(): def test_delete_cookie():
pass pass

@ -13,5 +13,5 @@ settings = {
} }
if __name__ == "__main__": if __name__ == "__main__":
app = application_factory(**settings) app = application_factory(settings)
app.main() app.main()

Loading…
Cancel
Save