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_1_many_ref_uses_unique_objects(self): post1 = LinkedPost(title='P1', content='A') post2 = LinkedPost(title='P2', content='A') author = LinkedAuthor(name='A1', posts=[post1, post2]) author.save() posts = LinkedPost.find().include('author').exec() p1 = posts[0] p2 = posts[1] self.assertIs(p1.author, p2.author) p1.author.name = 'New Name' p1.save() self.assertEqual(p2.author.name, 'New Name')
def test_fetched_objects_are_unmodified_many_1(self): author = LinkedAuthor(name='Me') post1 = LinkedPost(title='P1', content='A') post2 = LinkedPost(title='P2', content='A') author.posts = [post1, post2] author.save() posts = LinkedPost.find().include('author').exec() self.assertEqual(posts[0].is_new, False) self.assertEqual(posts[0].is_modified, False) self.assertEqual(posts[0].modified_fields, ()) self.assertEqual(posts[1].is_new, False) self.assertEqual(posts[1].is_modified, False) self.assertEqual(posts[1].modified_fields, ()) self.assertEqual(posts[0].author.is_new, False) self.assertEqual(posts[0].author.is_modified, False) self.assertEqual(posts[0].author.modified_fields, ()) self.assertEqual(posts[0].author, posts[1].author)
def test_many_1_ref_lookup_fetches_linked_object(self): author = LinkedAuthor(name='A1') 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='A2') 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 = LinkedPost.find(title='P1').include('author').exec() self.assertEqual(results[0].author.name, 'A1') self.assertEqual(results[1].author.name, 'A2')