Commits

Anonymous committed 29f121f

Handle database connection failures.

Comments (0)

Files changed (3)

 from util import touch_file
 
 
+class DatabaseError(Exception):
+    pass
+
+
 class DatabaseLog(object):
     def __init__(self, db, cursor):
         self.db = db
         self._read_timestamp()
 
     def _init_database(self):
-        self.db = psycopg.connect(self.postgres_config)
-        self.cursor = self.db.cursor()
+        try:
+            self.db = psycopg.connect(self.postgres_config)
+            self.cursor = self.db.cursor()
+        except psycopg.OperationalError, ex:
+            raise DatabaseError("Couldn't connect to database: %s" % ex)
 
     def _init_log(self):
         self.log = DatabaseLog(self.db, self.cursor)

tests/test_store.py

 
 import store
 
-from store import Store, CheesecakeScore, CheesecakeScoreFromValues, CheesecakeRun
+from store import Store, CheesecakeScore, CheesecakeScoreFromValues,\
+  CheesecakeRun, DatabaseError
 from util import create_empty_files_in_directory
 from util import set
 
         sub_scores_installability = {
             'py_pi_download': (50, 'downloaded package Durus-3.4.1.tar.gz following 1 link from http://www.mems-exchange.org/software/durus/Durus-3.4.1.tar.gz'),
             'unpack': (25, 'package unpacked successfully'),
-            'unpack_dir': (15, 'unpack directory is Durus-3.4.1 as expected'), 
-            'setup.py': (25, 'setup.py found'), 
+            'unpack_dir': (15, 'unpack directory is Durus-3.4.1 as expected'),
+            'setup.py': (25, 'setup.py found'),
             'install': (50, 'package installed in /tmp/cheesecakebJ2JTZ/tmp_install_Durus-3.4.1'),
             'generated_files': (0, '0 .pyc and 0 .pyo files found'),
         }
 
         assert 0 == self.store_mock.number_of_failures('cheesecake', '0.6')
 
+class TestStoreClass(unittest.TestCase):
+    "Class Store"
+
+    def test_will_throw_database_error_on_connection_error_during_init(self):
+        old_psycopg_connect = store.psycopg.connect
+        def raise_operational_exception(*a):
+            raise store.psycopg.OperationalError
+        store.psycopg.connect = raise_operational_exception
+
+        self.assertRaises(DatabaseError, lambda: Store())
+
+        store.psycopg.connect = old_psycopg_connect
+
 ################################################################################
 ## Functional tests.
 ################################################################################
 #!/usr/bin/env python
 """web.py: makes web apps (http://webpy.org)"""
+from __future__ import generators
+
 __version__ = "0.1381"
-__revision__ = "$Rev: 92 $"
+__revision__ = "$Rev: 94 $"
 __license__ = "public domain"
 __author__ = "Aaron Swartz <me@aaronsw.com>"
 __contributors__ = "see http://webpy.org/changes"
 
-from __future__ import generators
-
 # long term todo:
 #   - new form system
 #   - new templating system