def test_29_with_txn(self): db = Database(self._db_name) table = db.table(self._tb_name) self.generate_data1(db, self._tb_name) self.generate_data2(db, self._tb_name) with db.begin(): with self.assertRaises(xIndexMissing): next(table.find('123')) with db.begin(): idx = table.index('by_name', '{name}', duplicates=True) idx.empty(db.transaction.txn) self.generate_data1(db, self._tb_name) with db.begin(): table.drop_index('by_name') table.index('by_name', '{name}', duplicates=True) docs = list(table.find()) doc = docs[0] table.delete(doc) doc = docs[1] doc['age'] += 1 table.save(doc) docs = list(table.find()) doc = docs[0] self.assertEqual(doc['age'], 3001) all = db.tables_all cmp = ['__binlog__', '__metadata__', '_demo1_by_name', 'demo1'] all.sort() cmp.sort() self.assertEqual(all, cmp) db.binlog(False) all = db.tables_all all.sort() cmp = ['__metadata__', '_demo1_by_name', 'demo1'] cmp.sort() self.assertEqual(all, cmp) db.drop('demo1') with self.assertRaises(Exception): with db.begin(): raise Exception('catch this') self.assertEqual(db.tables_all, ['__metadata__']) db.binlog(False) db.sync(True) db = Database(self._db_name, binlog=False) db.binlog() db.binlog(False)
def test_30_ensure(self): db = Database(self._db_name) table = db.table(self._tb_name) self.generate_data1(db, self._tb_name) with db.begin(): index = table.ensure('by_name', '{name}', True, False) index = table.ensure('by_name', '{name}', True, False) index = table.ensure('by_name', '{name}', True, True)
def test_26_drop_reuse(self): db = Database(self._db_name) table = db.table(self._tb_name) self.generate_data(db, self._tb_name) db.drop(self._tb_name) table = db.table(self._tb_name) self.generate_data(db, self._tb_name) table.index('by_age', '{age:03}', duplicates=True) doc = table.seek_one('by_age', {'age': 3000}) self.assertEqual(doc['age'], 3000) self.assertEqual(doc['name'], 'Squizzey') for doc in table.find(): _id = doc['_id'] name = doc['name'] break with db.begin(): db.restructure(self._tb_name) table = db.table(self._tb_name) for doc in table.find(): #print(doc) self.assertEqual(doc['name'], name) self.assertEqual(doc['_id'], _id) break