2020-02-03 17:25:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from benedict.dicts.io import IODict
|
|
|
|
|
|
|
|
from .test_io_dict import io_dict_test_case
|
|
|
|
|
|
|
|
|
|
|
|
class io_dict_yaml_test_case(io_dict_test_case):
|
2022-02-13 10:35:43 +00:00
|
|
|
"""
|
|
|
|
This class describes an IODict / yaml test case.
|
|
|
|
"""
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_valid_data(self):
|
|
|
|
j = """
|
|
|
|
a: 1
|
|
|
|
b:
|
|
|
|
c: 3
|
|
|
|
d: 4
|
|
|
|
"""
|
|
|
|
# static method
|
|
|
|
d = IODict.from_yaml(j)
|
|
|
|
self.assertTrue(isinstance(d, dict))
|
2022-02-13 10:35:43 +00:00
|
|
|
self.assertEqual(
|
|
|
|
d,
|
|
|
|
{
|
|
|
|
"a": 1,
|
|
|
|
"b": {
|
|
|
|
"c": 3,
|
|
|
|
"d": 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-02-03 17:25:54 +00:00
|
|
|
# constructor
|
2022-02-13 10:35:43 +00:00
|
|
|
d = IODict(j, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
self.assertTrue(isinstance(d, dict))
|
2022-02-13 10:35:43 +00:00
|
|
|
self.assertEqual(
|
|
|
|
d,
|
|
|
|
{
|
|
|
|
"a": 1,
|
|
|
|
"b": {
|
|
|
|
"c": 3,
|
|
|
|
"d": 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_invalid_data(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
j = "Lorem ipsum est in ea occaecat nisi officia."
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(j)
|
|
|
|
# constructor
|
|
|
|
with self.assertRaises(ValueError):
|
2022-02-13 10:35:43 +00:00
|
|
|
IODict(j, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_valid_file_valid_content(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("valid-content.yml")
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
d = IODict.from_yaml(filepath)
|
|
|
|
self.assertTrue(isinstance(d, dict))
|
|
|
|
# constructor
|
2022-02-13 10:35:43 +00:00
|
|
|
d = IODict(filepath, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
self.assertTrue(isinstance(d, dict))
|
2020-03-13 12:54:53 +00:00
|
|
|
# constructor with format autodetection
|
|
|
|
d = IODict(filepath)
|
|
|
|
self.assertTrue(isinstance(d, dict))
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_valid_file_valid_content_invalid_format(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("valid-content.base64")
|
2020-02-03 17:25:54 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
|
|
|
# filepath = self.input_path('valid-content.json')
|
|
|
|
# with self.assertRaises(ValueError):
|
|
|
|
# IODict.from_yaml(filepath)
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("valid-content.qs")
|
2020-02-03 17:25:54 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("valid-content.toml")
|
2020-02-03 17:25:54 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("valid-content.xml")
|
2020-02-03 17:25:54 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
|
|
|
|
|
|
|
def test_from_yaml_with_valid_file_invalid_content(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("invalid-content.yml")
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
|
|
|
# constructor
|
|
|
|
with self.assertRaises(ValueError):
|
2022-02-13 10:35:43 +00:00
|
|
|
IODict(filepath, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_invalid_file(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
filepath = self.input_path("invalid-file.yml")
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(filepath)
|
|
|
|
# constructor
|
|
|
|
with self.assertRaises(ValueError):
|
2022-02-13 10:35:43 +00:00
|
|
|
IODict(filepath, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_valid_url_valid_content(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
url = self.input_url("valid-content.yml")
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
d = IODict.from_yaml(url)
|
|
|
|
self.assertTrue(isinstance(d, dict))
|
|
|
|
# constructor
|
2022-02-13 10:35:43 +00:00
|
|
|
d = IODict(url, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
self.assertTrue(isinstance(d, dict))
|
2020-03-13 12:54:53 +00:00
|
|
|
# constructor with format autodetection
|
|
|
|
d = IODict(url)
|
|
|
|
self.assertTrue(isinstance(d, dict))
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_valid_url_invalid_content(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
url = "https://github.com/fabiocaccamo/python-benedict"
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(url)
|
|
|
|
# constructor
|
|
|
|
with self.assertRaises(ValueError):
|
2022-02-13 10:35:43 +00:00
|
|
|
IODict(url, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_from_yaml_with_invalid_url(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
url = "https://github.com/fabiocaccamo/python-benedict-invalid"
|
2020-02-03 17:25:54 +00:00
|
|
|
# static method
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
IODict.from_yaml(url)
|
|
|
|
# constructor
|
|
|
|
with self.assertRaises(ValueError):
|
2022-02-13 10:35:43 +00:00
|
|
|
IODict(url, format="yaml")
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_to_yaml(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
d = IODict(
|
|
|
|
{
|
|
|
|
"x": 7,
|
|
|
|
"y": 8,
|
|
|
|
"z": 9,
|
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
|
|
|
"c": 3,
|
|
|
|
}
|
|
|
|
)
|
2020-02-03 17:25:54 +00:00
|
|
|
s = d.to_yaml()
|
|
|
|
self.assertEqual(d, IODict.from_yaml(s))
|
|
|
|
|
|
|
|
def test_to_yaml_file(self):
|
2022-02-13 10:35:43 +00:00
|
|
|
d = IODict(
|
|
|
|
{
|
|
|
|
"x": 7,
|
|
|
|
"y": 8,
|
|
|
|
"z": 9,
|
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
|
|
|
"c": 3,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
filepath = self.output_path("test_to_yaml_file.yml")
|
2020-02-03 17:25:54 +00:00
|
|
|
d.to_yaml(filepath=filepath)
|
|
|
|
self.assertFileExists(filepath)
|
|
|
|
self.assertEqual(d, IODict.from_yaml(filepath))
|