~ubuntu-branches/ubuntu/maverick/python-django/maverick

« back to all changes in this revision

Viewing changes to tests/regressiontests/null_fk_ordering/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Regression tests for proper working of ForeignKey(null=True). Tests these bugs:
 
3
 
 
4
    * #7512: including a nullable foreign key reference in Meta ordering has un
 
5
xpected results
 
6
 
 
7
"""
 
8
 
 
9
from django.db import models
 
10
 
 
11
# The first two models represent a very simple null FK ordering case.
 
12
class Author(models.Model):
 
13
    name = models.CharField(max_length=150)
 
14
 
 
15
class Article(models.Model):
 
16
    title = models.CharField(max_length=150)
 
17
    author = models.ForeignKey(Author, null=True)
 
18
 
 
19
    def __unicode__(self):
 
20
        return u'Article titled: %s' % (self.title, )
 
21
 
 
22
    class Meta:
 
23
        ordering = ['author__name', ]
 
24
 
 
25
 
 
26
# These following 4 models represent a far more complex ordering case.
 
27
class SystemInfo(models.Model):
 
28
    system_name = models.CharField(max_length=32)
 
29
 
 
30
class Forum(models.Model):
 
31
    system_info = models.ForeignKey(SystemInfo)
 
32
    forum_name = models.CharField(max_length=32)
 
33
 
 
34
class Post(models.Model):
 
35
    forum = models.ForeignKey(Forum, null=True)
 
36
    title = models.CharField(max_length=32)
 
37
 
 
38
    def __unicode__(self):
 
39
        return self.title
 
40
 
 
41
class Comment(models.Model):
 
42
    post = models.ForeignKey(Post, null=True)
 
43
    comment_text = models.CharField(max_length=250)
 
44
 
 
45
    class Meta:
 
46
        ordering = ['post__forum__system_info__system_name', 'comment_text']
 
47
 
 
48
    def __unicode__(self):
 
49
        return self.comment_text
 
50
 
 
51
 
 
52
__test__ = {'API_TESTS': """
 
53
# Regression test for #7512 -- ordering across nullable Foreign Keys shouldn't
 
54
# exclude results
 
55
>>> author_1 = Author.objects.create(name='Tom Jones')
 
56
>>> author_2 = Author.objects.create(name='Bob Smith')
 
57
>>> article_1 = Article.objects.create(title='No author on this article')
 
58
>>> article_2 = Article.objects.create(author=author_1, title='This article written by Tom Jones')
 
59
>>> article_3 = Article.objects.create(author=author_2, title='This article written by Bob Smith')
 
60
 
 
61
# We can't compare results directly (since different databases sort NULLs to
 
62
# different ends of the ordering), but we can check that all results are
 
63
# returned.
 
64
>>> len(list(Article.objects.all())) == 3
 
65
True
 
66
 
 
67
>>> s = SystemInfo.objects.create(system_name='System Info')
 
68
>>> f = Forum.objects.create(system_info=s, forum_name='First forum')
 
69
>>> p = Post.objects.create(forum=f, title='First Post')
 
70
>>> c1 = Comment.objects.create(post=p, comment_text='My first comment')
 
71
>>> c2 = Comment.objects.create(comment_text='My second comment')
 
72
>>> s2 = SystemInfo.objects.create(system_name='More System Info')
 
73
>>> f2 = Forum.objects.create(system_info=s2, forum_name='Second forum')
 
74
>>> p2 = Post.objects.create(forum=f2, title='Second Post')
 
75
>>> c3 = Comment.objects.create(comment_text='Another first comment')
 
76
>>> c4 = Comment.objects.create(post=p2, comment_text='Another second comment')
 
77
 
 
78
# We have to test this carefully. Some databases sort NULL values before
 
79
# everything else, some sort them afterwards. So we extract the ordered list
 
80
# and check the length. Before the fix, this list was too short (some values
 
81
# were omitted).
 
82
>>> len(list(Comment.objects.all())) == 4
 
83
True
 
84
 
 
85
"""
 
86
}