|
| 1 | +import json |
| 2 | +import random |
| 3 | +from decimal import Decimal |
| 4 | + |
| 5 | +import pytest |
| 6 | +from bson.decimal128 import Decimal128 |
| 7 | + |
| 8 | +from mongoengine import Decimal128Field, Document, ValidationError |
| 9 | +from tests.utils import MongoDBTestCase, get_as_pymongo |
| 10 | + |
| 11 | + |
| 12 | +class Decimal128Document(Document): |
| 13 | + dec128_fld = Decimal128Field() |
| 14 | + dec128_min_0 = Decimal128Field(min_value=0) |
| 15 | + dec128_max_100 = Decimal128Field(max_value=100) |
| 16 | + |
| 17 | + |
| 18 | +def generate_test_cls() -> Document: |
| 19 | + Decimal128Document.drop_collection() |
| 20 | + Decimal128Document(dec128_fld=None).save() |
| 21 | + Decimal128Document(dec128_fld=Decimal(1)).save() |
| 22 | + return Decimal128Document |
| 23 | + |
| 24 | + |
| 25 | +class TestDecimal128Field(MongoDBTestCase): |
| 26 | + def test_decimal128_validation_good(self): |
| 27 | + doc = Decimal128Document() |
| 28 | + |
| 29 | + doc.dec128_fld = Decimal(0) |
| 30 | + doc.validate() |
| 31 | + |
| 32 | + doc.dec128_fld = Decimal(50) |
| 33 | + doc.validate() |
| 34 | + |
| 35 | + doc.dec128_fld = Decimal(110) |
| 36 | + doc.validate() |
| 37 | + |
| 38 | + doc.dec128_fld = Decimal("110") |
| 39 | + doc.validate() |
| 40 | + |
| 41 | + def test_decimal128_validation_invalid(self): |
| 42 | + """Ensure that invalid values cannot be assigned.""" |
| 43 | + |
| 44 | + doc = Decimal128Document() |
| 45 | + |
| 46 | + doc.dec128_fld = "ten" |
| 47 | + |
| 48 | + with pytest.raises(ValidationError): |
| 49 | + doc.validate() |
| 50 | + |
| 51 | + def test_decimal128_validation_min(self): |
| 52 | + """Ensure that out of bounds values cannot be assigned.""" |
| 53 | + |
| 54 | + doc = Decimal128Document() |
| 55 | + |
| 56 | + doc.dec128_min_0 = Decimal(50) |
| 57 | + doc.validate() |
| 58 | + |
| 59 | + doc.dec128_min_0 = Decimal(-1) |
| 60 | + with pytest.raises(ValidationError): |
| 61 | + doc.validate() |
| 62 | + |
| 63 | + def test_decimal128_validation_max(self): |
| 64 | + """Ensure that out of bounds values cannot be assigned.""" |
| 65 | + |
| 66 | + doc = Decimal128Document() |
| 67 | + |
| 68 | + doc.dec128_max_100 = Decimal(50) |
| 69 | + doc.validate() |
| 70 | + |
| 71 | + doc.dec128_max_100 = Decimal(101) |
| 72 | + with pytest.raises(ValidationError): |
| 73 | + doc.validate() |
| 74 | + |
| 75 | + def test_eq_operator(self): |
| 76 | + cls = generate_test_cls() |
| 77 | + assert cls.objects(dec128_fld=1.0).count() == 1 |
| 78 | + assert cls.objects(dec128_fld=2.0).count() == 0 |
| 79 | + |
| 80 | + def test_ne_operator(self): |
| 81 | + cls = generate_test_cls() |
| 82 | + assert cls.objects(dec128_fld__ne=None).count() == 1 |
| 83 | + assert cls.objects(dec128_fld__ne=1).count() == 1 |
| 84 | + assert cls.objects(dec128_fld__ne=1.0).count() == 1 |
| 85 | + |
| 86 | + def test_gt_operator(self): |
| 87 | + cls = generate_test_cls() |
| 88 | + assert cls.objects(dec128_fld__gt=0.5).count() == 1 |
| 89 | + |
| 90 | + def test_lt_operator(self): |
| 91 | + cls = generate_test_cls() |
| 92 | + assert cls.objects(dec128_fld__lt=1.5).count() == 1 |
| 93 | + |
| 94 | + def test_field_exposed_as_python_Decimal(self): |
| 95 | + # from int |
| 96 | + model = Decimal128Document(dec128_fld=100).save() |
| 97 | + assert isinstance(model.dec128_fld, Decimal) |
| 98 | + model = Decimal128Document.objects.get(id=model.id) |
| 99 | + assert isinstance(model.dec128_fld, Decimal) |
| 100 | + assert model.dec128_fld == Decimal("100") |
| 101 | + |
| 102 | + def test_storage(self): |
| 103 | + # from int |
| 104 | + model = Decimal128Document(dec128_fld=100).save() |
| 105 | + assert get_as_pymongo(model) == { |
| 106 | + "_id": model.id, |
| 107 | + "dec128_fld": Decimal128("100"), |
| 108 | + } |
| 109 | + |
| 110 | + # from str |
| 111 | + model = Decimal128Document(dec128_fld="100.0").save() |
| 112 | + assert get_as_pymongo(model) == { |
| 113 | + "_id": model.id, |
| 114 | + "dec128_fld": Decimal128("100.0"), |
| 115 | + } |
| 116 | + |
| 117 | + # from float |
| 118 | + model = Decimal128Document(dec128_fld=100.0).save() |
| 119 | + assert get_as_pymongo(model) == { |
| 120 | + "_id": model.id, |
| 121 | + "dec128_fld": Decimal128("100"), |
| 122 | + } |
| 123 | + |
| 124 | + # from Decimal |
| 125 | + model = Decimal128Document(dec128_fld=Decimal(100)).save() |
| 126 | + assert get_as_pymongo(model) == { |
| 127 | + "_id": model.id, |
| 128 | + "dec128_fld": Decimal128("100"), |
| 129 | + } |
| 130 | + model = Decimal128Document(dec128_fld=Decimal("100.0")).save() |
| 131 | + assert get_as_pymongo(model) == { |
| 132 | + "_id": model.id, |
| 133 | + "dec128_fld": Decimal128("100.0"), |
| 134 | + } |
| 135 | + |
| 136 | + # from Decimal128 |
| 137 | + model = Decimal128Document(dec128_fld=Decimal128("100")).save() |
| 138 | + assert get_as_pymongo(model) == { |
| 139 | + "_id": model.id, |
| 140 | + "dec128_fld": Decimal128("100"), |
| 141 | + } |
| 142 | + |
| 143 | + def test_json(self): |
| 144 | + Decimal128Document.drop_collection() |
| 145 | + f = str(random.random()) |
| 146 | + Decimal128Document(dec128_fld=f).save() |
| 147 | + json_str = Decimal128Document.objects.to_json() |
| 148 | + array = json.loads(json_str) |
| 149 | + assert array[0]["dec128_fld"] == {"$numberDecimal": str(f)} |
0 commit comments