Skip to content

Commit 0be6d36

Browse files
author
Omer Katz
committed
Merge branch 'master' of git://github.com/mrTable/mongoengine
2 parents 6f5f5b4 + 3b71a6b commit 0be6d36

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,4 @@ that much better:
242242
* xiaost7 (https://github.com/xiaost7)
243243
* Victor Varvaryuk
244244
* Stanislav Kaledin (https://github.com/sallyruthstruik)
245+
* Dmitry Yantsen (https://github.com/mrTable)

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
Development
6+
===========
7+
- Fixed absent rounding for DecimalField when `force_string` is set. #1103
8+
59
Changes in 0.10.8
610
=================
711
- Added support for QuerySet.batch_size (#1426)

mongoengine/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def to_mongo(self, value):
341341
if value is None:
342342
return value
343343
if self.force_string:
344-
return unicode(value)
344+
return unicode(self.to_python(value))
345345
return float(self.to_python(value))
346346

347347
def validate(self, value):

tests/fields/fields.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,27 +483,41 @@ class Person(Document):
483483

484484
def test_decimal_storage(self):
485485
class Person(Document):
486-
btc = DecimalField(precision=4)
486+
float_value = DecimalField(precision=4)
487+
string_value = DecimalField(precision=4, force_string=True)
487488

488489
Person.drop_collection()
489-
Person(btc=10).save()
490-
Person(btc=10.1).save()
491-
Person(btc=10.11).save()
492-
Person(btc="10.111").save()
493-
Person(btc=Decimal("10.1111")).save()
494-
Person(btc=Decimal("10.11111")).save()
490+
values_to_store = [10, 10.1, 10.11, "10.111", Decimal("10.1111"), Decimal("10.11111")]
491+
for store_at_creation in [True, False]:
492+
for value in values_to_store:
493+
# to_python is called explicitly if values were sent in the kwargs of __init__
494+
if store_at_creation:
495+
Person(float_value=value, string_value=value).save()
496+
else:
497+
person = Person.objects.create()
498+
person.float_value = value
499+
person.string_value = value
500+
person.save()
495501

496502
# How its stored
497-
expected = [{'btc': 10.0}, {'btc': 10.1}, {'btc': 10.11},
498-
{'btc': 10.111}, {'btc': 10.1111}, {'btc': 10.1111}]
503+
expected = [
504+
{'float_value': 10.0, 'string_value': '10.0000'},
505+
{'float_value': 10.1, 'string_value': '10.1000'},
506+
{'float_value': 10.11, 'string_value': '10.1100'},
507+
{'float_value': 10.111, 'string_value': '10.1110'},
508+
{'float_value': 10.1111, 'string_value': '10.1111'},
509+
{'float_value': 10.1111, 'string_value': '10.1111'}]
510+
expected.extend(expected)
499511
actual = list(Person.objects.exclude('id').as_pymongo())
500512
self.assertEqual(expected, actual)
501513

502514
# How it comes out locally
503515
expected = [Decimal('10.0000'), Decimal('10.1000'), Decimal('10.1100'),
504516
Decimal('10.1110'), Decimal('10.1111'), Decimal('10.1111')]
505-
actual = list(Person.objects().scalar('btc'))
506-
self.assertEqual(expected, actual)
517+
expected.extend(expected)
518+
for field_name in ['float_value', 'string_value']:
519+
actual = list(Person.objects().scalar(field_name))
520+
self.assertEqual(expected, actual)
507521

508522
def test_boolean_validation(self):
509523
"""Ensure that invalid values cannot be assigned to boolean fields.

0 commit comments

Comments
 (0)