Use assertRaises more
It saves us some boilerplate *and* makes tighter assertions about the type of error. Change-Id: I34bf8017b5740203ee0b9f253e24f1869164f7c6
This commit is contained in:
@@ -721,20 +721,20 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
|
|
||||||
def test_get(self):
|
def test_get(self):
|
||||||
broker = DatabaseBroker(':memory:')
|
broker = DatabaseBroker(':memory:')
|
||||||
got_exc = False
|
with self.assertRaises(DatabaseConnectionError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
conn.execute('SELECT 1')
|
conn.execute('SELECT 1')
|
||||||
except Exception:
|
self.assertEqual(
|
||||||
got_exc = True
|
str(raised.exception),
|
||||||
|
"DB connection error (:memory:, 0):\nDB doesn't exist")
|
||||||
|
|
||||||
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'))
|
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'))
|
||||||
got_exc = False
|
with self.assertRaises(DatabaseConnectionError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
conn.execute('SELECT 1')
|
conn.execute('SELECT 1')
|
||||||
except Exception:
|
self.assertEqual(
|
||||||
got_exc = True
|
str(raised.exception),
|
||||||
self.assertTrue(got_exc)
|
"DB connection error (%s, 0):\nDB doesn't exist" % broker.db_file)
|
||||||
|
|
||||||
def stub(*args, **kwargs):
|
def stub(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
@@ -772,14 +772,11 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
os.path.join(dbpath, '1.db'))
|
os.path.join(dbpath, '1.db'))
|
||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
conn.execute('SELECT * FROM test')
|
conn.execute('SELECT * FROM test')
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to malformed database' %
|
'Quarantined %s to %s due to malformed database' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
# Test malformed schema database
|
# Test malformed schema database
|
||||||
@@ -788,14 +785,11 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
os.path.join(dbpath, '1.db'))
|
os.path.join(dbpath, '1.db'))
|
||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
conn.execute('SELECT * FROM test')
|
conn.execute('SELECT * FROM test')
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to malformed database' %
|
'Quarantined %s to %s due to malformed database' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
# Test corrupted database
|
# Test corrupted database
|
||||||
@@ -804,14 +798,11 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
os.path.join(dbpath, '1.db'))
|
os.path.join(dbpath, '1.db'))
|
||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
conn.execute('SELECT * FROM test')
|
conn.execute('SELECT * FROM test')
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to corrupted database' %
|
'Quarantined %s to %s due to corrupted database' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
|
|
||||||
@@ -828,25 +819,21 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'container'
|
broker.db_type = 'container'
|
||||||
|
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised:
|
||||||
try:
|
|
||||||
broker.get_raw_metadata()
|
broker.get_raw_metadata()
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to missing row in container_stat table' %
|
'Quarantined %s to %s due to missing row in container_stat table' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
|
|
||||||
def test_lock(self):
|
def test_lock(self):
|
||||||
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'), timeout=.1)
|
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'), timeout=.1)
|
||||||
got_exc = False
|
with self.assertRaises(DatabaseConnectionError) as raised, \
|
||||||
try:
|
broker.lock():
|
||||||
with broker.lock():
|
|
||||||
pass
|
pass
|
||||||
except Exception:
|
self.assertEqual(
|
||||||
got_exc = True
|
str(raised.exception),
|
||||||
self.assertTrue(got_exc)
|
"DB connection error (%s, 0):\nDB doesn't exist" % broker.db_file)
|
||||||
|
|
||||||
def stub(*args, **kwargs):
|
def stub(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
@@ -860,13 +847,12 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
timeout=.1)
|
timeout=.1)
|
||||||
broker2._initialize = stub
|
broker2._initialize = stub
|
||||||
with broker.lock():
|
with broker.lock():
|
||||||
got_exc = False
|
with self.assertRaises(LockTimeout) as raised, \
|
||||||
try:
|
broker2.lock():
|
||||||
with broker2.lock():
|
|
||||||
pass
|
pass
|
||||||
except LockTimeout:
|
self.assertEqual(str(raised.exception),
|
||||||
got_exc = True
|
'0.1 seconds: %s' % broker.db_file)
|
||||||
self.assertTrue(got_exc)
|
|
||||||
try:
|
try:
|
||||||
with broker.lock():
|
with broker.lock():
|
||||||
raise Exception('test')
|
raise Exception('test')
|
||||||
@@ -1211,15 +1197,10 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'container'
|
broker.db_type = 'container'
|
||||||
|
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised:
|
||||||
try:
|
broker.update_metadata({'First': ['1', normalize_timestamp(1)]})
|
||||||
first_timestamp = normalize_timestamp(1)
|
|
||||||
first_value = '1'
|
|
||||||
broker.update_metadata({'First': [first_value, first_timestamp]})
|
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to missing row in container_stat table' %
|
'Quarantined %s to %s due to missing row in container_stat table' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
|
|
||||||
@@ -1236,14 +1217,11 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'container'
|
broker.db_type = 'container'
|
||||||
|
|
||||||
exc = None
|
with self.assertRaises(sqlite3.DatabaseError) as raised, \
|
||||||
try:
|
broker.get() as conn:
|
||||||
with broker.get() as conn:
|
|
||||||
broker._reclaim_metadata(conn, 0)
|
broker._reclaim_metadata(conn, 0)
|
||||||
except Exception as err:
|
|
||||||
exc = err
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to missing row in container_stat table' %
|
'Quarantined %s to %s due to missing row in container_stat table' %
|
||||||
(dbpath, qpath))
|
(dbpath, qpath))
|
||||||
|
|
||||||
@@ -1272,19 +1250,13 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
metadata[key] = ('B', normalize_timestamp(1))
|
metadata[key] = ('B', normalize_timestamp(1))
|
||||||
key = 'X-Account-Meta-Foo'.format(c)
|
key = 'X-Account-Meta-Foo'.format(c)
|
||||||
metadata[key] = ('', normalize_timestamp(1))
|
metadata[key] = ('', normalize_timestamp(1))
|
||||||
try:
|
self.assertIsNone(DatabaseBroker.validate_metadata(metadata))
|
||||||
DatabaseBroker.validate_metadata(metadata)
|
|
||||||
except HTTPException:
|
|
||||||
self.fail('Unexpected HTTPException')
|
|
||||||
|
|
||||||
def test_metadata_raises_exception_on_non_utf8(self):
|
def test_metadata_raises_exception_on_non_utf8(self):
|
||||||
def try_validate(metadata):
|
def try_validate(metadata):
|
||||||
try:
|
with self.assertRaises(HTTPException) as raised:
|
||||||
DatabaseBroker.validate_metadata(metadata)
|
DatabaseBroker.validate_metadata(metadata)
|
||||||
except HTTPException as e:
|
self.assertEqual(str(raised.exception), '400 Bad Request')
|
||||||
self.assertEqual(str(e), '400 Bad Request')
|
|
||||||
else:
|
|
||||||
self.fail('HTTPException not raised')
|
|
||||||
ts = normalize_timestamp(1)
|
ts = normalize_timestamp(1)
|
||||||
try_validate({'X-Account-Meta-Foo': (b'\xff', ts)})
|
try_validate({'X-Account-Meta-Foo': (b'\xff', ts)})
|
||||||
try_validate({b'X-Container-Meta-\xff': ('bar', ts)})
|
try_validate({b'X-Container-Meta-\xff': ('bar', ts)})
|
||||||
@@ -1316,10 +1288,7 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
metadata['X-Account-Meta-k'] = (
|
metadata['X-Account-Meta-k'] = (
|
||||||
'v' * (MAX_META_OVERALL_SIZE - size - 1),
|
'v' * (MAX_META_OVERALL_SIZE - size - 1),
|
||||||
normalize_timestamp(1))
|
normalize_timestamp(1))
|
||||||
try:
|
self.assertIsNone(DatabaseBroker.validate_metadata(metadata))
|
||||||
DatabaseBroker.validate_metadata(metadata)
|
|
||||||
except HTTPException:
|
|
||||||
self.fail('Unexpected HTTPException')
|
|
||||||
|
|
||||||
def test_metadata_raises_exception_over_max_overall_size(self):
|
def test_metadata_raises_exception_over_max_overall_size(self):
|
||||||
metadata = {}
|
metadata = {}
|
||||||
@@ -1364,16 +1333,13 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
try:
|
try:
|
||||||
raise ex
|
raise ex
|
||||||
except (sqlite3.DatabaseError, DatabaseConnectionError):
|
except sqlite3.DatabaseError:
|
||||||
try:
|
with self.assertRaises(sqlite3.DatabaseError) as raised:
|
||||||
broker.possibly_quarantine(*sys.exc_info())
|
broker.possibly_quarantine(*sys.exc_info())
|
||||||
except Exception as exc:
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(raised.exception),
|
||||||
'Quarantined %s to %s due to %s database' %
|
'Quarantined %s to %s due to %s database' %
|
||||||
(dbpath, qpath, hint))
|
(dbpath, qpath, hint))
|
||||||
else:
|
|
||||||
self.fail('Expected an exception to be raised')
|
|
||||||
|
|
||||||
def test_skip_commits(self):
|
def test_skip_commits(self):
|
||||||
broker = DatabaseBroker(':memory:')
|
broker = DatabaseBroker(':memory:')
|
||||||
|
|||||||
Reference in New Issue
Block a user