Passed
Push — master ( 01a50b...c59786 )
by Fabio
01:14
created

IODictTestCase.test_to_base64_file()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 9.95
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
# BASE64
27
28
    def test_from_base64_with_valid_data(self):
29
        j = 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9'
30
        # j = '{"a": 1, "b": 2, "c": 3}'
31
        # static method
32
        d = IODict.from_base64(j)
33
        self.assertTrue(isinstance(d, dict))
34
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
35
        # constructor
36
        d = IODict(j)
37
        self.assertTrue(isinstance(d, dict))
38
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
39
40
    def test_from_base64_with_invalid_data(self):
41
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
42
        # static method
43
        with self.assertRaises(ValueError):
44
            d = IODict.from_base64(j)
45
        # constructor
46
        with self.assertRaises(ValueError):
47
            d = IODict(j)
48
49
    def test_from_base64_with_valid_file_valid_content(self):
50
        filepath = self.input_path('valid-content.base64')
51
        # static method
52
        d = IODict.from_base64(filepath)
53
        self.assertTrue(isinstance(d, dict))
54
        # constructor
55
        d = IODict(filepath)
56
        self.assertTrue(isinstance(d, dict))
57
58
    def test_from_base64_with_valid_file_valid_content_invalid_format(self):
59
        filepath = self.input_path('valid-content.json')
60
        with self.assertRaises(ValueError):
61
            d = IODict.from_base64(filepath)
62
        filepath = self.input_path('valid-content.toml')
63
        with self.assertRaises(ValueError):
64
            d = IODict.from_base64(filepath)
65
        filepath = self.input_path('valid-content.xml')
66
        with self.assertRaises(ValueError):
67
            d = IODict.from_base64(filepath)
68
        filepath = self.input_path('valid-content.yml')
69
        with self.assertRaises(ValueError):
70
            d = IODict.from_base64(filepath)
71
72
    def test_from_base64_with_valid_file_invalid_content(self):
73
        filepath = self.input_path('invalid-content.base64')
74
        # static method
75
        with self.assertRaises(ValueError):
76
            d = IODict.from_base64(filepath)
77
        # constructor
78
        with self.assertRaises(ValueError):
79
            d = IODict(filepath)
80
81
    def test_from_base64_with_invalid_file(self):
82
        filepath = self.input_path('invalid-file.base64')
83
        # static method
84
        with self.assertRaises(ValueError):
85
            d = IODict.from_base64(filepath)
86
        # constructor
87
        with self.assertRaises(ValueError):
88
            d = IODict(filepath)
89
90
    def test_from_base64_with_valid_url_valid_content(self):
91
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.base64'
92
        # static method
93
        d = IODict.from_base64(url)
94
        self.assertTrue(isinstance(d, dict))
95
        # constructor
96
        d = IODict(url)
97
        self.assertTrue(isinstance(d, dict))
98
99
    def test_from_base64_with_valid_url_invalid_content(self):
100
        url = 'https://github.com/fabiocaccamo/python-benedict'
101
        # static method
102
        with self.assertRaises(ValueError):
103
            d = IODict.from_base64(url)
104
        # constructor
105
        with self.assertRaises(ValueError):
106
            d = IODict(url)
107
108
    def test_from_base64_with_invalid_url(self):
109
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
110
        # static method
111
        with self.assertRaises(ValueError):
112
            d = IODict.from_base64(url)
113
        # constructor
114
        with self.assertRaises(ValueError):
115
            d = IODict(url)
116
117
    def test_to_base64(self):
118
        d = IODict({
119
            'a': 1,
120
            'b': 2,
121
            'c': 3,
122
        })
123
        s = d.to_base64(sort_keys=True)
124
        self.assertEqual(s, 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9')
125
126
    def test_to_base64_file(self):
127
        d = IODict({
128
            'a': 1,
129
            'b': 2,
130
            'c': 3,
131
        })
132
        filepath = self.output_path('test_to_base64_file.base64')
133
        s = d.to_base64(filepath=filepath, sort_keys=True)
134
        self.assertTrue(d, os.path.isfile(filepath))
135
        self.assertEqual(d, IODict.from_base64(filepath))
136
137
# JSON
138
139
    def test_from_json_with_valid_data(self):
140
        j = '{"a": 1, "b": 2, "c": 3}'
141
        # static method
142
        d = IODict.from_json(j)
143
        self.assertTrue(isinstance(d, dict))
144
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
145
        # constructor
146
        d = IODict(j)
147
        self.assertTrue(isinstance(d, dict))
148
        self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
149
150
    def test_from_json_with_invalid_data(self):
151
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
152
        # static method
153
        with self.assertRaises(ValueError):
154
            d = IODict.from_json(j)
155
        # constructor
156
        with self.assertRaises(ValueError):
157
            d = IODict(j)
158
159
    def test_from_json_with_valid_file_valid_content(self):
160
        filepath = self.input_path('valid-content.json')
161
        # static method
162
        d = IODict.from_json(filepath)
163
        self.assertTrue(isinstance(d, dict))
164
        # constructor
165
        d = IODict(filepath)
166
        self.assertTrue(isinstance(d, dict))
167
168
    def test_from_json_with_valid_file_valid_content_invalid_format(self):
169
        filepath = self.input_path('valid-content.base64')
170
        with self.assertRaises(ValueError):
171
            d = IODict.from_json(filepath)
172
        filepath = self.input_path('valid-content.toml')
173
        with self.assertRaises(ValueError):
174
            d = IODict.from_json(filepath)
175
        filepath = self.input_path('valid-content.xml')
176
        with self.assertRaises(ValueError):
177
            d = IODict.from_json(filepath)
178
        filepath = self.input_path('valid-content.yml')
179
        with self.assertRaises(ValueError):
180
            d = IODict.from_json(filepath)
181
182
    def test_from_json_with_valid_file_invalid_content(self):
183
        filepath = self.input_path('invalid-content.json')
184
        # static method
185
        with self.assertRaises(ValueError):
186
            d = IODict.from_json(filepath)
187
        # constructor
188
        with self.assertRaises(ValueError):
189
            d = IODict(filepath)
190
191
    def test_from_json_with_invalid_file(self):
192
        filepath = self.input_path('invalid-file.json')
193
        # static method
194
        with self.assertRaises(ValueError):
195
            d = IODict.from_json(filepath)
196
        # constructor
197
        with self.assertRaises(ValueError):
198
            d = IODict(filepath)
199
200
    def test_from_json_with_valid_url_valid_content(self):
201
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.json'
202
        # static method
203
        d = IODict.from_json(url)
204
        self.assertTrue(isinstance(d, dict))
205
        # constructor
206
        d = IODict(url)
207
        self.assertTrue(isinstance(d, dict))
208
209
    def test_from_json_with_valid_url_invalid_content(self):
210
        url = 'https://github.com/fabiocaccamo/python-benedict'
211
        # static method
212
        with self.assertRaises(ValueError):
213
            d = IODict.from_json(url)
214
        # constructor
215
        with self.assertRaises(ValueError):
216
            d = IODict(url)
217
218
    def test_from_json_with_invalid_url(self):
219
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
220
        # static method
221
        with self.assertRaises(ValueError):
222
            d = IODict.from_json(url)
223
        # constructor
224
        with self.assertRaises(ValueError):
225
            d = IODict(url)
226
227
    def test_to_json(self):
228
        d = IODict({
229
            'x': 7,
230
            'y': 8,
231
            'z': 9,
232
            'a': 1,
233
            'b': 2,
234
            'c': 3,
235
        })
236
        s = d.to_json(sort_keys=True)
237
        self.assertEqual(s, '{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}')
238
239
    def test_to_json_file(self):
240
        d = IODict({
241
            'x': 7,
242
            'y': 8,
243
            'z': 9,
244
            'a': 1,
245
            'b': 2,
246
            'c': 3,
247
        })
248
        filepath = self.output_path('test_to_json_file.json')
249
        s = d.to_json(filepath=filepath, sort_keys=True)
250
        self.assertTrue(d, os.path.isfile(filepath))
251
        self.assertEqual(d, IODict.from_json(filepath))
252
253
    # YAML
254
# TOML
255
256
    def test_from_toml_with_valid_data(self):
257
        j = """
258
            a = 1
259
260
            [b]
261
            c = 3
262
            d = 4
263
        """
264
        # static method
265
        d = IODict.from_toml(j)
266
        self.assertTrue(isinstance(d, dict))
267
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
268
        # constructor
269
        d = IODict(j)
270
        self.assertTrue(isinstance(d, dict))
271
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
272
273
    def test_from_toml_with_invalid_data(self):
274
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
275
        # static method
276
        with self.assertRaises(ValueError):
277
            d = IODict.from_toml(j)
278
        # constructor
279
        with self.assertRaises(ValueError):
280
            d = IODict(j)
281
282
    def test_from_toml_with_valid_file_valid_content(self):
283
        filepath = self.input_path('valid-content.toml')
284
        # static method
285
        d = IODict.from_toml(filepath)
286
        self.assertTrue(isinstance(d, dict))
287
        # constructor
288
        d = IODict(filepath)
289
        self.assertTrue(isinstance(d, dict))
290
291
    def test_from_toml_with_valid_file_valid_content_invalid_format(self):
292
        # filepath = self.input_path('valid-content.base64')
293
        # with self.assertRaises(ValueError):
294
        #     d = IODict.from_toml(filepath)
295
        filepath = self.input_path('valid-content.json')
296
        with self.assertRaises(ValueError):
297
            d = IODict.from_toml(filepath)
298
        filepath = self.input_path('valid-content.xml')
299
        with self.assertRaises(ValueError):
300
            d = IODict.from_toml(filepath)
301
        filepath = self.input_path('valid-content.yml')
302
        with self.assertRaises(ValueError):
303
            d = IODict.from_toml(filepath)
304
305
    def test_from_toml_with_valid_file_invalid_content(self):
306
        filepath = self.input_path('invalid-content.toml')
307
        # static method
308
        with self.assertRaises(ValueError):
309
            d = IODict.from_toml(filepath)
310
        # constructor
311
        with self.assertRaises(ValueError):
312
            d = IODict(filepath)
313
314
    def test_from_toml_with_invalid_file(self):
315
        filepath = self.input_path('invalid-file.toml')
316
        # static method
317
        with self.assertRaises(ValueError):
318
            d = IODict.from_toml(filepath)
319
        # constructor
320
        with self.assertRaises(ValueError):
321
            d = IODict(filepath)
322
323
    def test_from_toml_with_valid_url_valid_content(self):
324
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.toml'
325
        # static method
326
        d = IODict.from_toml(url)
327
        self.assertTrue(isinstance(d, dict))
328
        # constructor
329
        d = IODict(url)
330
        self.assertTrue(isinstance(d, dict))
331
332
    def test_from_toml_with_valid_url_invalid_content(self):
333
        url = 'https://github.com/fabiocaccamo/python-benedict'
334
        # static method
335
        with self.assertRaises(ValueError):
336
            d = IODict.from_toml(url)
337
        # constructor
338
        with self.assertRaises(ValueError):
339
            d = IODict(url)
340
341
    def test_from_toml_with_invalid_url(self):
342
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
343
        # static method
344
        with self.assertRaises(ValueError):
345
            d = IODict.from_toml(url)
346
        # constructor
347
        with self.assertRaises(ValueError):
348
            d = IODict(url)
349
350
    def test_to_toml(self):
351
        d = IODict({
352
            'x': 7,
353
            'y': 8,
354
            'z': 9,
355
            'a': 1,
356
            'b': 2,
357
            'c': 3,
358
        })
359
        s = d.to_toml()
360
        self.assertEqual(d, IODict.from_toml(s))
361
362
    def test_to_toml_file(self):
363
        d = IODict({
364
            'x': 7,
365
            'y': 8,
366
            'z': 9,
367
            'a': 1,
368
            'b': 2,
369
            'c': 3,
370
        })
371
        filepath = self.output_path('test_to_toml_file.toml')
372
        s = d.to_toml(filepath=filepath)
373
        self.assertTrue(d, os.path.isfile(filepath))
374
        self.assertEqual(d, IODict.from_toml(filepath))
375
376
# XML
377
378
    def test_from_xml_with_valid_data(self):
379
        j = """<?xml version="1.0" ?>
380
            <root>
381
                <a>1</a>
382
                <b>
383
                    <c>3</c>
384
                    <d>4</d>
385
                </b>
386
            </root>
387
        """
388
        # static method
389
        d = IODict.from_xml(j)
390
        self.assertTrue(isinstance(d, dict))
391
        self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },})
392
        # constructor
393
        d = IODict(j)
394
        self.assertTrue(isinstance(d, dict))
395
        self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },})
396
397
    def test_from_xml_with_invalid_data(self):
398
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
399
        # static method
400
        with self.assertRaises(ValueError):
401
            d = IODict.from_xml(j)
402
        # constructor
403
        with self.assertRaises(ValueError):
404
            d = IODict(j)
405
406
    def test_from_xml_with_valid_file_valid_content(self):
407
        filepath = self.input_path('valid-content.xml')
408
        # static method
409
        d = IODict.from_xml(filepath)
410
        self.assertTrue(isinstance(d, dict))
411
        # constructor
412
        d = IODict(filepath)
413
        self.assertTrue(isinstance(d, dict))
414
415
    def test_from_xml_with_valid_file_valid_content_invalid_format(self):
416
        filepath = self.input_path('valid-content.base64')
417
        with self.assertRaises(ValueError):
418
            d = IODict.from_xml(filepath)
419
        filepath = self.input_path('valid-content.json')
420
        with self.assertRaises(ValueError):
421
            d = IODict.from_xml(filepath)
422
        filepath = self.input_path('valid-content.toml')
423
        with self.assertRaises(ValueError):
424
            d = IODict.from_xml(filepath)
425
        filepath = self.input_path('valid-content.yml')
426
        with self.assertRaises(ValueError):
427
            d = IODict.from_xml(filepath)
428
429
    def test_from_xml_with_valid_file_invalid_content(self):
430
        filepath = self.input_path('invalid-content.xml')
431
        # static method
432
        with self.assertRaises(ValueError):
433
            d = IODict.from_xml(filepath)
434
        # constructor
435
        with self.assertRaises(ValueError):
436
            d = IODict(filepath)
437
438
    def test_from_xml_with_invalid_file(self):
439
        filepath = self.input_path('invalid-file.xml')
440
        # static method
441
        with self.assertRaises(ValueError):
442
            d = IODict.from_xml(filepath)
443
        # constructor
444
        with self.assertRaises(ValueError):
445
            d = IODict(filepath)
446
447
    def test_from_xml_with_valid_url_valid_content(self):
448
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.xml'
449
        # static method
450
        d = IODict.from_xml(url)
451
        self.assertTrue(isinstance(d, dict))
452
        # constructor
453
        d = IODict(url)
454
        self.assertTrue(isinstance(d, dict))
455
456
    def test_from_xml_with_valid_url_invalid_content(self):
457
        url = 'https://github.com/fabiocaccamo/python-benedict'
458
        # static method
459
        with self.assertRaises(ValueError):
460
            d = IODict.from_xml(url)
461
        # constructor
462
        with self.assertRaises(ValueError):
463
            d = IODict(url)
464
465
    def test_from_xml_with_invalid_url(self):
466
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
467
        # static method
468
        with self.assertRaises(ValueError):
469
            d = IODict.from_xml(url)
470
        # constructor
471
        with self.assertRaises(ValueError):
472
            d = IODict(url)
473
474
    def test_to_xml(self):
475
        d = IODict({
476
            'root': {
477
                'x': '7',
478
                'y': '8',
479
                'z': '9',
480
                'a': '1',
481
                'b': '2',
482
                'c': '3',
483
            },
484
        })
485
        s = d.to_xml()
486
        self.assertEqual(d, IODict.from_xml(s))
487
488
    def test_to_xml_file(self):
489
        d = IODict({
490
            'root': {
491
                'x': '7',
492
                'y': '8',
493
                'z': '9',
494
                'a': '1',
495
                'b': '2',
496
                'c': '3',
497
            },
498
        })
499
        filepath = self.output_path('test_to_xml_file.xml')
500
        s = d.to_xml(filepath=filepath)
501
        self.assertTrue(d, os.path.isfile(filepath))
502
        self.assertEqual(d, IODict.from_xml(filepath))
503
504
# YAML
505
506
    def test_from_yaml_with_valid_data(self):
507
        j = """
508
            a: 1
509
            b:
510
              c: 3
511
              d: 4
512
        """
513
        # static method
514
        d = IODict.from_yaml(j)
515
        self.assertTrue(isinstance(d, dict))
516
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
517
        # constructor
518
        d = IODict(j)
519
        self.assertTrue(isinstance(d, dict))
520
        self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
521
522
    def test_from_yaml_with_invalid_data(self):
523
        j = 'Lorem ipsum est in ea occaecat nisi officia.'
524
        # static method
525
        with self.assertRaises(ValueError):
526
            d = IODict.from_yaml(j)
527
        # constructor
528
        with self.assertRaises(ValueError):
529
            d = IODict(j)
530
531
    def test_from_yaml_with_valid_file_valid_content(self):
532
        filepath = self.input_path('valid-content.yml')
533
        # static method
534
        d = IODict.from_yaml(filepath)
535
        self.assertTrue(isinstance(d, dict))
536
        # constructor
537
        d = IODict(filepath)
538
        self.assertTrue(isinstance(d, dict))
539
540
    def test_from_yaml_with_valid_file_valid_content_invalid_format(self):
541
        filepath = self.input_path('valid-content.base64')
542
        with self.assertRaises(ValueError):
543
            d = IODict.from_yaml(filepath)
544
        # filepath = self.input_path('valid-content.json')
545
        # with self.assertRaises(ValueError):
546
        #    d = IODict.from_yaml(filepath)
547
        filepath = self.input_path('valid-content.toml')
548
        with self.assertRaises(ValueError):
549
            d = IODict.from_yaml(filepath)
550
        filepath = self.input_path('valid-content.xml')
551
        with self.assertRaises(ValueError):
552
            d = IODict.from_yaml(filepath)
553
554
    def test_from_yaml_with_valid_file_invalid_content(self):
555
        filepath = self.input_path('invalid-content.yml')
556
        # static method
557
        with self.assertRaises(ValueError):
558
            d = IODict.from_yaml(filepath)
559
        # constructor
560
        with self.assertRaises(ValueError):
561
            d = IODict(filepath)
562
563
    def test_from_yaml_with_invalid_file(self):
564
        filepath = self.input_path('invalid-file.yml')
565
        # static method
566
        with self.assertRaises(ValueError):
567
            d = IODict.from_yaml(filepath)
568
        # constructor
569
        with self.assertRaises(ValueError):
570
            d = IODict(filepath)
571
572
    def test_from_yaml_with_valid_url_valid_content(self):
573
        url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.yml'
574
        # static method
575
        d = IODict.from_yaml(url)
576
        self.assertTrue(isinstance(d, dict))
577
        # constructor
578
        d = IODict(url)
579
        self.assertTrue(isinstance(d, dict))
580
581
    def test_from_yaml_with_valid_url_invalid_content(self):
582
        url = 'https://github.com/fabiocaccamo/python-benedict'
583
        # static method
584
        with self.assertRaises(ValueError):
585
            d = IODict.from_yaml(url)
586
        # constructor
587
        with self.assertRaises(ValueError):
588
            d = IODict(url)
589
590
    def test_from_yaml_with_invalid_url(self):
591
        url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
592
        # static method
593
        with self.assertRaises(ValueError):
594
            d = IODict.from_yaml(url)
595
        # constructor
596
        with self.assertRaises(ValueError):
597
            d = IODict(url)
598
599
    def test_to_yaml(self):
600
        d = IODict({
601
            'x': 7,
602
            'y': 8,
603
            'z': 9,
604
            'a': 1,
605
            'b': 2,
606
            'c': 3,
607
        })
608
        s = d.to_yaml()
609
        self.assertEqual(d, IODict.from_yaml(s))
610
611
    def test_to_yaml_file(self):
612
        d = IODict({
613
            'x': 7,
614
            'y': 8,
615
            'z': 9,
616
            'a': 1,
617
            'b': 2,
618
            'c': 3,
619
        })
620
        filepath = self.output_path('test_to_yaml_file.yml')
621
        s = d.to_yaml(filepath=filepath)
622
        self.assertTrue(d, os.path.isfile(filepath))
623
        self.assertEqual(d, IODict.from_yaml(filepath))
624