summaryrefslogtreecommitdiff
path: root/simplejson/tests/test_decimal.py
blob: a694c7e404d43e1871fd3ca3a71ea789f1f4da21 (plain)
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
from decimal import Decimal
from unittest import TestCase

import simplejson as json

class TestDecimal(TestCase):
    NUMS = "1.0", "10.00", "1.1", "1234567890.1234567890", "500"
    def test_decimal_encode(self):
        for d in map(Decimal, self.NUMS):
            self.assertEquals(json.dumps(d, use_decimal=True), str(d))
    
    def test_decimal_decode(self):
        for s in self.NUMS:
            self.assertEquals(json.loads(s, parse_float=Decimal), Decimal(s))
    
    def test_decimal_roundtrip(self):
        for d in map(Decimal, self.NUMS):
            # The type might not be the same (int and Decimal) but they
            # should still compare equal.
            self.assertEquals(
                json.loads(
                    json.dumps(d, use_decimal=True), parse_float=Decimal),
                d)
            self.assertEquals(
                json.loads(
                    json.dumps([d], use_decimal=True), parse_float=Decimal),
                [d])

    def test_decimal_defaults(self):
        d = Decimal(1)
        # use_decimal=False is the default
        self.assertRaises(TypeError, json.dumps, d, use_decimal=False)
        self.assertRaises(TypeError, json.dumps, d)