Ticket #8687: m2m_through_regress-validation-tests.diff

File m2m_through_regress-validation-tests.diff, 2.0 KB (added by vung, 16 years ago)

The test may be brittle but it exposes the problem.

  • tests/regressiontests/m2m_through_regress/models.py

     
    176176>>> bob.group_set.filter(membership__price=50)
    177177[<Group: Roll>]
    178178
     179## Regression test for #8687 - NameError when validating m2m models.
     180## This test works by creating three models, patching them onto the current
     181## application (m2m_through_regress) and re-running get_validation_errors.
     182
     183>>> class BadPerson(models.Model):
     184...    members = models.ManyToManyField('BadGroup', through='BadMembership')
     185
     186>>> class BadGroup(models.Model):
     187...    members = models.ManyToManyField(BadPerson, through='BadMembership')
     188
     189>>> class BadMembership(models.Model):
     190...    person = models.ForeignKey(BadPerson)
     191...    person3 = models.ForeignKey(BadPerson, related_name="person3")
     192...    group = models.ForeignKey(BadGroup)
     193
     194>>> from regressiontests import m2m_through_regress
     195>>> setattr(m2m_through_regress.models, 'BadPerson', BadPerson)
     196>>> setattr(m2m_through_regress.models, 'BadGroup', BadGroup)
     197>>> setattr(m2m_through_regress.models, 'BadMembership', BadMembership)
     198
     199>>> from django.core.management.validation import get_validation_errors
     200
     201Something to swallow and remember errors:
     202>>> class StdOut(object):
     203...    def __init__(self): self.lines = []
     204...    def write(self, val): self.lines.append(val.strip())
     205>>> stdout = StdOut()
     206
     207We should get two errors, both related to BadMembership: one when
     208validating BadPerson and one when validating BadGroup:
     209>>> get_validation_errors(stdout) == 2
     210True
     211
     212>>> stdout.lines
     213['m2m_through_regress.badperson: Intermediary model BadMembership
     214    has more than one foreign key to BadPerson, which is ambiguous
     215    and is not permitted.', 'm2m_through_regress.badgroup: Intermediary
     216    model BadMembership has more than one foreign key to BadPerson, which
     217    is ambiguous and is not permitted.']
     218
    179219"""}
Back to Top