~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.db import models
2
2
 
3
3
class Poll(models.Model):
4
 
    question = models.CharField(maxlength=200)
 
4
    question = models.CharField(max_length=200)
5
5
 
6
 
    def __str__(self):
7
 
        return "Q: %s " % self.question
 
6
    def __unicode__(self):
 
7
        return u"Q: %s " % self.question
8
8
 
9
9
class Choice(models.Model):
10
10
    poll = models.ForeignKey(Poll)
11
 
    choice = models.CharField(maxlength=200)
 
11
    choice = models.CharField(max_length=200)
12
12
 
13
 
    def __str__(self):
14
 
        return "Choice: %s in poll %s" % (self.choice, self.poll)
 
13
    def __unicode__(self):
 
14
        return u"Choice: %s in poll %s" % (self.choice, self.poll)
15
15
 
16
16
__test__ = {'API_TESTS':"""
17
 
# Regression test for the use of None as a query value. None is interpreted as 
 
17
# Regression test for the use of None as a query value. None is interpreted as
18
18
# an SQL NULL, but only in __exact queries.
19
19
# Set up some initial polls and choices
20
20
>>> p1 = Poll(question='Why?')
24
24
>>> c2 = Choice(poll=p1, choice='Why Not?')
25
25
>>> c2.save()
26
26
 
27
 
# Exact query with value None returns nothing (=NULL in sql)
28
 
>>> Choice.objects.filter(id__exact=None)
 
27
# Exact query with value None returns nothing ("is NULL" in sql, but every 'id'
 
28
# field has a value).
 
29
>>> Choice.objects.filter(choice__exact=None)
29
30
[]
30
31
 
 
32
Excluding the previous result returns everything.
 
33
>>> Choice.objects.exclude(choice=None).order_by('id')
 
34
[<Choice: Choice: Because. in poll Q: Why? >, <Choice: Choice: Why Not? in poll Q: Why? >]
 
35
 
31
36
# Valid query, but fails because foo isn't a keyword
32
 
>>> Choice.objects.filter(foo__exact=None) 
 
37
>>> Choice.objects.filter(foo__exact=None)
33
38
Traceback (most recent call last):
34
39
...
35
 
TypeError: Cannot resolve keyword 'foo' into field
 
40
FieldError: Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll
36
41
 
37
42
# Can't use None on anything other than __exact
38
43
>>> Choice.objects.filter(id__gt=None)