def test_ignored_relations(self): balys_sruoga = Reviewer.objects.create(name='Balys Sruoga') george_orwell = Author.objects.create(name='George Orwell') charles_dickens = Author.objects.create(name='Charles Dickens') rel_article = Article( title='Round and round wherever', authors=[george_orwell], ) article = Article( title='Down and Out in Paris and London', authors=[george_orwell, charles_dickens], reviewer=balys_sruoga, related_articles=[rel_article], ) article_serialised = article.serializable_data() # check that reviewer and related_articles are not serialized (marked with serialize=False) self.assertNotIn('reviewer', article_serialised) self.assertNotIn('related_articles', article_serialised) rel_article.save() article.save() article_json = article.to_json() restored_article = Article.from_json(article_json) restored_article.save() restored_article = Article.objects.get(pk=restored_article.pk) # check that reviewer and related_articles haven't been touched self.assertEqual(balys_sruoga, restored_article.reviewer) self.assertIn(rel_article, restored_article.related_articles.all())
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 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'] )
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 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'])
class ParentalM2MTest(TestCase): def setUp(self): self.article = Article(title="Test Title") self.author_1 = Author.objects.create(name="Author 1") self.author_2 = Author.objects.create(name="Author 2") self.article.authors = [self.author_1, self.author_2] self.category_1 = Category.objects.create(name="Category 1") self.category_2 = Category.objects.create(name="Category 2") self.article.categories = [self.category_1, self.category_2] def test_uninitialised_m2m_relation(self): # Reading an m2m relation of a newly created object should return an empty queryset new_article = Article(title="Test title") self.assertEqual([], list(new_article.authors.all())) self.assertEqual(new_article.authors.count(), 0) # the manager should have a 'model' property pointing to the target model self.assertEqual(Author, new_article.authors.model) def test_parentalm2mfield(self): # Article should not exist in the database yet self.assertFalse(Article.objects.filter(title='Test Title').exists()) # Test lookup on parental M2M relation self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')]) self.assertEqual(self.article.authors.count(), 2) # the manager should have a 'model' property pointing to the target model self.assertEqual(Author, self.article.authors.model) # Test adding to the relation author_3 = Author.objects.create(name="Author 3") self.article.authors.add(author_3) self.assertEqual(['Author 1', 'Author 2', 'Author 3'], [ author.name for author in self.article.authors.all().order_by('name') ]) self.assertEqual(self.article.authors.count(), 3) # Test removing from the relation self.article.authors.remove(author_3) self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')]) self.assertEqual(self.article.authors.count(), 2) # Test clearing the relation self.article.authors.clear() self.assertEqual( [], [author.name for author in self.article.authors.order_by('name')]) self.assertEqual(self.article.authors.count(), 0) # Test the 'set' operation self.article.authors.set([self.author_2]) self.assertEqual(self.article.authors.count(), 1) self.assertEqual( ['Author 2'], [author.name for author in self.article.authors.order_by('name')]) # Test saving to / restoring from DB self.article.authors = [self.author_1, self.author_2] self.article.save() self.article = Article.objects.get(title="Test Title") self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')]) self.assertEqual(self.article.authors.count(), 2) def test_constructor(self): # Test passing values for M2M relations as kwargs to the constructor article2 = Article( title="Test article 2", authors=[self.author_1], categories=[self.category_2], ) self.assertEqual( ['Author 1'], [author.name for author in article2.authors.order_by('name')]) self.assertEqual(article2.authors.count(), 1) 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_save_m2m_with_update_fields(self): self.article.save() # modify both relations, but only commit the change to authors self.article.authors.clear() self.article.categories.clear() self.article.title = 'Updated title' self.article.save(update_fields=['title', 'authors']) self.updated_article = Article.objects.get(pk=self.article.pk) self.assertEqual(self.updated_article.title, 'Updated title') self.assertEqual(self.updated_article.authors.count(), 0) self.assertEqual(self.updated_article.categories.count(), 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']) def test_value_from_object(self): authors_field = Article._meta.get_field('authors') self.assertEqual(set(authors_field.value_from_object(self.article)), set([self.author_1, self.author_2])) self.article.save() self.assertEqual(set(authors_field.value_from_object(self.article)), set([self.author_1, self.author_2]))
class ParentalM2MTest(TestCase): def setUp(self): self.article = Article(title="Test Title") self.author_1 = Author.objects.create(name="Author 1") self.author_2 = Author.objects.create(name="Author 2") self.article.authors = [self.author_1, self.author_2] self.category_1 = Category.objects.create(name="Category 1") self.category_2 = Category.objects.create(name="Category 2") self.article.categories = [self.category_1, self.category_2] def test_uninitialised_m2m_relation(self): # Reading an m2m relation of a newly created object should return an empty queryset new_article = Article(title="Test title") self.assertEqual([], list(new_article.authors.all())) self.assertEqual(new_article.authors.count(), 0) # the manager should have a 'model' property pointing to the target model self.assertEqual(Author, new_article.authors.model) def test_parentalm2mfield(self): # Article should not exist in the database yet self.assertFalse(Article.objects.filter(title='Test Title').exists()) # Test lookup on parental M2M relation self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')] ) self.assertEqual(self.article.authors.count(), 2) # the manager should have a 'model' property pointing to the target model self.assertEqual(Author, self.article.authors.model) # Test adding to the relation author_3 = Author.objects.create(name="Author 3") self.article.authors.add(author_3) self.assertEqual( ['Author 1', 'Author 2', 'Author 3'], [author.name for author in self.article.authors.all().order_by('name')] ) self.assertEqual(self.article.authors.count(), 3) # Test removing from the relation self.article.authors.remove(author_3) self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')] ) self.assertEqual(self.article.authors.count(), 2) # Test clearing the relation self.article.authors.clear() self.assertEqual( [], [author.name for author in self.article.authors.order_by('name')] ) self.assertEqual(self.article.authors.count(), 0) # Test the 'set' operation self.article.authors.set([self.author_2]) self.assertEqual(self.article.authors.count(), 1) self.assertEqual( ['Author 2'], [author.name for author in self.article.authors.order_by('name')] ) # Test saving to / restoring from DB self.article.authors = [self.author_1, self.author_2] self.article.save() self.article = Article.objects.get(title="Test Title") self.assertEqual( ['Author 1', 'Author 2'], [author.name for author in self.article.authors.order_by('name')] ) self.assertEqual(self.article.authors.count(), 2) def test_constructor(self): # Test passing values for M2M relations as kwargs to the constructor article2 = Article( title="Test article 2", authors=[self.author_1], categories=[self.category_2], ) self.assertEqual( ['Author 1'], [author.name for author in article2.authors.order_by('name')] ) self.assertEqual(article2.authors.count(), 1) 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_save_m2m_with_update_fields(self): self.article.save() # modify both relations, but only commit the change to authors self.article.authors.clear() self.article.categories.clear() self.article.title = 'Updated title' self.article.save(update_fields=['title', 'authors']) self.updated_article = Article.objects.get(pk=self.article.pk) self.assertEqual(self.updated_article.title, 'Updated title') self.assertEqual(self.updated_article.authors.count(), 0) self.assertEqual(self.updated_article.categories.count(), 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'] ) def test_value_from_object(self): authors_field = Article._meta.get_field('authors') self.assertEqual( set(authors_field.value_from_object(self.article)), set([self.author_1, self.author_2]) ) self.article.save() self.assertEqual( set(authors_field.value_from_object(self.article)), set([self.author_1, self.author_2]) )