コード例 #1
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_23_function_index(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.generate_data(db, self._tb_name)
        table.index('by_compound', '{cat}|{name}', duplicates=True)
        table.index('by_age', '{age:03}', duplicates=True)

        results = []
        for doc in table.find('by_compound'):
            results.append(doc['cat'])
        self.assertEqual(results, ['A', 'A', 'A', 'B', 'B', 'B', 'B'])

        table.empty()
        table = db.table(self._tb_name)
        self.generate_data(db, self._tb_name)

        results = []
        for doc in table.find('by_compound'):
            results.append(doc['cat'])
        self.assertEqual(results, ['A', 'A', 'A', 'B', 'B', 'B', 'B'])

        for i in table.seek('by_compound', {'cat': 'A', 'name': 'Squizzey'}):
            #print("}}}",i)
            self.assertEqual(i['age'], 3000)

        for i in table.seek('by_compound', {'cat': 'B', 'name': 'John Doe'}):
            self.assertEqual(i['age'], 40)

        self.assertEqual(
            list(table.seek('by_compound', {
                'cat': 'C',
                'name': 'Squizzey'
            })), [])

        lower = {'cat': 'A', 'name': 'Squizzey'}
        upper = {'cat': 'B', 'name': 'Gareth Bult1'}
        iter = table.range('by_compound', lower, upper)
        results = list(iter)

        self.assertEqual(results[0]['name'], 'Squizzey')
        self.assertEqual(results[1]['name'], 'Gareth Bult1')

        #print(results[0])
        results[0]['name'] = '!Squizzey'
        results[0]['age'] = 1
        table.save(results[0])

        table._indexes['duff'] = None
        with self.assertRaises(AttributeError):
            table.save(results[0])

        self.assertEqual(
            list(table.find('by_compound'))[0]['name'], '!Squizzey')
        self.assertEqual(list(table.find('by_age'))[0]['age'], 1)
コード例 #2
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_14_table_empty(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        self.assertEqual(table.records, len(self._data))
        table.empty()
        table = db.table(self._tb_name)
        self.assertEqual(table.records, 0)
コード例 #3
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_13_index_exists(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.assertTrue(table.exists('by_name'))
        db.close()
        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.assertTrue(table.exists('by_name'))
コード例 #4
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_12_table_reopen(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        db.close()
        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.assertEqual(['by_age', 'by_age_name', 'by_name'], table.indexes)
コード例 #5
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 def test_08_delete(self):
     db = Database(self._db_name)
     table = db.table(self._tb_name)
     for doc in self._data:
         table.append(dict(doc))
     doc = next(table.find(limit=1))
     table.delete(doc)
     self.assertEqual(table.records, len(self._data) - 1)
コード例 #6
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 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)
コード例 #7
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 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, [])
コード例 #8
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_16_check_delete_exception(self):
        class f(object):
            pass

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        with self.assertRaises(TypeError):
            table.delete([f])
コード例 #9
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 def test_07_empty(self):
     db = Database(self._db_name)
     table = db.table(self._tb_name)
     for doc in self._data:
         table.append(dict(doc))
     self.assertEqual(table.records, len(self._data))
     table.empty()
     self.assertEqual(table.records, 0)
     self.assertTrue(db.exists('demo1'))
コード例 #10
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_25_seek_one(self):

        db = Database(self._db_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')
コード例 #11
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_19_count_with_txn(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        with db.env.begin() as txn:
            index = table.index('by_name')
            self.assertTrue(index.count(txn=txn), 7)
コード例 #12
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    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)
コード例 #13
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_15_check_append_exception(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        table._indexes = 10
        before = table.records
        with self.assertRaises(xWriteFail):
            table.append({'_id': -1})
        after = table.records
        self.assertEqual(before, after)
コード例 #14
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    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
コード例 #15
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_11_compound_index(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        self.generate_data(db, self._tb_name)
        ages = [doc['age'] for doc in self._data]
        ages.sort()
        ages.reverse()

        for row in table.find('by_age_name'):
            self.assertEqual(row['age'], ages.pop())

        with self.assertRaises(ValueError):
            table.index('broken', '{')
コード例 #16
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_18_check_unindex_exception(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        with self.assertRaises(xIndexMissing):
            table.drop_index('fred')

        table.index('by_name', '{name}')
        self.assertTrue('by_name' in table.indexes)
        table.drop_index('by_name')
        self.assertFalse('by_name' in table.indexes)

        table.index('duff', '{name}')
        table._indexes['duff'] = None
        with self.assertRaises(AttributeError):
            table.drop_index('duff')
コード例 #17
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_20_index_get(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        with db._env.begin() as txn:
            index = table.index('by_name')
            _id = index.get(txn, {'name': 'Squizzey'})
            doc = table.get(_id)
        self.assertTrue(doc['age'], 3000)
        self.assertTrue(doc['name'], 'Squizzey')
        with self.assertRaises(xIndexMissing):
            list(table.find('fred', 'fred'))
コード例 #18
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_22_reindex(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.generate_data(db, self._tb_name)
        by_age_name = table.index('by_age_name', '{age:03}{name}')
        by_name = table.index('by_name', '{name}')
        by_age = table.index('by_age', '{age:03}', duplicates=True)

        self.assertEqual(by_age_name.count(), 7)
        self.assertEqual(by_name.count(), 7)
        self.assertEqual(by_age.count(), 7)
        table.reindex()
        self.assertEqual(by_age_name.count(), 7)
        self.assertEqual(by_name.count(), 7)
        self.assertEqual(by_age.count(), 7)
コード例 #19
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_24_partial_index(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.generate_data(db, self._tb_name)
        table.index('by_admin', '{admin}', duplicates=True)
        try:
            for doc in table.find('by_admin'):
                pass
            #print("> {admin}".format(**doc), doc)
        except Exception as error:
            self.fail('partial key failure')
            raise error

        self.assertEqual(table.index('by_admin').count(), 3)
        with self.assertRaises(AttributeError):
            table.unindex('by_admin', 123)
コード例 #20
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_21_filters(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        self.generate_data(db, self._tb_name)
        result = list(table.find(expression=lambda doc: doc['age'] == 3000))[0]
        self.assertEqual(result['age'], 3000)
        self.assertEqual(result['name'], 'Squizzey')
        result = list(
            table.find('by_name', expression=lambda doc: doc['age'] == 21))[0]
        self.assertEqual(result['age'], 21)
        self.assertEqual(result['name'], 'Gareth Bult')
        result = list(
            table.find('by_name',
                       expression=lambda doc: doc['name'] == 'John Doe'))[0]
        self.assertEqual(result['age'], 40)
        self.assertEqual(result['name'], 'John Doe')
コード例 #21
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 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, [])
コード例 #22
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 def test_04_exists(self):
     db = Database(self._db_name)
     db.table(self._tb_name)
     self.assertTrue(db.exists('demo1'))
コード例 #23
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 def test_03_tables(self):
     db = Database(self._db_name)
     db.table(self._tb_name)
     self.assertEqual(db.tables, ['demo1'])
コード例 #24
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
 def test_02_create_table(self):
     db = Database(self._db_name, size=size_gb(0.1))
     table = db.table(self._tb_name)
     self.assertTrue(isinstance(table, Table))
コード例 #25
0
ファイル: addressbook.py プロジェクト: oddjobz/pymamba

class AddressModel(Table):
    _calculated = {}
    _display = [{
        'name': 'line1',
        'width': 30
    }, {
        'name': 'postcode',
        'width': 15
    }]


db = Database('databases/contacts_db', size=size_mb(10))

tbl_address_book = AddressBookModel(table=db.table('address_book'))
tbl_business = BusinessModel(table=db.table('business'))
tbl_person = PersonModel(table=db.table('person'))
tbl_address = AddressModel(table=db.table('address'))

address_book_business = ManyToMany(db, tbl_address_book, tbl_business)
address_book_person = ManyToMany(db, tbl_address_book, tbl_person)
business_person = ManyToMany(db, tbl_business, tbl_person)
business_address = ManyToMany(db, tbl_business, tbl_address)
person_address = ManyToMany(db, tbl_person, tbl_address)

print("Tables: ", db.tables)

doc = tbl_address_book.add({'name': 'Personal'})
doc.business.append({
    'name': 'My Business Name'
コード例 #26
0
    {
        'name': 'Fred Bloggs',
        'age': 45
    },
    {
        'name': 'John Doe',
        'age': 0
    },
    {
        'name': 'John Smith',
        'age': 40
    },
]

db = Database("MyDatabase")  # 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]')
コード例 #27
0
ファイル: model_example.py プロジェクト: oddjobz/pymamba
def modify_address(key, change):
    return modify(address_model, key, change)


def modify(model, key, change):
    doc = model.get(key.encode())
    if not doc:
        print('Unable to locate key "{}"'.format(key))
        return
    doc.modify(change)
    doc.save()

if __name__ == '__main__':

    database = Database('databases/people_database', {'env': {'map_size': 1024 * 1024 * 10}})
    user_model = UserModel(table=database.table('users'))
    address_model = AddressModel(table=database.table('addresses'))
    user_address = ManyToMany(database, user_model, address_model)
    #
    #   Really basic interface using functions built-in to the BaseModel class.
    #
    commands = {
        'lst': user_model.list,
        'add': user_model.add,
        'mod': modify_user,
        'lst_address': address_model.list,
        'add_address': address_model.add,
        'mod_address': modify_address,
    }
    try:
        commands[argv[1]](*argv[2:])
コード例 #28
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    def test_28_range(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        data = [
            {
                'code': 'F',
                'name': 'Tom'
            },
            {
                'code': 'E',
                'name': 'Dick'
            },
            {
                'code': 'E',
                'name': 'Dick1'
            },
            {
                'code': 'D',
                'name': 'Harry'
            },
            {
                'code': 'C',
                'name': 'Fred'
            },
            {
                'code': 'B',
                'name': 'John'
            },
            {
                'code': 'B',
                'name': 'John1'
            },
            {
                'code': 'A',
                'name': 'Sam'
            },
        ]
        for row in data:
            table.append(row)

        table.index('by_code', '{code}', duplicates=True)
        res = list(table.find('by_code'))
        self.assertEqual(res[0]['code'], 'A')
        self.assertEqual(res[-1]['code'], 'F')

        res = list(table.find())
        lower = res[0]['_id']
        upper = res[-1]['_id']
        #print('Lower={} Upper={}'.format(lower, upper))
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'F')
        self.assertEqual(natural[-1]['code'], 'A')

        res = list(table.find())
        lower = res[0]['_id']
        upper = res[-2]['_id']
        #print('Lower={} Upper={}'.format(lower, upper))
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        #for doc in natural:
        #    print(doc)

        self.assertEqual(natural[0]['code'], 'F')
        self.assertEqual(natural[-1]['code'], 'B')

        res = list(table.find())
        lower = res[0]['_id']
        upper = res[-1]['_id']
        #print('Lower={} Upper={}'.format(lower, upper))
        natural = list(
            table.range(None, {'_id': lower}, {'_id': upper}, inclusive=False))
        #for doc in natural:
        #    print(doc)

        self.assertEqual(natural[0]['code'], 'E')
        self.assertEqual(natural[-1]['code'], 'B')

        res = list(table.find())
        lower = None
        upper = res[-1]['_id']
        #print('Lower={} Upper={}'.format(lower, upper))
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'F')
        self.assertEqual(natural[-1]['code'], 'A')

        res = list(table.find())
        lower = res[0]['_id']
        upper = None
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'F')
        self.assertEqual(natural[-1]['code'], 'A')

        res = list(table.find())
        lower = None
        upper = None
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'F')
        self.assertEqual(natural[-1]['code'], 'A')

        lower = res[0]['_id']
        upper = res[0]['_id']
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'F')

        lower = res[-1]['_id']
        upper = res[-1]['_id']
        natural = list(table.range(None, {'_id': lower}, {'_id': upper}))
        self.assertEqual(natural[0]['code'], 'A')

        lower = res[0]['_id']
        upper = res[0]['_id']
        natural = list(
            table.range(None, {'_id': lower}, {'_id': upper}, inclusive=False))
        self.assertEqual(natural, [])

        lower = res[-1]['_id']
        upper = res[-1]['_id']
        natural = list(
            table.range(None, {'_id': lower}, {'_id': upper}, inclusive=False))
        self.assertEqual(natural, [])

        lower = res[0]['_id']
        upper = res[1]['_id']
        natural = list(
            table.range(None, {'_id': lower}, {'_id': upper}, inclusive=False))
        self.assertEqual(natural, [])

        lower = res[0]['_id']
        upper = res[2]['_id']
        natural = list(
            table.range(None, {'_id': lower}, {'_id': upper}, inclusive=False))
        self.assertEqual(natural[0]['_id'], res[1]['_id'])

        table.index('by_code', '{code}')
        res = list(table.range('by_code', {'code': '0'}, {'code': 'Z'}))
        self.assertEqual(res[0]['code'], 'A')
        self.assertEqual(res[-1]['code'], 'F')

        res = list(table.range('by_code', {'code': 'B'}, {'code': 'E'}))
        self.assertEqual(res[0]['code'], 'B')
        self.assertEqual(res[-1]['code'], 'E')

        res = list(
            table.range('by_code', {'code': 'B'}, {'code': 'E'},
                        inclusive=False))
        self.assertEqual(res[0]['code'], 'C')
        self.assertEqual(res[-1]['code'], 'D')

        res = list(
            table.range('by_code', {'code': 'A'}, {'code': 'F'},
                        inclusive=False))
        self.assertEqual(res[0]['code'], 'B')
        self.assertEqual(res[-1]['code'], 'E')

        res = list(table.range('by_code', None, None))
        self.assertEqual(res[0]['code'], 'A')
        self.assertEqual(res[-1]['code'], 'F')
コード例 #29
0
ファイル: tool.py プロジェクト: oddjobz/pymamba
"""
This is a debugging tool, doesn't do much for now ..
"""

from pymamba import Database, size_mb

db = Database('databases/contacts_db', size=size_mb(10))
for name in db.tables_all:
    if name[0] == '@':
        continue
    table = db.table(name)
    print(name, table.records)
    if name[:4] == 'rel_':
        for i in table.find():
            print( "  ", i)
コード例 #30
0
ファイル: test_mamba.py プロジェクト: oddjobz/pymamba
    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')