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_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_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_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_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_1_many_unlink_is_saved(self): author = LinkedAuthor(name='A') post = LinkedPost(title='T', content='C') author.posts.append(post) author.save() author.posts.remove(post) author.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_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_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, ())
def test_linked_object_is_not_new_after_saved(self): input = { 'name': 'Ti', 'posts': [{ 'title': 'Bo Lo Iong', 'content': 'Pieng Iu' }, { 'title': 'Bo Lo Iong', 'content': 'Pieng Iu' }] } author = LinkedAuthor(**input) self.assertEqual(author.is_new, True) self.assertEqual(author.posts[0].is_new, True) self.assertEqual(author.posts[1].is_new, True) author.save() self.assertEqual(author.is_new, False) self.assertEqual(author.posts[0].is_new, False) self.assertEqual(author.posts[1].is_new, False)
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')
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_encode_new_object_correctly_with_existing_objects_1_many(self): author = LinkedAuthor(name='A') post = LinkedPost(title='P', content='P') author.save() post.author = author post.save()