Converted model_fields regression tests into unittests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10499 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f146132831
commit
3524c74f0b
|
@ -1,4 +1,3 @@
|
|||
|
||||
from django.db import models
|
||||
|
||||
try:
|
||||
|
@ -38,63 +37,3 @@ class BigD(models.Model):
|
|||
|
||||
class BigS(models.Model):
|
||||
s = models.SlugField(max_length=255)
|
||||
|
||||
__test__ = {'API_TESTS':"""
|
||||
# Create a couple of Places.
|
||||
>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
|
||||
>>> f.id
|
||||
1
|
||||
>>> b = Bar(b = "bcd")
|
||||
>>> b.a
|
||||
<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')
|
||||
[]
|
||||
|
||||
# Regression test for #5079 -- ensure decimals don't go through a corrupting
|
||||
# float conversion during save.
|
||||
>>> bd = BigD(d="12.9")
|
||||
>>> bd.save()
|
||||
>>> bd = BigD.objects.get(pk=bd.pk)
|
||||
>>> bd.d == decimal.Decimal("12.9")
|
||||
True
|
||||
|
||||
# Regression test for #9706: ensure SlugField honors max_length.
|
||||
>>> bs = BigS.objects.create(s = 'slug' * 50)
|
||||
>>> bs = BigS.objects.get(pk=bs.pk)
|
||||
>>> bs.s == 'slug' * 50
|
||||
True
|
||||
"""}
|
||||
|
|
|
@ -1,79 +1,109 @@
|
|||
"""
|
||||
>>> from django.db.models.fields import *
|
||||
>>> try:
|
||||
... from decimal import Decimal
|
||||
... except ImportError:
|
||||
... from django.utils._decimal import Decimal
|
||||
import datetime
|
||||
import unittest
|
||||
import django.test
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from models import Foo, Bar, Whiz, BigD, BigS
|
||||
try:
|
||||
from decimal import Decimal
|
||||
except ImportError:
|
||||
from django.utils._decimal import Decimal
|
||||
|
||||
# DecimalField
|
||||
class DecimalFieldTests(django.test.TestCase):
|
||||
def test_to_python(self):
|
||||
f = models.DecimalField(max_digits=4, decimal_places=2)
|
||||
self.assertEqual(f.to_python(3), Decimal("3"))
|
||||
self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
|
||||
self.assertRaises(ValidationError, f.to_python, "abc")
|
||||
|
||||
>>> f = DecimalField(max_digits=4, decimal_places=2)
|
||||
def test_default(self):
|
||||
f = models.DecimalField(default=Decimal("0.00"))
|
||||
self.assertEqual(f.get_default(), Decimal("0.00"))
|
||||
|
||||
>>> f.to_python(3) == Decimal("3")
|
||||
True
|
||||
def test_format(self):
|
||||
f = models.DecimalField(max_digits=5, decimal_places=1)
|
||||
self.assertEqual(f._format(f.to_python(2)), u'2.0')
|
||||
self.assertEqual(f._format(f.to_python('2.6')), u'2.6')
|
||||
self.assertEqual(f._format(None), None)
|
||||
|
||||
>>> f.to_python("3.14") == Decimal("3.14")
|
||||
True
|
||||
def test_get_db_prep_lookup(self):
|
||||
f = models.DecimalField(max_digits=5, decimal_places=1)
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', None), [None])
|
||||
|
||||
>>> f.to_python("abc")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: This value must be a decimal number.
|
||||
def test_filter_with_strings(self):
|
||||
"""
|
||||
We should be able to filter decimal fields using strings (#8023)
|
||||
"""
|
||||
Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
|
||||
self.assertEqual(list(Foo.objects.filter(d=u'1.23')), [])
|
||||
|
||||
>>> f = DecimalField(default=Decimal("0.00"))
|
||||
>>> f.get_default()
|
||||
Decimal("0.00")
|
||||
def test_save_wihout_float_conversion(self):
|
||||
"""
|
||||
Ensure decimals don't go through a corrupting float conversion during
|
||||
save (#5079).
|
||||
"""
|
||||
bd = BigD(d="12.9")
|
||||
bd.save()
|
||||
bd = BigD.objects.get(pk=bd.pk)
|
||||
self.assertEqual(bd.d, Decimal("12.9"))
|
||||
|
||||
>>> f = DecimalField(max_digits=5, decimal_places=1)
|
||||
>>> x = f.to_python(2)
|
||||
>>> y = f.to_python('2.6')
|
||||
class ForeignKeyTests(django.test.TestCase):
|
||||
def test_callable_default(self):
|
||||
"""Test the use of a lazy callable for ForeignKey.default"""
|
||||
a = Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
|
||||
b = Bar.objects.create(b="bcd")
|
||||
self.assertEqual(b.a, a)
|
||||
|
||||
>>> f._format(x)
|
||||
u'2.0'
|
||||
>>> f._format(y)
|
||||
u'2.6'
|
||||
>>> f._format(None)
|
||||
>>> f.get_db_prep_lookup('exact', None)
|
||||
[None]
|
||||
class DateTimeFieldTests(unittest.TestCase):
|
||||
def test_datetimefield_to_python_usecs(self):
|
||||
"""DateTimeField.to_python should support usecs"""
|
||||
f = models.DateTimeField()
|
||||
self.assertEqual(f.to_python('2001-01-02 03:04:05.000006'),
|
||||
datetime.datetime(2001, 1, 2, 3, 4, 5, 6))
|
||||
self.assertEqual(f.to_python('2001-01-02 03:04:05.999999'),
|
||||
datetime.datetime(2001, 1, 2, 3, 4, 5, 999999))
|
||||
|
||||
# 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)
|
||||
def test_timefield_to_python_usecs(self):
|
||||
"""TimeField.to_python should support usecs"""
|
||||
f = models.TimeField()
|
||||
self.assertEqual(f.to_python('01:02:03.000004'),
|
||||
datetime.time(1, 2, 3, 4))
|
||||
self.assertEqual(f.to_python('01:02:03.999999'),
|
||||
datetime.time(1, 2, 3, 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)
|
||||
class BooleanFieldTests(unittest.TestCase):
|
||||
def _test_get_db_prep_lookup(self, f):
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', True), [True])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', '1'), [True])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', 1), [True])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', False), [False])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', '0'), [False])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', 0), [False])
|
||||
self.assertEqual(f.get_db_prep_lookup('exact', None), [None])
|
||||
|
||||
# Boolean and null boolean fields
|
||||
>>> f = BooleanField()
|
||||
>>> for val in (True, '1', 1):
|
||||
... f.get_db_prep_lookup('exact', val)
|
||||
[True]
|
||||
[True]
|
||||
[True]
|
||||
>>> for val in (False, '0', 0):
|
||||
... f.get_db_prep_lookup('exact', val)
|
||||
[False]
|
||||
[False]
|
||||
[False]
|
||||
def test_booleanfield_get_db_prep_lookup(self):
|
||||
self._test_get_db_prep_lookup(models.BooleanField())
|
||||
|
||||
>>> f = NullBooleanField()
|
||||
>>> for val in (True, '1', 1):
|
||||
... f.get_db_prep_lookup('exact', val)
|
||||
[True]
|
||||
[True]
|
||||
[True]
|
||||
>>> for val in (False, '0', 0):
|
||||
... f.get_db_prep_lookup('exact', val)
|
||||
[False]
|
||||
[False]
|
||||
[False]
|
||||
>>> f.get_db_prep_lookup('exact', None)
|
||||
[None]
|
||||
def test_nullbooleanfield_get_db_prep_lookup(self):
|
||||
self._test_get_db_prep_lookup(models.NullBooleanField())
|
||||
|
||||
"""
|
||||
class ChoicesTests(django.test.TestCase):
|
||||
def test_choices_and_field_display(self):
|
||||
"""
|
||||
Check that get_choices and get_flatchoices interact with
|
||||
get_FIELD_display to return the expected values (#7913).
|
||||
"""
|
||||
self.assertEqual(Whiz(c=1).get_c_display(), 'First') # A nested value
|
||||
self.assertEqual(Whiz(c=0).get_c_display(), 'Other') # A top level value
|
||||
self.assertEqual(Whiz(c=9).get_c_display(), 9) # Invalid value
|
||||
self.assertEqual(Whiz(c=None).get_c_display(), None) # Blank value
|
||||
self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value
|
||||
|
||||
class SlugFieldTests(django.test.TestCase):
|
||||
def test_slugfield_max_length(self):
|
||||
"""
|
||||
Make sure SlugField honors max_length (#9706)
|
||||
"""
|
||||
bs = BigS.objects.create(s = 'slug'*50)
|
||||
bs = BigS.objects.get(pk=bs.pk)
|
||||
self.assertEqual(bs.s, 'slug'*50)
|
Loading…
Reference in New Issue