def test_many_many_can_be_nullified(self): course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1.students = [student1, student2] course2.students = [student1, student2] course1.save() # this triggers save also for course2 collname = 'linkedcoursesstudentslinkedstudentscourses' collection = Connection('linked').collection(collname) self.assertEqual(collection.count_documents({}), 4) course1.delete() self.assertEqual(collection.count_documents({}), 2)
def test_many_many_unlink_is_saved(self): course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1.students = [student1, student2] course2.students = [student1, student2] course1.save() course1.students.remove(student1) course1.save() collname = 'linkedcoursesstudentslinkedstudentscourses' collection = Connection('linked').collection(collname) self.assertEqual(collection.count_documents({}), 3)
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_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_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)