Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

TestClass3.keys()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
1
import json
2
import unittest
3
from datetime import datetime
4
5
from coalib.output.JSONEncoder import create_json_encoder
6
7
8
class TestClass1(object):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable object does not seem to be defined.
Loading history...
9
10
    def __init__(self):
11
        self.a = 0
12
13
14
class TestClass2(object):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable object does not seem to be defined.
Loading history...
15
16
    def __init__(self):
17
        self.a = 0
18
        self.b = TestClass1()
19
20
21
class TestClass3(object):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable object does not seem to be defined.
Loading history...
22
23
    def __init__(self):
24
        self.a = 0
25
        self.b = TestClass1()
26
27
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
28
    def __getitem__(key):
29
        return "val"
30
31
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
32
    def keys():
33
        return ["key"]
34
35
36
class PropertiedClass(object):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable object does not seem to be defined.
Loading history...
37
38
    def __init__(self):
39
        self._a = 5
40
41
    @property
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable property does not seem to be defined.
Loading history...
42
    def prop(self):
43
        return self._a
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
44
45
46
class JSONAbleClass(object):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable object does not seem to be defined.
Loading history...
47
48
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
49
    def __json__():
50
        return ['dont', 'panic']
51
52
53
class JSONEncoderTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
54
    JSONEncoder = create_json_encoder(use_relpath=True)
55
    kw = {"cls": JSONEncoder, "sort_keys": True}
56
57
    def test_builtins(self):
58
        self.assertEquals('"test"', json.dumps("test", **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
59
        self.assertEquals('1', json.dumps(1, **self.kw))
60
        self.assertEquals('true', json.dumps(True, **self.kw))
61
        self.assertEquals('null', json.dumps(None, **self.kw))
62
63
    def test_iter(self):
64
        self.assertEquals('[0, 1]', json.dumps([0, 1], **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
65
        self.assertEquals('[0, 1]', json.dumps((0, 1), **self.kw))
66
        self.assertEquals('[0, 1]', json.dumps(range(2), **self.kw))
67
68
    def test_dict(self):
69
        self.assertEquals('{"0": 1}', json.dumps({0: 1}, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
70
        self.assertEquals('{"0": 1}', json.dumps({"0": 1}, **self.kw))
71
        self.assertEquals('{"0": "1"}', json.dumps({"0": "1"}, **self.kw))
72
73
    def test_time(self):
74
        tf = datetime.today()
75
        self.assertEquals('"' + tf.isoformat() + '"',
76
                          json.dumps(tf, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable tf does not seem to be defined.
Loading history...
77
78
    def test_class1(self):
79
        tc1 = TestClass1()
80
        self.assertEquals('{"a": 0}', json.dumps(tc1, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tc1 does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
81
        self.assertEquals('[{"a": 0}]', json.dumps([tc1], **self.kw))
82
        self.assertEquals('{"0": {"a": 0}}', json.dumps({0: tc1}, **self.kw))
83
84
    def test_class2(self):
85
        tc2 = TestClass2()
86
        self.assertEquals('{"a": 0, "b": {"a": 0}}',
87
                          json.dumps(tc2, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tc2 does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
88
89
    def test_class3(self):
90
        tc3 = TestClass3()
91
        self.assertEquals('{"key": "val"}',
92
                          json.dumps(tc3, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable tc3 does not seem to be defined.
Loading history...
93
94
    def test_propertied_class(self):
95
        uut = PropertiedClass()
96
        self.assertEqual('{"prop": 5}', json.dumps(uut, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
97
98
    def test_jsonable_class(self):
99
        uut = JSONAbleClass()
100
        self.assertEqual('["dont", "panic"]', json.dumps(uut, **self.kw))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
101
102
    def test_type_error(self):
103
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
104
            json.dumps(1j, **self.kw)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
105