hmarr/mongoengine — Issue #332: DecimalField inequality search is unusable
Author: tko
Created: 2011-10-25 16:37:12+00:00
State: open

```python
from decimal import Decimal

class QuerySetDecimalTest(unittest.TestCase):

def setUp(self):
connect(db='mongoenginetest')

class Person(Document):
money = DecimalField()
Person.drop_collection()

Person(money=6).save()
Person(money=8).save()
Person(money=10).save()

self.Person = Person

def test_as_decimal(self):
self.assertEqual(2, len(self.Person.objects(money__gt=Decimal("7"))))

def test_as_int(self):
self.assertEqual(2, len(self.Person.objects(money__gt=7)))

def test_as_string(self):
self.assertEqual(2, len(self.Person.objects(money__gt="7")))
```
None of the searches produce expected results, and one of them even throws an exception.

```
InvalidDocument: Cannot encode object: Decimal('7')

self.assertEqual(2, len(self.Person.objects(money__gt=7)))
AssertionError: 2 != 0

self.assertEqual(2, len(self.Person.objects(money__gt="7")))
AssertionError: 2 != 1
```