python-benedict/tests/dicts/io/test_io_dict_plist.py

255 lines
8.6 KiB
Python
Raw Normal View History

2020-09-09 14:45:39 +00:00
import datetime as dt
2020-09-09 16:55:43 +00:00
import plistlib
2020-09-09 14:45:39 +00:00
import six
from benedict.dicts.io import IODict
from .test_io_dict import io_dict_test_case
2020-09-09 14:45:39 +00:00
class io_dict_plist_test_case(io_dict_test_case):
2022-02-13 10:35:43 +00:00
"""
This class describes an IODict / plist test case.
"""
2020-09-09 14:45:39 +00:00
def __init__(self, *args, **kwargs):
super(io_dict_plist_test_case, self).__init__(*args, **kwargs)
if six.PY2:
2022-02-14 22:52:50 +00:00
# fmt: off
2020-09-09 14:45:39 +00:00
self._dict = dict(
2022-02-14 22:52:50 +00:00
aString=u"Doodah",
2022-02-13 10:35:43 +00:00
aList=[
2022-02-14 22:52:50 +00:00
u"A",
u"B",
2022-02-13 10:35:43 +00:00
12,
32.1,
[1, 2, 3],
],
aFloat=0.1,
anInt=728,
aDict=dict(
2022-02-14 22:52:50 +00:00
anotherString=u"<hello & hi there!>",
aThirdString=u"M\xe4ssig, Ma\xdf",
2022-02-13 10:35:43 +00:00
aTrueValue=True,
aFalseValue=False,
2020-09-09 14:45:39 +00:00
),
2022-02-13 10:35:43 +00:00
someData=plistlib.Data("<binary gunk>"),
someMoreData=plistlib.Data("<lots of binary gunk>" * 10),
aDate=dt.datetime(
1985, 4, 3, 23, 55
), # dt.datetime.fromtimestamp(481413300),
2020-09-09 14:45:39 +00:00
)
2022-02-14 22:52:50 +00:00
# fmt: on
2020-09-09 14:45:39 +00:00
else:
self._dict = dict(
2022-02-13 10:35:43 +00:00
aString="Doodah",
aList=[
"A",
"B",
12,
32.1,
[1, 2, 3],
],
aFloat=0.1,
anInt=728,
aDict=dict(
anotherString="<hello & hi there!>",
aThirdString="M\xe4ssig, Ma\xdf",
aTrueValue=True,
aFalseValue=False,
2020-09-09 14:45:39 +00:00
),
2022-02-13 10:35:43 +00:00
someData=bytes("<binary gunk>", encoding="utf-8"),
someMoreData=bytes("<lots of binary gunk>" * 10, encoding="utf-8"),
aDate=dt.datetime(
1985, 4, 3, 23, 55
), # dt.datetime.fromtimestamp(481413300),
2020-09-09 14:45:39 +00:00
)
# self._dict = {
# 'aString': 'Doodah',
# 'aList': ['A', 'B', 12, 32.1, [1, 2, 3]],
# 'aFloat': 0.1,
# 'anInt': 728,
# 'aDict': {
# 'anotherString': '<hello & hi there!>',
# 'aThirdString': 'M\xe4ssig, Ma\xdf',
# 'aTrueValue': True,
# 'aFalseValue': False,
# },
# 'someData': b'<binary gunk>',
# 'someMoreData': b'<lots of binary gunk>' * 10,
# 'aDate': dt.datetime.fromtimestamp(481406100),
# }
self._plist = """
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aDate</key>
2020-09-10 16:07:09 +00:00
<date>1985-04-03T23:55:00Z</date>
2020-09-09 14:45:39 +00:00
<key>aDict</key>
<dict>
<key>aFalseValue</key>
<false/>
<key>aThirdString</key>
<string>Mässig, Maß</string>
<key>aTrueValue</key>
<true/>
<key>anotherString</key>
<string>&lt;hello &amp; hi there!&gt;</string>
</dict>
<key>aFloat</key>
<real>0.1</real>
<key>aList</key>
<array>
<string>A</string>
<string>B</string>
<integer>12</integer>
<real>32.1</real>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
</array>
</array>
<key>aString</key>
<string>Doodah</string>
<key>anInt</key>
<integer>728</integer>
<key>someData</key>
<data>
PGJpbmFyeSBndW5rPg==
</data>
<key>someMoreData</key>
<data>
PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2Yg
YmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1
bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMg
b2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5
IGd1bms+
</data>
</dict>
</plist>
"""
def test_from_plist_with_valid_data(self):
j = self._plist
# static method
d = IODict.from_plist(j)
self.assertTrue(isinstance(d, dict))
2022-02-13 10:35:43 +00:00
self.assertEqual(d.get("aDate"), self._dict.get("aDate"))
2020-09-09 14:45:39 +00:00
self.assertEqual(d, self._dict)
# constructor
2022-02-13 10:35:43 +00:00
d = IODict(j, format="plist")
2020-09-09 14:45:39 +00:00
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, self._dict)
def test_from_plist_with_invalid_data(self):
2022-02-13 10:35:43 +00:00
j = "Lorem ipsum est in ea occaecat nisi officia."
2020-09-09 14:45:39 +00:00
# static method
with self.assertRaises(ValueError):
IODict.from_plist(j)
# constructor
with self.assertRaises(ValueError):
2022-02-13 10:35:43 +00:00
IODict(j, format="plist")
2020-09-09 14:45:39 +00:00
def test_from_plist_with_valid_file_valid_content(self):
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.plist")
2020-09-09 14:45:39 +00:00
# static method
d = IODict.from_plist(filepath)
self.assertTrue(isinstance(d, dict))
# constructor
2022-02-13 10:35:43 +00:00
d = IODict(filepath, format="plist")
2020-09-09 14:45:39 +00:00
self.assertTrue(isinstance(d, dict))
# constructor with format autodetection
d = IODict(filepath)
self.assertTrue(isinstance(d, dict))
def test_from_plist_with_valid_file_valid_content_invalid_format(self):
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.base64")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.csv")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.json")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.pickle")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.qs")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.toml")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.xml")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
2022-02-13 10:35:43 +00:00
filepath = self.input_path("valid-content.yml")
2020-09-09 14:45:39 +00:00
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
def test_from_plist_with_valid_file_invalid_content(self):
2022-02-13 10:35:43 +00:00
filepath = self.input_path("invalid-content.plist")
2020-09-09 14:45:39 +00:00
# static method
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
# constructor
with self.assertRaises(ValueError):
2022-02-13 10:35:43 +00:00
IODict(filepath, format="plist")
2020-09-09 14:45:39 +00:00
def test_from_plist_with_invalid_file(self):
2022-02-13 10:35:43 +00:00
filepath = self.input_path("invalid-file.plist")
2020-09-09 14:45:39 +00:00
# static method
with self.assertRaises(ValueError):
IODict.from_plist(filepath)
# constructor
with self.assertRaises(ValueError):
2022-02-13 10:35:43 +00:00
IODict(filepath, format="plist")
2020-09-09 14:45:39 +00:00
2020-09-10 16:09:36 +00:00
def test_from_plist_with_valid_url_valid_content(self):
2022-02-13 10:35:43 +00:00
url = self.input_url("valid-content.plist")
2020-09-10 16:09:36 +00:00
# static method
d = IODict.from_plist(url)
self.assertTrue(isinstance(d, dict))
# constructor
2022-02-13 10:35:43 +00:00
d = IODict(url, format="plist")
2020-09-10 16:09:36 +00:00
self.assertTrue(isinstance(d, dict))
# constructor with format autodetection
d = IODict(url)
self.assertTrue(isinstance(d, dict))
2020-09-09 14:45:39 +00:00
def test_from_plist_with_valid_url_invalid_content(self):
2022-02-13 10:35:43 +00:00
url = "https://github.com/fabiocaccamo/python-benedict"
2020-09-09 14:45:39 +00:00
# static method
with self.assertRaises(ValueError):
IODict.from_plist(url)
# constructor
with self.assertRaises(ValueError):
2022-02-13 10:35:43 +00:00
IODict(url, format="plist")
2020-09-09 14:45:39 +00:00
def test_from_plist_with_invalid_url(self):
2022-02-13 10:35:43 +00:00
url = "https://github.com/fabiocaccamo/python-benedict-invalid"
2020-09-09 14:45:39 +00:00
# static method
with self.assertRaises(ValueError):
IODict.from_plist(url)
# constructor
with self.assertRaises(ValueError):
2022-02-13 10:35:43 +00:00
IODict(url, format="plist")
2020-09-09 14:45:39 +00:00
def test_to_plist(self):
# example data taken from:
# https://docs.python.org/3/library/plistlib.html#examples
d = IODict(self._dict)
s = d.to_plist()
# print(s)
self.assertEqual(d, IODict.from_plist(s))
def test_to_plist_file(self):
d = IODict(self._dict)
2022-02-13 10:35:43 +00:00
filepath = self.output_path("test_to_plist_file.plist")
2020-09-09 14:45:39 +00:00
d.to_plist(filepath=filepath)
self.assertFileExists(filepath)
self.assertEqual(d, IODict.from_plist(filepath))