Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted get_or_create_regress tests from doctests to unittests. We …

…have always been at war with doctests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14177 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d030707864f11c5a2e5652b40d275558eae12f7 1 parent 9235d24
Alex Gaynor authored October 12, 2010
80  tests/regressiontests/get_or_create_regress/models.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.db import models
2 2
 
  3
+
3 4
 class Publisher(models.Model):
4 5
     name = models.CharField(max_length=100)
5 6
 
@@ -10,82 +11,3 @@ class Book(models.Model):
10 11
     name = models.CharField(max_length=100)
11 12
     authors = models.ManyToManyField(Author, related_name='books')
12 13
     publisher = models.ForeignKey(Publisher, related_name='books')
13  
-
14  
-
15  
-__test__ = {'one':"""
16  
-#
17  
-# RelatedManager
18  
-#
19  
-
20  
-# First create a Publisher.
21  
->>> p = Publisher.objects.create(name='Acme Publishing')
22  
-
23  
-# Create a book through the publisher.
24  
->>> book, created = p.books.get_or_create(name='The Book of Ed & Fred')
25  
->>> created
26  
-True
27  
-
28  
-# The publisher should have one book.
29  
->>> p.books.count()
30  
-1
31  
-
32  
-# Try get_or_create again, this time nothing should be created.
33  
->>> book, created = p.books.get_or_create(name='The Book of Ed & Fred')
34  
->>> created
35  
-False
36  
-
37  
-# And the publisher should still have one book.
38  
->>> p.books.count()
39  
-1
40  
-
41  
-#
42  
-# ManyRelatedManager
43  
-#
44  
-
45  
-# Add an author to the book.
46  
->>> ed, created = book.authors.get_or_create(name='Ed')
47  
->>> created
48  
-True
49  
-
50  
-# Book should have one author.
51  
->>> book.authors.count()
52  
-1
53  
-
54  
-# Try get_or_create again, this time nothing should be created.
55  
->>> ed, created = book.authors.get_or_create(name='Ed')
56  
->>> created
57  
-False
58  
-
59  
-# And the book should still have one author.
60  
->>> book.authors.count()
61  
-1
62  
-
63  
-# Add a second author to the book.
64  
->>> fred, created = book.authors.get_or_create(name='Fred')
65  
->>> created
66  
-True
67  
-
68  
-# The book should have two authors now.
69  
->>> book.authors.count()
70  
-2
71  
-
72  
-# Create an Author not tied to any books.
73  
->>> Author.objects.create(name='Ted')
74  
-<Author: Author object>
75  
-
76  
-# There should be three Authors in total. The book object should have two.
77  
->>> Author.objects.count()
78  
-3
79  
->>> book.authors.count()
80  
-2
81  
-
82  
-# Try creating a book through an author.
83  
->>> ed.books.get_or_create(name="Ed's Recipies", publisher=p)
84  
-(<Book: Book object>, True)
85  
-
86  
-# Now Ed has two Books, Fred just one.
87  
->>> ed.books.count()
88  
-2
89  
->>> fred.books.count()
90  
-1
91  
-"""}
53  tests/regressiontests/get_or_create_regress/tests.py
... ...
@@ -0,0 +1,53 @@
  1
+from django.test import TestCase
  2
+
  3
+from models import Author, Publisher
  4
+
  5
+
  6
+class GetOrCreateTests(TestCase):
  7
+    def test_related(self):
  8
+        p = Publisher.objects.create(name="Acme Publishing")
  9
+        # Create a book through the publisher.
  10
+        book, created = p.books.get_or_create(name="The Book of Ed & Fred")
  11
+        self.assertTrue(created)
  12
+        # The publisher should have one book.
  13
+        self.assertEqual(p.books.count(), 1)
  14
+
  15
+        # Try get_or_create again, this time nothing should be created.
  16
+        book, created = p.books.get_or_create(name="The Book of Ed & Fred")
  17
+        self.assertFalse(created)
  18
+        # And the publisher should still have one book.
  19
+        self.assertEqual(p.books.count(), 1)
  20
+
  21
+        # Add an author to the book.
  22
+        ed, created = book.authors.get_or_create(name="Ed")
  23
+        self.assertTrue(created)
  24
+        # Book should have one author.
  25
+        self.assertEqual(book.authors.count(), 1)
  26
+
  27
+        # Try get_or_create again, this time nothing should be created.
  28
+        ed, created = book.authors.get_or_create(name="Ed")
  29
+        self.assertFalse(created)
  30
+        # And the book should still have one author.
  31
+        self.assertEqual(book.authors.count(), 1)
  32
+
  33
+        # Add a second author to the book.
  34
+        fred, created = book.authors.get_or_create(name="Fred")
  35
+        self.assertTrue(created)
  36
+
  37
+        # The book should have two authors now.
  38
+        self.assertEqual(book.authors.count(), 2)
  39
+
  40
+        # Create an Author not tied to any books.
  41
+        Author.objects.create(name="Ted")
  42
+
  43
+        # There should be three Authors in total. The book object should have two.
  44
+        self.assertEqual(Author.objects.count(), 3)
  45
+        self.assertEqual(book.authors.count(), 2)
  46
+
  47
+        # Try creating a book through an author.
  48
+        _, created = ed.books.get_or_create(name="Ed's Recipes", publisher=p)
  49
+        self.assertTrue(created)
  50
+
  51
+        # Now Ed has two Books, Fred just one.
  52
+        self.assertEqual(ed.books.count(), 2)
  53
+        self.assertEqual(fred.books.count(), 1)

0 notes on commit 2d03070

Please sign in to comment.
Something went wrong with that request. Please try again.