forked from mongodb/mongo-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decimal128.py
More file actions
84 lines (69 loc) · 2.84 KB
/
test_decimal128.py
File metadata and controls
84 lines (69 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2016-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Decimal128."""
import codecs
import glob
import json
import os.path
import pickle
import sys
from binascii import unhexlify
from decimal import Decimal, DecimalException
sys.path[0:0] = [""]
from bson import BSON
from bson.decimal128 import Decimal128, create_decimal128_context
from bson.json_util import dumps, loads
from bson.py3compat import b
from test import client_context, unittest
class TestDecimal128(unittest.TestCase):
def test_round_trip(self):
if not client_context.version.at_least(3, 3, 6):
raise unittest.SkipTest(
'Round trip test requires MongoDB >= 3.3.6')
coll = client_context.client.pymongo_test.test
coll.drop()
dec128 = Decimal128.from_bid(
b'\x00@cR\xbf\xc6\x01\x00\x00\x00\x00\x00\x00\x00\x1c0')
coll.insert_one({'dec128': dec128})
doc = coll.find_one({'dec128': dec128})
self.assertIsNotNone(doc)
self.assertEqual(doc['dec128'], dec128)
def test_pickle(self):
dec128 = Decimal128.from_bid(
b'\x00@cR\xbf\xc6\x01\x00\x00\x00\x00\x00\x00\x00\x1c0')
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
pkl = pickle.dumps(dec128, protocol=protocol)
self.assertEqual(dec128, pickle.loads(pkl))
def test_special(self):
dnan = Decimal('NaN')
dnnan = Decimal('-NaN')
dsnan = Decimal('sNaN')
dnsnan = Decimal('-sNaN')
dnan128 = Decimal128(dnan)
dnnan128 = Decimal128(dnnan)
dsnan128 = Decimal128(dsnan)
dnsnan128 = Decimal128(dnsnan)
# Due to the comparison rules for decimal.Decimal we have to
# compare strings.
self.assertEqual(str(dnan), str(dnan128.to_decimal()))
self.assertEqual(str(dnnan), str(dnnan128.to_decimal()))
self.assertEqual(str(dsnan), str(dsnan128.to_decimal()))
self.assertEqual(str(dnsnan), str(dnsnan128.to_decimal()))
def test_decimal128_context(self):
ctx = create_decimal128_context()
self.assertEqual("NaN", str(ctx.copy().create_decimal(".13.1")))
self.assertEqual("Infinity", str(ctx.copy().create_decimal("1E6145")))
self.assertEqual("0E-6176", str(ctx.copy().create_decimal("1E-6177")))
if __name__ == '__main__':
unittest.main()