Ticket #15117: 15117_get_or_create.patch

File 15117_get_or_create.patch, 1.8 KB (added by Dougal Matthews, 13 years ago)
  • tests/modeltests/get_or_create/models.py

    diff --git a/tests/modeltests/get_or_create/models.py b/tests/modeltests/get_or_create/models.py
    index db5719b..efc50f9 100644
    a b given parameters. If an object isn't found, it creates one with the given  
    66parameters.
    77"""
    88
    9 from django.db import models, IntegrityError
     9from django.db import models
    1010
    1111class Person(models.Model):
    1212    first_name = models.CharField(max_length=100)
    class Person(models.Model):  
    1919class ManualPrimaryKeyTest(models.Model):
    2020    id = models.IntegerField(primary_key=True)
    2121    data = models.CharField(max_length=100)
     22
     23
     24class IntegrityErrorTest(models.Model):
     25    country = models.ForeignKey(Person, primary_key=True)
     26    info = models.TextField(blank=True, default='')
     27
  • tests/modeltests/get_or_create/tests.py

    diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
    index 3323c88..5b65537 100644
    a b from datetime import date  
    33from django.db import IntegrityError
    44from django.test import TestCase
    55
    6 from models import Person, ManualPrimaryKeyTest
     6from models import Person, ManualPrimaryKeyTest, IntegrityErrorTest
    77
    88
    99class GetOrCreateTests(TestCase):
    class GetOrCreateTests(TestCase):  
    5050            ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
    5151        )
    5252        self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
     53
     54
     55    def test_get_or_create_integrityerror(self):
     56
     57        self.assertRaises(IntegrityError,
     58            IntegrityErrorTest.objects.get_or_create, pk=1)
     59
     60        p = Person()
     61        p.id = 1
     62
     63        self.assertRaises(IntegrityError,
     64            IntegrityErrorTest.objects.get_or_create, p=p)
     65
Back to Top