From 90200dbe9ce2d6ced0fa5fb0b3b5cd274015a616 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Mon, 12 Apr 2010 15:59:20 +0100 Subject: [PATCH] Fixed DecimalField bug --- mongoengine/fields.py | 3 +++ tests/fields.py | 10 ++++++++-- tests/queryset.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 9a9f4e0e..94a1e242 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -155,6 +155,9 @@ class DecimalField(BaseField): if not isinstance(value, basestring): value = unicode(value) return decimal.Decimal(value) + + def to_mongo(self, value): + return unicode(value) def validate(self, value): if not isinstance(value, decimal.Decimal): diff --git a/tests/fields.py b/tests/fields.py index 986f0e26..24acc02e 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -136,12 +136,16 @@ class FieldTest(unittest.TestCase): height = DecimalField(min_value=Decimal('0.1'), max_value=Decimal('3.5')) + Person.drop_collection() + person = Person() person.height = Decimal('1.89') - person.validate() + person.save() + person.reload() + self.assertEqual(person.height, Decimal('1.89')) person.height = '2.0' - person.validate() + person.save() person.height = 0.01 self.assertRaises(ValidationError, person.validate) person.height = Decimal('0.01') @@ -149,6 +153,8 @@ class FieldTest(unittest.TestCase): person.height = Decimal('4.0') self.assertRaises(ValidationError, person.validate) + Person.drop_collection() + def test_boolean_validation(self): """Ensure that invalid values cannot be assigned to boolean fields. """ diff --git a/tests/queryset.py b/tests/queryset.py index 668f0355..e1fa6f45 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -623,6 +623,27 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() + def test_update_pull(self): + """Ensure that the 'pull' update operation works correctly. + """ + class Comment(EmbeddedDocument): + content = StringField() + + class BlogPost(Document): + slug = StringField() + comments = ListField(EmbeddedDocumentField(Comment)) + + comment1 = Comment(content="test1") + comment2 = Comment(content="test2") + + post = BlogPost(slug="test", comments=[comment1, comment2]) + post.save() + self.assertTrue(comment2 in post.comments) + + BlogPost.objects(slug="test").update(pull__comments__content="test2") + post.reload() + self.assertTrue(comment2 not in post.comments) + def test_order_by(self): """Ensure that QuerySets may be ordered. """