def test_Modify(self): tabname = 'test_Modify' self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access']) self.tdb.Insert(tabname, { 'Name': 'Index to MP3 files.doc', 'Type': 'Word', 'Access': '8' }) self.tdb.Insert(tabname, {'Name': 'Nifty.MP3', 'Access': '1'}) self.tdb.Insert(tabname, {'Type': 'Unknown', 'Access': '0'}) def set_type(type): if type is None: return 'MP3' else: return type def increment_access(count): return str(int(count) + 1) def remove_value(value): return None self.tdb.Modify(tabname, conditions={'Access': dbtables.ExactCond('0')}, mappings={'Access': remove_value}) self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%MP3%')}, mappings={'Type': set_type}) self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%')}, mappings={'Access': increment_access}) try: self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%')}, mappings={'Access': 'What is your quest?'}) except TypeError: pass else: raise RuntimeError, 'why was TypeError not raised for bad callable?' values = self.tdb.Select( tabname, None, conditions={'Type': dbtables.ExactCond('Unknown')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Name'], None, values) self.assertEqual(values[0]['Access'], None, values) values = self.tdb.Select( tabname, None, conditions={'Name': dbtables.ExactCond('Nifty.MP3')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Type'], 'MP3', values) self.assertEqual(values[0]['Access'], '2', values) values = self.tdb.Select( tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Type'], 'Word', values) self.assertEqual(values[0]['Access'], '9', values) return
def test04_MultiCondSelect(self): tabname = "test04_MultiCondSelect" try: self.tdb.Drop(tabname) except dbtables.TableDBError: pass self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e']) try: self.tdb.Insert(tabname, { 'a': "", 'e': pickle.dumps([{ 4: 5, 6: 7 }, 'foo'], 1), 'f': "Zero" }) self.fail('Expected an exception') except dbtables.TableDBError: pass self.tdb.Insert(tabname, { 'a': "A", 'b': "B", 'c': "C", 'd': "D", 'e': "E" }) self.tdb.Insert(tabname, { 'a': "-A", 'b': "-B", 'c': "-C", 'd': "-D", 'e': "-E" }) self.tdb.Insert(tabname, { 'a': "A-", 'b': "B-", 'c': "C-", 'd': "D-", 'e': "E-" }) if verbose: self.tdb._db_print() # This select should return 0 rows. it is designed to test # the bug identified and fixed in sourceforge bug # 590449 # (Big Thanks to "Rob Tillotson (n9mtb)" for tracking this down # and supplying a fix!! This one caused many headaches to say # the least...) values = self.tdb.Select(tabname, ['b', 'a', 'd'], conditions={ 'e': dbtables.ExactCond('E'), 'a': dbtables.ExactCond('A'), 'd': dbtables.PrefixCond('-') }) self.assertEqual(len(values), 0, values)
def test04_MultiCondSelect(self): tabname = 'test04_MultiCondSelect' try: self.tdb.Drop(tabname) except dbtables.TableDBError: pass self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e']) try: self.tdb.Insert(tabname, { 'a': '', 'e': pickle.dumps([{ 4: 5, 6: 7 }, 'foo'], 1), 'f': 'Zero' }) self.fail('Expected an exception') except dbtables.TableDBError: pass self.tdb.Insert(tabname, { 'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E' }) self.tdb.Insert(tabname, { 'a': '-A', 'b': '-B', 'c': '-C', 'd': '-D', 'e': '-E' }) self.tdb.Insert(tabname, { 'a': 'A-', 'b': 'B-', 'c': 'C-', 'd': 'D-', 'e': 'E-' }) if verbose: self.tdb._db_print() values = self.tdb.Select(tabname, ['b', 'a', 'd'], conditions={ 'e': dbtables.ExactCond('E'), 'a': dbtables.ExactCond('A'), 'd': dbtables.PrefixCond('-') }) self.assertEqual(len(values), 0, values)
def test_CondObjs(self): tabname = "test_CondObjs" self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e', 'p']) self.tdb.Insert(tabname, { 'a': "the letter A", 'b': "the letter B", 'c': "is for cookie" }) self.tdb.Insert( tabname, { 'a': "is for aardvark", 'e': "the letter E", 'c': "is for cookie", 'd': "is for dog" }) self.tdb.Insert( tabname, { 'a': "the letter A", 'e': "the letter E", 'c': "is for cookie", 'p': "is for Python" }) values = self.tdb.Select( tabname, ['p', 'e'], conditions={'e': dbtables.PrefixCond('the l')}) self.assertEqual(len(values), 2, values) self.assertEqual(values[0]['e'], values[1]['e'], values) self.assertNotEqual(values[0]['p'], values[1]['p'], values) values = self.tdb.Select( tabname, ['d', 'a'], conditions={'a': dbtables.LikeCond('%aardvark%')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['d'], "is for dog", values) self.assertEqual(values[0]['a'], "is for aardvark", values) values = self.tdb.Select( tabname, None, { 'b': dbtables.Cond(), 'e': dbtables.LikeCond('%letter%'), 'a': dbtables.PrefixCond('is'), 'd': dbtables.ExactCond('is for dog'), 'c': dbtables.PrefixCond('is for'), 'p': lambda s: not s }) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['d'], "is for dog", values) self.assertEqual(values[0]['a'], "is for aardvark", values)
def test03(self): tabname = 'test03' try: self.tdb.Drop(tabname) except dbtables.TableDBError: pass if verbose: print '...before CreateTable...' self.tdb._db_print() self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e']) if verbose: print '...after CreateTable...' self.tdb._db_print() self.tdb.Drop(tabname) if verbose: print '...after Drop...' self.tdb._db_print() self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e']) try: self.tdb.Insert(tabname, { 'a': '', 'e': pickle.dumps([{ 4: 5, 6: 7 }, 'foo'], 1), 'f': 'Zero' }) self.fail('Expected an exception') except dbtables.TableDBError: pass try: self.tdb.Select(tabname, [], conditions={'foo': '123'}) self.fail('Expected an exception') except dbtables.TableDBError: pass self.tdb.Insert(tabname, { 'a': '42', 'b': 'bad', 'c': 'meep', 'e': 'Fuzzy wuzzy was a bear' }) self.tdb.Insert( tabname, { 'a': '581750', 'b': 'good', 'd': 'bla', 'c': 'black', 'e': 'fuzzy was here' }) self.tdb.Insert( tabname, { 'a': '800000', 'b': 'good', 'd': 'bla', 'c': 'black', 'e': 'Fuzzy wuzzy is a bear' }) if verbose: self.tdb._db_print() values = self.tdb.Select(tabname, ['b', 'a', 'd'], conditions={ 'e': re.compile('wuzzy').search, 'a': re.compile('^[0-9]+$').match }) self.assertEqual(len(values), 2) self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')}) values = self.tdb.Select( tabname, ['a', 'd', 'b'], conditions={'e': dbtables.PrefixCond('Fuzzy')}) self.assertEqual(len(values), 1) self.assertEqual(values[0]['d'], None) values = self.tdb.Select(tabname, ['b'], conditions={'c': lambda c: c == 'meep'}) self.assertEqual(len(values), 1) self.assertEqual(values[0]['b'], 'bad') return
def test_Modify(self): tabname = "test_Modify" self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access']) self.tdb.Insert(tabname, { 'Name': 'Index to MP3 files.doc', 'Type': 'Word', 'Access': '8' }) self.tdb.Insert(tabname, {'Name': 'Nifty.MP3', 'Access': '1'}) self.tdb.Insert(tabname, {'Type': 'Unknown', 'Access': '0'}) def set_type(type): if type is None: return 'MP3' return type def increment_access(count): return str(int(count) + 1) def remove_value(value): return None self.tdb.Modify(tabname, conditions={'Access': dbtables.ExactCond('0')}, mappings={'Access': remove_value}) self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%MP3%')}, mappings={'Type': set_type}) self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%')}, mappings={'Access': increment_access}) try: self.tdb.Modify(tabname, conditions={'Name': dbtables.LikeCond('%')}, mappings={'Access': 'What is your quest?'}) except TypeError: # success, the string value in mappings isn't callable pass else: raise RuntimeError, "why was TypeError not raised for bad callable?" # Delete key in select conditions values = self.tdb.Select( tabname, None, conditions={'Type': dbtables.ExactCond('Unknown')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Name'], None, values) self.assertEqual(values[0]['Access'], None, values) # Modify value by select conditions values = self.tdb.Select( tabname, None, conditions={'Name': dbtables.ExactCond('Nifty.MP3')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Type'], "MP3", values) self.assertEqual(values[0]['Access'], "2", values) # Make sure change applied only to select conditions values = self.tdb.Select( tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')}) self.assertEqual(len(values), 1, values) self.assertEqual(values[0]['Type'], "Word", values) self.assertEqual(values[0]['Access'], "9", values)