Ticket #14226: test.diff

File test.diff, 3.5 KB (added by Martin Chase, 14 years ago)

Patch to add/fix tests under fixtures_regress

  • tests/regressiontests/fixtures_regress/models.py

    diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py
    index 28a4c8b..bcfb607 100644
    a b class ExternalDependency(models.Model):  
    231231    def natural_key(self):
    232232        return self.name
    233233    natural_key.dependencies = ['fixtures_regress.book']
     234
     235
     236class Tagger(models.Model):
     237    name = models.CharField(max_length=20)
     238    topics = models.ManyToManyField("Tag", through="TaggerTag")
     239
     240    def natural_key(self):
     241        return self.name
     242
     243
     244class Posting(models.Model):
     245    tagger = models.ForeignKey(Tagger)
     246    text = models.TextField()
     247    time = models.DateTimeField()
     248
     249    def natural_key(self):
     250        return (self.tagger.name, self.time)
     251    natural_key.dependencies = ['fixtures_regress.tagger']
     252
     253
     254class Tag(models.Model):
     255    name = models.CharField(max_length=20)
     256    postings = models.ManyToManyField(Posting, through="PostingTag")
     257
     258    def natural_key(self):
     259        return self.name
     260
     261
     262class PostingTag(models.Model):
     263    tag = models.ForeignKey(Tag)
     264    posting = models.ForeignKey(Posting)
     265
     266    def natural_key(self):
     267        return (self.tag.natural_key(), self.posting.natural_key())
     268
     269
     270class TaggerTag(models.Model):
     271    tagger = models.ForeignKey(Tagger)
     272    tag = models.ForeignKey(Tag)
     273
     274    def natural_key(self):
     275        return (self.tag.natural_key(), self.tagger.natural_key())
  • tests/regressiontests/fixtures_regress/tests.py

    diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
    index 9c10e20..a395f7a 100644
    a b from models import NKChild, RefToNKChild  
    2121from models import Circle1, Circle2, Circle3
    2222from models import ExternalDependency
    2323from models import animal_pre_save_check
     24from models import Tagger, Posting, Tag, TaggerTag, PostingTag
    2425
    2526
    2627class TestFixtures(TestCase):
    class TestFixtures(TestCase):  
    361362        sorted_deps = sort_dependencies(
    362363            [('fixtures_regress', [Person, Circle1, Store, Book])]
    363364            )
    364         self.assertEqual(
    365             sorted_deps,
    366             [Circle1, Store, Person, Book]
    367             )
     365        # The specific placement of Circle1 is irrelevant.
     366        assert sorted_deps.index(Store) < sorted_deps.index(Person)
     367        assert sorted_deps.index(Person) < sorted_deps.index(Book)
    368368
    369369    def test_dependency_sorting_tight_circular(self):
    370370        self.assertRaisesMessage(
    class TestFixtures(TestCase):  
    390390            [('fixtures_regress', [Book, Circle3])],
    391391            )
    392392
     393    def test_dependency_sorting_m2m(self):
     394        """
     395        Test for ticket #14226 -- ManyToMany relationships can be
     396        defined on any side of the relationship.
     397        """
     398        sorted_deps = sort_dependencies(
     399            [('fixtures_regress', [Posting, Tagger, TaggerTag, Tag, PostingTag])],
     400            )
     401        assert sorted_deps.index(Tag) < sorted_deps.index(Tagger)
     402        assert sorted_deps.index(Tagger) < sorted_deps.index(Posting)
     403        assert sorted_deps.index(Posting) < sorted_deps.index(PostingTag)
     404        assert sorted_deps.index(Tag) < sorted_deps.index(PostingTag)
     405        assert sorted_deps.index(Tagger) < sorted_deps.index(TaggerTag)
     406        assert sorted_deps.index(Tag) < sorted_deps.index(TaggerTag)
     407
    393408    def test_dependency_sorting_long(self):
    394409        self.assertRaisesMessage(
    395410            CommandError,
Back to Top