Completed
Push — master ( ad01fe...01a50b )
by Fabio
03:50
created

tests.test_io_dict.IODictTestCase.test_to_toml()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nop 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://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.json'
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.assertTrue(d, os.path.isfile(filepath))
126
        self.assertEqual(d, IODict.from_json(filepath))
127
128
    # YAML
129
# TOML
130
131
    def test_from_toml_with_valid_data(self):
132
        j = """
133
            a = 1
134
135
            [b]
136
            c = 3
137
            d = 4
138
        """
139
        # static method
140
        d = IODict.from_toml(j)
141
        self.assertTrue(isinstance(d, dict))
142
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
143
        # constructor
144
        d = IODict(j)
145
        self.assertTrue(isinstance(d, dict))
146
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
147
148
    def test_from_toml_with_invalid_data(self):
149
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
150
        # static method
151
        with self.assertRaises(ValueError):
152
            d = IODict.from_toml(j)
153
        # constructor
154
        with self.assertRaises(ValueError):
155
            d = IODict(j)
156
157
    def test_from_toml_with_valid_file_valid_content(self):
158
        filepath = self.input_path('valid-content.toml')
159
        # static method
160
        d = IODict.from_toml(filepath)
161
        self.assertTrue(isinstance(d, dict))
162
        # constructor
163
        d = IODict(filepath)
164
        self.assertTrue(isinstance(d, dict))
165
166
    def test_from_toml_with_valid_file_invalid_content(self):
167
        filepath = self.input_path('invalid-content.toml')
168
        # static method
169
        with self.assertRaises(ValueError):
170
            d = IODict.from_toml(filepath)
171
        # constructor
172
        with self.assertRaises(ValueError):
173
            d = IODict(filepath)
174
175
    def test_from_toml_with_invalid_file(self):
176
        filepath = self.input_path('invalid-file.toml')
177
        # static method
178
        with self.assertRaises(ValueError):
179
            d = IODict.from_toml(filepath)
180
        # constructor
181
        with self.assertRaises(ValueError):
182
            d = IODict(filepath)
183
184
    def test_from_toml_with_valid_url_valid_content(self):
185
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.toml'
186
        # static method
187
        d = IODict.from_toml(url)
188
        self.assertTrue(isinstance(d, dict))
189
        # constructor
190
        d = IODict(url)
191
        self.assertTrue(isinstance(d, dict))
192
193
    def test_from_toml_with_valid_url_invalid_content(self):
194
        url = 'https://github.com/fabiocaccamo/python-benedict'
195
        # static method
196
        with self.assertRaises(ValueError):
197
            d = IODict.from_toml(url)
198
        # constructor
199
        with self.assertRaises(ValueError):
200
            d = IODict(url)
201
202
    def test_from_toml_with_invalid_url(self):
203
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
204
        # static method
205
        with self.assertRaises(ValueError):
206
            d = IODict.from_toml(url)
207
        # constructor
208
        with self.assertRaises(ValueError):
209
            d = IODict(url)
210
211
    def test_to_toml(self):
212
        d = IODict({
213
            'x': 7,
214
            'y': 8,
215
            'z': 9,
216
            'a': 1,
217
            'b': 2,
218
            'c': 3,
219
        })
220
        s = d.to_toml()
221
        self.assertEqual(d, IODict.from_toml(s))
222
223
    def test_to_toml_file(self):
224
        d = IODict({
225
            'x': 7,
226
            'y': 8,
227
            'z': 9,
228
            'a': 1,
229
            'b': 2,
230
            'c': 3,
231
        })
232
        filepath = self.output_path('test_to_toml_file.toml')
233
        s = d.to_toml(filepath=filepath)
234
        self.assertTrue(d, os.path.isfile(filepath))
235
        self.assertEqual(d, IODict.from_toml(filepath))
236
237
# XML
238
239
    def test_from_xml_with_valid_data(self):
240
        j = """<?xml version="1.0" ?>
241
            <root>
242
                <a>1</a>
243
                <b>
244
                    <c>3</c>
245
                    <d>4</d>
246
                </b>
247
            </root>
248
        """
249
        # static method
250
        d = IODict.from_xml(j)
251
        self.assertTrue(isinstance(d, dict))
252
        self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },})
253
        # constructor
254
        d = IODict(j)
255
        self.assertTrue(isinstance(d, dict))
256
        self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },})
257
258
    def test_from_xml_with_invalid_data(self):
259
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
260
        # static method
261
        with self.assertRaises(ValueError):
262
            d = IODict.from_xml(j)
263
        # constructor
264
        with self.assertRaises(ValueError):
265
            d = IODict(j)
266
267
    def test_from_xml_with_valid_file_valid_content(self):
268
        filepath = self.input_path('valid-content.xml')
269
        # static method
270
        d = IODict.from_xml(filepath)
271
        self.assertTrue(isinstance(d, dict))
272
        # constructor
273
        d = IODict(filepath)
274
        self.assertTrue(isinstance(d, dict))
275
276
    def test_from_xml_with_valid_file_invalid_content(self):
277
        filepath = self.input_path('invalid-content.xml')
278
        # static method
279
        with self.assertRaises(ValueError):
280
            d = IODict.from_xml(filepath)
281
        # constructor
282
        with self.assertRaises(ValueError):
283
            d = IODict(filepath)
284
285
    def test_from_xml_with_invalid_file(self):
286
        filepath = self.input_path('invalid-file.xml')
287
        # static method
288
        with self.assertRaises(ValueError):
289
            d = IODict.from_xml(filepath)
290
        # constructor
291
        with self.assertRaises(ValueError):
292
            d = IODict(filepath)
293
294
    # def test_from_xml_with_valid_url_valid_content(self):
295
    #     url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.xml'
296
    #     # static method
297
    #     d = IODict.from_xml(url)
298
    #     self.assertTrue(isinstance(d, dict))
299
    #     # constructor
300
    #     d = IODict(url)
301
    #     self.assertTrue(isinstance(d, dict))
302
303
    def test_from_xml_with_valid_url_invalid_content(self):
304
        url = 'https://github.com/fabiocaccamo/python-benedict'
305
        # static method
306
        with self.assertRaises(ValueError):
307
            d = IODict.from_xml(url)
308
        # constructor
309
        with self.assertRaises(ValueError):
310
            d = IODict(url)
311
312
    def test_from_xml_with_invalid_url(self):
313
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
314
        # static method
315
        with self.assertRaises(ValueError):
316
            d = IODict.from_xml(url)
317
        # constructor
318
        with self.assertRaises(ValueError):
319
            d = IODict(url)
320
321
    def test_to_xml(self):
322
        d = IODict({
323
            'root': {
324
                'x': '7',
325
                'y': '8',
326
                'z': '9',
327
                'a': '1',
328
                'b': '2',
329
                'c': '3',
330
            },
331
        })
332
        s = d.to_xml()
333
        self.assertEqual(d, IODict.from_xml(s))
334
335
    def test_to_xml_file(self):
336
        d = IODict({
337
            'root': {
338
                'x': '7',
339
                'y': '8',
340
                'z': '9',
341
                'a': '1',
342
                'b': '2',
343
                'c': '3',
344
            },
345
        })
346
        filepath = self.output_path('test_to_xml_file.xml')
347
        s = d.to_xml(filepath=filepath)
348
        self.assertTrue(d, os.path.isfile(filepath))
349
        self.assertEqual(d, IODict.from_xml(filepath))
350
351
# YAML
352
353
    def test_from_yaml_with_valid_data(self):
354
        j = """
355
            a: 1
356
            b:
357
              c: 3
358
              d: 4
359
        """
360
        # static method
361
        d = IODict.from_yaml(j)
362
        self.assertTrue(isinstance(d, dict))
363
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
364
        # constructor
365
        d = IODict(j)
366
        self.assertTrue(isinstance(d, dict))
367
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
368
369
    def test_from_yaml_with_invalid_data(self):
370
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
371
        # static method
372
        with self.assertRaises(ValueError):
373
            d = IODict.from_yaml(j)
374
        # constructor
375
        with self.assertRaises(ValueError):
376
            d = IODict(j)
377
378
    def test_from_yaml_with_valid_file_valid_content(self):
379
        filepath = self.input_path('valid-content.yml')
380
        # static method
381
        d = IODict.from_yaml(filepath)
382
        self.assertTrue(isinstance(d, dict))
383
        # constructor
384
        d = IODict(filepath)
385
        self.assertTrue(isinstance(d, dict))
386
387
    def test_from_yaml_with_valid_file_invalid_content(self):
388
        filepath = self.input_path('invalid-content.yml')
389
        # static method
390
        with self.assertRaises(ValueError):
391
            d = IODict.from_yaml(filepath)
392
        # constructor
393
        with self.assertRaises(ValueError):
394
            d = IODict(filepath)
395
396
    def test_from_yaml_with_invalid_file(self):
397
        filepath = self.input_path('invalid-file.yml')
398
        # static method
399
        with self.assertRaises(ValueError):
400
            d = IODict.from_yaml(filepath)
401
        # constructor
402
        with self.assertRaises(ValueError):
403
            d = IODict(filepath)
404
405
    def test_from_yaml_with_valid_url_valid_content(self):
406
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.yml'
407
        # static method
408
        d = IODict.from_yaml(url)
409
        self.assertTrue(isinstance(d, dict))
410
        # constructor
411
        d = IODict(url)
412
        self.assertTrue(isinstance(d, dict))
413
414
    def test_from_yaml_with_valid_url_invalid_content(self):
415
        url = 'https://github.com/fabiocaccamo/python-benedict'
416
        # static method
417
        with self.assertRaises(ValueError):
418
            d = IODict.from_yaml(url)
419
        # constructor
420
        with self.assertRaises(ValueError):
421
            d = IODict(url)
422
423
    def test_from_yaml_with_invalid_url(self):
424
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
425
        # static method
426
        with self.assertRaises(ValueError):
427
            d = IODict.from_yaml(url)
428
        # constructor
429
        with self.assertRaises(ValueError):
430
            d = IODict(url)
431
432
    def test_to_yaml(self):
433
        d = IODict({
434
            'x': 7,
435
            'y': 8,
436
            'z': 9,
437
            'a': 1,
438
            'b': 2,
439
            'c': 3,
440
        })
441
        s = d.to_yaml()
442
        self.assertEqual(d, IODict.from_yaml(s))
443
444
    def test_to_yaml_file(self):
445
        d = IODict({
446
            'x': 7,
447
            'y': 8,
448
            'z': 9,
449
            'a': 1,
450
            'b': 2,
451
            'c': 3,
452
        })
453
        filepath = self.output_path('test_to_yaml_file.yml')
454
        s = d.to_yaml(filepath=filepath)
455
        self.assertTrue(d, os.path.isfile(filepath))
456
        self.assertEqual(d, IODict.from_yaml(filepath))
457