def test_1_many_ref_lookup_fetches_linked_objects(self): author = LinkedAuthor(name='Kai Nang Piang Tshung') post1 = LinkedPost(title='P1', content='A') post2 = LinkedPost(title='P2', content='A') post3 = LinkedPost(title='P3', content='A') post4 = LinkedPost(title='B1', content='A') post5 = LinkedPost(title='B2', content='A') post6 = LinkedPost(title='B3', content='A') author.posts = [post1, post2, post3, post4, post5, post6] author.save() author = LinkedAuthor(name='Khui Tsia Koi Tsuang Kai Low Rider') post1 = LinkedPost(title='P1', content='B') post2 = LinkedPost(title='P2', content='B') post3 = LinkedPost(title='P3', content='B') post4 = LinkedPost(title='B1', content='B') post5 = LinkedPost(title='B2', content='B') post6 = LinkedPost(title='B3', content='B') author.posts = [post1, post2, post3, post4, post5, post6] author.save() results = LinkedAuthor.find() \ .include('posts', LinkedPost.find(title={'$regex': 'B'}))\ .exec() self.assertEqual(len(results), 2) self.assertEqual(len(results[0].posts), 3) self.assertEqual(len(results[1].posts), 3) self.assertEqual(results[0].posts[0].title, 'B1') self.assertEqual(results[0].posts[1].title, 'B2') self.assertEqual(results[0].posts[2].title, 'B3') self.assertEqual(results[1].posts[0].title, 'B1') self.assertEqual(results[1].posts[1].title, 'B2') self.assertEqual(results[1].posts[2].title, 'B3')
def test_fetched_objects_are_unmodified_1_many(self): author = LinkedAuthor(name='Me') post1 = LinkedPost(title='P1', content='A') post2 = LinkedPost(title='P2', content='A') author.posts = [post1, post2] author.save() authors = LinkedAuthor.find().include('posts').exec() a = authors[0] p1 = a.posts[0] p2 = a.posts[1] self.assertEqual(a.is_new, False) self.assertEqual(a.is_modified, False) self.assertEqual(a.modified_fields, ()) self.assertEqual(p1.is_new, False) self.assertEqual(p1.is_modified, False) self.assertEqual(p1.modified_fields, ()) self.assertEqual(p2.is_new, False) self.assertEqual(p2.is_modified, False) self.assertEqual(p2.modified_fields, ())