test_decimalfield.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import unittest
  2. from decimal import Decimal
  3. from django.core import validators
  4. from django.core.exceptions import ValidationError
  5. from django.db import connection, models
  6. from django.test import TestCase
  7. from .models import BigD, Foo
  8. class DecimalFieldTests(TestCase):
  9. def test_to_python(self):
  10. f = models.DecimalField(max_digits=4, decimal_places=2)
  11. self.assertEqual(f.to_python(3), Decimal('3'))
  12. self.assertEqual(f.to_python('3.14'), Decimal('3.14'))
  13. # to_python() converts floats and honors max_digits.
  14. self.assertEqual(f.to_python(3.1415926535897), Decimal('3.142'))
  15. self.assertEqual(f.to_python(2.4), Decimal('2.400'))
  16. # Uses default rounding of ROUND_HALF_EVEN.
  17. self.assertEqual(f.to_python(2.0625), Decimal('2.062'))
  18. self.assertEqual(f.to_python(2.1875), Decimal('2.188'))
  19. msg = '“abc” value must be a decimal number.'
  20. with self.assertRaisesMessage(ValidationError, msg):
  21. f.to_python('abc')
  22. def test_default(self):
  23. f = models.DecimalField(default=Decimal('0.00'))
  24. self.assertEqual(f.get_default(), Decimal('0.00'))
  25. def test_get_prep_value(self):
  26. f = models.DecimalField(max_digits=5, decimal_places=1)
  27. self.assertIsNone(f.get_prep_value(None))
  28. self.assertEqual(f.get_prep_value('2.4'), Decimal('2.4'))
  29. def test_filter_with_strings(self):
  30. """
  31. Should be able to filter decimal fields using strings (#8023).
  32. """
  33. foo = Foo.objects.create(a='abc', d=Decimal('12.34'))
  34. self.assertEqual(list(Foo.objects.filter(d='12.34')), [foo])
  35. def test_save_without_float_conversion(self):
  36. """
  37. Ensure decimals don't go through a corrupting float conversion during
  38. save (#5079).
  39. """
  40. bd = BigD(d='12.9')
  41. bd.save()
  42. bd = BigD.objects.get(pk=bd.pk)
  43. self.assertEqual(bd.d, Decimal('12.9'))
  44. @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite stores values rounded to 15 significant digits.')
  45. def test_fetch_from_db_without_float_rounding(self):
  46. big_decimal = BigD.objects.create(d=Decimal('.100000000000000000000000000005'))
  47. big_decimal.refresh_from_db()
  48. self.assertEqual(big_decimal.d, Decimal('.100000000000000000000000000005'))
  49. def test_lookup_really_big_value(self):
  50. """
  51. Really big values can be used in a filter statement.
  52. """
  53. # This should not crash.
  54. Foo.objects.filter(d__gte=100000000000)
  55. def test_max_digits_validation(self):
  56. field = models.DecimalField(max_digits=2)
  57. expected_message = validators.DecimalValidator.messages['max_digits'] % {'max': 2}
  58. with self.assertRaisesMessage(ValidationError, expected_message):
  59. field.clean(100, None)
  60. def test_max_decimal_places_validation(self):
  61. field = models.DecimalField(decimal_places=1)
  62. expected_message = validators.DecimalValidator.messages['max_decimal_places'] % {'max': 1}
  63. with self.assertRaisesMessage(ValidationError, expected_message):
  64. field.clean(Decimal('0.99'), None)
  65. def test_max_whole_digits_validation(self):
  66. field = models.DecimalField(max_digits=3, decimal_places=1)
  67. expected_message = validators.DecimalValidator.messages['max_whole_digits'] % {'max': 2}
  68. with self.assertRaisesMessage(ValidationError, expected_message):
  69. field.clean(Decimal('999'), None)
  70. def test_roundtrip_with_trailing_zeros(self):
  71. """Trailing zeros in the fractional part aren't truncated."""
  72. obj = Foo.objects.create(a='bar', d=Decimal('8.320'))
  73. obj.refresh_from_db()
  74. self.assertEqual(obj.d.compare_total(Decimal('8.320')), Decimal('0'))