Skip to content

Commit d251216

Browse files
author
Ted S
authored
Merge pull request #22 from laslabs/feature/0.1/clear-env-and-db
[ADD] carepoint: Add method to clear global session
2 parents 1f3316e + ad33c08 commit d251216

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

carepoint/db/carepoint.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ def __init__(
8181
params.update(db_args)
8282
if engine_args:
8383
params.update(engine_args)
84-
# @TODO: Lazy load, once other dbs needed
85-
if not self.dbs.get('cph'):
86-
self.dbs['cph'] = Db(**params)
87-
if not self.env.get('cph'):
88-
self.env['cph'] = sessionmaker(
89-
autocommit=False,
90-
autoflush=False,
91-
bind=self.dbs['cph'],
92-
expire_on_commit=True,
93-
)
9484
if smb_user is None:
9585
self.smb_creds = {
9686
'user': user,
@@ -101,6 +91,27 @@ def __init__(
10191
'user': smb_user,
10292
'passwd': smb_passwd,
10393
}
94+
self.db_params = params
95+
self._init_env(False)
96+
97+
def _init_env(self, clear=False):
98+
""" It initializes the global db and environments
99+
100+
Params:
101+
clear: (bool) True to clear the global session
102+
"""
103+
if clear:
104+
self.dbs.clear()
105+
# @TODO: Lazy load, once other dbs needed
106+
if not self.dbs.get('cph'):
107+
self.dbs['cph'] = Db(**self.db_params)
108+
if not self.env.get('cph'):
109+
self.env['cph'] = sessionmaker(
110+
autocommit=False,
111+
autoflush=False,
112+
bind=self.dbs['cph'],
113+
expire_on_commit=True,
114+
)
104115

105116
def _get_model_session(self, model_obj):
106117
""" It yields a session for the model_obj """

carepoint/tests/db/test_carepoint.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ def test_carepoint_init(self, sup_mk, db_mk):
5454
cp = Carepoint(**self.cp_args)
5555
sup_mk.assert_called_once_with(Carepoint, cp)
5656

57-
def test_carepoint_assigns_instance_dbs(self):
58-
self.assertEqual(
59-
self.db_mock(), self.carepoint.dbs['cph'],
60-
)
61-
6257
def test_carepoint_initial_iter_refresh(self):
6358
self.assertFalse(
6459
self.carepoint.iter_refresh
@@ -77,6 +72,10 @@ def test_cph_db_init(self):
7772
def test_cph_db_assign(self):
7873
self.carepoint.dbs['cph'] = self.db_mock
7974

75+
def test_carepoint_assigns_db_params(self):
76+
""" It should assign the database params as an instance var """
77+
self.assertIsInstance(self.carepoint.db_params, dict)
78+
8079
def test_non_dir(self):
8180
'''
8281
Test to make sure that an EnvironmentError is raised with an
@@ -103,6 +102,13 @@ def test_model_methods(self):
103102
model_obj = self.carepoint['TestModel']
104103
self.assertTrue(model_obj.run()) # < classmethods are exposed
105104

105+
def test_init_env_clear(self):
106+
""" It should clear the global environment when True """
107+
with mock.patch.object(self.carepoint, 'dbs') as dbs:
108+
dbs.clear.side_effect = EndTestException
109+
with self.assertRaises(EndTestException):
110+
self.carepoint._init_env(True)
111+
106112
#
107113
# Test the session handler
108114
#

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup_vals = {
1111
'name': 'carepoint',
12-
'version': '0.1.6',
12+
'version': '0.1.7',
1313
'author': 'LasLabs Inc.',
1414
'author_email': '[email protected]',
1515
'description': 'This library will allow you to interact with CarePoint '

0 commit comments

Comments
 (0)