Skip to content

Commit

Permalink
Merge pull request #22 from laslabs/feature/0.1/clear-env-and-db
Browse files Browse the repository at this point in the history
[ADD] carepoint: Add method to clear global session
  • Loading branch information
Ted S authored Oct 25, 2016
2 parents 1f3316e + ad33c08 commit d251216
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
31 changes: 21 additions & 10 deletions carepoint/db/carepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 """
Expand Down
16 changes: 11 additions & 5 deletions carepoint/tests/db/test_carepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down

0 comments on commit d251216

Please sign in to comment.