def test_fetched_objects_are_unmodified_many_many(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include('courses').exec() self.assertEqual(students[0].is_new, False) self.assertEqual(students[0].is_modified, False) self.assertEqual(students[0].modified_fields, ()) self.assertEqual(students[1].is_new, False) self.assertEqual(students[1].is_modified, False) self.assertEqual(students[1].modified_fields, ()) self.assertEqual(students[0].courses[0].is_new, False) self.assertEqual(students[0].courses[0].is_modified, False) self.assertEqual(students[0].courses[0].modified_fields, ()) self.assertEqual(students[0].courses[1].is_new, False) self.assertEqual(students[0].courses[1].is_modified, False) self.assertEqual(students[0].courses[1].modified_fields, ()) self.assertEqual(students[1].courses[0].is_new, False) self.assertEqual(students[1].courses[0].is_modified, False) self.assertEqual(students[1].courses[0].modified_fields, ()) self.assertEqual(students[1].courses[1].is_new, False) self.assertEqual(students[1].courses[1].is_modified, False) self.assertEqual(students[1].courses[1].modified_fields, ())
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_fetched_objects_are_filtered_many_many(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include( 'courses', LinkedCourse.find(name='C1')).exec() self.assertEqual(len(students[0].courses), 1) self.assertEqual(len(students[1].courses), 1)
def test_many_many_ref_uses_unique_objects(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include( 'courses', LinkedCourse.find(name='C1')).exec() s1 = students[0] s2 = students[1] self.assertIs(s1.courses[0], s2.courses[0])
def test_fetched_objects_are_paged_many_many(self): student = LinkedStudent(name='S1') for i in range(100): course = LinkedCourse(name=f's{str(i).zfill(3)}') student.courses.append(course) student.save() student = LinkedStudent.one().include( 'courses', LinkedCourse.find().order('name', -1).page_size(10).page_number(2)).exec() course_names = [course.name for course in student.courses] self.assertEqual(course_names, [ 's089', 's088', 's087', 's086', 's085', 's084', 's083', 's082', 's081', 's080' ]) student = LinkedStudent.one().include( 'courses', LinkedCourse.find().order('name', -1).page_size(10).page_number(4)).exec() course_names = [course.name for course in student.courses] self.assertEqual(course_names, [ 's069', 's068', 's067', 's066', 's065', 's064', 's063', 's062', 's061', 's060' ]) student = LinkedStudent.one().include( 'courses', LinkedCourse.find().order('name', 1).page_size(8).page_number(1)).exec() course_names = [course.name for course in student.courses] self.assertEqual( course_names, ['s000', 's001', 's002', 's003', 's004', 's005', 's006', 's007'])
def test_many_many_ref_lookup_can_be_filtered(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include( 'courses', LinkedCourse.find(name='C1')).exec() self.assertEqual(len(students), 2) self.assertEqual(len(students[0].courses), 1) self.assertEqual(len(students[1].courses), 1) self.assertEqual(students[0].courses[0].name, course1.name) self.assertEqual(students[1].courses[0].name, course1.name)
def test_many_many_ref_lookup_fetches_linked_objects(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include('courses').exec() self.assertEqual(len(students), 2) self.assertEqual(len(students[0].courses), 2) self.assertEqual(len(students[1].courses), 2) self.assertEqual(students[0].id, student2.id) self.assertEqual(students[1].id, student1.id) self.assertEqual(students[0].courses[0].id, course2.id) self.assertEqual(students[0].courses[1].id, course1.id) self.assertEqual(students[1].courses[0].id, course2.id) self.assertEqual(students[1].courses[1].id, course1.id)
def test_fetched_objects_are_sorted_many_many(self): student1 = LinkedStudent(name='S1') student2 = LinkedStudent(name='S2') course1 = LinkedCourse(name='C1') course2 = LinkedCourse(name='C2') student1.courses = [course1, course2] student2.courses = [course1, course2] student1.save() students = LinkedStudent.find().include( 'courses', LinkedCourse.find().order('name', -1)).exec() self.assertEqual(students[0].courses[0].name, 'C2') self.assertEqual(students[0].courses[1].name, 'C1') self.assertEqual(students[1].courses[0].name, 'C2') self.assertEqual(students[1].courses[1].name, 'C1') students = LinkedStudent.find().include( 'courses', LinkedCourse.find().order('name', 1)).exec() self.assertEqual(students[0].courses[0].name, 'C1') self.assertEqual(students[0].courses[1].name, 'C2') self.assertEqual(students[1].courses[0].name, 'C1') self.assertEqual(students[1].courses[1].name, 'C2')