Passed
Push — master ( 68a360...546f3a )
by Fabio
03:54
created

tests.test_io_dict   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
eloc 88
dl 0
loc 126
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A IODictTestCase.input_path() 0 4 1
A IODictTestCase.test_from_json_with_valid_url_invalid_content() 0 8 3
A IODictTestCase.test_from_json_with_invalid_url() 0 8 3
A IODictTestCase.test_to_json() 0 11 1
A IODictTestCase.test_from_json_with_valid_data() 0 10 1
A IODictTestCase.tearDownClass() 0 3 1
A IODictTestCase.test_from_json_with_invalid_file() 0 8 3
A IODictTestCase.test_from_json_with_invalid_data() 0 8 3
A IODictTestCase.test_from_json_with_valid_url_valid_content() 0 8 1
A IODictTestCase.test_to_json_file() 0 12 1
A IODictTestCase.test_from_json_with_valid_file_valid_content() 0 8 1
A IODictTestCase.test_from_json_with_valid_file_invalid_content() 0 8 3
A IODictTestCase.output_path() 0 4 1
1
# -*- coding: utf-8 -*-
2
3
from benedict.dicts.io import IODict
4
5
import os
6
import shutil
7
import unittest
8
9
10
class IODictTestCase(unittest.TestCase):
11
12
    @classmethod
13
    def tearDownClass(cls):
14
        shutil.rmtree(cls.output_path(filepath=''))
15
16
    @staticmethod
17
    def input_path(filepath):
18
        dir_path = os.path.dirname(os.path.realpath(__file__))
19
        return os.path.join(dir_path, 'input/{}'.format(filepath))
20
21
    @staticmethod
22
    def output_path(filepath):
23
        dir_path = os.path.dirname(os.path.realpath(__file__))
24
        return os.path.join(dir_path, 'output/{}'.format(filepath))
25
26
    # JSON
27
28
    def test_from_json_with_valid_data(self):
29
        j = '{"a": 1, "b": 2, "c": 3}'
30
        # static method
31
        d = IODict.from_json(j)
32
        self.assertTrue(isinstance(d, dict))
33
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
34
        # constructor
35
        d = IODict(j)
36
        self.assertTrue(isinstance(d, dict))
37
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
38
39
    def test_from_json_with_invalid_data(self):
40
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
41
        # static method
42
        with self.assertRaises(ValueError):
43
            d = IODict.from_json(j)
44
        # constructor
45
        with self.assertRaises(ValueError):
46
            d = IODict(j)
47
48
    def test_from_json_with_valid_file_valid_content(self):
49
        filepath = self.input_path('valid-content.json')
50
        # static method
51
        d = IODict.from_json(filepath)
52
        self.assertTrue(isinstance(d, dict))
53
        # constructor
54
        d = IODict(filepath)
55
        self.assertTrue(isinstance(d, dict))
56
57
    def test_from_json_with_valid_file_invalid_content(self):
58
        filepath = self.input_path('invalid-content.json')
59
        # static method
60
        with self.assertRaises(ValueError):
61
            d = IODict.from_json(filepath)
62
        # constructor
63
        with self.assertRaises(ValueError):
64
            d = IODict(filepath)
65
66
    def test_from_json_with_invalid_file(self):
67
        filepath = self.input_path('invalid-file.json')
68
        # static method
69
        with self.assertRaises(ValueError):
70
            d = IODict.from_json(filepath)
71
        # constructor
72
        with self.assertRaises(ValueError):
73
            d = IODict(filepath)
74
75
    def test_from_json_with_valid_url_valid_content(self):
76
        url = 'https://jsonplaceholder.typicode.com/users'
77
        # static method
78
        d = IODict.from_json(url)
79
        self.assertTrue(isinstance(d, dict))
80
        # constructor
81
        d = IODict(url)
82
        self.assertTrue(isinstance(d, dict))
83
84
    def test_from_json_with_valid_url_invalid_content(self):
85
        url = 'https://github.com/fabiocaccamo/python-benedict'
86
        # static method
87
        with self.assertRaises(ValueError):
88
            d = IODict.from_json(url)
89
        # constructor
90
        with self.assertRaises(ValueError):
91
            d = IODict(url)
92
93
    def test_from_json_with_invalid_url(self):
94
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
95
        # static method
96
        with self.assertRaises(ValueError):
97
            d = IODict.from_json(url)
98
        # constructor
99
        with self.assertRaises(ValueError):
100
            d = IODict(url)
101
102
    def test_to_json(self):
103
        d = IODict({
104
            'x': 7,
105
            'y': 8,
106
            'z': 9,
107
            'a': 1,
108
            'b': 2,
109
            'c': 3,
110
        })
111
        s = d.to_json(sort_keys=True)
112
        self.assertEqual(s, '{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}')
113
114
    def test_to_json_file(self):
115
        d = IODict({
116
            'x': 7,
117
            'y': 8,
118
            'z': 9,
119
            'a': 1,
120
            'b': 2,
121
            'c': 3,
122
        })
123
        filepath = self.output_path('test_to_json_file.json')
124
        s = d.to_json(filepath=filepath, sort_keys=True)
125
        self.assertEqual(d, IODict.from_json(filepath))
126
127
    # def test_from_query_string(self):
128
    #     pass
129
130
    # def test_from_query_string_file(self):
131
    #     pass
132
133
    # def test_from_query_string_url(self):
134
    #     pass
135
136
    # def test_from_toml_string(self):
137
    #     pass
138
139
    # def test_from_toml_file(self):
140
    #     pass
141
142
    # def test_from_toml_url(self):
143
    #     pass
144
145
    # def test_from_xml_string(self):
146
    #     pass
147
148
    # def test_from_xml_file(self):
149
    #     pass
150
151
    # def test_from_xml_url(self):
152
    #     pass
153
154
    # def test_from_yaml_string(self):
155
    #     pass
156
157
    # def test_from_yaml_file(self):
158
    #     pass
159
160
    # def test_from_yaml_url(self):
161
    #     pass
162
163
    # def test_to_base64(self):
164
    #     pass
165
166
    # def test_to_base64_file(self):
167
    #     pass
168
169
    # def test_to_query_string(self):
170
    #     pass
171
172
    # def test_to_query_string_file(self):
173
    #     pass
174
175
    # def test_to_toml(self):
176
    #     pass
177
178
    # def test_to_toml_file(self):
179
    #     pass
180
181
    # def test_to_xml(self):
182
    #     pass
183
184
    # def test_to_xml_file(self):
185
    #     pass
186
187
    # def test_to_yaml(self):
188
    #     pass
189
190
    # def test_to_yaml_file(self):
191
    #     pass
192