From 7622143add200da56408ca439600c59a9fdba852 Mon Sep 17 00:00:00 2001 From: rich-hart Date: Thu, 12 Feb 2015 10:33:00 -0600 Subject: [PATCH] creating an attribute in PostgresqlStorage class to save db_connection string for restart function. --- cnxauthoring/storage/postgresql.py | 3 ++- .../tests/test_storage/test_postgresql.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cnxauthoring/storage/postgresql.py b/cnxauthoring/storage/postgresql.py index ae631f4..3a413f3 100644 --- a/cnxauthoring/storage/postgresql.py +++ b/cnxauthoring/storage/postgresql.py @@ -61,6 +61,7 @@ class PostgresqlStorage(BaseStorage): def __init__(self, db_connection_string=None): #initialize db self.conn = psycopg2.connect(db_connection_string) + self.db_connection_string = db_connection_string def get(self, type_=Document, **kwargs): """Retrieve ``Document`` objects from storage.""" @@ -369,4 +370,4 @@ def search(self, limits, type_=Document, submitter_id=None): def restart(self): """Restart the interface""" - self.conn = psycopg2.connect(self.conn.dsn) + self.conn = psycopg2.connect(self.db_connection_string) diff --git a/cnxauthoring/tests/test_storage/test_postgresql.py b/cnxauthoring/tests/test_storage/test_postgresql.py index 3b145d2..6fe76f6 100644 --- a/cnxauthoring/tests/test_storage/test_postgresql.py +++ b/cnxauthoring/tests/test_storage/test_postgresql.py @@ -81,3 +81,22 @@ def test_add_get_and_remove_binder(self): self.assertEqual({k: tuple(sorted(v)) for k, v in result.acls.items()}, {'user2': ('view',)}) + def test_restart(self): + from ...storage.database import CONNECTION_SETTINGS_KEY + import psycopg2 + settings = integration_test_settings() + test_db = settings[CONNECTION_SETTINGS_KEY] + self.addCleanup(setattr, self.storage, 'conn', psycopg2.connect(test_db)) + + # 0 if the connection is open, nonzero if it is closed or broken. + OPEN = 0 + + self.assertEqual(self.storage.conn.closed, OPEN) + + self.storage.conn.close() + + self.assertNotEqual(self.storage.conn.closed, OPEN) + + self.storage.restart() + + self.assertEqual(self.storage.conn.closed, OPEN)