Fixed ordering related test failure

Also PEP8 + python_2_unicode_compatible cleanup done.
This commit is contained in:
Anssi Kääriäinen 2013-08-06 09:58:07 +03:00
parent 04489c7dbf
commit 263b873599
2 changed files with 33 additions and 18 deletions

View File

@ -5,14 +5,15 @@ from django.db.models.fields.related import ReverseSingleRelatedObjectDescriptor
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import get_language from django.utils.translation import get_language
@python_2_unicode_compatible
class Country(models.Model): class Country(models.Model):
# Table Column Fields # Table Column Fields
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
def __unicode__(self): def __str__(self):
return self.name return self.name
@python_2_unicode_compatible
class Person(models.Model): class Person(models.Model):
# Table Column Fields # Table Column Fields
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
@ -26,9 +27,10 @@ class Person(models.Model):
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __unicode__(self): def __str__(self):
return self.name return self.name
@python_2_unicode_compatible
class Group(models.Model): class Group(models.Model):
# Table Column Fields # Table Column Fields
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
@ -38,10 +40,11 @@ class Group(models.Model):
class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
def __unicode__(self): def __str__(self):
return self.name return self.name
@python_2_unicode_compatible
class Membership(models.Model): class Membership(models.Model):
# Table Column Fields # Table Column Fields
membership_country = models.ForeignKey(Country) membership_country = models.ForeignKey(Country)
@ -51,17 +54,19 @@ class Membership(models.Model):
group_id = models.IntegerField() group_id = models.IntegerField()
# Relation Fields # Relation Fields
person = models.ForeignObject(Person, person = models.ForeignObject(
Person,
from_fields=['membership_country', 'person_id'], from_fields=['membership_country', 'person_id'],
to_fields=['person_country_id', 'id']) to_fields=['person_country_id', 'id'])
group = models.ForeignObject(Group, group = models.ForeignObject(
Group,
from_fields=['membership_country', 'group_id'], from_fields=['membership_country', 'group_id'],
to_fields=['group_country', 'id']) to_fields=['group_country', 'id'])
class Meta: class Meta:
ordering = ('date_joined', 'invite_reason') ordering = ('date_joined', 'invite_reason')
def __unicode__(self): def __str__(self):
return "%s is a member of %s" % (self.person.name, self.group.name) return "%s is a member of %s" % (self.person.name, self.group.name)
@ -73,17 +78,20 @@ class Friendship(models.Model):
to_friend_id = models.IntegerField() to_friend_id = models.IntegerField()
# Relation Fields # Relation Fields
from_friend = models.ForeignObject(Person, from_friend = models.ForeignObject(
Person,
from_fields=['from_friend_country', 'from_friend_id'], from_fields=['from_friend_country', 'from_friend_id'],
to_fields=['person_country_id', 'id'], to_fields=['person_country_id', 'id'],
related_name='from_friend') related_name='from_friend')
to_friend_country = models.ForeignObject(Country, to_friend_country = models.ForeignObject(
Country,
from_fields=['to_friend_country_id'], from_fields=['to_friend_country_id'],
to_fields=['id'], to_fields=['id'],
related_name='to_friend_country') related_name='to_friend_country')
to_friend = models.ForeignObject(Person, to_friend = models.ForeignObject(
Person,
from_fields=['to_friend_country_id', 'to_friend_id'], from_fields=['to_friend_country_id', 'to_friend_id'],
to_fields=['person_country_id', 'id'], to_fields=['person_country_id', 'id'],
related_name='to_friend') related_name='to_friend')
@ -159,5 +167,6 @@ class ArticleTag(models.Model):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
class ArticleIdea(models.Model): class ArticleIdea(models.Model):
articles = models.ManyToManyField(Article, related_name="ideas", related_query_name="idea_things") articles = models.ManyToManyField(Article, related_name="ideas",
related_query_name="idea_things")
name = models.CharField(max_length=255) name = models.CharField(max_length=255)

View File

@ -9,6 +9,9 @@ from django.utils.translation import activate
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django import forms from django import forms
# Note that these tests are testing internal implementation details.
# ForeignObject is not part of public API.
class MultiColumnFKTests(TestCase): class MultiColumnFKTests(TestCase):
def setUp(self): def setUp(self):
# Creating countries # Creating countries
@ -142,9 +145,9 @@ class MultiColumnFKTests(TestCase):
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat) Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
with self.assertNumQueries(1): with self.assertNumQueries(1):
people = [m.person for m in Membership.objects.select_related('person')] people = [m.person for m in Membership.objects.select_related('person').order_by('pk')]
normal_people = [m.person for m in Membership.objects.all()] normal_people = [m.person for m in Membership.objects.all().order_by('pk')]
self.assertEqual(people, normal_people) self.assertEqual(people, normal_people)
def test_prefetch_foreignkey_forward_works(self): def test_prefetch_foreignkey_forward_works(self):
@ -152,19 +155,22 @@ class MultiColumnFKTests(TestCase):
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat) Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
with self.assertNumQueries(2): with self.assertNumQueries(2):
people = [m.person for m in Membership.objects.prefetch_related('person')] people = [
m.person for m in Membership.objects.prefetch_related('person').order_by('pk')]
normal_people = [m.person for m in Membership.objects.all()] normal_people = [m.person for m in Membership.objects.order_by('pk')]
self.assertEqual(people, normal_people) self.assertEqual(people, normal_people)
def test_prefetch_foreignkey_reverse_works(self): def test_prefetch_foreignkey_reverse_works(self):
Membership.objects.create(membership_country=self.usa, person=self.bob, group=self.cia) Membership.objects.create(membership_country=self.usa, person=self.bob, group=self.cia)
Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat) Membership.objects.create(membership_country=self.usa, person=self.jim, group=self.democrat)
with self.assertNumQueries(2): with self.assertNumQueries(2):
membership_sets = [list(p.membership_set.all()) membership_sets = [
for p in Person.objects.prefetch_related('membership_set')] list(p.membership_set.all())
for p in Person.objects.prefetch_related('membership_set').order_by('pk')]
normal_membership_sets = [list(p.membership_set.all()) for p in Person.objects.all()] normal_membership_sets = [list(p.membership_set.all())
for p in Person.objects.order_by('pk')]
self.assertEqual(membership_sets, normal_membership_sets) self.assertEqual(membership_sets, normal_membership_sets)
def test_m2m_through_forward_returns_valid_members(self): def test_m2m_through_forward_returns_valid_members(self):