Ticket #17519: 17519.1.diff

File 17519.1.diff, 4.1 KB (added by Simon Charette, 12 years ago)

Added tests

  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index fcc6ab7..44a5e58 100644
    a b class BaseDatabaseCreation(object):  
    137137        """
    138138        from django.db.backends.util import truncate_name
    139139
    140         if not model._meta.managed or model._meta.proxy:
     140        if not model._meta.managed:
    141141            return []
    142142        qn = self.connection.ops.quote_name
    143143        final_output = []
  • tests/regressiontests/backends/models.py

    diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
    index af4952d..f54f1f7 100644
    a b class Reporter(models.Model):  
    6363        return "%s %s" % (self.first_name, self.last_name)
    6464
    6565
     66class ReporterProxy(Reporter):
     67    class Meta:
     68        proxy = True
     69
     70
    6671class Article(models.Model):
    6772    headline = models.CharField(max_length=100)
    6873    pub_date = models.DateField()
    6974    reporter = models.ForeignKey(Reporter)
     75    reporter_proxy = models.ForeignKey(ReporterProxy, null=True,
     76                                       related_name='reporter_proxy')
    7077
    7178    def __unicode__(self):
    7279        return self.headline
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index a6425c5..fcdc581 100644
    a b class FkConstraintsTests(TransactionTestCase):  
    438438        Try to create a model instance that violates a FK constraint. If it
    439439        fails it should fail with IntegrityError.
    440440        """
    441         a = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
     441        a1 = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
    442442        try:
    443             a.save()
     443            a1.save()
    444444        except IntegrityError:
    445             return
    446         self.skipTest("This backend does not support integrity checks.")
     445            pass
     446        else:
     447            self.skipTest("This backend does not support integrity checks.")
     448        # Now that we know this backend supports integrity checks we make sure
     449        # constraints are also enforced for proxy models. Refs #17519
     450        a2 = models.Article(headline='This is another test', reporter=self.r,
     451                            pub_date=datetime.datetime(2012, 8, 3),
     452                            reporter_proxy_id=30)
     453        self.assertRaises(IntegrityError, a2.save)
    447454
    448455    def test_integrity_checks_on_update(self):
    449456        """
    class FkConstraintsTests(TransactionTestCase):  
    452459        """
    453460        # Create an Article.
    454461        models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
    455         # Retrive it from the DB
    456         a = models.Article.objects.get(headline="Test article")
    457         a.reporter_id = 30
     462        # Retrieve it from the DB
     463        a1 = models.Article.objects.get(headline="Test article")
     464        a1.reporter_id = 30
    458465        try:
    459             a.save()
     466            a1.save()
    460467        except IntegrityError:
    461             return
    462         self.skipTest("This backend does not support integrity checks.")
     468            pass
     469        else:
     470            self.skipTest("This backend does not support integrity checks.")
     471        # Now that we know this backend supports integrity checks we make sure
     472        # constraints are also enforced for proxy models. Refs #17519
     473        # Create another article
     474        r_proxy = models.ReporterProxy.objects.get(pk=self.r.pk)
     475        models.Article.objects.create(headline='Another article',
     476                                      pub_date=datetime.datetime(1988, 5, 15),
     477                                      reporter=self.r, reporter_proxy=r_proxy)
     478        # Retreive the second article from the DB
     479        a2 = models.Article.objects.get(headline='Another article')
     480        a2.reporter_proxy_id = 30
     481        self.assertRaises(IntegrityError, a2.save)
    463482
    464483    def test_disable_constraint_checks_manually(self):
    465484        """
Back to Top