summaryrefslogtreecommitdiff
path: root/tests/get_or_create/tests.py
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@gmail.com>2014-10-08 05:11:09 +0700
committerLoic Bistuer <loic.bistuer@gmail.com>2014-10-08 05:11:09 +0700
commit0eccf8fbea143234c9af3d290d41d5c315e714f9 (patch)
tree23de5765c575664856a3478774e1fc23a79ed33a /tests/get_or_create/tests.py
parent8a4e9be3e561c3532bc06d63491736b075a35b2a (diff)
downloaddjango-0eccf8fbea143234c9af3d290d41d5c315e714f9.tar.gz
Fixed misplaced test case.
Diffstat (limited to 'tests/get_or_create/tests.py')
-rw-r--r--tests/get_or_create/tests.py118
1 files changed, 59 insertions, 59 deletions
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index 56339a2556..33ac1317a5 100644
--- a/tests/get_or_create/tests.py
+++ b/tests/get_or_create/tests.py
@@ -66,6 +66,65 @@ class GetOrCreateTests(TestCase):
Person.objects.get_or_create, first_name="Tom", last_name="Smith"
)
+ def test_get_or_create_on_related_manager(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)
+ # The 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)
+
+ # Use the publisher's primary key value instead of a model instance.
+ _, created = ed.books.get_or_create(name='The Great Book of Ed', publisher_id=p.id)
+ self.assertTrue(created)
+
+ # Try get_or_create again, this time nothing should be created.
+ _, created = ed.books.get_or_create(name='The Great Book of Ed', publisher_id=p.id)
+ self.assertFalse(created)
+
+ # The publisher should have three books.
+ self.assertEqual(p.books.count(), 3)
+
class GetOrCreateTestsWithManualPKs(TestCase):
@@ -289,62 +348,3 @@ class UpdateOrCreateTests(TestCase):
self.assertFalse(created)
self.assertEqual(book.name, name)
self.assertEqual(author.books.count(), 1)
-
- 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)
- # The 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)
-
- # Use the publisher's primary key value instead of a model instance.
- _, created = ed.books.get_or_create(name='The Great Book of Ed', publisher_id=p.id)
- self.assertTrue(created)
-
- # Try get_or_create again, this time nothing should be created.
- _, created = ed.books.get_or_create(name='The Great Book of Ed', publisher_id=p.id)
- self.assertFalse(created)
-
- # The publisher should have three books.
- self.assertEqual(p.books.count(), 3)