Code

Ticket #14226: test.diff

File test.diff, 3.5 KB (added by outofculture, 4 years ago)

Patch to add/fix tests under fixtures_regress

Line 
1diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py
2index 28a4c8b..bcfb607 100644
3--- a/tests/regressiontests/fixtures_regress/models.py
4+++ b/tests/regressiontests/fixtures_regress/models.py
5@@ -231,3 +231,45 @@ class ExternalDependency(models.Model):
6     def natural_key(self):
7         return self.name
8     natural_key.dependencies = ['fixtures_regress.book']
9+
10+
11+class Tagger(models.Model):
12+    name = models.CharField(max_length=20)
13+    topics = models.ManyToManyField("Tag", through="TaggerTag")
14+
15+    def natural_key(self):
16+        return self.name
17+
18+
19+class Posting(models.Model):
20+    tagger = models.ForeignKey(Tagger)
21+    text = models.TextField()
22+    time = models.DateTimeField()
23+
24+    def natural_key(self):
25+        return (self.tagger.name, self.time)
26+    natural_key.dependencies = ['fixtures_regress.tagger']
27+
28+
29+class Tag(models.Model):
30+    name = models.CharField(max_length=20)
31+    postings = models.ManyToManyField(Posting, through="PostingTag")
32+
33+    def natural_key(self):
34+        return self.name
35+
36+
37+class PostingTag(models.Model):
38+    tag = models.ForeignKey(Tag)
39+    posting = models.ForeignKey(Posting)
40+
41+    def natural_key(self):
42+        return (self.tag.natural_key(), self.posting.natural_key())
43+
44+
45+class TaggerTag(models.Model):
46+    tagger = models.ForeignKey(Tagger)
47+    tag = models.ForeignKey(Tag)
48+
49+    def natural_key(self):
50+        return (self.tag.natural_key(), self.tagger.natural_key())
51diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
52index 9c10e20..a395f7a 100644
53--- a/tests/regressiontests/fixtures_regress/tests.py
54+++ b/tests/regressiontests/fixtures_regress/tests.py
55@@ -21,6 +21,7 @@ from models import NKChild, RefToNKChild
56 from models import Circle1, Circle2, Circle3
57 from models import ExternalDependency
58 from models import animal_pre_save_check
59+from models import Tagger, Posting, Tag, TaggerTag, PostingTag
60 
61 
62 class TestFixtures(TestCase):
63@@ -361,10 +362,9 @@ class TestFixtures(TestCase):
64         sorted_deps = sort_dependencies(
65             [('fixtures_regress', [Person, Circle1, Store, Book])]
66             )
67-        self.assertEqual(
68-            sorted_deps,
69-            [Circle1, Store, Person, Book]
70-            )
71+        # The specific placement of Circle1 is irrelevant.
72+        assert sorted_deps.index(Store) < sorted_deps.index(Person)
73+        assert sorted_deps.index(Person) < sorted_deps.index(Book)
74 
75     def test_dependency_sorting_tight_circular(self):
76         self.assertRaisesMessage(
77@@ -390,6 +390,21 @@ class TestFixtures(TestCase):
78             [('fixtures_regress', [Book, Circle3])],
79             )
80 
81+    def test_dependency_sorting_m2m(self):
82+        """
83+        Test for ticket #14226 -- ManyToMany relationships can be
84+        defined on any side of the relationship.
85+        """
86+        sorted_deps = sort_dependencies(
87+            [('fixtures_regress', [Posting, Tagger, TaggerTag, Tag, PostingTag])],
88+            )
89+        assert sorted_deps.index(Tag) < sorted_deps.index(Tagger)
90+        assert sorted_deps.index(Tagger) < sorted_deps.index(Posting)
91+        assert sorted_deps.index(Posting) < sorted_deps.index(PostingTag)
92+        assert sorted_deps.index(Tag) < sorted_deps.index(PostingTag)
93+        assert sorted_deps.index(Tagger) < sorted_deps.index(TaggerTag)
94+        assert sorted_deps.index(Tag) < sorted_deps.index(TaggerTag)
95+
96     def test_dependency_sorting_long(self):
97         self.assertRaisesMessage(
98             CommandError,