コード例 #1
0
    def test_07_complex(self):
        self.generate_data_1()
        doc = list(self._user_model.find())[0]
        doc.addresses.append({'address': 'address1', 'postcode': 'postcode1'})
        doc.addresses.append({'address': 'address2', 'postcode': 'postcode2'})
        doc.save()
        doc = list(self._user_model.find())[0]
        self.assertEqual(len(doc.addresses), 2)
        del doc.addresses[0]
        doc.save()
        doc = list(self._user_model.find())[0]
        self.assertEqual(len(doc.addresses), 1)
        self.assertEqual(doc.addresses[0].postcode, 'postcode2')
        address = list(self._address_model.find())[0]
        doc.addresses.append(address)
        doc.save()
        self.assertEqual(len(doc.addresses), 2)
        doc = list(self._user_model.find())[0]
        doc.addresses[0].postcode="GB"
        doc.save()

        self._database.close()
        self._database = Database('test_orm', {'env': {'map_size': 1024 * 1024 * 10}})
        self._user_model = UserModel(table=self._database.table('users'))
        self._address_model = AddressModel(table=self._database.table('addresses'))
        self._links = ManyToMany(self._database, self._user_model, self._address_model)

        doc = list(self._user_model.find())[0]
        self.assertEqual(len(doc.addresses), 2)
        self.assertEqual(doc.addresses[0].postcode, 'GB')
コード例 #2
0
 def setUp(self):
     call(['rm', '-rf', "databases/test_models"])
     self._database = Database('databases/test_models',
                               {'env': {
                                   'map_size': 1024 * 1024 * 10
                               }})
     self._model = Model(table=self._database.table('model'))
コード例 #3
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])
コード例 #4
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, [])
コード例 #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_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'))
コード例 #8
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')
コード例 #9
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)
コード例 #10
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)
コード例 #11
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)
コード例 #12
0
    def setUp(self):
        call(['rm', '-rf', 'databases/test_orm_addressbook'])
        self._db = Database('databases/test_orm_addressbook', size=size_mb(10))
        self.tbl_address_book = AddressBookModel(table=self._db.table('address_book'))
        self.tbl_business = BusinessModel(table=self._db.table('business'))
        self.tbl_person = PersonModel(table=self._db.table('person'))
        self.tbl_address = AddressModel(table=self._db.table('address'))

        ManyToMany(self._db, self.tbl_address_book, self.tbl_business)
        ManyToMany(self._db, self.tbl_address_book, self.tbl_person)
        ManyToMany(self._db, self.tbl_business, self.tbl_person)
        ManyToMany(self._db, self.tbl_business, self.tbl_address)
        ManyToMany(self._db, self.tbl_person, self.tbl_address)
コード例 #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_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', '{')
コード例 #15
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'))
コード例 #16
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)
コード例 #17
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')
コード例 #18
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)
コード例 #19
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')
コード例 #20
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)
コード例 #21
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'))
コード例 #22
0
    def test_06_add_with_linked(self):
        self.generate_data_1()
        doc = list(self._user_model.find())[0]
        doc.addresses.append({'line1': 'NEW LINE', 'postcode': 'NEW'})
        doc.addresses.append({'line1': 'NEW LINE1', 'postcode': 'NEW1'})
        doc.save()
        self.assertEqual(doc.addresses[0].postcode, 'NEW')
        self.assertEqual(len(doc.addresses), 2)

        doc = list(self._user_model.find())[0]
        self.assertEqual(doc.addresses[1].postcode, 'NEW1')
        self.assertEqual(len(doc.addresses), 2)

        self._database.close()
        self._database = Database('test_orm', {'env': {'map_size': 1024 * 1024 * 10}})
        self._user_model = UserModel(table=self._database.table('users'))
        self._address_model = AddressModel(table=self._database.table('addresses'))
        self._links = ManyToMany(self._database, self._user_model, self._address_model)

        doc = list(self._user_model.find())[0]
        self.assertEqual(doc.addresses[0].postcode, 'NEW')
        self.assertEqual(doc.addresses[1].postcode, 'NEW1')
        self.assertEqual(len(doc.addresses), 2)
コード例 #23
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
コード例 #24
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)
コード例 #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'})
コード例 #26
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:
コード例 #27
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))
コード例 #28
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, [])
コード例 #29
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'))
コード例 #30
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'])