From bc7b4a999b5ddfebbc2449c3290e70a791755bce Mon Sep 17 00:00:00 2001 From: Emmanuel Leblond Date: Fri, 10 Jul 2015 14:31:42 +0200 Subject: [PATCH] Add unittest underlying the bug --- tests/fields/fields.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 9f9db25d6..46ab4f849 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -481,6 +481,34 @@ class Person(Document): actual = list(Person.objects().scalar('btc')) self.assertEqual(expected, actual) + def test_decimal_force_string(self): + class Person(Document): + btc = DecimalField(precision=4, force_string=True) + + Person.drop_collection() + Person(btc=10).save() + Person(btc=10.1).save() + Person(btc=10.11).save() + Person(btc="10.111").save() + Person(btc=Decimal("10.1111")).save() + Person(btc=Decimal("10.11111")).save() + + # How its stored + expected = [{'btc': '10.0000'}, {'btc': '10.1000'}, {'btc': '10.1100'}, + {'btc': '10.1110'}, {'btc': '10.1111'}, {'btc': '10.1111'}] + expected = [{'btc': '10.0000'}] + actual = list(Person.objects.exclude('id').as_pymongo()) + self.assertEqual(expected, actual) + + # Test as well setting on existing object + Person.drop_collection() + for value in (10, 10.1, 10.11, "10.111", "10.1111", "10.11111"): + p = Person().save() + p.btc = value + p.save() + actual = list(Person.objects.exclude('id').as_pymongo()) + self.assertEqual(expected, actual) + def test_boolean_validation(self): """Ensure that invalid values cannot be assigned to boolean fields. """