|
| 1 | +from django.core.exceptions import ValidationError |
| 2 | +from django.test import TestCase |
| 3 | +from django.utils import translation |
| 4 | + |
| 5 | +from hypha.apply.funds.blocks import LocalizedFloatField |
| 6 | + |
| 7 | + |
| 8 | +class TestLocalizedFloatField(TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.field = LocalizedFloatField() |
| 11 | + |
| 12 | + # --- Both separators present: last one is the decimal --- |
| 13 | + |
| 14 | + def test_dot_thousands_comma_decimal(self): |
| 15 | + """1.000,50 → 1000.5 (dot-thousands, comma-decimal)""" |
| 16 | + self.assertAlmostEqual(self.field.clean("1.000,50"), 1000.5) |
| 17 | + |
| 18 | + def test_comma_thousands_dot_decimal(self): |
| 19 | + """1,000.50 → 1000.5 (comma-thousands, dot-decimal)""" |
| 20 | + self.assertAlmostEqual(self.field.clean("1,000.50"), 1000.5) |
| 21 | + |
| 22 | + # --- Multiple identical separators: all thousands --- |
| 23 | + |
| 24 | + def test_dot_thousands_millions(self): |
| 25 | + """1.000.000 → 1000000 (dot-thousands)""" |
| 26 | + self.assertAlmostEqual(self.field.clean("1.000.000"), 1_000_000) |
| 27 | + |
| 28 | + def test_comma_thousands_millions(self): |
| 29 | + """1,000,000 → 1000000 (comma-thousands)""" |
| 30 | + self.assertAlmostEqual(self.field.clean("1,000,000"), 1_000_000) |
| 31 | + |
| 32 | + # --- Single separator: 3 digits after = thousands, 1-2 digits = decimal --- |
| 33 | + |
| 34 | + def test_dot_thousands(self): |
| 35 | + """10.000 → 10000 (dot-thousands)""" |
| 36 | + self.assertAlmostEqual(self.field.clean("10.000"), 10_000) |
| 37 | + |
| 38 | + def test_comma_thousands(self): |
| 39 | + """10,000 → 10000 (comma-thousands)""" |
| 40 | + self.assertAlmostEqual(self.field.clean("10,000"), 10_000) |
| 41 | + |
| 42 | + def test_dot_decimal(self): |
| 43 | + """10.50 → 10.5 (dot-decimal)""" |
| 44 | + self.assertAlmostEqual(self.field.clean("10.50"), 10.5) |
| 45 | + |
| 46 | + def test_comma_decimal(self): |
| 47 | + """10,50 → 10.5 (comma-decimal)""" |
| 48 | + self.assertAlmostEqual(self.field.clean("10,50"), 10.5) |
| 49 | + |
| 50 | + def test_comma_decimal_one_digit(self): |
| 51 | + """1,5 → 1.5 (comma-decimal)""" |
| 52 | + self.assertAlmostEqual(self.field.clean("1,5"), 1.5) |
| 53 | + |
| 54 | + def test_large_amount_comma_decimal(self): |
| 55 | + """10000,00 → 10000.0 (comma-decimal, no thousands separator)""" |
| 56 | + self.assertAlmostEqual(self.field.clean("10000,00"), 10_000.0) |
| 57 | + |
| 58 | + # --- Plain numbers and integer/float return type --- |
| 59 | + |
| 60 | + def test_plain_integer(self): |
| 61 | + self.assertEqual(self.field.clean("10000"), 10_000) |
| 62 | + self.assertIsInstance(self.field.clean("10000"), int) |
| 63 | + |
| 64 | + def test_whole_number_returns_int(self): |
| 65 | + """10000.00 and 10000,00 should be stored as 10000, not 10000.0""" |
| 66 | + self.assertIsInstance(self.field.clean("10000.00"), int) |
| 67 | + self.assertIsInstance(self.field.clean("10000,00"), int) |
| 68 | + |
| 69 | + def test_fractional_number_returns_float(self): |
| 70 | + """10000.50 should be stored as 10000.5, not 10000""" |
| 71 | + result = self.field.clean("10000.50") |
| 72 | + self.assertIsInstance(result, float) |
| 73 | + self.assertAlmostEqual(result, 10000.5) |
| 74 | + |
| 75 | + def test_zero(self): |
| 76 | + self.assertEqual(self.field.clean("0"), 0) |
| 77 | + self.assertIsInstance(self.field.clean("0"), int) |
| 78 | + |
| 79 | + # --- prepare_value: display stored value in locale format --- |
| 80 | + |
| 81 | + def test_prepare_value_uses_locale_decimal_separator(self): |
| 82 | + """Stored float 10000.5 should display as "10000,5" in a comma-decimal locale.""" |
| 83 | + with translation.override("sv"): |
| 84 | + self.assertEqual(self.field.prepare_value(10000.5), "10000,5") |
| 85 | + |
| 86 | + def test_prepare_value_integer_unchanged(self): |
| 87 | + """Stored int 10000 should display as 10000 (no decimal separator added).""" |
| 88 | + with translation.override("sv"): |
| 89 | + self.assertEqual(self.field.prepare_value(10000), 10000) |
| 90 | + |
| 91 | + def test_prepare_value_string_unchanged(self): |
| 92 | + """A string value (re-display after validation error) is not reformatted.""" |
| 93 | + with translation.override("sv"): |
| 94 | + self.assertEqual(self.field.prepare_value("10000,5"), "10000,5") |
| 95 | + |
| 96 | + # --- Validation --- |
| 97 | + |
| 98 | + def test_negative_rejected_when_min_value_set(self): |
| 99 | + field = LocalizedFloatField(min_value=0) |
| 100 | + with self.assertRaises(ValidationError): |
| 101 | + field.clean("-1") |
0 commit comments