From 0ef3e86eef23aea0d2e82e115139d913a8c092cc Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 12 Oct 2010 00:55:55 +0000 Subject: [PATCH] Migrated many_to_one_null doctests. Thanks to George Sakkis for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14166 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/many_to_one_null/models.py | 105 -------------------- tests/modeltests/many_to_one_null/tests.py | 84 ++++++++++++++++ 2 files changed, 84 insertions(+), 105 deletions(-) create mode 100644 tests/modeltests/many_to_one_null/tests.py diff --git a/tests/modeltests/many_to_one_null/models.py b/tests/modeltests/many_to_one_null/models.py index cee0e21a72..5f824b4d21 100644 --- a/tests/modeltests/many_to_one_null/models.py +++ b/tests/modeltests/many_to_one_null/models.py @@ -22,108 +22,3 @@ class Article(models.Model): def __unicode__(self): return self.headline - -__test__ = {'API_TESTS':""" -# Create a Reporter. ->>> r = Reporter(name='John Smith') ->>> r.save() - -# Create an Article. ->>> a = Article(headline="First", reporter=r) ->>> a.save() - ->>> a.reporter.id -1 - ->>> a.reporter - - -# Article objects have access to their related Reporter objects. ->>> r = a.reporter - -# Create an Article via the Reporter object. ->>> a2 = r.article_set.create(headline="Second") ->>> a2 - ->>> a2.reporter.id -1 - -# Reporter objects have access to their related Article objects. ->>> r.article_set.all() -[, ] ->>> r.article_set.filter(headline__startswith='Fir') -[] ->>> r.article_set.count() -2 - -# Create an Article with no Reporter by passing "reporter=None". ->>> a3 = Article(headline="Third", reporter=None) ->>> a3.save() ->>> a3.id -3 ->>> print a3.reporter -None - -# Need to reget a3 to refresh the cache ->>> a3 = Article.objects.get(pk=3) ->>> print a3.reporter.id -Traceback (most recent call last): - ... -AttributeError: 'NoneType' object has no attribute 'id' - -# Accessing an article's 'reporter' attribute returns None -# if the reporter is set to None. ->>> print a3.reporter -None - -# To retrieve the articles with no reporters set, use "reporter__isnull=True". ->>> Article.objects.filter(reporter__isnull=True) -[] - -# We can achieve the same thing by filtering for the case where the reporter is -# None. ->>> Article.objects.filter(reporter=None) -[] - -# Set the reporter for the Third article ->>> r.article_set.add(a3) ->>> r.article_set.all() -[, , ] - -# Remove an article from the set, and check that it was removed. ->>> r.article_set.remove(a3) ->>> r.article_set.all() -[, ] ->>> Article.objects.filter(reporter__isnull=True) -[] - -# Create another article and reporter ->>> r2 = Reporter(name='Paul Jones') ->>> r2.save() ->>> a4 = r2.article_set.create(headline='Fourth') ->>> r2.article_set.all() -[] - -# Try to remove a4 from a set it does not belong to ->>> r.article_set.remove(a4) -Traceback (most recent call last): -... -DoesNotExist: is not related to . - ->>> r2.article_set.all() -[] - -# Use descriptor assignment to allocate ForeignKey. Null is legal, so -# existing members of set that are not in the assignment set are set null ->>> r2.article_set = [a2, a3] ->>> r2.article_set.all() -[, ] - -# Clear the rest of the set ->>> r.article_set.clear() ->>> r.article_set.all() -[] ->>> Article.objects.filter(reporter__isnull=True) -[, ] - -"""} diff --git a/tests/modeltests/many_to_one_null/tests.py b/tests/modeltests/many_to_one_null/tests.py new file mode 100644 index 0000000000..6b7b0a7413 --- /dev/null +++ b/tests/modeltests/many_to_one_null/tests.py @@ -0,0 +1,84 @@ +from django.test import TestCase +from models import Reporter, Article + +class ManyToOneNullTests(TestCase): + + def setUp(self): + # Create a Reporter. + self.r = Reporter(name='John Smith') + self.r.save() + # Create an Article. + self.a = Article(headline="First", reporter=self.r) + self.a.save() + # Create an Article via the Reporter object. + self.a2 = self.r.article_set.create(headline="Second") + # Create an Article with no Reporter by passing "reporter=None". + self.a3 = Article(headline="Third", reporter=None) + self.a3.save() + # Create another article and reporter + self.r2 = Reporter(name='Paul Jones') + self.r2.save() + self.a4 = self.r2.article_set.create(headline='Fourth') + + def test_get_related(self): + self.assertEqual(self.a.reporter.id, self.r.id) + # Article objects have access to their related Reporter objects. + r = self.a.reporter + self.assertEqual(r.id, self.r.id) + + def test_created_via_related_set(self): + self.assertEqual(self.a2.reporter.id, self.r.id) + + def test_related_set(self): + # Reporter objects have access to their related Article objects. + self.assertQuerysetEqual(self.r.article_set.all(), + ['', '']) + self.assertQuerysetEqual(self.r.article_set.filter(headline__startswith='Fir'), + ['']) + self.assertEqual(self.r.article_set.count(), 2) + + def test_created_without_related(self): + self.assertEqual(self.a3.reporter, None) + # Need to reget a3 to refresh the cache + a3 = Article.objects.get(pk=3) + self.assertRaises(AttributeError, getattr, a3.reporter, 'id') + # Accessing an article's 'reporter' attribute returns None + # if the reporter is set to None. + self.assertEqual(a3.reporter, None) + # To retrieve the articles with no reporters set, use "reporter__isnull=True". + self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), + ['']) + # We can achieve the same thing by filtering for the case where the + # reporter is None. + self.assertQuerysetEqual(Article.objects.filter(reporter=None), + ['']) + # Set the reporter for the Third article + self.assertQuerysetEqual(self.r.article_set.all(), + ['', '']) + self.r.article_set.add(a3) + self.assertQuerysetEqual(self.r.article_set.all(), + ['', '', '']) + # Remove an article from the set, and check that it was removed. + self.r.article_set.remove(a3) + self.assertQuerysetEqual(self.r.article_set.all(), + ['', '']) + self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), + ['']) + + def test_remove_from_wrong_set(self): + self.assertQuerysetEqual(self.r2.article_set.all(), ['']) + # Try to remove a4 from a set it does not belong to + self.assertRaises(Reporter.DoesNotExist, self.r.article_set.remove, self.a4) + self.assertQuerysetEqual(self.r2.article_set.all(), ['']) + + def test_assign_clear_related_set(self): + # Use descriptor assignment to allocate ForeignKey. Null is legal, so + # existing members of set that are not in the assignment set are set null + self.r2.article_set = [self.a2, self.a3] + self.assertQuerysetEqual(self.r2.article_set.all(), + ['', '']) + # Clear the rest of the set + self.r.article_set.clear() + self.assertQuerysetEqual(self.r.article_set.all(), []) + self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), + ['', ''])