Ticket #11850: 11850-test.patch

File 11850-test.patch, 2.0 KB (added by FunkyBob, 13 years ago)

Patch to test ordering

  • modeltests/m2m_intermediary/tests.py

     
    44
    55from django.test import TestCase
    66
    7 from .models import Reporter, Article, Writer
     7from .models import Reporter, Article, Writer, First, Second, Through
    88
    99
    1010class M2MIntermediaryTests(TestCase):
     
    3838            ],
    3939            lambda w: (unicode(w.reporter), w.position)
    4040        )
     41
     42    def test_intermediary_ordering(self):
     43        f = First.objects.create(title='First')
     44        # Create out of order, in case pk ordering is 'default' in the DBMS
     45        s2 = Second.objects.create(title='Second 2')
     46        s3 = Second.objects.create(title='Second 3')
     47        s1 = Second.objects.create(title='Second 1')
     48
     49        t2 = Through.objects.create(first=f, second=s2, order=2)
     50        t1 = Through.objects.create(first=f, second=s1, order=1)
     51        t3 = Through.objects.create(first=f, second=s2, order=3)
     52
     53        self.assertQuerysetEqual(
     54            f.seconds.all(), [
     55                u'Second 1',
     56                u'Second 2',
     57                u'Second 3',
     58            ],
     59            lambda w: w.title
     60        )
  • modeltests/m2m_intermediary/models.py

     
    3535    def __unicode__(self):
    3636        return u'%s (%s)' % (self.reporter, self.position)
    3737
     38class First(models.Model):
     39    title = models.CharField(max_length=10)
     40    seconds = models.ManyToManyField('Second', through='Through')
     41
     42class Second(models.Model):
     43    title = models.CharField(max_length=10)
     44
     45class Through(models.Model):
     46    first = models.ForeignKey('First')
     47    second = models.ForeignKey('Second')
     48    order = models.IntegerField()
     49
     50    class Meta:
     51        ordering = ('order',)
     52
Back to Top