# -*- coding: utf-8 -*- from benedict.dicts.io import IODict from .test_io_dict import io_dict_test_case import datetime as dt import plistlib import six class io_dict_plist_test_case(io_dict_test_case): """ This class describes an IODict / plist test case. """ def __init__(self, *args, **kwargs): super(io_dict_plist_test_case, self).__init__(*args, **kwargs) if six.PY2: # fmt: off self._dict = dict( aString=u"Doodah", aList=[ u"A", u"B", 12, 32.1, [1, 2, 3], ], aFloat=0.1, anInt=728, aDict=dict( anotherString=u"", aThirdString=u"M\xe4ssig, Ma\xdf", aTrueValue=True, aFalseValue=False, ), someData=plistlib.Data(""), someMoreData=plistlib.Data("" * 10), aDate=dt.datetime( 1985, 4, 3, 23, 55 ), # dt.datetime.fromtimestamp(481413300), ) # fmt: on else: self._dict = dict( aString="Doodah", aList=[ "A", "B", 12, 32.1, [1, 2, 3], ], aFloat=0.1, anInt=728, aDict=dict( anotherString="", aThirdString="M\xe4ssig, Ma\xdf", aTrueValue=True, aFalseValue=False, ), someData=bytes("", encoding="utf-8"), someMoreData=bytes("" * 10, encoding="utf-8"), aDate=dt.datetime( 1985, 4, 3, 23, 55 ), # dt.datetime.fromtimestamp(481413300), ) # self._dict = { # 'aString': 'Doodah', # 'aList': ['A', 'B', 12, 32.1, [1, 2, 3]], # 'aFloat': 0.1, # 'anInt': 728, # 'aDict': { # 'anotherString': '', # 'aThirdString': 'M\xe4ssig, Ma\xdf', # 'aTrueValue': True, # 'aFalseValue': False, # }, # 'someData': b'', # 'someMoreData': b'' * 10, # 'aDate': dt.datetime.fromtimestamp(481406100), # } self._plist = """ aDate 1985-04-03T23:55:00Z aDict aFalseValue aThirdString Mässig, Maß aTrueValue anotherString <hello & hi there!> aFloat 0.1 aList A B 12 32.1 1 2 3 aString Doodah anInt 728 someData PGJpbmFyeSBndW5rPg== someMoreData PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2Yg YmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1 bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMg b2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5IGd1bms+PGxvdHMgb2YgYmluYXJ5 IGd1bms+ """ def test_from_plist_with_valid_data(self): j = self._plist # static method d = IODict.from_plist(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d.get("aDate"), self._dict.get("aDate")) self.assertEqual(d, self._dict) # constructor d = IODict(j, format="plist") self.assertTrue(isinstance(d, dict)) self.assertEqual(d, self._dict) def test_from_plist_with_invalid_data(self): j = "Lorem ipsum est in ea occaecat nisi officia." # static method with self.assertRaises(ValueError): IODict.from_plist(j) # constructor with self.assertRaises(ValueError): IODict(j, format="plist") def test_from_plist_with_valid_file_valid_content(self): filepath = self.input_path("valid-content.plist") # static method d = IODict.from_plist(filepath) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(filepath, format="plist") 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): filepath = self.input_path("valid-content.base64") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.csv") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.json") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.pickle") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.qs") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.toml") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.xml") with self.assertRaises(ValueError): IODict.from_plist(filepath) filepath = self.input_path("valid-content.yml") with self.assertRaises(ValueError): IODict.from_plist(filepath) def test_from_plist_with_valid_file_invalid_content(self): filepath = self.input_path("invalid-content.plist") # static method with self.assertRaises(ValueError): IODict.from_plist(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format="plist") def test_from_plist_with_invalid_file(self): filepath = self.input_path("invalid-file.plist") # static method with self.assertRaises(ValueError): IODict.from_plist(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format="plist") def test_from_plist_with_valid_url_valid_content(self): url = self.input_url("valid-content.plist") # static method d = IODict.from_plist(url) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(url, format="plist") self.assertTrue(isinstance(d, dict)) # constructor with format autodetection d = IODict(url) self.assertTrue(isinstance(d, dict)) def test_from_plist_with_valid_url_invalid_content(self): url = "https://github.com/fabiocaccamo/python-benedict" # static method with self.assertRaises(ValueError): IODict.from_plist(url) # constructor with self.assertRaises(ValueError): IODict(url, format="plist") def test_from_plist_with_invalid_url(self): url = "https://github.com/fabiocaccamo/python-benedict-invalid" # static method with self.assertRaises(ValueError): IODict.from_plist(url) # constructor with self.assertRaises(ValueError): IODict(url, format="plist") 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) filepath = self.output_path("test_to_plist_file.plist") d.to_plist(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_plist(filepath))