Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dternyak committed Sep 30, 2018
1 parent 0a85117 commit c931220
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
7 changes: 6 additions & 1 deletion api/comment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from molten.contrib.sqlalchemy import (
Session
)
import logging

from .models import Comment

Expand All @@ -21,9 +22,13 @@ class CommentSchema:

def list_comments(session: Session) -> List[CommentSchema]:
comments = session.query(Comment).all()
logging.info(comments)
return [
CommentSchema(
**ob
id=ob.id,
content=ob.content,
user_id=ob.user_id,
date_created=ob.date_created
) for ob in comments
]

Expand Down
7 changes: 6 additions & 1 deletion api/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class UserSchema:
def list_users(session: Session) -> List[UserSchema]:
user_obs = session.query(User).all()
return [
UserSchema(**ob) for ob in user_obs
UserSchema(
id=ob.id,
email_address=ob.email_address,
display_name=ob.display_name,
title=ob.title
) for ob in user_obs
]


Expand Down
1 change: 1 addition & 0 deletions custom/animal_case/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def keys_to_camel_case(content):
:param content: dict
:return: dict
"""
print(content)
return {
to_camel_case(key): value for key, value in _unpack(dict(content))
}
Expand Down
5 changes: 3 additions & 2 deletions custom/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def render(self, status: str, response_data: Any) -> Response:
except TypeError:
pass
# TODO - fix hack to prevent camel casing openAPI data
if not response_data.get('openapi'):
response_data = parse_keys(response_data, types='camel')
if type(response_data) == dict:
if not response_data.get('openapi'):
response_data = parse_keys(response_data, types='camel')
content = json.dumps(response_data, default=self.default)
return Response(status, content=content, headers={
"content-type": "application/json; charset=utf-8",
Expand Down
14 changes: 13 additions & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from molten import URLEncodingParser, MultiPartParser
from molten.contrib.sqlalchemy import (
SQLAlchemyEngineComponent, SQLAlchemyMiddleware,
SQLAlchemySessionComponent)
SQLAlchemySessionComponent, EngineData)
from molten.contrib.toml_settings import TOMLSettingsComponent
from molten.openapi import HTTPSecurityScheme, Metadata, OpenAPIHandler, OpenAPIUIHandler

from api.comment.views import list_comments, create_comment, get_comment, delete_comment
from api.user.views import list_users, create_user, get_user, delete_user
from custom import JSONParser, JSONRenderer
from db import Base

get_docs = OpenAPIUIHandler()

Expand Down Expand Up @@ -60,6 +61,16 @@
]


def init_db(engine_data: EngineData):
Base.metadata.create_all(engine_data.engine)


def setup_db(app):
# Initialize the DB by injecting EngineData into init_db and calling it.
resolver = app.injector.get_resolver()
resolver.resolve(init_db)()


def create_app(_components=None, _middleware=None, _routes=None):
app = App(
components=_components or components,
Expand All @@ -72,4 +83,5 @@ def create_app(_components=None, _middleware=None, _routes=None):
],
renderers=[JSONRenderer()]
)
setup_db(app)
return app
7 changes: 2 additions & 5 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

@task
def test(c, cov=False, verbose=False):
c.run("export ENVIRONMENT=test")
pytest_command = 'pytest tests'
pytest_command = 'export ENVIRONMENT=test && pytest tests'
if cov:
pytest_command += ' --cov=api'
if verbose:
Expand All @@ -14,8 +13,6 @@ def test(c, cov=False, verbose=False):

@task
def start(c, log_level='info'):
c.run("export ENVIRONMENT=common")
gunicorn_command = 'gunicorn --reload app:app'
gunicorn_command = 'export ENVIRONMENT=common && gunicorn --reload app:app'
gunicorn_command += ' --log-level={}'.format(log_level)
print(gunicorn_command)
c.run(gunicorn_command)
20 changes: 4 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
import pytest
import toml
from molten import testing
from molten.contrib.sqlalchemy import (
EngineData)

from db import Base
from index import create_app


def init_db(engine_data: EngineData):
Base.metadata.create_all(engine_data.engine)


def setup_db(app):
# Initialize the DB by injecting EngineData into init_db and calling it.
resolver = app.injector.get_resolver()
resolver.resolve(init_db)()
with open("settings.toml") as conffile:
toml_data = toml.loads(conffile.read())


# requires function scope so that database is removed on every tests
# TODO - find a less hacky way to clear database after each test
@pytest.fixture(scope="function")
def app():
app = create_app()
setup_db(app)
yield app
import os
test_db = "test.db.sqlite3"
os.path.isfile(test_db)
os.remove(test_db)
os.remove(toml_data["test"]["database_engine_dsn"].split('///')[1])


@pytest.fixture(scope="function")
Expand Down

0 comments on commit c931220

Please sign in to comment.