Use unittest for assertions

This commit is contained in:
Neal Norwitz 2007-10-14 18:40:37 +00:00
parent 3884690d69
commit d29f1d2241
1 changed files with 34 additions and 33 deletions

View File

@ -78,7 +78,8 @@ class TableDBTestCase(unittest.TestCase):
tabname, [colname], conditions={colname: None}) tabname, [colname], conditions={colname: None})
colval = pickle.loads(values[0][colname]) colval = pickle.loads(values[0][colname])
assert(colval > 3.141 and colval < 3.142) self.assert_(colval > 3.141)
self.assert_(colval < 3.142)
def test02(self): def test02(self):
@ -103,11 +104,11 @@ class TableDBTestCase(unittest.TestCase):
values = self.tdb.Select(tabname, [col2], values = self.tdb.Select(tabname, [col2],
conditions={col0: lambda x: pickle.loads(x) >= 8}) conditions={col0: lambda x: pickle.loads(x) >= 8})
assert len(values) == 2 self.assertEqual(len(values), 2)
if values[0]['Species'] == 'Penguin' : if values[0]['Species'] == 'Penguin' :
assert values[1]['Species'] == 'SR-71A Blackbird' self.assertEqual(values[1]['Species'], 'SR-71A Blackbird')
elif values[0]['Species'] == 'SR-71A Blackbird' : elif values[0]['Species'] == 'SR-71A Blackbird' :
assert values[1]['Species'] == 'Penguin' self.assertEqual(values[1]['Species'], 'Penguin')
else : else :
if verbose: if verbose:
print "values= %r" % (values,) print "values= %r" % (values,)
@ -137,13 +138,13 @@ class TableDBTestCase(unittest.TestCase):
{'a': "", {'a': "",
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1), 'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
'f': "Zero"}) 'f': "Zero"})
assert 0 self.fail('Expected an exception')
except dbtables.TableDBError: except dbtables.TableDBError:
pass pass
try: try:
self.tdb.Select(tabname, [], conditions={'foo': '123'}) self.tdb.Select(tabname, [], conditions={'foo': '123'})
assert 0 self.fail('Expected an exception')
except dbtables.TableDBError: except dbtables.TableDBError:
pass pass
@ -172,20 +173,20 @@ class TableDBTestCase(unittest.TestCase):
values = self.tdb.Select(tabname, ['b', 'a', 'd'], values = self.tdb.Select(tabname, ['b', 'a', 'd'],
conditions={'e': re.compile('wuzzy').search, conditions={'e': re.compile('wuzzy').search,
'a': re.compile('^[0-9]+$').match}) 'a': re.compile('^[0-9]+$').match})
assert len(values) == 2 self.assertEqual(len(values), 2)
# now lets delete one of them and try again # now lets delete one of them and try again
self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')}) self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')})
values = self.tdb.Select( values = self.tdb.Select(
tabname, ['a', 'd', 'b'], tabname, ['a', 'd', 'b'],
conditions={'e': dbtables.PrefixCond('Fuzzy')}) conditions={'e': dbtables.PrefixCond('Fuzzy')})
assert len(values) == 1 self.assertEqual(len(values), 1)
assert values[0]['d'] == None self.assertEqual(values[0]['d'], None)
values = self.tdb.Select(tabname, ['b'], values = self.tdb.Select(tabname, ['b'],
conditions={'c': lambda c: c == 'meep'}) conditions={'c': lambda c: c == 'meep'})
assert len(values) == 1 self.assertEqual(len(values), 1)
assert values[0]['b'] == "bad" self.assertEqual(values[0]['b'], "bad")
def test04_MultiCondSelect(self): def test04_MultiCondSelect(self):
@ -201,7 +202,7 @@ class TableDBTestCase(unittest.TestCase):
{'a': "", {'a': "",
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1), 'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
'f': "Zero"}) 'f': "Zero"})
assert 0 self.fail('Expected an exception')
except dbtables.TableDBError: except dbtables.TableDBError:
pass pass
@ -225,7 +226,7 @@ class TableDBTestCase(unittest.TestCase):
'a': dbtables.ExactCond('A'), 'a': dbtables.ExactCond('A'),
'd': dbtables.PrefixCond('-') 'd': dbtables.PrefixCond('-')
} ) } )
assert len(values) == 0, values self.assertEqual(len(values), 0, values)
def test_CreateOrExtend(self): def test_CreateOrExtend(self):
@ -238,7 +239,7 @@ class TableDBTestCase(unittest.TestCase):
{'taste': 'crap', {'taste': 'crap',
'filling': 'no', 'filling': 'no',
'is it Guinness?': 'no'}) 'is it Guinness?': 'no'})
assert 0, "Insert should've failed due to bad column name" self.fail("Insert should've failed due to bad column name")
except: except:
pass pass
self.tdb.CreateOrExtendTable(tabname, self.tdb.CreateOrExtendTable(tabname,
@ -272,16 +273,16 @@ class TableDBTestCase(unittest.TestCase):
values = self.tdb.Select( values = self.tdb.Select(
tabname, ['p', 'e'], tabname, ['p', 'e'],
conditions={'e': dbtables.PrefixCond('the l')}) conditions={'e': dbtables.PrefixCond('the l')})
assert len(values) == 2, values self.assertEqual(len(values), 2, values)
assert values[0]['e'] == values[1]['e'], values self.assertEqual(values[0]['e'], values[1]['e'], values)
assert values[0]['p'] != values[1]['p'], values self.assertNotEqual(values[0]['p'], values[1]['p'], values)
values = self.tdb.Select( values = self.tdb.Select(
tabname, ['d', 'a'], tabname, ['d', 'a'],
conditions={'a': dbtables.LikeCond('%aardvark%')}) conditions={'a': dbtables.LikeCond('%aardvark%')})
assert len(values) == 1, values self.assertEqual(len(values), 1, values)
assert values[0]['d'] == "is for dog", values self.assertEqual(values[0]['d'], "is for dog", values)
assert values[0]['a'] == "is for aardvark", values self.assertEqual(values[0]['a'], "is for aardvark", values)
values = self.tdb.Select(tabname, None, values = self.tdb.Select(tabname, None,
{'b': dbtables.Cond(), {'b': dbtables.Cond(),
@ -290,9 +291,9 @@ class TableDBTestCase(unittest.TestCase):
'd':dbtables.ExactCond('is for dog'), 'd':dbtables.ExactCond('is for dog'),
'c':dbtables.PrefixCond('is for'), 'c':dbtables.PrefixCond('is for'),
'p':lambda s: not s}) 'p':lambda s: not s})
assert len(values) == 1, values self.assertEqual(len(values), 1, values)
assert values[0]['d'] == "is for dog", values self.assertEqual(values[0]['d'], "is for dog", values)
assert values[0]['a'] == "is for aardvark", values self.assertEqual(values[0]['a'], "is for aardvark", values)
def test_Delete(self): def test_Delete(self):
tabname = "test_Delete" tabname = "test_Delete"
@ -308,7 +309,7 @@ class TableDBTestCase(unittest.TestCase):
self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')}) self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')})
values = self.tdb.Select(tabname, ['y'], values = self.tdb.Select(tabname, ['y'],
conditions={'x': dbtables.PrefixCond('X')}) conditions={'x': dbtables.PrefixCond('X')})
assert len(values) == 0 self.assertEqual(len(values), 0)
def test_Modify(self): def test_Modify(self):
tabname = "test_Modify" tabname = "test_Modify"
@ -354,24 +355,24 @@ class TableDBTestCase(unittest.TestCase):
values = self.tdb.Select( values = self.tdb.Select(
tabname, None, tabname, None,
conditions={'Type': dbtables.ExactCond('Unknown')}) conditions={'Type': dbtables.ExactCond('Unknown')})
assert len(values) == 1, values self.assertEqual(len(values), 1, values)
assert values[0]['Name'] == None, values self.assertEqual(values[0]['Name'], None, values)
assert values[0]['Access'] == None, values self.assertEqual(values[0]['Access'], None, values)
# Modify value by select conditions # Modify value by select conditions
values = self.tdb.Select( values = self.tdb.Select(
tabname, None, tabname, None,
conditions={'Name': dbtables.ExactCond('Nifty.MP3')}) conditions={'Name': dbtables.ExactCond('Nifty.MP3')})
assert len(values) == 1, values self.assertEqual(len(values), 1, values)
assert values[0]['Type'] == "MP3", values self.assertEqual(values[0]['Type'], "MP3", values)
assert values[0]['Access'] == "2", values self.assertEqual(values[0]['Access'], "2", values)
# Make sure change applied only to select conditions # Make sure change applied only to select conditions
values = self.tdb.Select( values = self.tdb.Select(
tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')}) tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')})
assert len(values) == 1, values self.assertEqual(len(values), 1, values)
assert values[0]['Type'] == "Word", values self.assertEqual(values[0]['Type'], "Word", values)
assert values[0]['Access'] == "9", values self.assertEqual(values[0]['Access'], "9", values)
def test_suite(): def test_suite():