diff options
author | Loic Bistuer <loic.bistuer@gmail.com> | 2014-10-08 03:27:31 +0700 |
---|---|---|
committer | Loic Bistuer <loic.bistuer@gmail.com> | 2014-10-08 03:27:31 +0700 |
commit | ed37f7e979186c99a6f351c289eb486461601d80 (patch) | |
tree | 1ad209c9be85ddc07eba5ae3cd956b8cd44e1223 /tests/get_or_create | |
parent | c1ef234e310fb984ced37eac8c8b90f5b6155e5f (diff) | |
download | django-ed37f7e979186c99a6f351c289eb486461601d80.tar.gz |
Fixed #23611 -- update_or_create failing from a related manager
Added update_or_create to RelatedManager, ManyRelatedManager and
GenericRelatedObjectManager.
Added missing get_or_create to GenericRelatedObjectManager.
Diffstat (limited to 'tests/get_or_create')
-rw-r--r-- | tests/get_or_create/tests.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index d4dc961a18..56339a2556 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -9,7 +9,7 @@ from django.utils.encoding import DjangoUnicodeDecodeError from django.test import TestCase, TransactionTestCase from .models import (DefaultPerson, Person, ManualPrimaryKeyTest, Profile, - Tag, Thing, Publisher, Author) + Tag, Thing, Publisher, Author, Book) class GetOrCreateTests(TestCase): @@ -239,6 +239,57 @@ class UpdateOrCreateTests(TestCase): formatted_traceback = traceback.format_exc() self.assertIn('obj.save', formatted_traceback) + def test_create_with_related_manager(self): + """ + Should be able to use update_or_create from the related manager to + create a book. Refs #23611. + """ + p = Publisher.objects.create(name="Acme Publishing") + book, created = p.books.update_or_create(name="The Book of Ed & Fred") + self.assertTrue(created) + self.assertEqual(p.books.count(), 1) + + def test_update_with_related_manager(self): + """ + Should be able to use update_or_create from the related manager to + update a book. Refs #23611. + """ + p = Publisher.objects.create(name="Acme Publishing") + book = Book.objects.create(name="The Book of Ed & Fred", publisher=p) + self.assertEqual(p.books.count(), 1) + name = "The Book of Django" + book, created = p.books.update_or_create(defaults={'name': name}, id=book.id) + self.assertFalse(created) + self.assertEqual(book.name, name) + self.assertEqual(p.books.count(), 1) + + def test_create_with_many(self): + """ + Should be able to use update_or_create from the m2m related manager to + create a book. Refs #23611. + """ + p = Publisher.objects.create(name="Acme Publishing") + author = Author.objects.create(name="Ted") + book, created = author.books.update_or_create(name="The Book of Ed & Fred", publisher=p) + self.assertTrue(created) + self.assertEqual(author.books.count(), 1) + + def test_update_with_many(self): + """ + Should be able to use update_or_create from the m2m related manager to + update a book. Refs #23611. + """ + p = Publisher.objects.create(name="Acme Publishing") + author = Author.objects.create(name="Ted") + book = Book.objects.create(name="The Book of Ed & Fred", publisher=p) + book.authors.add(author) + self.assertEqual(author.books.count(), 1) + name = "The Book of Django" + book, created = author.books.update_or_create(defaults={'name': name}, id=book.id) + 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. |