~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to tests/regressiontests/model_fields/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
 
 
2
from django.db import models
 
3
 
 
4
try:
 
5
    import decimal
 
6
except ImportError:
 
7
    from django.utils import _decimal as decimal    # Python 2.3 fallback
 
8
 
 
9
class Foo(models.Model):
 
10
    a = models.CharField(max_length=10)
 
11
    d = models.DecimalField(max_digits=5, decimal_places=3)
 
12
 
 
13
def get_foo():
 
14
    return Foo.objects.get(id=1)
 
15
 
 
16
class Bar(models.Model):
 
17
    b = models.CharField(max_length=10)
 
18
    a = models.ForeignKey(Foo, default=get_foo)
 
19
 
 
20
class Whiz(models.Model):
 
21
    CHOICES = (
 
22
        ('Group 1', (
 
23
                (1,'First'),
 
24
                (2,'Second'),
 
25
            )
 
26
        ),
 
27
        ('Group 2', (
 
28
                (3,'Third'),
 
29
                (4,'Fourth'),
 
30
            )
 
31
        ),
 
32
        (0,'Other'),
 
33
    )
 
34
    c = models.IntegerField(choices=CHOICES, null=True)
 
35
 
 
36
__test__ = {'API_TESTS':"""
 
37
# Create a couple of Places.
 
38
>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
 
39
>>> f.id
 
40
1
 
41
>>> b = Bar(b = "bcd")
 
42
>>> b.a
 
43
<Foo: Foo object>
 
44
>>> b.save()
 
45
 
 
46
# Regression tests for #7913
 
47
# Check that get_choices and get_flatchoices interact with
 
48
# get_FIELD_display to return the expected values.
 
49
 
 
50
# Test a nested value
 
51
>>> w = Whiz(c=1)
 
52
>>> w.save()
 
53
>>> w.get_c_display()
 
54
u'First'
 
55
 
 
56
# Test a top level value
 
57
>>> w.c = 0
 
58
>>> w.get_c_display()
 
59
u'Other'
 
60
 
 
61
# Test an invalid data value
 
62
>>> w.c = 9
 
63
>>> w.get_c_display()
 
64
9
 
65
 
 
66
# Test a blank data value
 
67
>>> w.c = None
 
68
>>> print w.get_c_display()
 
69
None
 
70
 
 
71
# Test an empty data value
 
72
>>> w.c = ''
 
73
>>> w.get_c_display()
 
74
u''
 
75
 
 
76
# Regression test for #8023: should be able to filter decimal fields using
 
77
# strings (which is what gets passed through from, e.g., the admin interface).
 
78
>>> Foo.objects.filter(d=u'1.23')
 
79
[]
 
80
 
 
81
 
 
82
"""}