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

« back to all changes in this revision

Viewing changes to tests/regressiontests/model_fields/tests.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
import datetime
 
2
import unittest
 
3
 
 
4
import django.test
 
5
from django import forms
 
6
from django.db import models
 
7
from django.core.exceptions import ValidationError
 
8
 
 
9
from models import Foo, Bar, Whiz, BigD, BigS, Image
 
10
 
 
11
try:
 
12
    from decimal import Decimal
 
13
except ImportError:
 
14
    from django.utils._decimal import Decimal
 
15
 
 
16
 
 
17
# If PIL available, do these tests.
 
18
if Image:
 
19
    from imagefield import \
 
20
            ImageFieldTests, \
 
21
            ImageFieldTwoDimensionsTests, \
 
22
            ImageFieldNoDimensionsTests, \
 
23
            ImageFieldOneDimensionTests, \
 
24
            ImageFieldDimensionsFirstTests, \
 
25
            ImageFieldUsingFileTests, \
 
26
            TwoImageFieldTests
 
27
 
 
28
 
 
29
class DecimalFieldTests(django.test.TestCase):
 
30
    def test_to_python(self):
 
31
        f = models.DecimalField(max_digits=4, decimal_places=2)
 
32
        self.assertEqual(f.to_python(3), Decimal("3"))
 
33
        self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
 
34
        self.assertRaises(ValidationError, f.to_python, "abc")
 
35
 
 
36
    def test_default(self):
 
37
        f = models.DecimalField(default=Decimal("0.00"))
 
38
        self.assertEqual(f.get_default(), Decimal("0.00"))
 
39
 
 
40
    def test_format(self):
 
41
        f = models.DecimalField(max_digits=5, decimal_places=1)
 
42
        self.assertEqual(f._format(f.to_python(2)), u'2.0')
 
43
        self.assertEqual(f._format(f.to_python('2.6')), u'2.6')
 
44
        self.assertEqual(f._format(None), None)
 
45
 
 
46
    def test_get_db_prep_lookup(self):
 
47
        f = models.DecimalField(max_digits=5, decimal_places=1)
 
48
        self.assertEqual(f.get_db_prep_lookup('exact', None), [None])
 
49
 
 
50
    def test_filter_with_strings(self):
 
51
        """
 
52
        We should be able to filter decimal fields using strings (#8023)
 
53
        """
 
54
        Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
 
55
        self.assertEqual(list(Foo.objects.filter(d=u'1.23')), [])
 
56
 
 
57
    def test_save_without_float_conversion(self):
 
58
        """
 
59
        Ensure decimals don't go through a corrupting float conversion during
 
60
        save (#5079).
 
61
        """
 
62
        bd = BigD(d="12.9")
 
63
        bd.save()
 
64
        bd = BigD.objects.get(pk=bd.pk)
 
65
        self.assertEqual(bd.d, Decimal("12.9"))
 
66
 
 
67
    def test_lookup_really_big_value(self):
 
68
        """
 
69
        Ensure that really big values can be used in a filter statement, even
 
70
        with older Python versions.
 
71
        """
 
72
        # This should not crash. That counts as a win for our purposes.
 
73
        Foo.objects.filter(d__gte=100000000000)
 
74
 
 
75
class ForeignKeyTests(django.test.TestCase):
 
76
    def test_callable_default(self):
 
77
        """Test the use of a lazy callable for ForeignKey.default"""
 
78
        a = Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
 
79
        b = Bar.objects.create(b="bcd")
 
80
        self.assertEqual(b.a, a)
 
81
 
 
82
class DateTimeFieldTests(unittest.TestCase):
 
83
    def test_datetimefield_to_python_usecs(self):
 
84
        """DateTimeField.to_python should support usecs"""
 
85
        f = models.DateTimeField()
 
86
        self.assertEqual(f.to_python('2001-01-02 03:04:05.000006'),
 
87
                         datetime.datetime(2001, 1, 2, 3, 4, 5, 6))
 
88
        self.assertEqual(f.to_python('2001-01-02 03:04:05.999999'),
 
89
                         datetime.datetime(2001, 1, 2, 3, 4, 5, 999999))
 
90
 
 
91
    def test_timefield_to_python_usecs(self):
 
92
        """TimeField.to_python should support usecs"""
 
93
        f = models.TimeField()
 
94
        self.assertEqual(f.to_python('01:02:03.000004'),
 
95
                         datetime.time(1, 2, 3, 4))
 
96
        self.assertEqual(f.to_python('01:02:03.999999'),
 
97
                         datetime.time(1, 2, 3, 999999))
 
98
 
 
99
class BooleanFieldTests(unittest.TestCase):
 
100
    def _test_get_db_prep_lookup(self, f):
 
101
        self.assertEqual(f.get_db_prep_lookup('exact', True), [True])
 
102
        self.assertEqual(f.get_db_prep_lookup('exact', '1'), [True])
 
103
        self.assertEqual(f.get_db_prep_lookup('exact', 1), [True])
 
104
        self.assertEqual(f.get_db_prep_lookup('exact', False), [False])
 
105
        self.assertEqual(f.get_db_prep_lookup('exact', '0'), [False])
 
106
        self.assertEqual(f.get_db_prep_lookup('exact', 0), [False])
 
107
        self.assertEqual(f.get_db_prep_lookup('exact', None), [None])
 
108
 
 
109
    def test_booleanfield_get_db_prep_lookup(self):
 
110
        self._test_get_db_prep_lookup(models.BooleanField())
 
111
 
 
112
    def test_nullbooleanfield_get_db_prep_lookup(self):
 
113
        self._test_get_db_prep_lookup(models.NullBooleanField())
 
114
 
 
115
    def test_booleanfield_choices_blank(self):
 
116
        """
 
117
        Test that BooleanField with choices and defaults doesn't generate a
 
118
        formfield with the blank option (#9640, #10549).
 
119
        """
 
120
        choices = [(1, u'Si'), (2, 'No')]
 
121
        f = models.BooleanField(choices=choices, default=1, null=True)
 
122
        self.assertEqual(f.formfield().choices, [('', '---------')] + choices)
 
123
 
 
124
        f = models.BooleanField(choices=choices, default=1, null=False)
 
125
        self.assertEqual(f.formfield().choices, choices)
 
126
 
 
127
class ChoicesTests(django.test.TestCase):
 
128
    def test_choices_and_field_display(self):
 
129
        """
 
130
        Check that get_choices and get_flatchoices interact with
 
131
        get_FIELD_display to return the expected values (#7913).
 
132
        """
 
133
        self.assertEqual(Whiz(c=1).get_c_display(), 'First')    # A nested value
 
134
        self.assertEqual(Whiz(c=0).get_c_display(), 'Other')    # A top level value
 
135
        self.assertEqual(Whiz(c=9).get_c_display(), 9)          # Invalid value
 
136
        self.assertEqual(Whiz(c=None).get_c_display(), None)    # Blank value
 
137
        self.assertEqual(Whiz(c='').get_c_display(), '')        # Empty value
 
138
 
 
139
class SlugFieldTests(django.test.TestCase):
 
140
    def test_slugfield_max_length(self):
 
141
        """
 
142
        Make sure SlugField honors max_length (#9706)
 
143
        """
 
144
        bs = BigS.objects.create(s = 'slug'*50)
 
145
        bs = BigS.objects.get(pk=bs.pk)
 
146
        self.assertEqual(bs.s, 'slug'*50)