summaryrefslogtreecommitdiff
path: root/tests/regressiontests/model_fields
diff options
context:
space:
mode:
Diffstat (limited to 'tests/regressiontests/model_fields')
-rw-r--r--tests/regressiontests/model_fields/models.py60
-rw-r--r--tests/regressiontests/model_fields/tests.py38
2 files changed, 85 insertions, 13 deletions
diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
index 7e07227961..455e2b3ded 100644
--- a/tests/regressiontests/model_fields/models.py
+++ b/tests/regressiontests/model_fields/models.py
@@ -1,8 +1,14 @@
from django.db import models
+try:
+ import decimal
+except ImportError:
+ from django.utils import _decimal as decimal # Python 2.3 fallback
+
class Foo(models.Model):
a = models.CharField(max_length=10)
+ d = models.DecimalField(max_digits=5, decimal_places=3)
def get_foo():
return Foo.objects.get(id=1)
@@ -11,9 +17,25 @@ class Bar(models.Model):
b = models.CharField(max_length=10)
a = models.ForeignKey(Foo, default=get_foo)
+class Whiz(models.Model):
+ CHOICES = (
+ ('Group 1', (
+ (1,'First'),
+ (2,'Second'),
+ )
+ ),
+ ('Group 2', (
+ (3,'Third'),
+ (4,'Fourth'),
+ )
+ ),
+ (0,'Other'),
+ )
+ c = models.IntegerField(choices=CHOICES, null=True)
+
__test__ = {'API_TESTS':"""
# Create a couple of Places.
->>> f = Foo.objects.create(a='abc')
+>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
>>> f.id
1
>>> b = Bar(b = "bcd")
@@ -21,4 +43,40 @@ __test__ = {'API_TESTS':"""
<Foo: Foo object>
>>> b.save()
+# Regression tests for #7913
+# Check that get_choices and get_flatchoices interact with
+# get_FIELD_display to return the expected values.
+
+# Test a nested value
+>>> w = Whiz(c=1)
+>>> w.save()
+>>> w.get_c_display()
+u'First'
+
+# Test a top level value
+>>> w.c = 0
+>>> w.get_c_display()
+u'Other'
+
+# Test an invalid data value
+>>> w.c = 9
+>>> w.get_c_display()
+9
+
+# Test a blank data value
+>>> w.c = None
+>>> print w.get_c_display()
+None
+
+# Test an empty data value
+>>> w.c = ''
+>>> w.get_c_display()
+u''
+
+# Regression test for #8023: should be able to filter decimal fields using
+# strings (which is what gets passed through from, e.g., the admin interface).
+>>> Foo.objects.filter(d=u'1.23')
+[]
+
+
"""}
diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
index c2ba9ee008..5aedcd15fc 100644
--- a/tests/regressiontests/model_fields/tests.py
+++ b/tests/regressiontests/model_fields/tests.py
@@ -1,15 +1,19 @@
"""
>>> from django.db.models.fields import *
+>>> try:
+... from decimal import Decimal
+... except ImportError:
+... from django.utils._decimal import Decimal
# DecimalField
->>> f = DecimalField()
+>>> f = DecimalField(max_digits=4, decimal_places=2)
->>> f.to_python(3)
-Decimal("3")
+>>> f.to_python(3) == Decimal("3")
+True
->>> f.to_python("3.14")
-Decimal("3.14")
+>>> f.to_python("3.14") == Decimal("3.14")
+True
>>> f.to_python("abc")
Traceback (most recent call last):
@@ -20,16 +24,26 @@ ValidationError: [u'This value must be a decimal number.']
>>> x = f.to_python(2)
>>> y = f.to_python('2.6')
->>> f.get_db_prep_save(x)
+>>> f._format(x)
u'2.0'
->>> f.get_db_prep_save(y)
+>>> f._format(y)
u'2.6'
->>> f.get_db_prep_save(None)
->>> f.get_db_prep_lookup('exact', x)
-[u'2.0']
->>> f.get_db_prep_lookup('exact', y)
-[u'2.6']
+>>> f._format(None)
>>> f.get_db_prep_lookup('exact', None)
[None]
+# DateTimeField and TimeField to_python should support usecs:
+>>> f = DateTimeField()
+>>> f.to_python('2001-01-02 03:04:05.000006')
+datetime.datetime(2001, 1, 2, 3, 4, 5, 6)
+>>> f.to_python('2001-01-02 03:04:05.999999')
+datetime.datetime(2001, 1, 2, 3, 4, 5, 999999)
+
+>>> f = TimeField()
+>>> f.to_python('01:02:03.000004')
+datetime.time(1, 2, 3, 4)
+>>> f.to_python('01:02:03.999999')
+datetime.time(1, 2, 3, 999999)
+
+
"""