| Total Complexity | 174 | 
| Total Lines | 1000 | 
| Duplicated Lines | 7.4 % | 
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like tests.test_io_dict often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 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 io_dict_test_case(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, format='base64') | ||
| 37 | self.assertTrue(isinstance(d, dict)) | ||
| 38 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 39 | # constructor with subformat | ||
| 40 | d = IODict(j, format='base64', subformat='json') | ||
| 41 | self.assertTrue(isinstance(d, dict)) | ||
| 42 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 43 | |||
| 44 | def test_from_base64_with_valid_data_without_padding(self): | ||
| 45 | j = 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDMsICJkIjogNH0' | ||
| 46 | # eyJhIjogMSwgImIiOiAyLCAiYyI6IDMsICJkIjogNH0= | ||
| 47 |         # j = '{"a": 1, "b": 2, "c": 3, "d": 4}' | ||
| 48 | # static method | ||
| 49 | d = IODict.from_base64(j) | ||
| 50 | self.assertTrue(isinstance(d, dict)) | ||
| 51 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, 'd': 4}) | ||
| 52 | # constructor | ||
| 53 | d = IODict(j, format='base64') | ||
| 54 | self.assertTrue(isinstance(d, dict)) | ||
| 55 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, 'd': 4}) | ||
| 56 | |||
| 57 | def test_from_base64_with_invalid_data(self): | ||
| 58 | j = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 59 | # static method | ||
| 60 | with self.assertRaises(ValueError): | ||
| 61 | IODict.from_base64(j) | ||
| 62 | # constructor | ||
| 63 | with self.assertRaises(ValueError): | ||
| 64 | IODict(j, format='base64') | ||
| 65 | |||
| 66 | def test_from_base64_with_valid_file_valid_content(self): | ||
| 67 |         filepath = self.input_path('valid-content.base64') | ||
| 68 | # static method | ||
| 69 | d = IODict.from_base64(filepath) | ||
| 70 | self.assertTrue(isinstance(d, dict)) | ||
| 71 | # constructor | ||
| 72 | d = IODict(filepath, format='base64') | ||
| 73 | self.assertTrue(isinstance(d, dict)) | ||
| 74 | |||
| 75 | def test_from_base64_with_valid_file_valid_content_invalid_format(self): | ||
| 76 |         filepath = self.input_path('valid-content.json') | ||
| 77 | with self.assertRaises(ValueError): | ||
| 78 | IODict.from_base64(filepath) | ||
| 79 |         filepath = self.input_path('valid-content.qs') | ||
| 80 | with self.assertRaises(ValueError): | ||
| 81 | IODict.from_base64(filepath) | ||
| 82 |         filepath = self.input_path('valid-content.toml') | ||
| 83 | with self.assertRaises(ValueError): | ||
| 84 | IODict.from_base64(filepath) | ||
| 85 |         filepath = self.input_path('valid-content.xml') | ||
| 86 | with self.assertRaises(ValueError): | ||
| 87 | IODict.from_base64(filepath) | ||
| 88 |         filepath = self.input_path('valid-content.yml') | ||
| 89 | with self.assertRaises(ValueError): | ||
| 90 | IODict.from_base64(filepath) | ||
| 91 | |||
| 92 | def test_from_base64_with_valid_file_invalid_content(self): | ||
| 93 |         filepath = self.input_path('invalid-content.base64') | ||
| 94 | # static method | ||
| 95 | with self.assertRaises(ValueError): | ||
| 96 | IODict.from_base64(filepath) | ||
| 97 | # constructor | ||
| 98 | with self.assertRaises(ValueError): | ||
| 99 | IODict(filepath, format='base64') | ||
| 100 | |||
| 101 | def test_from_base64_with_invalid_file(self): | ||
| 102 |         filepath = self.input_path('invalid-file.base64') | ||
| 103 | # static method | ||
| 104 | with self.assertRaises(ValueError): | ||
| 105 | IODict.from_base64(filepath) | ||
| 106 | # constructor | ||
| 107 | with self.assertRaises(ValueError): | ||
| 108 | IODict(filepath, format='base64') | ||
| 109 | |||
| 110 | def test_from_base64_with_valid_url_valid_content(self): | ||
| 111 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.base64' | ||
| 112 | # static method | ||
| 113 | d = IODict.from_base64(url) | ||
| 114 | self.assertTrue(isinstance(d, dict)) | ||
| 115 | # constructor | ||
| 116 | d = IODict(url, format='base64') | ||
| 117 | self.assertTrue(isinstance(d, dict)) | ||
| 118 | |||
| 119 | def test_from_base64_with_valid_url_invalid_content(self): | ||
| 120 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 121 | # static method | ||
| 122 | with self.assertRaises(ValueError): | ||
| 123 | IODict.from_base64(url) | ||
| 124 | # constructor | ||
| 125 | with self.assertRaises(ValueError): | ||
| 126 | IODict(url, format='base64') | ||
| 127 | |||
| 128 | def test_from_base64_with_invalid_url(self): | ||
| 129 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 130 | # static method | ||
| 131 | with self.assertRaises(ValueError): | ||
| 132 | IODict.from_base64(url) | ||
| 133 | # constructor | ||
| 134 | with self.assertRaises(ValueError): | ||
| 135 | IODict(url, format='base64') | ||
| 136 | |||
| 137 | def test_to_base64(self): | ||
| 138 |         d = IODict({ | ||
| 139 | 'a': 1, | ||
| 140 | 'b': 2, | ||
| 141 | 'c': 3, | ||
| 142 | }) | ||
| 143 | s = d.to_base64(sort_keys=True) | ||
| 144 | self.assertEqual(s, 'eyJhIjogMSwgImIiOiAyLCAiYyI6IDN9') | ||
| 145 | |||
| 146 | def test_to_base64_file(self): | ||
| 147 |         d = IODict({ | ||
| 148 | 'a': 1, | ||
| 149 | 'b': 2, | ||
| 150 | 'c': 3, | ||
| 151 | }) | ||
| 152 |         filepath = self.output_path('test_to_base64_file.base64') | ||
| 153 | d.to_base64(filepath=filepath, sort_keys=True) | ||
| 154 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 155 | self.assertEqual(d, IODict.from_base64(filepath)) | ||
| 156 | |||
| 157 | # CSV | ||
| 158 | |||
| 159 | def test_from_csv_with_valid_data(self): | ||
| 160 | s = """id,name,age,height,weight | ||
| 161 | 1,Alice,20,62,120.6 | ||
| 162 | 2,Freddie,21,74,190.6 | ||
| 163 | 3,Bob,17,68,120.0 | ||
| 164 | 4,François,32,75,110.05 | ||
| 165 | """ | ||
| 166 |         r = { | ||
| 167 | 'values': [ | ||
| 168 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 169 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 170 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 171 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 172 | ], | ||
| 173 | } | ||
| 174 | # static method | ||
| 175 | d = IODict.from_csv(s) | ||
| 176 | self.assertTrue(isinstance(d, dict)) | ||
| 177 | self.assertEqual(d, r) | ||
| 178 | # constructor | ||
| 179 | d = IODict(s, format='csv') | ||
| 180 | self.assertTrue(isinstance(d, dict)) | ||
| 181 | self.assertEqual(d, r) | ||
| 182 | |||
| 183 | # def test_from_csv_with_invalid_data(self): | ||
| 184 | # s = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 185 | # # static method | ||
| 186 | # with self.assertRaises(ValueError): | ||
| 187 | # print(IODict.from_csv(s)) | ||
| 188 | # # constructor | ||
| 189 | # with self.assertRaises(ValueError): | ||
| 190 | # IODict(s, format='csv') | ||
| 191 | |||
| 192 | def test_from_csv_with_valid_file_valid_content(self): | ||
| 193 |         filepath = self.input_path('valid-content.csv') | ||
| 194 | # static method | ||
| 195 | d = IODict.from_csv(filepath) | ||
| 196 | self.assertTrue(isinstance(d, dict)) | ||
| 197 | # constructor | ||
| 198 | d = IODict(filepath, format='csv') | ||
| 199 | self.assertTrue(isinstance(d, dict)) | ||
| 200 | |||
| 201 | # def test_from_csv_with_valid_file_valid_content_invalid_format(self): | ||
| 202 |     #     filepath = self.input_path('valid-content.base64') | ||
| 203 | # with self.assertRaises(ValueError): | ||
| 204 | # IODict.from_csv(filepath) | ||
| 205 |     #     filepath = self.input_path('valid-content.qs') | ||
| 206 | # with self.assertRaises(ValueError): | ||
| 207 | # IODict.from_csv(filepath) | ||
| 208 |     #     filepath = self.input_path('valid-content.toml') | ||
| 209 | # with self.assertRaises(ValueError): | ||
| 210 | # IODict.from_csv(filepath) | ||
| 211 |     #     filepath = self.input_path('valid-content.xml') | ||
| 212 | # with self.assertRaises(ValueError): | ||
| 213 | # IODict.from_csv(filepath) | ||
| 214 |     #     filepath = self.input_path('valid-content.yml') | ||
| 215 | # with self.assertRaises(ValueError): | ||
| 216 | # IODict.from_csv(filepath) | ||
| 217 | |||
| 218 | # def test_from_csv_with_valid_file_invalid_content(self): | ||
| 219 |     #     filepath = self.input_path('invalid-content.csv') | ||
| 220 | # # static method | ||
| 221 | # with self.assertRaises(ValueError): | ||
| 222 | # IODict.from_csv(filepath) | ||
| 223 | # # constructor | ||
| 224 | # with self.assertRaises(ValueError): | ||
| 225 | # IODict(filepath, format='csv') | ||
| 226 | |||
| 227 | def test_from_csv_with_invalid_file(self): | ||
| 228 |         filepath = self.input_path('invalid-file.csv') | ||
| 229 | # static method | ||
| 230 | with self.assertRaises(ValueError): | ||
| 231 | IODict.from_csv(filepath) | ||
| 232 | # constructor | ||
| 233 | with self.assertRaises(ValueError): | ||
| 234 | IODict(filepath, format='csv') | ||
| 235 | |||
| 236 | # TODO: python 2.7 max compatibility | ||
| 237 | # def test_from_csv_with_valid_url_valid_content(self): | ||
| 238 | # url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.csv' | ||
| 239 | # # static method | ||
| 240 | # d = IODict.from_csv(url) | ||
| 241 | # self.assertTrue(isinstance(d, dict)) | ||
| 242 | # # constructor | ||
| 243 | # d = IODict(url, format='csv') | ||
| 244 | # self.assertTrue(isinstance(d, dict)) | ||
| 245 | |||
| 246 | # def test_from_csv_with_valid_url_invalid_content(self): | ||
| 247 | # url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 248 | # # static method | ||
| 249 | # with self.assertRaises(ValueError): | ||
| 250 | # IODict.from_csv(url) | ||
| 251 | # # constructor | ||
| 252 | # with self.assertRaises(ValueError): | ||
| 253 | # IODict(url, format='csv') | ||
| 254 | |||
| 255 | def test_from_csv_with_invalid_url(self): | ||
| 256 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 257 | # static method | ||
| 258 | with self.assertRaises(ValueError): | ||
| 259 | IODict.from_csv(url) | ||
| 260 | # constructor | ||
| 261 | with self.assertRaises(ValueError): | ||
| 262 | IODict(url, format='csv') | ||
| 263 | |||
| 264 | def test_to_csv(self): | ||
| 265 |         d = IODict({ | ||
| 266 | 'values': [ | ||
| 267 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 268 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 269 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 270 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 271 | ], | ||
| 272 | }) | ||
| 273 | s = d.to_csv() | ||
| 274 | r = """age,height,id,name,weight | ||
| 275 | 20,62,1,Alice,120.6 | ||
| 276 | 21,74,2,Freddie,190.6 | ||
| 277 | 17,68,3,Bob,120.0 | ||
| 278 | 32,75,4,François,110.05 | ||
| 279 | """ | ||
| 280 | self.assertEqual(s, r) | ||
| 281 | |||
| 282 | def test_to_csv_with_custom_columns(self): | ||
| 283 |         d = IODict({ | ||
| 284 | 'values': [ | ||
| 285 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 286 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 287 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 288 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 289 | ], | ||
| 290 | }) | ||
| 291 | s = d.to_csv(key='values', columns=['id', 'name', 'family_name', 'age', 'height', 'gender', 'weight']) | ||
| 292 | r = """id,name,family_name,age,height,gender,weight | ||
| 293 | 1,Alice,,20,62,,120.6 | ||
| 294 | 2,Freddie,,21,74,,190.6 | ||
| 295 | 3,Bob,,17,68,,120.0 | ||
| 296 | 4,François,,32,75,,110.05 | ||
| 297 | """ | ||
| 298 | self.assertEqual(s, r) | ||
| 299 | |||
| 300 | def test_to_csv_with_custom_delimiter_and_quotes(self): | ||
| 301 |         d = IODict({ | ||
| 302 | 'values': [ | ||
| 303 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 304 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 305 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 306 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 307 | ], | ||
| 308 | }) | ||
| 309 | s = d.to_csv(columns=['id', 'name', 'age', 'height', 'weight'], delimiter=";", quote=True) | ||
| 310 | r = """"id";"name";"age";"height";"weight" | ||
| 311 | "1";"Alice";"20";"62";"120.6" | ||
| 312 | "2";"Freddie";"21";"74";"190.6" | ||
| 313 | "3";"Bob";"17";"68";"120.0" | ||
| 314 | "4";"François";"32";"75";"110.05" | ||
| 315 | """ | ||
| 316 | self.assertEqual(s, r) | ||
| 317 | |||
| 318 | def test_to_csv_with_custom_key_valid(self): | ||
| 319 |         d = IODict({ | ||
| 320 | 'results': [ | ||
| 321 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 322 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 323 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 324 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 325 | ], | ||
| 326 | }) | ||
| 327 |         s = d.to_csv('results', columns=['id', 'name', 'age', 'height', 'weight']) | ||
| 328 | r = """id,name,age,height,weight | ||
| 329 | 1,Alice,20,62,120.6 | ||
| 330 | 2,Freddie,21,74,190.6 | ||
| 331 | 3,Bob,17,68,120.0 | ||
| 332 | 4,François,32,75,110.05 | ||
| 333 | """ | ||
| 334 | self.assertEqual(s, r) | ||
| 335 | |||
| 336 | def test_to_csv_with_custom_key_invalid(self): | ||
| 337 |         d = IODict({ | ||
| 338 | 'values': [ | ||
| 339 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 340 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 341 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 342 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 343 | ], | ||
| 344 | }) | ||
| 345 | with self.assertRaises(KeyError): | ||
| 346 |             s = d.to_csv('invalid_values', columns=['id', 'name', 'age', 'height', 'weight']) | ||
| 347 | |||
| 348 | def test_to_csv_file(self): | ||
| 349 |         d = IODict({ | ||
| 350 | 'values': [ | ||
| 351 |                 { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, | ||
| 352 |                 { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, | ||
| 353 |                 { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, | ||
| 354 |                 { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, | ||
| 355 | ], | ||
| 356 | }) | ||
| 357 |         filepath = self.output_path('test_to_csv_file.csv') | ||
| 358 | d.to_csv(filepath=filepath) | ||
| 359 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 360 | self.assertEqual(d, IODict.from_csv(filepath)) | ||
| 361 | |||
| 362 | # JSON | ||
| 363 | |||
| 364 | def test_from_json_with_valid_data(self): | ||
| 365 |         j = '{"a": 1, "b": 2, "c": 3}' | ||
| 366 | # static method | ||
| 367 | d = IODict.from_json(j) | ||
| 368 | self.assertTrue(isinstance(d, dict)) | ||
| 369 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 370 | # constructor | ||
| 371 | d = IODict(j, format='json') | ||
| 372 | self.assertTrue(isinstance(d, dict)) | ||
| 373 |         self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 374 | |||
| 375 | def test_from_json_with_valid_data_list(self): | ||
| 376 | j = '[0,1,2,3,4,5]' | ||
| 377 | # static method | ||
| 378 | d = IODict.from_json(j) | ||
| 379 | self.assertTrue(isinstance(d, dict)) | ||
| 380 |         self.assertEqual(d, { 'values': [0, 1, 2, 3, 4, 5] }) | ||
| 381 | # constructor | ||
| 382 | d = IODict(j, format='json') | ||
| 383 | self.assertTrue(isinstance(d, dict)) | ||
| 384 |         self.assertEqual(d, { 'values': [0, 1, 2, 3, 4, 5] }) | ||
| 385 | |||
| 386 | # def test_from_json_with_valid_data_and_trailing_whitespace(self): | ||
| 387 |     #     j = '{"a": 1, "b": 2, "c": 3}\n\r\t\n' | ||
| 388 | # # static method | ||
| 389 | # d = IODict.from_json(j) | ||
| 390 | # self.assertTrue(isinstance(d, dict)) | ||
| 391 |     #     self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 392 | |||
| 393 | # def test_from_json_with_valid_data_and_trailing_null_chars(self): | ||
| 394 |     #     j = '{"a": 1, "b": 2, "c": 3}\x00\x00' | ||
| 395 | # # static method | ||
| 396 | # d = IODict.from_json(j) | ||
| 397 | # self.assertTrue(isinstance(d, dict)) | ||
| 398 |     #     self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 399 | |||
| 400 | # def test_from_json_with_valid_data_and_trailing_null_chars_and_whitespace(self): | ||
| 401 |     #     j = '{"a": 1, "b": 2, "c": 3}\n\x00\x00\n\t\n' | ||
| 402 | # # static method | ||
| 403 | # d = IODict.from_json(j) | ||
| 404 | # self.assertTrue(isinstance(d, dict)) | ||
| 405 |     #     self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) | ||
| 406 | |||
| 407 | def test_from_json_with_invalid_data(self): | ||
| 408 | j = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 409 | # static method | ||
| 410 | with self.assertRaises(ValueError): | ||
| 411 | IODict.from_json(j) | ||
| 412 | # constructor | ||
| 413 | with self.assertRaises(ValueError): | ||
| 414 | IODict(j, format='json') | ||
| 415 | |||
| 416 | def test_from_json_with_valid_file_valid_content(self): | ||
| 417 |         filepath = self.input_path('valid-content.json') | ||
| 418 | # static method | ||
| 419 | d = IODict.from_json(filepath) | ||
| 420 | self.assertTrue(isinstance(d, dict)) | ||
| 421 | # constructor | ||
| 422 | d = IODict(filepath, format='json') | ||
| 423 | self.assertTrue(isinstance(d, dict)) | ||
| 424 | |||
| 425 | def test_from_json_with_valid_file_valid_content_invalid_format(self): | ||
| 426 |         filepath = self.input_path('valid-content.base64') | ||
| 427 | with self.assertRaises(ValueError): | ||
| 428 | IODict.from_json(filepath) | ||
| 429 |         filepath = self.input_path('valid-content.qs') | ||
| 430 | with self.assertRaises(ValueError): | ||
| 431 | IODict.from_json(filepath) | ||
| 432 |         filepath = self.input_path('valid-content.toml') | ||
| 433 | with self.assertRaises(ValueError): | ||
| 434 | IODict.from_json(filepath) | ||
| 435 |         filepath = self.input_path('valid-content.xml') | ||
| 436 | with self.assertRaises(ValueError): | ||
| 437 | IODict.from_json(filepath) | ||
| 438 |         filepath = self.input_path('valid-content.yml') | ||
| 439 | with self.assertRaises(ValueError): | ||
| 440 | IODict.from_json(filepath) | ||
| 441 | |||
| 442 | def test_from_json_with_valid_file_invalid_content(self): | ||
| 443 |         filepath = self.input_path('invalid-content.json') | ||
| 444 | # static method | ||
| 445 | with self.assertRaises(ValueError): | ||
| 446 | IODict.from_json(filepath) | ||
| 447 | # constructor | ||
| 448 | with self.assertRaises(ValueError): | ||
| 449 | IODict(filepath, format='json') | ||
| 450 | |||
| 451 | def test_from_json_with_invalid_file(self): | ||
| 452 |         filepath = self.input_path('invalid-file.json') | ||
| 453 | # static method | ||
| 454 | with self.assertRaises(ValueError): | ||
| 455 | IODict.from_json(filepath) | ||
| 456 | # constructor | ||
| 457 | with self.assertRaises(ValueError): | ||
| 458 | IODict(filepath, format='json') | ||
| 459 | |||
| 460 | def test_from_json_with_valid_url_valid_content(self): | ||
| 461 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.json' | ||
| 462 | # static method | ||
| 463 | d = IODict.from_json(url) | ||
| 464 | self.assertTrue(isinstance(d, dict)) | ||
| 465 | # constructor | ||
| 466 | d = IODict(url, format='json') | ||
| 467 | self.assertTrue(isinstance(d, dict)) | ||
| 468 | |||
| 469 | def test_from_json_with_valid_url_invalid_content(self): | ||
| 470 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 471 | # static method | ||
| 472 | with self.assertRaises(ValueError): | ||
| 473 | IODict.from_json(url) | ||
| 474 | # constructor | ||
| 475 | with self.assertRaises(ValueError): | ||
| 476 | IODict(url, format='json') | ||
| 477 | |||
| 478 | def test_from_json_with_invalid_url(self): | ||
| 479 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 480 | # static method | ||
| 481 | with self.assertRaises(ValueError): | ||
| 482 | IODict.from_json(url) | ||
| 483 | # constructor | ||
| 484 | with self.assertRaises(ValueError): | ||
| 485 | IODict(url, format='json') | ||
| 486 | |||
| 487 | def test_to_json(self): | ||
| 488 |         d = IODict({ | ||
| 489 | 'x': 7, | ||
| 490 | 'y': 8, | ||
| 491 | 'z': 9, | ||
| 492 | 'a': 1, | ||
| 493 | 'b': 2, | ||
| 494 | 'c': 3, | ||
| 495 | }) | ||
| 496 | s = d.to_json(sort_keys=True) | ||
| 497 |         self.assertEqual(s, '{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}') | ||
| 498 | |||
| 499 | def test_to_json_file(self): | ||
| 500 |         d = IODict({ | ||
| 501 | 'x': 7, | ||
| 502 | 'y': 8, | ||
| 503 | 'z': 9, | ||
| 504 | 'a': 1, | ||
| 505 | 'b': 2, | ||
| 506 | 'c': 3, | ||
| 507 | }) | ||
| 508 |         filepath = self.output_path('test_to_json_file.json') | ||
| 509 | d.to_json(filepath=filepath, sort_keys=True) | ||
| 510 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 511 | self.assertEqual(d, IODict.from_json(filepath)) | ||
| 512 | |||
| 513 | # QUERY STRING | ||
| 514 | |||
| 515 | def test_from_query_string_with_valid_data(self): | ||
| 516 | s = 'ok=1&test=2&page=3&lib=python%20benedict&author=Fabio+Caccamo&author=Fabio%20Caccamo' | ||
| 517 |         r = { 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' } | ||
| 518 | # static method | ||
| 519 | d = IODict.from_query_string(s) | ||
| 520 | self.assertTrue(isinstance(d, dict)) | ||
| 521 | self.assertEqual(d, r) | ||
| 522 | # constructor | ||
| 523 | d = IODict(s, format='query_string') | ||
| 524 | self.assertTrue(isinstance(d, dict)) | ||
| 525 | self.assertEqual(d, r) | ||
| 526 | |||
| 527 | def test_from_query_string_with_invalid_data(self): | ||
| 528 | s = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 529 | # static method | ||
| 530 | with self.assertRaises(ValueError): | ||
| 531 | IODict.from_query_string(s) | ||
| 532 | # constructor | ||
| 533 | with self.assertRaises(ValueError): | ||
| 534 | IODict(s, format='query_string') | ||
| 535 | |||
| 536 | def test_from_query_string_with_valid_file_valid_content(self): | ||
| 537 |         filepath = self.input_path('valid-content.qs') | ||
| 538 | # static method | ||
| 539 | d = IODict.from_query_string(filepath) | ||
| 540 | self.assertTrue(isinstance(d, dict)) | ||
| 541 | # constructor | ||
| 542 | d = IODict(filepath, format='query_string') | ||
| 543 | self.assertTrue(isinstance(d, dict)) | ||
| 544 | |||
| 545 | def test_from_query_string_with_valid_file_valid_content_invalid_format(self): | ||
| 546 |         filepath = self.input_path('valid-content.base64') | ||
| 547 | with self.assertRaises(ValueError): | ||
| 548 | IODict.from_query_string(filepath) | ||
| 549 |         filepath = self.input_path('valid-content.json') | ||
| 550 | with self.assertRaises(ValueError): | ||
| 551 | IODict.from_query_string(filepath) | ||
| 552 |         filepath = self.input_path('valid-content.toml') | ||
| 553 | with self.assertRaises(ValueError): | ||
| 554 | IODict.from_query_string(filepath) | ||
| 555 |         filepath = self.input_path('valid-content.xml') | ||
| 556 | with self.assertRaises(ValueError): | ||
| 557 | IODict.from_query_string(filepath) | ||
| 558 |         filepath = self.input_path('valid-content.yml') | ||
| 559 | with self.assertRaises(ValueError): | ||
| 560 | IODict.from_query_string(filepath) | ||
| 561 | |||
| 562 | def test_from_query_string_with_valid_file_invalid_content(self): | ||
| 563 |         filepath = self.input_path('invalid-content.qs') | ||
| 564 | # static method | ||
| 565 | with self.assertRaises(ValueError): | ||
| 566 | IODict.from_query_string(filepath) | ||
| 567 | # constructor | ||
| 568 | with self.assertRaises(ValueError): | ||
| 569 | IODict(filepath, format='query_string') | ||
| 570 | |||
| 571 | def test_from_query_string_with_invalid_file(self): | ||
| 572 |         filepath = self.input_path('invalid-file.qs') | ||
| 573 | # static method | ||
| 574 | with self.assertRaises(ValueError): | ||
| 575 | IODict.from_query_string(filepath) | ||
| 576 | # constructor | ||
| 577 | with self.assertRaises(ValueError): | ||
| 578 | IODict(filepath, format='query_string') | ||
| 579 | |||
| 580 | def test_from_query_string_with_valid_url_valid_content(self): | ||
| 581 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.qs' | ||
| 582 | # static method | ||
| 583 | d = IODict.from_query_string(url) | ||
| 584 | self.assertTrue(isinstance(d, dict)) | ||
| 585 | # constructor | ||
| 586 | d = IODict(url, format='query_string') | ||
| 587 | self.assertTrue(isinstance(d, dict)) | ||
| 588 | |||
| 589 | def test_from_query_string_with_valid_url_invalid_content(self): | ||
| 590 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 591 | # static method | ||
| 592 | with self.assertRaises(ValueError): | ||
| 593 | IODict.from_query_string(url) | ||
| 594 | # constructor | ||
| 595 | with self.assertRaises(ValueError): | ||
| 596 | IODict(url, format='query_string') | ||
| 597 | |||
| 598 | def test_from_query_string_with_invalid_url(self): | ||
| 599 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 600 | # static method | ||
| 601 | with self.assertRaises(ValueError): | ||
| 602 | IODict.from_query_string(url) | ||
| 603 | # constructor | ||
| 604 | with self.assertRaises(ValueError): | ||
| 605 | IODict(url, format='query_string') | ||
| 606 | |||
| 607 | def test_to_query_string(self): | ||
| 608 |         data = { 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' } | ||
| 609 |         d = IODict({ 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' }) | ||
| 610 | s = d.to_query_string() | ||
| 611 | self.assertEqual(d, IODict.from_query_string(s)) | ||
| 612 | |||
| 613 | def test_to_query_string_file(self): | ||
| 614 |         d = IODict({ 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' }) | ||
| 615 |         filepath = self.output_path('test_to_query_string_file.qs') | ||
| 616 | d.to_query_string(filepath=filepath) | ||
| 617 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 618 | self.assertEqual(d, IODict.from_query_string(filepath)) | ||
| 619 | |||
| 620 | # TOML | ||
| 621 | |||
| 622 | def test_from_toml_with_valid_data(self): | ||
| 623 | j = """ | ||
| 624 | a = 1 | ||
| 625 | |||
| 626 | [b] | ||
| 627 | c = 3 | ||
| 628 | d = 4 | ||
| 629 | """ | ||
| 630 | # static method | ||
| 631 | d = IODict.from_toml(j) | ||
| 632 | self.assertTrue(isinstance(d, dict)) | ||
| 633 |         self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) | ||
| 634 | # constructor | ||
| 635 | d = IODict(j, format='toml') | ||
| 636 | self.assertTrue(isinstance(d, dict)) | ||
| 637 |         self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) | ||
| 638 | |||
| 639 | def test_from_toml_with_invalid_data(self): | ||
| 640 | j = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 641 | # static method | ||
| 642 | with self.assertRaises(ValueError): | ||
| 643 | IODict.from_toml(j) | ||
| 644 | # constructor | ||
| 645 | with self.assertRaises(ValueError): | ||
| 646 | IODict(j, format='toml') | ||
| 647 | |||
| 648 | def test_from_toml_with_valid_file_valid_content(self): | ||
| 649 |         filepath = self.input_path('valid-content.toml') | ||
| 650 | # static method | ||
| 651 | d = IODict.from_toml(filepath) | ||
| 652 | self.assertTrue(isinstance(d, dict)) | ||
| 653 | # constructor | ||
| 654 | d = IODict(filepath, format='toml') | ||
| 655 | self.assertTrue(isinstance(d, dict)) | ||
| 656 | |||
| 657 | def test_from_toml_with_valid_file_valid_content_invalid_format(self): | ||
| 658 |         # filepath = self.input_path('valid-content.base64') | ||
| 659 | # with self.assertRaises(ValueError): | ||
| 660 | # d = IODict.from_toml(filepath) | ||
| 661 |         filepath = self.input_path('valid-content.json') | ||
| 662 | with self.assertRaises(ValueError): | ||
| 663 | IODict.from_toml(filepath) | ||
| 664 |         filepath = self.input_path('valid-content.qs') | ||
| 665 | with self.assertRaises(ValueError): | ||
| 666 | IODict.from_toml(filepath) | ||
| 667 |         filepath = self.input_path('valid-content.xml') | ||
| 668 | with self.assertRaises(ValueError): | ||
| 669 | IODict.from_toml(filepath) | ||
| 670 |         filepath = self.input_path('valid-content.yml') | ||
| 671 | with self.assertRaises(ValueError): | ||
| 672 | IODict.from_toml(filepath) | ||
| 673 | |||
| 674 | def test_from_toml_with_valid_file_invalid_content(self): | ||
| 675 |         filepath = self.input_path('invalid-content.toml') | ||
| 676 | # static method | ||
| 677 | with self.assertRaises(ValueError): | ||
| 678 | IODict.from_toml(filepath) | ||
| 679 | # constructor | ||
| 680 | with self.assertRaises(ValueError): | ||
| 681 | IODict(filepath, format='toml') | ||
| 682 | |||
| 683 | def test_from_toml_with_invalid_file(self): | ||
| 684 |         filepath = self.input_path('invalid-file.toml') | ||
| 685 | # static method | ||
| 686 | with self.assertRaises(ValueError): | ||
| 687 | IODict.from_toml(filepath) | ||
| 688 | # constructor | ||
| 689 | with self.assertRaises(ValueError): | ||
| 690 | IODict(filepath, format='toml') | ||
| 691 | |||
| 692 | def test_from_toml_with_valid_url_valid_content(self): | ||
| 693 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.toml' | ||
| 694 | # static method | ||
| 695 | d = IODict.from_toml(url) | ||
| 696 | self.assertTrue(isinstance(d, dict)) | ||
| 697 | # constructor | ||
| 698 | d = IODict(url, format='toml') | ||
| 699 | self.assertTrue(isinstance(d, dict)) | ||
| 700 | |||
| 701 | def test_from_toml_with_valid_url_invalid_content(self): | ||
| 702 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 703 | # static method | ||
| 704 | with self.assertRaises(ValueError): | ||
| 705 | IODict.from_toml(url) | ||
| 706 | # constructor | ||
| 707 | with self.assertRaises(ValueError): | ||
| 708 | IODict(url, format='toml') | ||
| 709 | |||
| 710 | def test_from_toml_with_invalid_url(self): | ||
| 711 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 712 | # static method | ||
| 713 | with self.assertRaises(ValueError): | ||
| 714 | IODict.from_toml(url) | ||
| 715 | # constructor | ||
| 716 | with self.assertRaises(ValueError): | ||
| 717 | IODict(url, format='toml') | ||
| 718 | |||
| 719 | def test_to_toml(self): | ||
| 720 |         d = IODict({ | ||
| 721 | 'x': 7, | ||
| 722 | 'y': 8, | ||
| 723 | 'z': 9, | ||
| 724 | 'a': 1, | ||
| 725 | 'b': 2, | ||
| 726 | 'c': 3, | ||
| 727 | }) | ||
| 728 | s = d.to_toml() | ||
| 729 | self.assertEqual(d, IODict.from_toml(s)) | ||
| 730 | |||
| 731 | def test_to_toml_file(self): | ||
| 732 |         d = IODict({ | ||
| 733 | 'x': 7, | ||
| 734 | 'y': 8, | ||
| 735 | 'z': 9, | ||
| 736 | 'a': 1, | ||
| 737 | 'b': 2, | ||
| 738 | 'c': 3, | ||
| 739 | }) | ||
| 740 |         filepath = self.output_path('test_to_toml_file.toml') | ||
| 741 | d.to_toml(filepath=filepath) | ||
| 742 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 743 | self.assertEqual(d, IODict.from_toml(filepath)) | ||
| 744 | |||
| 745 | # XML | ||
| 746 | |||
| 747 | def test_from_xml_with_valid_data(self): | ||
| 748 | j = """ | ||
| 749 | <?xml version="1.0" ?> | ||
| 750 | <root> | ||
| 751 | <a>1</a> | ||
| 752 | <b> | ||
| 753 | <c>3</c> | ||
| 754 | <d>4</d> | ||
| 755 | </b> | ||
| 756 | </root> | ||
| 757 | """ | ||
| 758 | # static method | ||
| 759 | d = IODict.from_xml(j) | ||
| 760 | self.assertTrue(isinstance(d, dict)) | ||
| 761 |         self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },}) | ||
| 762 | # constructor | ||
| 763 | d = IODict(j, format='xml') | ||
| 764 | self.assertTrue(isinstance(d, dict)) | ||
| 765 |         self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },}) | ||
| 766 | |||
| 767 | def test_from_xml_with_invalid_data(self): | ||
| 768 | j = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 769 | # static method | ||
| 770 | with self.assertRaises(ValueError): | ||
| 771 | IODict.from_xml(j) | ||
| 772 | # constructor | ||
| 773 | with self.assertRaises(ValueError): | ||
| 774 | IODict(j, format='xml') | ||
| 775 | |||
| 776 | def test_from_xml_with_valid_file_valid_content(self): | ||
| 777 |         filepath = self.input_path('valid-content.xml') | ||
| 778 | # static method | ||
| 779 | d = IODict.from_xml(filepath) | ||
| 780 | self.assertTrue(isinstance(d, dict)) | ||
| 781 | # constructor | ||
| 782 | d = IODict(filepath, format='xml') | ||
| 783 | self.assertTrue(isinstance(d, dict)) | ||
| 784 | |||
| 785 | def test_from_xml_with_valid_file_valid_content_invalid_format(self): | ||
| 786 |         filepath = self.input_path('valid-content.base64') | ||
| 787 | with self.assertRaises(ValueError): | ||
| 788 | IODict.from_xml(filepath) | ||
| 789 |         filepath = self.input_path('valid-content.json') | ||
| 790 | with self.assertRaises(ValueError): | ||
| 791 | IODict.from_xml(filepath) | ||
| 792 |         filepath = self.input_path('valid-content.qs') | ||
| 793 | with self.assertRaises(ValueError): | ||
| 794 | IODict.from_xml(filepath) | ||
| 795 |         filepath = self.input_path('valid-content.toml') | ||
| 796 | with self.assertRaises(ValueError): | ||
| 797 | IODict.from_xml(filepath) | ||
| 798 |         filepath = self.input_path('valid-content.yml') | ||
| 799 | with self.assertRaises(ValueError): | ||
| 800 | IODict.from_xml(filepath) | ||
| 801 | |||
| 802 | def test_from_xml_with_valid_file_invalid_content(self): | ||
| 803 |         filepath = self.input_path('invalid-content.xml') | ||
| 804 | # static method | ||
| 805 | with self.assertRaises(ValueError): | ||
| 806 | IODict.from_xml(filepath) | ||
| 807 | # constructor | ||
| 808 | with self.assertRaises(ValueError): | ||
| 809 | IODict(filepath, format='xml') | ||
| 810 | |||
| 811 | def test_from_xml_with_invalid_file(self): | ||
| 812 |         filepath = self.input_path('invalid-file.xml') | ||
| 813 | # static method | ||
| 814 | with self.assertRaises(ValueError): | ||
| 815 | IODict.from_xml(filepath) | ||
| 816 | # constructor | ||
| 817 | with self.assertRaises(ValueError): | ||
| 818 | IODict(filepath, format='xml') | ||
| 819 | |||
| 820 | def test_from_xml_with_valid_url_valid_content(self): | ||
| 821 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.xml' | ||
| 822 | # static method | ||
| 823 | d = IODict.from_xml(url) | ||
| 824 | self.assertTrue(isinstance(d, dict)) | ||
| 825 | # constructor | ||
| 826 | d = IODict(url, format='xml') | ||
| 827 | self.assertTrue(isinstance(d, dict)) | ||
| 828 | |||
| 829 | def test_from_xml_with_valid_url_invalid_content(self): | ||
| 830 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 831 | # static method | ||
| 832 | with self.assertRaises(ValueError): | ||
| 833 | IODict.from_xml(url) | ||
| 834 | # constructor | ||
| 835 | with self.assertRaises(ValueError): | ||
| 836 | IODict(url, format='xml') | ||
| 837 | |||
| 838 | def test_from_xml_with_invalid_url(self): | ||
| 839 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 840 | # static method | ||
| 841 | with self.assertRaises(ValueError): | ||
| 842 | IODict.from_xml(url) | ||
| 843 | # constructor | ||
| 844 | with self.assertRaises(ValueError): | ||
| 845 | IODict(url, format='xml') | ||
| 846 | |||
| 847 | def test_to_xml(self): | ||
| 848 |         d = IODict({ | ||
| 849 |             'root': { | ||
| 850 | 'x': '7', | ||
| 851 | 'y': '8', | ||
| 852 | 'z': '9', | ||
| 853 | 'a': '1', | ||
| 854 | 'b': '2', | ||
| 855 | 'c': '3', | ||
| 856 | }, | ||
| 857 | }) | ||
| 858 | s = d.to_xml() | ||
| 859 | self.assertEqual(d, IODict.from_xml(s)) | ||
| 860 | |||
| 861 | def test_to_xml_file(self): | ||
| 862 |         d = IODict({ | ||
| 863 |             'root': { | ||
| 864 | 'x': '7', | ||
| 865 | 'y': '8', | ||
| 866 | 'z': '9', | ||
| 867 | 'a': '1', | ||
| 868 | 'b': '2', | ||
| 869 | 'c': '3', | ||
| 870 | }, | ||
| 871 | }) | ||
| 872 |         filepath = self.output_path('test_to_xml_file.xml') | ||
| 873 | d.to_xml(filepath=filepath) | ||
| 874 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 875 | self.assertEqual(d, IODict.from_xml(filepath)) | ||
| 876 | |||
| 877 | # YAML | ||
| 878 | |||
| 879 | def test_from_yaml_with_valid_data(self): | ||
| 880 | j = """ | ||
| 881 | a: 1 | ||
| 882 | b: | ||
| 883 | c: 3 | ||
| 884 | d: 4 | ||
| 885 | """ | ||
| 886 | # static method | ||
| 887 | d = IODict.from_yaml(j) | ||
| 888 | self.assertTrue(isinstance(d, dict)) | ||
| 889 |         self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) | ||
| 890 | # constructor | ||
| 891 | d = IODict(j, format='yaml') | ||
| 892 | self.assertTrue(isinstance(d, dict)) | ||
| 893 |         self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) | ||
| 894 | |||
| 895 | def test_from_yaml_with_invalid_data(self): | ||
| 896 | j = 'Lorem ipsum est in ea occaecat nisi officia.' | ||
| 897 | # static method | ||
| 898 | with self.assertRaises(ValueError): | ||
| 899 | IODict.from_yaml(j) | ||
| 900 | # constructor | ||
| 901 | with self.assertRaises(ValueError): | ||
| 902 | IODict(j, format='yaml') | ||
| 903 | |||
| 904 | def test_from_yaml_with_valid_file_valid_content(self): | ||
| 905 |         filepath = self.input_path('valid-content.yml') | ||
| 906 | # static method | ||
| 907 | d = IODict.from_yaml(filepath) | ||
| 908 | self.assertTrue(isinstance(d, dict)) | ||
| 909 | # constructor | ||
| 910 | d = IODict(filepath, format='yaml') | ||
| 911 | self.assertTrue(isinstance(d, dict)) | ||
| 912 | |||
| 913 | def test_from_yaml_with_valid_file_valid_content_invalid_format(self): | ||
| 914 |         filepath = self.input_path('valid-content.base64') | ||
| 915 | with self.assertRaises(ValueError): | ||
| 916 | IODict.from_yaml(filepath) | ||
| 917 |         # filepath = self.input_path('valid-content.json') | ||
| 918 | # with self.assertRaises(ValueError): | ||
| 919 | # IODict.from_yaml(filepath) | ||
| 920 |         filepath = self.input_path('valid-content.qs') | ||
| 921 | with self.assertRaises(ValueError): | ||
| 922 | IODict.from_yaml(filepath) | ||
| 923 |         filepath = self.input_path('valid-content.toml') | ||
| 924 | with self.assertRaises(ValueError): | ||
| 925 | IODict.from_yaml(filepath) | ||
| 926 |         filepath = self.input_path('valid-content.xml') | ||
| 927 | with self.assertRaises(ValueError): | ||
| 928 | IODict.from_yaml(filepath) | ||
| 929 | |||
| 930 | def test_from_yaml_with_valid_file_invalid_content(self): | ||
| 931 |         filepath = self.input_path('invalid-content.yml') | ||
| 932 | # static method | ||
| 933 | with self.assertRaises(ValueError): | ||
| 934 | IODict.from_yaml(filepath) | ||
| 935 | # constructor | ||
| 936 | with self.assertRaises(ValueError): | ||
| 937 | IODict(filepath, format='yaml') | ||
| 938 | |||
| 939 | def test_from_yaml_with_invalid_file(self): | ||
| 940 |         filepath = self.input_path('invalid-file.yml') | ||
| 941 | # static method | ||
| 942 | with self.assertRaises(ValueError): | ||
| 943 | IODict.from_yaml(filepath) | ||
| 944 | # constructor | ||
| 945 | with self.assertRaises(ValueError): | ||
| 946 | IODict(filepath, format='yaml') | ||
| 947 | |||
| 948 | def test_from_yaml_with_valid_url_valid_content(self): | ||
| 949 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.yml' | ||
| 950 | # static method | ||
| 951 | d = IODict.from_yaml(url) | ||
| 952 | self.assertTrue(isinstance(d, dict)) | ||
| 953 | # constructor | ||
| 954 | d = IODict(url, format='yaml') | ||
| 955 | self.assertTrue(isinstance(d, dict)) | ||
| 956 | |||
| 957 | def test_from_yaml_with_valid_url_invalid_content(self): | ||
| 958 | url = 'https://github.com/fabiocaccamo/python-benedict' | ||
| 959 | # static method | ||
| 960 | with self.assertRaises(ValueError): | ||
| 961 | IODict.from_yaml(url) | ||
| 962 | # constructor | ||
| 963 | with self.assertRaises(ValueError): | ||
| 964 | IODict(url, format='yaml') | ||
| 965 | |||
| 966 | def test_from_yaml_with_invalid_url(self): | ||
| 967 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' | ||
| 968 | # static method | ||
| 969 | with self.assertRaises(ValueError): | ||
| 970 | IODict.from_yaml(url) | ||
| 971 | # constructor | ||
| 972 | with self.assertRaises(ValueError): | ||
| 973 | IODict(url, format='yaml') | ||
| 974 | |||
| 975 | def test_to_yaml(self): | ||
| 976 |         d = IODict({ | ||
| 977 | 'x': 7, | ||
| 978 | 'y': 8, | ||
| 979 | 'z': 9, | ||
| 980 | 'a': 1, | ||
| 981 | 'b': 2, | ||
| 982 | 'c': 3, | ||
| 983 | }) | ||
| 984 | s = d.to_yaml() | ||
| 985 | self.assertEqual(d, IODict.from_yaml(s)) | ||
| 986 | |||
| 987 | def test_to_yaml_file(self): | ||
| 988 |         d = IODict({ | ||
| 989 | 'x': 7, | ||
| 990 | 'y': 8, | ||
| 991 | 'z': 9, | ||
| 992 | 'a': 1, | ||
| 993 | 'b': 2, | ||
| 994 | 'c': 3, | ||
| 995 | }) | ||
| 996 |         filepath = self.output_path('test_to_yaml_file.yml') | ||
| 997 | d.to_yaml(filepath=filepath) | ||
| 998 | self.assertTrue(d, os.path.isfile(filepath)) | ||
| 999 | self.assertEqual(d, IODict.from_yaml(filepath)) | ||
| 1000 |