(self)
| 8 | |
| 9 | class TestDecimalField(MongoDBTestCase): |
| 10 | def test_storage(self): |
| 11 | class Person(Document): |
| 12 | float_value = DecimalField(precision=4) |
| 13 | string_value = DecimalField(precision=4, force_string=True) |
| 14 | |
| 15 | Person.drop_collection() |
| 16 | values_to_store = [ |
| 17 | 10, |
| 18 | 10.1, |
| 19 | 10.11, |
| 20 | "10.111", |
| 21 | Decimal("10.1111"), |
| 22 | Decimal("10.11111"), |
| 23 | ] |
| 24 | for store_at_creation in [True, False]: |
| 25 | for value in values_to_store: |
| 26 | # to_python is called explicitly if values were sent in the kwargs of __init__ |
| 27 | if store_at_creation: |
| 28 | Person(float_value=value, string_value=value).save() |
| 29 | else: |
| 30 | person = Person.objects.create() |
| 31 | person.float_value = value |
| 32 | person.string_value = value |
| 33 | person.save() |
| 34 | |
| 35 | # How its stored |
| 36 | expected = [ |
| 37 | {"float_value": 10.0, "string_value": "10.0000"}, |
| 38 | {"float_value": 10.1, "string_value": "10.1000"}, |
| 39 | {"float_value": 10.11, "string_value": "10.1100"}, |
| 40 | {"float_value": 10.111, "string_value": "10.1110"}, |
| 41 | {"float_value": 10.1111, "string_value": "10.1111"}, |
| 42 | {"float_value": 10.1111, "string_value": "10.1111"}, |
| 43 | ] |
| 44 | expected.extend(expected) |
| 45 | actual = list(Person.objects.exclude("id").as_pymongo()) |
| 46 | assert expected == actual |
| 47 | |
| 48 | # How it comes out locally |
| 49 | expected = [ |
| 50 | Decimal("10.0000"), |
| 51 | Decimal("10.1000"), |
| 52 | Decimal("10.1100"), |
| 53 | Decimal("10.1110"), |
| 54 | Decimal("10.1111"), |
| 55 | Decimal("10.1111"), |
| 56 | ] |
| 57 | expected.extend(expected) |
| 58 | for field_name in ["float_value", "string_value"]: |
| 59 | actual = list(Person.objects().scalar(field_name)) |
| 60 | assert expected == actual |
| 61 | |
| 62 | def test_save_none(self): |
| 63 | class Person(Document): |
nothing calls this directly
no test coverage detected