Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / simplejson / tests / test_decimal.py @ 545

History | View | Annotate | Download (2.48 KB)

1
import decimal
2
from decimal import Decimal
3
from unittest import TestCase
4
from simplejson.compat import StringIO, reload_module
5

    
6
import simplejson as json
7

    
8
class TestDecimal(TestCase):
9
    NUMS = "1.0", "10.00", "1.1", "1234567890.1234567890", "500"
10
    def dumps(self, obj, **kw):
11
        sio = StringIO()
12
        json.dump(obj, sio, **kw)
13
        res = json.dumps(obj, **kw)
14
        self.assertEqual(res, sio.getvalue())
15
        return res
16

    
17
    def loads(self, s, **kw):
18
        sio = StringIO(s)
19
        res = json.loads(s, **kw)
20
        self.assertEqual(res, json.load(sio, **kw))
21
        return res
22

    
23
    def test_decimal_encode(self):
24
        for d in map(Decimal, self.NUMS):
25
            self.assertEqual(self.dumps(d, use_decimal=True), str(d))
26

    
27
    def test_decimal_decode(self):
28
        for s in self.NUMS:
29
            self.assertEqual(self.loads(s, parse_float=Decimal), Decimal(s))
30

    
31
    def test_stringify_key(self):
32
        for d in map(Decimal, self.NUMS):
33
            v = {d: d}
34
            self.assertEqual(
35
                self.loads(
36
                    self.dumps(v, use_decimal=True), parse_float=Decimal),
37
                {str(d): d})
38

    
39
    def test_decimal_roundtrip(self):
40
        for d in map(Decimal, self.NUMS):
41
            # The type might not be the same (int and Decimal) but they
42
            # should still compare equal.
43
            for v in [d, [d], {'': d}]:
44
                self.assertEqual(
45
                    self.loads(
46
                        self.dumps(v, use_decimal=True), parse_float=Decimal),
47
                    v)
48

    
49
    def test_decimal_defaults(self):
50
        d = Decimal('1.1')
51
        # use_decimal=True is the default
52
        self.assertRaises(TypeError, json.dumps, d, use_decimal=False)
53
        self.assertEqual('1.1', json.dumps(d))
54
        self.assertEqual('1.1', json.dumps(d, use_decimal=True))
55
        self.assertRaises(TypeError, json.dump, d, StringIO(),
56
                          use_decimal=False)
57
        sio = StringIO()
58
        json.dump(d, sio)
59
        self.assertEqual('1.1', sio.getvalue())
60
        sio = StringIO()
61
        json.dump(d, sio, use_decimal=True)
62
        self.assertEqual('1.1', sio.getvalue())
63

    
64
    def test_decimal_reload(self):
65
        # Simulate a subinterpreter that reloads the Python modules but not
66
        # the C code https://github.com/simplejson/simplejson/issues/34
67
        global Decimal
68
        Decimal = reload_module(decimal).Decimal
69
        import simplejson.encoder
70
        simplejson.encoder.Decimal = Decimal
71
        self.test_decimal_roundtrip()