5
from django import forms
6
from django.db import models
7
from django.core.exceptions import ValidationError
9
from models import Foo, Bar, Whiz, BigD, BigS, Image
12
from decimal import Decimal
14
from django.utils._decimal import Decimal
17
# If PIL available, do these tests.
19
from imagefield import \
21
ImageFieldTwoDimensionsTests, \
22
ImageFieldNoDimensionsTests, \
23
ImageFieldOneDimensionTests, \
24
ImageFieldDimensionsFirstTests, \
25
ImageFieldUsingFileTests, \
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")
36
def test_default(self):
37
f = models.DecimalField(default=Decimal("0.00"))
38
self.assertEqual(f.get_default(), Decimal("0.00"))
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)
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])
50
def test_filter_with_strings(self):
52
We should be able to filter decimal fields using strings (#8023)
54
Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
55
self.assertEqual(list(Foo.objects.filter(d=u'1.23')), [])
57
def test_save_without_float_conversion(self):
59
Ensure decimals don't go through a corrupting float conversion during
64
bd = BigD.objects.get(pk=bd.pk)
65
self.assertEqual(bd.d, Decimal("12.9"))
67
def test_lookup_really_big_value(self):
69
Ensure that really big values can be used in a filter statement, even
70
with older Python versions.
72
# This should not crash. That counts as a win for our purposes.
73
Foo.objects.filter(d__gte=100000000000)
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)
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))
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))
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])
109
def test_booleanfield_get_db_prep_lookup(self):
110
self._test_get_db_prep_lookup(models.BooleanField())
112
def test_nullbooleanfield_get_db_prep_lookup(self):
113
self._test_get_db_prep_lookup(models.NullBooleanField())
115
def test_booleanfield_choices_blank(self):
117
Test that BooleanField with choices and defaults doesn't generate a
118
formfield with the blank option (#9640, #10549).
120
choices = [(1, u'Si'), (2, 'No')]
121
f = models.BooleanField(choices=choices, default=1, null=True)
122
self.assertEqual(f.formfield().choices, [('', '---------')] + choices)
124
f = models.BooleanField(choices=choices, default=1, null=False)
125
self.assertEqual(f.formfield().choices, choices)
127
class ChoicesTests(django.test.TestCase):
128
def test_choices_and_field_display(self):
130
Check that get_choices and get_flatchoices interact with
131
get_FIELD_display to return the expected values (#7913).
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
139
class SlugFieldTests(django.test.TestCase):
140
def test_slugfield_max_length(self):
142
Make sure SlugField honors max_length (#9706)
144
bs = BigS.objects.create(s = 'slug'*50)
145
bs = BigS.objects.get(pk=bs.pk)
146
self.assertEqual(bs.s, 'slug'*50)