@@ -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