Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed ordering related test failure

Also PEP8 + python_2_unicode_compatible cleanup done.

Backport of 263b873 from master
  • Loading branch information...
commit 363b81c59cc0252f605de7d30433934b4662cfb3 1 parent 891cdf1
Anssi Kääriäinen authored August 06, 2013
31  tests/foreign_object/models.py
@@ -5,14 +5,15 @@
5 5
 from django.utils.encoding import python_2_unicode_compatible
6 6
 from django.utils.translation import get_language
7 7
 
  8
+@python_2_unicode_compatible
8 9
 class Country(models.Model):
9 10
     # Table Column Fields
10 11
     name = models.CharField(max_length=50)
11 12
 
12  
-    def __unicode__(self):
  13
+    def __str__(self):
13 14
         return self.name
14 15
 
15  
-
  16
+@python_2_unicode_compatible
16 17
 class Person(models.Model):
17 18
     # Table Column Fields
18 19
     name = models.CharField(max_length=128)
@@ -26,9 +27,10 @@ class Person(models.Model):
26 27
     class Meta:
27 28
         ordering = ('name',)
28 29
 
29  
-    def __unicode__(self):
  30
+    def __str__(self):
30 31
         return self.name
31 32
 
  33
+@python_2_unicode_compatible
32 34
 class Group(models.Model):
33 35
     # Table Column Fields
34 36
     name = models.CharField(max_length=128)
@@ -38,10 +40,11 @@ class Group(models.Model):
38 40
     class Meta:
39 41
         ordering = ('name',)
40 42
 
41  
-    def __unicode__(self):
  43
+    def __str__(self):
42 44
         return self.name
43 45
 
44 46
 
  47
+@python_2_unicode_compatible
45 48
 class Membership(models.Model):
46 49
     # Table Column Fields
47 50
     membership_country = models.ForeignKey(Country)
@@ -51,17 +54,19 @@ class Membership(models.Model):
51 54
     group_id = models.IntegerField()
52 55
 
53 56
     # Relation Fields
54  
-    person = models.ForeignObject(Person,
  57
+    person = models.ForeignObject(
  58
+        Person,
55 59
         from_fields=['membership_country', 'person_id'],
56 60
         to_fields=['person_country_id', 'id'])
57  
-    group = models.ForeignObject(Group,
  61
+    group = models.ForeignObject(
  62
+        Group,
58 63
         from_fields=['membership_country', 'group_id'],
59 64
         to_fields=['group_country', 'id'])
60 65
 
61 66
     class Meta:
62 67
         ordering = ('date_joined', 'invite_reason')
63 68
 
64  
-    def __unicode__(self):
  69
+    def __str__(self):
65 70
         return "%s is a member of %s" % (self.person.name, self.group.name)
66 71
 
67 72
 
@@ -73,17 +78,20 @@ class Friendship(models.Model):
73 78
     to_friend_id = models.IntegerField()
74 79
 
75 80
     # Relation Fields
76  
-    from_friend = models.ForeignObject(Person,
  81
+    from_friend = models.ForeignObject(
  82
+        Person,
77 83
         from_fields=['from_friend_country', 'from_friend_id'],
78 84
         to_fields=['person_country_id', 'id'],
79 85
         related_name='from_friend')
80 86
 
81  
-    to_friend_country = models.ForeignObject(Country,
  87
+    to_friend_country = models.ForeignObject(
  88
+        Country,
82 89
         from_fields=['to_friend_country_id'],
83 90
         to_fields=['id'],
84 91
         related_name='to_friend_country')
85 92
 
86  
-    to_friend = models.ForeignObject(Person,
  93
+    to_friend = models.ForeignObject(
  94
+        Person,
87 95
         from_fields=['to_friend_country_id', 'to_friend_id'],
88 96
         to_fields=['person_country_id', 'id'],
89 97
         related_name='to_friend')
@@ -159,5 +167,6 @@ class ArticleTag(models.Model):
159 167
     name = models.CharField(max_length=255)
160 168
 
161 169
 class ArticleIdea(models.Model):
162  
-    articles = models.ManyToManyField(Article, related_name="ideas", related_query_name="idea_things")
  170
+    articles = models.ManyToManyField(Article, related_name="ideas",
  171
+                                      related_query_name="idea_things")
163 172
     name = models.CharField(max_length=255)
20  tests/foreign_object/tests.py
@@ -9,6 +9,9 @@
9 9
 from django.core.exceptions import FieldError
10 10
 from django import forms
11 11
 
  12
+# Note that these tests are testing internal implementation details.
  13
+# ForeignObject is not part of public API.
  14
+
12 15
 class MultiColumnFKTests(TestCase):
13 16
     def setUp(self):
14 17
         # Creating countries
@@ -142,9 +145,9 @@ def test_select_related_foreignkey_forward_works(self):
142 145
         Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
143 146
 
144 147
         with self.assertNumQueries(1):
145  
-            people = [m.person for m in Membership.objects.select_related('person')]
  148
+            people = [m.person for m in Membership.objects.select_related('person').order_by('pk')]
146 149
 
147  
-        normal_people = [m.person for m in Membership.objects.all()]
  150
+        normal_people = [m.person for m in Membership.objects.all().order_by('pk')]
148 151
         self.assertEqual(people, normal_people)
149 152
 
150 153
     def test_prefetch_foreignkey_forward_works(self):
@@ -152,19 +155,22 @@ def test_prefetch_foreignkey_forward_works(self):
152 155
         Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
153 156
 
154 157
         with self.assertNumQueries(2):
155  
-            people = [m.person for m in Membership.objects.prefetch_related('person')]
  158
+            people = [
  159
+                m.person for m in Membership.objects.prefetch_related('person').order_by('pk')]
156 160
 
157  
-        normal_people = [m.person for m in Membership.objects.all()]
  161
+        normal_people = [m.person for m in Membership.objects.order_by('pk')]
158 162
         self.assertEqual(people, normal_people)
159 163
 
160 164
     def test_prefetch_foreignkey_reverse_works(self):
161 165
         Membership.objects.create(membership_country=self.usa, person=self.bob, group=self.cia)
162 166
         Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
163 167
         with self.assertNumQueries(2):
164  
-            membership_sets = [list(p.membership_set.all())
165  
-                               for p in Person.objects.prefetch_related('membership_set')]
  168
+            membership_sets = [
  169
+                list(p.membership_set.all())
  170
+                for p in Person.objects.prefetch_related('membership_set').order_by('pk')]
166 171
 
167  
-        normal_membership_sets = [list(p.membership_set.all()) for p in Person.objects.all()]
  172
+        normal_membership_sets = [list(p.membership_set.all())
  173
+                                  for p in Person.objects.order_by('pk')]
168 174
         self.assertEqual(membership_sets, normal_membership_sets)
169 175
 
170 176
     def test_m2m_through_forward_returns_valid_members(self):

0 notes on commit 363b81c

Please sign in to comment.
Something went wrong with that request. Please try again.