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)
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')
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)
'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' }).person.append({ 'name': 'My Person Name' }).address.append({ 'line1': 'My Address Line 1', 'postcode': 'MY POSTCODE'
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:]) exit() except IndexError:
class AddressModel(Table): _display = [{ 'name': 'address', 'width': 30 }, { 'name': 'postcode', 'width': 15 }] db = Database('my_db', {'env': {'map_size': 1024 * 1024 * 10}}) user_model = UserModel(table=db.table('users')) address_model = AddressModel(table=db.table('addresses')) links = ManyToMany(db, user_model, address_model) user = user_model.add({ 'forename': 'john', 'surname': 'smith', 'dob': datetime.date(1971, 12, 1) }) print(user) user.addresses.append({'address': 'address1', 'postcode': 'postcode1'}) user.addresses.append({'address': 'address2', 'postcode': 'postcode2'}) user.save() print(user) print(user.addresses) user_model.list() address_model.list()
def setUp(self): call(['rm', '-rf', "test_orm"]) 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)