From ad33c0832746a22f4f8ea7bc575d447131a3ca82 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Mon, 24 Oct 2016 18:00:47 -0700 Subject: [PATCH] [ADD] carepoint: Add method to clear global session * Break session creation into `_init_env` * Add param to clear the session --- carepoint/db/carepoint.py | 31 +++++++++++++++++++--------- carepoint/tests/db/test_carepoint.py | 16 +++++++++----- setup.py | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/carepoint/db/carepoint.py b/carepoint/db/carepoint.py index 16af199..a6643ab 100644 --- a/carepoint/db/carepoint.py +++ b/carepoint/db/carepoint.py @@ -81,16 +81,6 @@ def __init__( params.update(db_args) if engine_args: params.update(engine_args) - # @TODO: Lazy load, once other dbs needed - if not self.dbs.get('cph'): - self.dbs['cph'] = Db(**params) - if not self.env.get('cph'): - self.env['cph'] = sessionmaker( - autocommit=False, - autoflush=False, - bind=self.dbs['cph'], - expire_on_commit=True, - ) if smb_user is None: self.smb_creds = { 'user': user, @@ -101,6 +91,27 @@ def __init__( 'user': smb_user, 'passwd': smb_passwd, } + self.db_params = params + self._init_env(False) + + def _init_env(self, clear=False): + """ It initializes the global db and environments + + Params: + clear: (bool) True to clear the global session + """ + if clear: + self.dbs.clear() + # @TODO: Lazy load, once other dbs needed + if not self.dbs.get('cph'): + self.dbs['cph'] = Db(**self.db_params) + if not self.env.get('cph'): + self.env['cph'] = sessionmaker( + autocommit=False, + autoflush=False, + bind=self.dbs['cph'], + expire_on_commit=True, + ) def _get_model_session(self, model_obj): """ It yields a session for the model_obj """ diff --git a/carepoint/tests/db/test_carepoint.py b/carepoint/tests/db/test_carepoint.py index e8132bd..acb60d6 100644 --- a/carepoint/tests/db/test_carepoint.py +++ b/carepoint/tests/db/test_carepoint.py @@ -54,11 +54,6 @@ def test_carepoint_init(self, sup_mk, db_mk): cp = Carepoint(**self.cp_args) sup_mk.assert_called_once_with(Carepoint, cp) - def test_carepoint_assigns_instance_dbs(self): - self.assertEqual( - self.db_mock(), self.carepoint.dbs['cph'], - ) - def test_carepoint_initial_iter_refresh(self): self.assertFalse( self.carepoint.iter_refresh @@ -77,6 +72,10 @@ def test_cph_db_init(self): def test_cph_db_assign(self): self.carepoint.dbs['cph'] = self.db_mock + def test_carepoint_assigns_db_params(self): + """ It should assign the database params as an instance var """ + self.assertIsInstance(self.carepoint.db_params, dict) + def test_non_dir(self): ''' Test to make sure that an EnvironmentError is raised with an @@ -103,6 +102,13 @@ def test_model_methods(self): model_obj = self.carepoint['TestModel'] self.assertTrue(model_obj.run()) # < classmethods are exposed + def test_init_env_clear(self): + """ It should clear the global environment when True """ + with mock.patch.object(self.carepoint, 'dbs') as dbs: + dbs.clear.side_effect = EndTestException + with self.assertRaises(EndTestException): + self.carepoint._init_env(True) + # # Test the session handler # diff --git a/setup.py b/setup.py index c375eeb..9d76338 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup_vals = { 'name': 'carepoint', - 'version': '0.1.6', + 'version': '0.1.7', 'author': 'LasLabs Inc.', 'author_email': 'support@laslabs.com', 'description': 'This library will allow you to interact with CarePoint '