test_decimalfield.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from decimal import Decimal
  2. from django.core import validators
  3. from django.core.exceptions import ValidationError
  4. from django.db import models
  5. from django.test import TestCase
  6. from .models import BigD, Foo
  7. class DecimalFieldTests(TestCase):
  8. def test_to_python(self):
  9. f = models.DecimalField(max_digits=4, decimal_places=2)
  10. self.assertEqual(f.to_python(3), Decimal('3'))
  11. self.assertEqual(f.to_python('3.14'), Decimal('3.14'))
  12. with self.assertRaises(ValidationError):
  13. f.to_python('abc')
  14. def test_default(self):
  15. f = models.DecimalField(default=Decimal('0.00'))
  16. self.assertEqual(f.get_default(), Decimal('0.00'))
  17. def test_format(self):
  18. f = models.DecimalField(max_digits=5, decimal_places=1)
  19. self.assertEqual(f._format(f.to_python(2)), '2.0')
  20. self.assertEqual(f._format(f.to_python('2.6')), '2.6')
  21. self.assertEqual(f._format(None), None)
  22. def test_get_prep_value(self):
  23. f = models.DecimalField(max_digits=5, decimal_places=1)
  24. self.assertEqual(f.get_prep_value(None), None)
  25. self.assertEqual(f.get_prep_value('2.4'), Decimal('2.4'))
  26. def test_filter_with_strings(self):
  27. """
  28. Should be able to filter decimal fields using strings (#8023).
  29. """
  30. foo = Foo.objects.create(a='abc', d=Decimal('12.34'))
  31. self.assertEqual(list(Foo.objects.filter(d='12.34')), [foo])
  32. def test_save_without_float_conversion(self):
  33. """
  34. Ensure decimals don't go through a corrupting float conversion during
  35. save (#5079).
  36. """
  37. bd = BigD(d='12.9')
  38. bd.save()
  39. bd = BigD.objects.get(pk=bd.pk)
  40. self.assertEqual(bd.d, Decimal('12.9'))
  41. def test_lookup_really_big_value(self):
  42. """
  43. Really big values can be used in a filter statement.
  44. """
  45. # This should not crash.
  46. Foo.objects.filter(d__gte=100000000000)
  47. def test_max_digits_validation(self):
  48. field = models.DecimalField(max_digits=2)
  49. expected_message = validators.DecimalValidator.messages['max_digits'] % {'max': 2}
  50. with self.assertRaisesMessage(ValidationError, expected_message):
  51. field.clean(100, None)
  52. def test_max_decimal_places_validation(self):
  53. field = models.DecimalField(decimal_places=1)
  54. expected_message = validators.DecimalValidator.messages['max_decimal_places'] % {'max': 1}
  55. with self.assertRaisesMessage(ValidationError, expected_message):
  56. field.clean(Decimal('0.99'), None)
  57. def test_max_whole_digits_validation(self):
  58. field = models.DecimalField(max_digits=3, decimal_places=1)
  59. expected_message = validators.DecimalValidator.messages['max_whole_digits'] % {'max': 2}
  60. with self.assertRaisesMessage(ValidationError, expected_message):
  61. field.clean(Decimal('999'), None)