def setUp(self) -> None: collection = Connection.get_collection(SimplePerson) collection.delete_many({}) collection = Connection.get_collection(SimpleSinger) collection.delete_many({}) collection = Connection.get_collection(SimpleAlbum) collection.delete_many({})
def test_1l_1f_allows_deletion(self): user = LinkedDUser(name='crazy') user.save() collection = Connection.get_collection(LinkedDUser) self.assertEqual(collection.count_documents({}), 1) user.delete() collection = Connection.get_collection(LinkedDUser) self.assertEqual(collection.count_documents({}), 0)
def test1f_1l_allows_deletion(self): profile = LinkedDProfile(name='crazy') profile.save() collection = Connection.get_collection(LinkedDProfile) self.assertEqual(collection.count_documents({}), 1) profile.delete() collection = Connection.get_collection(LinkedDProfile) self.assertEqual(collection.count_documents({}), 0)
def test_1_many_allows_deletion(self): notebook = LinkedNotebook(name='N', notes=[]) notebook.save() collection = Connection.get_collection(LinkedNotebook) self.assertEqual(collection.count_documents({}), 1) notebook.delete() collection = Connection.get_collection(LinkedNotebook) self.assertEqual(collection.count_documents({}), 0)
def test_many_1_allows_deletion(self): note = LinkedRNote(name='N') note.save() collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 1) note.delete() collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 0)
def test_many_many_allows_deletion(self): owner = LinkedOwner(name='OK') owner.save() collection = Connection.get_collection(LinkedOwner) self.assertEqual(collection.count_documents({}), 1) owner.delete() collection = Connection.get_collection(LinkedOwner) self.assertEqual(collection.count_documents({}), 0)
def setUp(self) -> None: collection = Connection.get_collection(SimpleRecord) collection.delete_many({}) collection = Connection.get_collection(SimpleORecord) collection.delete_many({}) collection = Connection.get_collection(LinkedRecord) collection.delete_many({}) collection = Connection.get_collection(LinkedContent) collection.delete_many({})
def test_1l_1f_list_can_be_nullified(self): author = LinkedAuthor(name='A', posts=[ LinkedPost(title='P1', content='C1'), LinkedPost(title='P2', content='C2')]) author.save() collection = Connection.get_collection(LinkedAuthor) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedPost) self.assertEqual(collection.count_documents({}), 2) author.delete() for obj in collection.find(): self.assertNotIn('authorId', obj)
def test_1l_1f_cascade_delete(self): balance = LinkedBalance(name='A', account=LinkedAccount(name='B')) balance.save() collection = Connection.get_collection(LinkedAccount) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedBalance) self.assertEqual(collection.count_documents({}), 1) balance.delete() collection = Connection.get_collection(LinkedAccount) self.assertEqual(collection.count_documents({}), 0) collection = Connection.get_collection(LinkedBalance) self.assertEqual(collection.count_documents({}), 0)
def test_many_1_cascade_delete(self): buyer = LinkedCBuyer(name='B') order1 = LinkedCOrder(name='O1') order2 = LinkedCOrder(name='O2') buyer.orders = [order1, order2] buyer.save() collection = Connection.get_collection(LinkedCOrder) self.assertEqual(collection.count_documents({}), 2) order1.delete() self.assertEqual(collection.count_documents({}), 0) collection = Connection.get_collection(LinkedCBuyer) self.assertEqual(collection.count_documents({}), 0)
def test_1l_1f_instance_can_be_nullified(self): user = LinkedUser(name='crazy') profile = LinkedProfile(name='six') user.profile = profile user.save() collection = Connection.get_collection(LinkedUser) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedProfile) self.assertEqual(collection.count_documents({}), 1) user.delete() for obj in collection.find(): self.assertNotIn('userId', obj)
def test_many_1_denies_deletion(self): notebook = LinkedRNotebook(name='N', notes=[{'name': 'A'}, {'name': 'B'}]) notebook.save() collection = Connection.get_collection(LinkedRNotebook) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 2) self.assertRaises(DeletionDeniedException, notebook.notes[0].delete) collection = Connection.get_collection(LinkedRNotebook) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 2)
def test_1f_1l_denies_deletion(self): user = LinkedCUser(name='crazy') profile = LinkedDProfile(name='six') user.profile = profile user.save() collection = Connection.get_collection(LinkedCUser) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedDProfile) self.assertEqual(collection.count_documents({}), 1) self.assertRaises(DeletionDeniedException, profile.delete) collection = Connection.get_collection(LinkedCUser) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedDProfile) self.assertEqual(collection.count_documents({}), 1)
def test_many_1_reverse_deny_has_nullify_effect(self): notebook = LinkedRNotebook(name='N', notes=[{'name': 'A'}, {'name': 'B'}]) notebook.save() collection = Connection.get_collection(LinkedRNotebook) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 2) notebook.delete() collection = Connection.get_collection(LinkedRNotebook) self.assertEqual(collection.count_documents({}), 0) collection = Connection.get_collection(LinkedRNote) self.assertEqual(collection.count_documents({}), 2) for item in collection.find({}): self.assertNotIn('notebookId', item)
def test_encode_embedded_instance_list(self): @pymongo @jsonclass class MediumEncodeEmbeddedInstanceAddress: id: str = types.readonly.str.primary.mongoid.required line1: str @pymongo @jsonclass class MediumEncodeEmbeddedInstance: id: str = types.readonly.str.primary.mongoid.required addresses: List[MediumEncodeEmbeddedInstanceAddress] medium_object = MediumEncodeEmbeddedInstance( addresses=[{ 'line1': 'Flam Road' }, { 'line1': 'Plam Road' }]) batch_command = Encoder().encode_root(medium_object) commands = batch_command.commands self.assertEqual(len(commands), 1) command = commands[0] self.assertIs(command.collection, Connection.get_collection(MediumEncodeEmbeddedInstance)) data = command.object addresses = data['addresses'] self.assertEqual(len(addresses), 2) self.assertEqual(addresses[0]['line1'], 'Flam Road') self.assertEqual(addresses[1]['line1'], 'Plam Road')
def test_encode_embedded_instance(self): @pymongo @jsonclass class SimpleEncodeEmbeddedInstanceAddress: id: str = types.readonly.str.primary.mongoid.required line1: str @pymongo @jsonclass class SimpleEncodeEmbeddedInstance: id: str = types.readonly.str.primary.mongoid.required address: SimpleEncodeEmbeddedInstanceAddress simple_object = SimpleEncodeEmbeddedInstance( address={'line1': 'Flam Road'}) batch_command = Encoder().encode_root(simple_object) commands = batch_command.commands self.assertEqual(len(commands), 1) command = commands[0] self.assertIs(command.collection, Connection.get_collection(SimpleEncodeEmbeddedInstance)) data = command.object address = data['address'] self.assertIsInstance(address['_id'], ObjectId) self.assertEqual(address['line1'], 'Flam Road')
def setUp(self) -> None: collection = Connection.get_collection(PLUser) collection.delete_many({}) collection = Connection.get_collection(PLArticle) collection.delete_many({}) # collection = Connection.get_collection(LLPLUser) # collection.delete_many({}) # collection = Connection.get_collection(LLPLArticle) # collection.delete_many({}) collection = Connection.get_collection(LJPLUser) collection.delete_many({}) collection = Connection.get_collection(LJPLArticle) collection.delete_many({}) collection = Connection('preload').collection( 'ljplarticlesauthorsljplusersarticles') collection.delete_many({})
def test_many_many_cascade_deny_triggers_partial_deletion(self): c1 = LinkedCompany(name='C1') c2 = LinkedCompany(name='C2') o1 = LinkedOwner(name='O1') o2 = LinkedOwner(name='O2') c1.owners = [o1, o2] c2.owners = [o1, o2] c1.save() c1.delete() collection = Connection.get_collection(LinkedCompany) self.assertEqual(collection.count_documents({}), 1) collection = Connection.get_collection(LinkedOwner) self.assertEqual(collection.count_documents({}), 2) collection = Connection('linked').collection('linkedcompaniesowners' 'linkedownerscompanies') self.assertEqual(collection.count_documents({}), 2)
def test_local_key_from_param_is_saved(self): a1 = LinkedAuthor(name='A1') a1.save() post = LinkedPost(title='P1', content='P2', authorId=a1.id) post.save() collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertEqual(item['authorId'], ObjectId(a1.id))
def test_object_can_be_removed_from_database(self): song = SimpleSong(name='Long', year=2020, artist='Thao') song.save() collection = Connection.get_collection(SimpleSong) self.assertEqual(collection.count_documents({}), 1) song.delete() self.assertEqual(collection.count_documents({}), 0) self.assertEqual(song.is_deleted, True)
def test_set_local_key_to_unset_is_saved(self): post = LinkedPost(title='P1', content='P2') author = LinkedAuthor(name='A1') post.author = author post.save() post.author_id = None post.save() collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertNotIn('authorId', item)
def test_set_local_key_to_id_is_saved(self): post = LinkedPost(title='P1', content='P2') author = LinkedAuthor(name='A1') post.save() author.save() post.author_id = author.id post.save() collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertEqual(item['authorId'], ObjectId(author.id))
def test_fl_many_many_is_saved(self): song1 = LinkedSong(name='song1') song2 = LinkedSong(name='song2') singer1 = LinkedSinger(name='singer1') singer2 = LinkedSinger(name='singer2') song1.singers = [singer1, singer2] song2.singers = [singer1, singer2] song1.save() collection = Connection.get_collection(LinkedSong) for item in collection.find({}): self.assertEqual(item['singerIds'], [ObjectId(singer1.id), ObjectId(singer2.id)])
def test_object_with_enum_is_saved_into_database(self): artist = SimpleArtist(name='Kaosai', gender='MALE') artist.save() collection = Connection.get_collection(SimpleArtist) self.assertEqual(collection.count_documents({}), 1) for item in collection.find(): self.assertEqual(item['name'], 'Kaosai') self.assertEqual(item['gender'], 1) self.assertIsInstance(item['_id'], ObjectId) self.assertIsInstance(item['updatedAt'], datetime) self.assertIsInstance(item['createdAt'], datetime) self.assertEqual( set(item.keys()), {'_id', 'name', 'gender', 'updatedAt', 'createdAt'})
def test_many_many_denies_deletion(self): c1 = LinkedCompany(name='C1') c2 = LinkedCompany(name='C2') o1 = LinkedOwner(name='O1') o2 = LinkedOwner(name='O2') c1.owners = [o1, o2] c2.owners = [o1, o2] o1.save() collection = Connection.get_collection(LinkedCompany) self.assertEqual(collection.count_documents({}), 2) collection = Connection.get_collection(LinkedOwner) self.assertEqual(collection.count_documents({}), 2) collection = Connection('linked').collection('linkedcompaniesowners' 'linkedownerscompanies') self.assertEqual(collection.count_documents({}), 4) self.assertRaises(DeletionDeniedException, o1.delete) collection = Connection.get_collection(LinkedCompany) self.assertEqual(collection.count_documents({}), 2) collection = Connection.get_collection(LinkedOwner) self.assertEqual(collection.count_documents({}), 2) collection = Connection('linked').collection('linkedcompaniesowners' 'linkedownerscompanies') self.assertEqual(collection.count_documents({}), 4)
def test_linked_objects_are_saved_at_the_same_time(self): input = { 'name': 'Ti', 'posts': [{ 'title': 'Bo Lo Iong', 'content': 'Pieng Iu' }, { 'title': 'Bo Lo Iong', 'content': 'Pieng Iu' }] } author = LinkedAuthor(**input) author.save() collection = Connection.get_collection(LinkedAuthor) self.assertEqual(collection.count_documents({}), 1) oid = ObjectId() for item in collection.find(): self.assertEqual(item['name'], 'Ti') self.assertIsInstance(item['_id'], ObjectId) oid = item['_id'] self.assertIsInstance(item['updatedAt'], datetime) self.assertIsInstance(item['createdAt'], datetime) self.assertEqual(set(item.keys()), {'_id', 'name', 'updatedAt', 'createdAt'}) collection = Connection.get_collection(LinkedPost) self.assertEqual(collection.count_documents({}), 2) for item in collection.find(): self.assertEqual(item['title'], 'Bo Lo Iong') self.assertEqual(item['content'], 'Pieng Iu') self.assertEqual(item['authorId'], oid) self.assertIsInstance(item['_id'], ObjectId) self.assertIsInstance(item['updatedAt'], datetime) self.assertIsInstance(item['createdAt'], datetime) self.assertEqual(set(item.keys()), { '_id', 'title', 'content', 'authorId', 'updatedAt', 'createdAt' })
def test_many_many_cascade_delete(self): soldier1 = LinkedSoldier(name='S1') soldier2 = LinkedSoldier(name='S2') bomb1 = LinkedBomb(name='B1') bomb2 = LinkedBomb(name='B2') soldier1.bombs = [bomb1, bomb2] soldier2.bombs = [bomb1, bomb2] soldier1.save() soldier2.save() collection = Connection.get_collection(LinkedSoldier) self.assertEqual(collection.count_documents({}), 2) collection = Connection.get_collection(LinkedBomb) self.assertEqual(collection.count_documents({}), 2) collection = Connection('linked').collection('linkedbombssoldiers' 'linkedsoldiersbombs') self.assertEqual(collection.count_documents({}), 4) soldier1.delete() collection = Connection.get_collection(LinkedSoldier) self.assertEqual(collection.count_documents({}), 0) collection = Connection.get_collection(LinkedBomb) self.assertEqual(collection.count_documents({}), 0) collection = Connection('linked').collection('linkedbombssoldiers' 'linkedsoldiersbombs') self.assertEqual(collection.count_documents({}), 0)
def test_object_is_saved_into_database(self): song = SimpleSong(name='Long', year=2020, artist='Thao') song.save() collection = Connection.get_collection(SimpleSong) self.assertEqual(collection.count_documents({}), 1) for item in collection.find(): self.assertEqual(item['name'], 'Long') self.assertEqual(item['year'], 2020) self.assertEqual(item['artist'], 'Thao') self.assertIsInstance(item['_id'], ObjectId) self.assertIsInstance(item['updatedAt'], datetime) self.assertIsInstance(item['createdAt'], datetime) self.assertEqual( set(item.keys()), {'_id', 'name', 'year', 'artist', 'updatedAt', 'createdAt'})
def test_many_1_unlink_unset_is_saved(self): author = LinkedAuthor(name='A') post = LinkedPost(title='T', content='C') post.author = author post.save() post.author_id = None post.save() self.assertEqual(author.is_new, False) self.assertEqual(author.is_modified, False) self.assertEqual(author.modified_fields, ()) self.assertEqual(post.is_new, False) self.assertEqual(post.is_modified, False) self.assertEqual(post.modified_fields, ()) collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertNotIn('authorId', item)
def test_1f_1l_object_unlink_is_saved(self): user = LinkedUser(name='u') profile = LinkedProfile(name='p') user.profile = profile user.save() user.profile = None user.save() self.assertEqual(profile.is_new, False) self.assertEqual(profile.is_modified, False) self.assertEqual(profile.modified_fields, ()) self.assertEqual(user.is_new, False) self.assertEqual(user.is_modified, False) self.assertEqual(user.modified_fields, ()) collection = Connection.get_collection(LinkedProfile) for item in collection.find({}): self.assertNotIn('userId', item)