Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
creating an attribute in PostgresqlStorage class to save db_connection
Browse files Browse the repository at this point in the history
string for restart function.
  • Loading branch information
rich-hart committed Feb 12, 2015
1 parent 50b8f31 commit 7622143
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cnxauthoring/storage/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions cnxauthoring/tests/test_storage/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7622143

Please sign in to comment.