Skip to content

Commit

Permalink
Added postgres session factory, updated the version, Updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
srikalyan committed Nov 27, 2019
1 parent f0b864d commit 9ba21c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project provides the glue between [microcosm] projects and [connexion]. It
1. Connexion: An instance of connexion
2. flask: An instance of flask created by connexion
3. app: Same as flask instance.
4. postgress_session_factory: Binds the Postgres SQLAlchemy session context to Flask

The reason for providing flask and app is to make sure that connexion's version of flask overrides [microcosm-flask]'s
app.
Expand Down
15 changes: 12 additions & 3 deletions microcosm_connexion/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
port=typed(type=int, default_value=5000)
)
def configure_connexion(graph):
"""
Creates and configures connexion's app instance
:param graph: Instance of microcosm graph
:return: connexion instance
"""
connexion_app = connexion.App(graph.metadata.import_name,
port=graph.config.connexion.port,
debug=graph.metadata.debug)
Expand All @@ -23,11 +28,15 @@ def configure_connexion(graph):
return connexion_app


@defaults(
db_key="db",
)
def configure_postgres_session_factory(graph):
"""
Bind the SQLAlchemy session context to Flask.
Binds the Postgres SQLAlchemy session context to Flask.
:param graph: Instance of microcosm graph
:return:
"""
from microcosm_postgres.context import SessionContext

return register_session_factory(graph, "db", SessionContext.make)
return register_session_factory(graph, graph.config.postgres_session_factory.db_key, SessionContext.make)
15 changes: 13 additions & 2 deletions microcosm_connexion/tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
from hamcrest import assert_that, is_, equal_to
from microcosm_postgres.context import SessionContext
from mock import Mock, patch

from microcosm_connexion.factories import configure_connexion
from microcosm_connexion.factories import configure_connexion, configure_postgres_session_factory


@patch("microcosm_connexion.factories.connexion")
def test_configure_connexion(mock_connexion):
graph = Mock()
connexion_instance = configure_connexion(graph)

assert_that(connexion_instance, is_(equal_to(mock_connexion.App.return_value)))

mock_connexion.App.assert_called_once_with(graph.metadata.import_name,
port=graph.config.connexion.port,
debug=graph.metadata.debug)

assert_that(graph.flask, is_(equal_to(connexion_instance.app)))
assert_that(graph.app, is_(equal_to(connexion_instance.app)))


@patch("microcosm_connexion.factories.register_session_factory")
def test_configure_postgres_session_factory(mock_register_session_factory):
graph = Mock()

assert_that(configure_postgres_session_factory(graph), is_(equal_to(mock_register_session_factory.return_value)))

mock_register_session_factory.assert_called_once_with(graph, graph.config.postgres_session_factory.db_key,
SessionContext.make)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="microcosm_connexion",
version="0.1.0" + __QUALIFIER__,
version="0.2.0" + __QUALIFIER__,
description="A python library that exposes microcosm factories for connexions",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down Expand Up @@ -41,6 +41,7 @@
"console_scripts": [],
"microcosm.factories": [
"connexion = microcosm_connexion.factories:configure_connexion",
"postgres_session_factory = microcosm_connexion.factories:configure_postgres_session_factory",
],
},
)
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ envlist=py36, py37, py38, flake8, sdist, wheel
skip_missing_interpreters=true

[testenv]
commands = python setup.py test
commands =
pip install .[postgres]
python setup.py test

[testenv:flake8]
commands=flake8 microcosm_connexion
Expand Down

0 comments on commit 9ba21c1

Please sign in to comment.