Skip to content

Commit

Permalink
Fixed django#10692 -- Fixed DecimalField lookups for extreme values.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10545 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Apr 12, 2009
1 parent 8931d8d commit e981402
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 4 additions & 1 deletion django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,13 @@ def format_number(self, value):
from django.db.backends import util
return util.format_number(value, self.max_digits, self.decimal_places)

def get_db_prep_value(self, value):
def get_db_prep_save(self, value):
return connection.ops.value_to_db_decimal(self.to_python(value),
self.max_digits, self.decimal_places)

def get_db_prep_value(self, value):
return self.to_python(value)

def formfield(self, **kwargs):
defaults = {
'max_digits': self.max_digits,
Expand Down
30 changes: 21 additions & 9 deletions tests/regressiontests/model_fields/tests.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import datetime
import unittest

import django.test
from django import forms
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

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")

def test_default(self):
f = models.DecimalField(default=Decimal("0.00"))
self.assertEqual(f.get_default(), Decimal("0.00"))

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)

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])
Expand All @@ -38,7 +41,7 @@ def test_filter_with_strings(self):
Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
self.assertEqual(list(Foo.objects.filter(d=u'1.23')), [])

def test_save_wihout_float_conversion(self):
def test_save_without_float_conversion(self):
"""
Ensure decimals don't go through a corrupting float conversion during
save (#5079).
Expand All @@ -48,6 +51,14 @@ def test_save_wihout_float_conversion(self):
bd = BigD.objects.get(pk=bd.pk)
self.assertEqual(bd.d, Decimal("12.9"))

def test_lookup_really_big_value(self):
"""
Ensure that really big values can be used in a filter statement, even
with older Python versions.
"""
# This should not crash. That counts as a win for our purposes.
Foo.objects.filter(d__gte=100000000000)

class ForeignKeyTests(django.test.TestCase):
def test_callable_default(self):
"""Test the use of a lazy callable for ForeignKey.default"""
Expand All @@ -63,11 +74,11 @@ def test_datetimefield_to_python_usecs(self):
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))

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'),
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))
Expand Down Expand Up @@ -111,12 +122,13 @@ def test_choices_and_field_display(self):
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)
self.assertEqual(bs.s, 'slug'*50)

0 comments on commit e981402

Please sign in to comment.