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_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_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_set_local_key_to_unset_is_saved(self): post = LinkedPost(title='P1', content='P2') author = LinkedAuthor(name='A1') post.author = author post.save() post.author_id = None post.save() collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertNotIn('authorId', item)
def test_alter_local_key_is_saved(self): post = LinkedPost(title='P1', content='P2') a1 = LinkedAuthor(name='A1') post.author = a1 post.save() a2 = LinkedAuthor(name='A2') post.author_id = a2.id post.save() collection = Connection.get_collection(LinkedPost) for item in collection.find({}): self.assertEqual(item['authorId'], ObjectId(a2.id))
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_many_1_unlink_unset_is_saved(self): author = LinkedAuthor(name='A') post = LinkedPost(title='T', content='C') post.author = author post.save() post.author_id = None post.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_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()
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_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')