summaryrefslogtreecommitdiff
path: root/tests/get_or_create
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-04-21 12:12:09 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-04-21 12:12:09 +0200
commit25b2ce896b93982e5e89931625e20349fb815fda (patch)
tree9d9c5417a9bacaaa71837c9086dd9ac04116d584 /tests/get_or_create
parent3f01e82c591b880faa5066ba28a3f11b2b82154c (diff)
Consolidated get_or_create tests.
Diffstat (limited to 'tests/get_or_create')
-rw-r--r--tests/get_or_create/models.py22
-rw-r--r--tests/get_or_create/tests.py62
2 files changed, 75 insertions, 9 deletions
diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py
index 1a85de2e74..a8e9a3f3f9 100644
--- a/tests/get_or_create/models.py
+++ b/tests/get_or_create/models.py
@@ -1,11 +1,3 @@
-"""
-33. get_or_create()
-
-``get_or_create()`` does what it says: it tries to look up an object with the
-given parameters. If an object isn't found, it creates one with the given
-parameters.
-"""
-
from __future__ import unicode_literals
from django.db import models
@@ -42,3 +34,17 @@ class Tag(models.Model):
class Thing(models.Model):
name = models.CharField(max_length=256)
tags = models.ManyToManyField(Tag)
+
+
+class Publisher(models.Model):
+ name = models.CharField(max_length=100)
+
+
+class Author(models.Model):
+ name = models.CharField(max_length=100)
+
+
+class Book(models.Model):
+ name = models.CharField(max_length=100)
+ authors = models.ManyToManyField(Author, related_name='books')
+ publisher = models.ForeignKey(Publisher, related_name='books', db_column="publisher_id_column")
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index 3e70bdd482..d4dc961a18 100644
--- a/tests/get_or_create/tests.py
+++ b/tests/get_or_create/tests.py
@@ -8,7 +8,8 @@ from django.db import IntegrityError, DatabaseError
from django.utils.encoding import DjangoUnicodeDecodeError
from django.test import TestCase, TransactionTestCase
-from .models import DefaultPerson, Person, ManualPrimaryKeyTest, Profile, Tag, Thing
+from .models import (DefaultPerson, Person, ManualPrimaryKeyTest, Profile,
+ Tag, Thing, Publisher, Author)
class GetOrCreateTests(TestCase):
@@ -237,3 +238,62 @@ class UpdateOrCreateTests(TestCase):
except IntegrityError:
formatted_traceback = traceback.format_exc()
self.assertIn('obj.save', formatted_traceback)
+
+ 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)