Author: Alex Date: 2010-10-11 20:54:48 -0500 (Mon, 11 Oct 2010) New Revision: 14178
Added: django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/tests.py Modified: django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/models.py Log: [1.2.X] Converted get_or_create_regress tests from doctests to unittests. We have always been at war with doctests. Backport of [14177]. Modified: django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/models.py =================================================================== --- django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/models.py 2010-10-12 01:54:19 UTC (rev 14177) +++ django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/models.py 2010-10-12 01:54:48 UTC (rev 14178) @@ -1,5 +1,6 @@ from django.db import models + class Publisher(models.Model): name = models.CharField(max_length=100) @@ -10,82 +11,3 @@ name = models.CharField(max_length=100) authors = models.ManyToManyField(Author, related_name='books') publisher = models.ForeignKey(Publisher, related_name='books') - - -__test__ = {'one':""" -# -# RelatedManager -# - -# First create a Publisher. ->>> p = Publisher.objects.create(name='Acme Publishing') - -# Create a book through the publisher. ->>> book, created = p.books.get_or_create(name='The Book of Ed & Fred') ->>> created -True - -# The publisher should have one book. ->>> p.books.count() -1 - -# Try get_or_create again, this time nothing should be created. ->>> book, created = p.books.get_or_create(name='The Book of Ed & Fred') ->>> created -False - -# And the publisher should still have one book. ->>> p.books.count() -1 - -# -# ManyRelatedManager -# - -# Add an author to the book. ->>> ed, created = book.authors.get_or_create(name='Ed') ->>> created -True - -# Book should have one author. ->>> book.authors.count() -1 - -# Try get_or_create again, this time nothing should be created. ->>> ed, created = book.authors.get_or_create(name='Ed') ->>> created -False - -# And the book should still have one author. ->>> book.authors.count() -1 - -# Add a second author to the book. ->>> fred, created = book.authors.get_or_create(name='Fred') ->>> created -True - -# The book should have two authors now. ->>> book.authors.count() -2 - -# Create an Author not tied to any books. ->>> Author.objects.create(name='Ted') -<Author: Author object> - -# There should be three Authors in total. The book object should have two. ->>> Author.objects.count() -3 ->>> book.authors.count() -2 - -# Try creating a book through an author. ->>> ed.books.get_or_create(name="Ed's Recipies", publisher=p) -(<Book: Book object>, True) - -# Now Ed has two Books, Fred just one. ->>> ed.books.count() -2 ->>> fred.books.count() -1 -"""} Added: django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/tests.py =================================================================== --- django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/tests.py (rev 0) +++ django/branches/releases/1.2.X/tests/regressiontests/get_or_create_regress/tests.py 2010-10-12 01:54:48 UTC (rev 14178) @@ -0,0 +1,53 @@ +from django.test import TestCase + +from models import Author, Publisher + + +class GetOrCreateTests(TestCase): + def test_related(self): + p = Publisher.objects.create(name="Acme Publishing") + # Create a book through the publisher. + book, created = p.books.get_or_create(name="The Book of Ed & Fred") + self.assertTrue(created) + # The publisher should have one book. + self.assertEqual(p.books.count(), 1) + + # Try get_or_create again, this time nothing should be created. + book, created = p.books.get_or_create(name="The Book of Ed & Fred") + self.assertFalse(created) + # And the publisher should still have one book. + self.assertEqual(p.books.count(), 1) + + # Add an author to the book. + ed, created = book.authors.get_or_create(name="Ed") + self.assertTrue(created) + # Book should have one author. + self.assertEqual(book.authors.count(), 1) + + # Try get_or_create again, this time nothing should be created. + ed, created = book.authors.get_or_create(name="Ed") + self.assertFalse(created) + # And the book should still have one author. + self.assertEqual(book.authors.count(), 1) + + # Add a second author to the book. + fred, created = book.authors.get_or_create(name="Fred") + self.assertTrue(created) + + # The book should have two authors now. + self.assertEqual(book.authors.count(), 2) + + # Create an Author not tied to any books. + Author.objects.create(name="Ted") + + # There should be three Authors in total. The book object should have two. + self.assertEqual(Author.objects.count(), 3) + self.assertEqual(book.authors.count(), 2) + + # Try creating a book through an author. + _, created = ed.books.get_or_create(name="Ed's Recipes", publisher=p) + self.assertTrue(created) + + # Now Ed has two Books, Fred just one. + self.assertEqual(ed.books.count(), 2) + self.assertEqual(fred.books.count(), 1) -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.