diff --git a/README.md b/README.md index 252a0f3..8c874fc 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/microcosm_connexion/factories.py b/microcosm_connexion/factories.py index 8d34b34..a213634 100644 --- a/microcosm_connexion/factories.py +++ b/microcosm_connexion/factories.py @@ -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) @@ -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) diff --git a/microcosm_connexion/tests/test_factories.py b/microcosm_connexion/tests/test_factories.py index ae87bef..5bc8a32 100644 --- a/microcosm_connexion/tests/test_factories.py +++ b/microcosm_connexion/tests/test_factories.py @@ -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) diff --git a/setup.py b/setup.py index fe85a1a..59150cc 100644 --- a/setup.py +++ b/setup.py @@ -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", @@ -41,6 +41,7 @@ "console_scripts": [], "microcosm.factories": [ "connexion = microcosm_connexion.factories:configure_connexion", + "postgres_session_factory = microcosm_connexion.factories:configure_postgres_session_factory", ], }, ) diff --git a/tox.ini b/tox.ini index a4b60db..cc90d53 100644 --- a/tox.ini +++ b/tox.ini @@ -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