Completed
Push — master ( 1c4121...07028b )
by Fabio
03:58
created

IODictTestCase.test_to_query_string()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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