def test_parentalm2mfield(self): article = Article(title="Test Title") author_1 = Author(name="Author 1") author_2 = Author(name="Author 2") article.authors = [author_1, author_2] category_1 = Category(name="Category 1") category_2 = Category(name="Category 2") article.categories = [category_1, category_2] self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in article.authors.all().order_by('name')] ) self.assertEqual(article.authors.count(), 2) author_3 = Author(name="Author 3") article.authors.add(author_3) self.assertEqual( ['Author 1', 'Author 2', 'Author 3'], [author.name for author in article.authors.all().order_by('name')] ) self.assertEqual(article.authors.count(), 3) article.authors.remove(author_3) self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in article.authors.all().order_by('name')] ) self.assertEqual(article.authors.count(), 2) article.authors.clear() self.assertEqual( [], [author.name for author in article.authors.all().order_by('name')] ) self.assertEqual(article.authors.count(), 0) article.authors = [author_1, author_2] article.save() article = Article.objects.get(title="Test Title") self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in article.authors.all().order_by('name')] ) self.assertEqual(article.authors.count(), 2)
def setUp(self): # Create 10 articles with 10 authors each. authors = Author.objects.bulk_create( Author(id=i, name=str(i)) for i in range(10)) authors = Author.objects.all() for i in range(10): article = Article(title=str(i)) article.authors = authors article.save()
def test_reverse_m2m_field(self): article = Article(title="Test Title") author_1 = Author(name="Author 1") author_2 = Author(name="Author 2") article.authors = [author_1, author_2] category_1 = Category(name="Category 1") category_2 = Category(name="Category 2") article.categories = [category_1, category_2] article.save() author_1 = Author.objects.get(name="Author 1") self.assertEqual(author_1.articles_by_author.all().count(), 1) self.assertEqual(author_1.articles_by_author.get(), Article.objects.filter(title="Test Title").get()) article_2 = Article(title="Test Title 2") article_2.authors = [author_1] article_2.save() author_1 = Author.objects.get(name="Author 1") self.assertEqual(author_1.articles_by_author.all().count(), 2) self.assertEqual(list(author_1.articles_by_author.values_list('title', flat=True)), ['Test Title', 'Test Title 2'])
def test_ordering(self): # our fake querysets should respect the ordering defined on the target model bela_bartok = Author.objects.create(name='Bela Bartok') graham_greene = Author.objects.create(name='Graham Greene') janis_joplin = Author.objects.create(name='Janis Joplin') simon_sharma = Author.objects.create(name='Simon Sharma') william_wordsworth = Author.objects.create(name='William Wordsworth') article3 = Article(title="Test article 3") article3.authors = [ janis_joplin, william_wordsworth, bela_bartok, simon_sharma, graham_greene ] self.assertEqual( list(article3.authors.all()), [bela_bartok, graham_greene, janis_joplin, simon_sharma, william_wordsworth] )
def test_reverse_m2m_field(self): # article is unsaved, so should not be returned by the reverse relation on author self.assertEqual(self.author_1.articles_by_author.count(), 0) self.article.save() # should now be able to look up on the reverse relation self.assertEqual(self.author_1.articles_by_author.count(), 1) self.assertEqual(self.author_1.articles_by_author.get(), self.article) article_2 = Article(title="Test Title 2") article_2.authors = [self.author_1] article_2.save() self.assertEqual(self.author_1.articles_by_author.all().count(), 2) self.assertEqual( list( self.author_1.articles_by_author.order_by('title').values_list( 'title', flat=True)), ['Test Title', 'Test Title 2'])
def test_reverse_m2m_field(self): # article is unsaved, so should not be returned by the reverse relation on author self.assertEqual(self.author_1.articles_by_author.count(), 0) self.article.save() # should now be able to look up on the reverse relation self.assertEqual(self.author_1.articles_by_author.count(), 1) self.assertEqual(self.author_1.articles_by_author.get(), self.article) article_2 = Article(title="Test Title 2") article_2.authors = [self.author_1] article_2.save() self.assertEqual(self.author_1.articles_by_author.all().count(), 2) self.assertEqual( list(self.author_1.articles_by_author.order_by('title').values_list('title', flat=True)), ['Test Title', 'Test Title 2'] )