Ejemplo n.º 1
0
 def test_09_create_drop_index(self):
     db = Database(self._db_name)
     table = db.table(self._tb_name)
     table.index('by_name', '{name}')
     table.index('by_age', '{age:03}', duplicates=True)
     self.assertEqual(table.indexes(), ['by_age', 'by_name'])
     db.drop(self._tb_name)
     self.assertEqual(db.tables, [])
Ejemplo n.º 2
0
    def test_29_with_txn(self):

        return
        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.set_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.set_binlog(False)
        db.sync(True)

        db = Database(self._db_name, binlog=False)
        db.set_binlog()
        db.set_binlog(False)
Ejemplo n.º 3
0
    def test_27_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

        db.restructure(self._tb_name)

        table = db.table(self._tb_name)
        for doc in table.find():
            self.assertEqual(doc['name'], name)
            self.assertEqual(doc['_id'], _id)
            break
Ejemplo n.º 4
0
    def test_08_delete_binlog(self):
        db = Database(self._db_name)
        db.set_binlog(True)
        self.generate_data(db, self._tb_name)
        self.assertEqual(db.tables_all,
                         ['__binidx__', '__binlog__', '__metadata__', 'demo1'])
        db.close()

        db = Database(self._db_name)
        db.drop(self._tb_name)
        db.drop('__binlog__')
        self.assertEqual(db.tables_all, ['__binidx__', '__metadata__'])
        with self.assertRaises(xTableMissing):
            db.drop('fred')
Ejemplo n.º 5
0
db = Database("databases/raw")  # Open (/create) a database
table = db.table('people')   # Open (/create) a table

print('>> Index table by name and age')
table.index('by_name', '{name}')
table.index('by_age', '{age:03}', duplicates=True)

print('>> Adding data')
for item in data:
    table.append(item)
print("Count=", table.records)

print('>> Scanning table sequentially')
for record in table.find():
    print('{name} is {age} years old'.format(**record))

print('>> Scanning tables in name order [string index]')
for record in table.find('by_name'):
    print('{name} sorted alphabetically'.format(**record))

print('>> Scanning table in age order [numerical index]')
for record in table.find('by_age'):
    print('{age} - {name} in ascending order of age'.format(**record))

print('>> Scanning on name index with filter')
for record in table.find('by_name', expression=lambda doc: doc['age'] > 40):
    print('{name} is {age} years old (filtered age>40)'.format(**record))

db.drop('people')
db.close()
Ejemplo n.º 6
0
 def test_26_drop_reuse(self):
     db = Database(self._db_name)
     self.generate_data1(db, self._tb_name)
     table = db.table(self._tb_name)
     d = db.drop(self._tb_name)
     self.generate_data1(db, self._tb_name)
Ejemplo n.º 7
0
    def test_17_check_drop_exception(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        with self.assertRaises(xTableMissing):
            db.drop('no table')
Ejemplo n.º 8
0
    def test_10_put_data(self):

        people = {}
        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        for item in self._data:
            table.append(item)
            people[item['name']] = item
        for item in table.find():
            key = item.get('name', None)
            self.assertIsNotNone(key)
            if key:
                person = people.get(key, None)
                self.assertIsNotNone(person)
                if person:
                    self.assertEqual(person['age'], item['age'])
                    self.assertEqual(person['_id'], item['_id'])

        last = ''
        for item in table.find('by_name'):
            key = item.get('name', None)
            self.assertIsNotNone(key)
            if key:
                person = people.get(key, None)
                self.assertIsNotNone(person)
                if person:
                    self.assertEqual(person['age'], item['age'])
                    self.assertEqual(person['_id'], item['_id'])
                    self.assertGreaterEqual(person['name'], last)
                    last = person['name']

        last = 0
        for item in table.find('by_age'):
            key = item.get('name', None)
            self.assertIsNotNone(key)
            if key:
                person = people.get(key, None)
                self.assertIsNotNone(person)
                if person:
                    self.assertEqual(person['age'], item['age'])
                    self.assertEqual(person['_id'], item['_id'])
                    self.assertGreaterEqual(person['age'], last)
                    last = person['age']

        self.assertEqual(table.records, len(self._data))
        self.assertEqual(table.index('by_name').count(), len(self._data))
        self.assertEqual(table.index('by_age').count(), len(self._data))

        for record in table.find('by_age', limit=3):
            table.delete(record['_id'])

        self.assertEqual(table.records, len(self._data) - 3)
        self.assertEqual(table.index('by_name').count(), len(self._data) - 3)
        self.assertEqual(table.index('by_age').count(), len(self._data) - 3)

        last = 0
        for item in table.find('by_age'):
            key = item.get('name', None)
            self.assertIsNotNone(key)
            if key:
                person = people.get(key, None)
                self.assertIsNotNone(person)
                if person:
                    self.assertEqual(person['age'], item['age'])
                    self.assertEqual(person['_id'], item['_id'])
                    self.assertGreaterEqual(person['age'], last)
                    last = person['age']

        db.drop(self._tb_name)
        self.assertEqual(db.tables, [])
Ejemplo n.º 9
0
 def test_05_drop(self):
     db = Database(self._db_name)
     db.table(self._tb_name)
     db.drop(self._tb_name)
     self.assertFalse(db.exists('demo1'))
     self.assertEqual(db.tables, [])