Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #10692 -- Fixed DecimalField lookups for extreme values.

Backport of r10545 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10546 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1e0c34dd7fb006273ee313330dbf21bab66462d0 1 parent f0c9bc5
@malcolmt malcolmt authored
View
5 django/db/models/fields/__init__.py
@@ -611,10 +611,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,
View
30 tests/regressiontests/model_fields/tests.py
@@ -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])
@@ -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).
@@ -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"""
@@ -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))
@@ -111,7 +122,7 @@ 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):
"""
@@ -119,4 +130,5 @@ def test_slugfield_max_length(self):
"""
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 1e0c34d

Please sign in to comment.
Something went wrong with that request. Please try again.