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_from_xml_with_valid_data(self): j = """ <?xml version="1.0" ?> <root> <a>1</a> <b> <c>3</c> <d>4</d> </b> </root> """ # static method d = IODict.from_xml(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d.get('root'), { 'a': '1', 'b': { 'c': '3', 'd': '4' }, }) # constructor d = IODict(j, format='xml') self.assertTrue(isinstance(d, dict)) self.assertEqual(d.get('root'), { 'a': '1', 'b': { 'c': '3', 'd': '4' }, })
def test_from_toml_with_valid_data(self): j = """ a = 1 [b] c = 3 d = 4 """ # static method d = IODict.from_toml(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, { 'a': 1, 'b': { 'c': 3, 'd': 4 }, }) # constructor d = IODict(j, format='toml') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, { 'a': 1, 'b': { 'c': 3, 'd': 4 }, })
def test_to_ini_file(self): d = IODict({ 'serveraliveinterval': 45, 'compression': True, 'compressionlevel': 9, 'forwardx11': True, 'bitbucket.org': { 'user': '******', 'serveraliveinterval': 45, 'compression': True, 'compressionlevel': 9, 'forwardx11': True }, 'topsecret.server.com': { 'port': 50022, 'forwardx11': False, 'serveraliveinterval': 45, 'compression': True, 'compressionlevel': 9 } }) filepath = self.output_path('test_to_ini_file.ini') d.to_ini(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_ini(filepath))
def test_from_base64_with_valid_data(self): j = 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9' # j = '{"a": 1, "b": 2, "c": 3}' # static method d = IODict.from_base64(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) # constructor d = IODict(j, format='base64') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) # constructor with subformat d = IODict(j, format='base64', subformat='json') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
def test_from_csv_with_valid_file_valid_content(self): filepath = self.input_path('valid-content.csv') # static method d = IODict.from_csv(filepath) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(filepath, format='csv') self.assertTrue(isinstance(d, dict))
def test_from_ini_with_valid_file_invalid_content(self): filepath = self.input_path('invalid-content.ini') # static method with self.assertRaises(ValueError): IODict.from_ini(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format='ini')
def test_to_base64(self): d = IODict({ 'a': 1, 'b': 2, 'c': 3, }) s = d.to_base64(sort_keys=True) self.assertEqual(s, 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9')
def test_from_toml_with_invalid_data(self): j = 'Lorem ipsum est in ea occaecat nisi officia.' # static method with self.assertRaises(ValueError): IODict.from_toml(j) # constructor with self.assertRaises(ValueError): IODict(j, format='toml')
def test_to_pickle_file(self): d = IODict({ 'date': self._get_pickle_decoded(), }) filepath = self.output_path('test_to_pickle_file.pickle') d.to_pickle(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_pickle(filepath))
def test_from_query_string_with_valid_url_invalid_content(self): url = 'https://github.com/fabiocaccamo/python-benedict' # static method with self.assertRaises(ValueError): IODict.from_query_string(url) # constructor with self.assertRaises(ValueError): IODict(url, format='query_string')
def test_from_ini_with_invalid_url(self): url = 'https://github.com/fabiocaccamo/python-benedict-invalid' # static method with self.assertRaises(ValueError): IODict.from_ini(url) # constructor with self.assertRaises(ValueError): IODict(url, format='ini')
def test_from_query_string_with_invalid_file(self): filepath = self.input_path('invalid-file.qs') # static method with self.assertRaises(ValueError): IODict.from_query_string(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format='query_string')
def test_from_ini_with_invalid_data(self): s = 'Lorem ipsum est in ea occaecat nisi officia.' # static method with self.assertRaises(ValueError): IODict.from_ini(s) # constructor with self.assertRaises(ValueError): IODict(s, format='ini')
def test_from_toml_with_invalid_file(self): filepath = self.input_path('invalid-file.toml') # static method with self.assertRaises(ValueError): IODict.from_toml(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format='toml')
def test_from_json_with_valid_data_empty(self): j = '{}' # static method d = IODict.from_json(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {}) # constructor d = IODict(j, format='json') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {})
def test_to_base64_file(self): d = IODict({ 'a': 1, 'b': 2, 'c': 3, }) filepath = self.output_path('test_to_base64_file.base64') d.to_base64(filepath=filepath, sort_keys=True) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_base64(filepath))
def test_from_json_with_valid_data_list(self): j = '[0,1,2,3,4,5]' # static method d = IODict.from_json(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {'values': [0, 1, 2, 3, 4, 5]}) # constructor d = IODict(j, format='json') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {'values': [0, 1, 2, 3, 4, 5]})
def test_from_ini_with_valid_file_valid_content(self): filepath = self.input_path('valid-content.ini') # static method d = IODict.from_ini(filepath) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(filepath, format='ini') self.assertTrue(isinstance(d, dict)) # constructor with format autodetection d = IODict(filepath) self.assertTrue(isinstance(d, dict))
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_to_json(self): d = IODict({ 'x': 7, 'y': 8, 'z': 9, 'a': 1, 'b': 2, 'c': 3, }) s = d.to_json(sort_keys=True) self.assertEqual(s, '{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}')
def test_from_query_string_with_valid_url_valid_content(self): url = self.input_url('valid-content.qs') # static method d = IODict.from_query_string(url) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(url, format='query_string') self.assertTrue(isinstance(d, dict)) # constructor with format autodetection d = IODict(url) self.assertTrue(isinstance(d, dict))
def test_from_pickle_with_valid_data(self): j = self._get_pickle_encoded() r = self._get_pickle_decoded() # static method d = IODict.from_pickle(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, r) # constructor d = IODict(j, format='pickle') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, r)
def test_to_toml(self): d = IODict({ 'x': 7, 'y': 8, 'z': 9, 'a': 1, 'b': 2, 'c': 3, }) s = d.to_toml() self.assertEqual(d, IODict.from_toml(s))
def test_from_json_with_valid_file_valid_content_but_unexpected_extension( self): filepath = self.input_path('valid-content.json.txt') # static method d = IODict.from_json(filepath) self.assertTrue(isinstance(d, dict)) # constructor d = IODict(filepath, format='json') self.assertTrue(isinstance(d, dict)) # constructor with format autodetection d = IODict(filepath) self.assertTrue(isinstance(d, dict))
def test_to_query_string_file(self): d = IODict({ 'ok': '1', 'test': '2', 'page': '3', 'lib': 'python benedict', 'author': 'Fabio Caccamo' }) filepath = self.output_path('test_to_query_string_file.qs') d.to_query_string(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_query_string(filepath))
def test_from_base64_with_valid_data_without_padding(self): j = 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDMsICJkIjogNH0' # eyJhIjogMSwgImIiOiAyLCAiYyI6IDMsICJkIjogNH0= # j = '{"a": 1, "b": 2, "c": 3, "d": 4}' # static method d = IODict.from_base64(j) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {'a': 1, 'b': 2, 'c': 3, 'd': 4}) # constructor d = IODict(j, format='base64') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, {'a': 1, 'b': 2, 'c': 3, 'd': 4})
def test_to_json_file(self): d = IODict({ 'x': 7, 'y': 8, 'z': 9, 'a': 1, 'b': 2, 'c': 3, }) filepath = self.output_path('test_to_json_file.json') d.to_json(filepath=filepath, sort_keys=True) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_json(filepath))
def test_to_xml(self): d = IODict({ 'root': { 'x': '7', 'y': '8', 'z': '9', 'a': '1', 'b': '2', 'c': '3', }, }) s = d.to_xml() self.assertEqual(d, IODict.from_xml(s))
def test_to_toml_file(self): d = IODict({ 'x': 7, 'y': 8, 'z': 9, 'a': 1, 'b': 2, 'c': 3, }) filepath = self.output_path('test_to_toml_file.toml') d.to_toml(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_toml(filepath))