| Total Complexity | 171 |
| Total Lines | 999 |
| Duplicated Lines | 7.41 % |
| 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 | View Code Duplication | 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 | # def test_from_csv_with_valid_url_valid_content(self): |
||
| 237 | # url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.csv' |
||
| 238 | # # static method |
||
| 239 | # d = IODict.from_csv(url) |
||
| 240 | # self.assertTrue(isinstance(d, dict)) |
||
| 241 | # # constructor |
||
| 242 | # d = IODict(url, format='csv') |
||
| 243 | # self.assertTrue(isinstance(d, dict)) |
||
| 244 | |||
| 245 | # def test_from_csv_with_valid_url_invalid_content(self): |
||
| 246 | # url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 247 | # # static method |
||
| 248 | # with self.assertRaises(ValueError): |
||
| 249 | # IODict.from_csv(url) |
||
| 250 | # # constructor |
||
| 251 | # with self.assertRaises(ValueError): |
||
| 252 | # IODict(url, format='csv') |
||
| 253 | |||
| 254 | def test_from_csv_with_invalid_url(self): |
||
| 255 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 256 | # static method |
||
| 257 | with self.assertRaises(ValueError): |
||
| 258 | IODict.from_csv(url) |
||
| 259 | # constructor |
||
| 260 | with self.assertRaises(ValueError): |
||
| 261 | IODict(url, format='csv') |
||
| 262 | |||
| 263 | View Code Duplication | def test_to_csv(self): |
|
| 264 | d = IODict({ |
||
| 265 | 'values': [ |
||
| 266 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 267 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 268 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 269 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 270 | ], |
||
| 271 | }) |
||
| 272 | s = d.to_csv() |
||
| 273 | r = """age,height,id,name,weight |
||
| 274 | 20,62,1,Alice,120.6 |
||
| 275 | 21,74,2,Freddie,190.6 |
||
| 276 | 17,68,3,Bob,120.0 |
||
| 277 | 32,75,4,François,110.05 |
||
| 278 | """ |
||
| 279 | self.assertEqual(s, r) |
||
| 280 | |||
| 281 | View Code Duplication | def test_to_csv_with_custom_columns(self): |
|
| 282 | d = IODict({ |
||
| 283 | 'values': [ |
||
| 284 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 285 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 286 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 287 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 288 | ], |
||
| 289 | }) |
||
| 290 | s = d.to_csv(key='values', columns=['id', 'name', 'family_name', 'age', 'height', 'gender', 'weight']) |
||
| 291 | r = """id,name,family_name,age,height,gender,weight |
||
| 292 | 1,Alice,,20,62,,120.6 |
||
| 293 | 2,Freddie,,21,74,,190.6 |
||
| 294 | 3,Bob,,17,68,,120.0 |
||
| 295 | 4,François,,32,75,,110.05 |
||
| 296 | """ |
||
| 297 | self.assertEqual(s, r) |
||
| 298 | |||
| 299 | def test_to_csv_with_custom_delimiter_and_quotes(self): |
||
| 300 | d = IODict({ |
||
| 301 | 'values': [ |
||
| 302 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 303 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 304 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 305 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 306 | ], |
||
| 307 | }) |
||
| 308 | s = d.to_csv(columns=['id', 'name', 'age', 'height', 'weight'], delimiter=";", quote=True) |
||
| 309 | r = """"id";"name";"age";"height";"weight" |
||
| 310 | "1";"Alice";"20";"62";"120.6" |
||
| 311 | "2";"Freddie";"21";"74";"190.6" |
||
| 312 | "3";"Bob";"17";"68";"120.0" |
||
| 313 | "4";"François";"32";"75";"110.05" |
||
| 314 | """ |
||
| 315 | self.assertEqual(s, r) |
||
| 316 | |||
| 317 | View Code Duplication | def test_to_csv_with_custom_key_valid(self): |
|
| 318 | d = IODict({ |
||
| 319 | 'results': [ |
||
| 320 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 321 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 322 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 323 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 324 | ], |
||
| 325 | }) |
||
| 326 | s = d.to_csv('results', columns=['id', 'name', 'age', 'height', 'weight']) |
||
| 327 | r = """id,name,age,height,weight |
||
| 328 | 1,Alice,20,62,120.6 |
||
| 329 | 2,Freddie,21,74,190.6 |
||
| 330 | 3,Bob,17,68,120.0 |
||
| 331 | 4,François,32,75,110.05 |
||
| 332 | """ |
||
| 333 | self.assertEqual(s, r) |
||
| 334 | |||
| 335 | def test_to_csv_with_custom_key_invalid(self): |
||
| 336 | d = IODict({ |
||
| 337 | 'values': [ |
||
| 338 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 339 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 340 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 341 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 342 | ], |
||
| 343 | }) |
||
| 344 | with self.assertRaises(KeyError): |
||
| 345 | s = d.to_csv('invalid_values', columns=['id', 'name', 'age', 'height', 'weight']) |
||
| 346 | |||
| 347 | def test_to_csv_file(self): |
||
| 348 | d = IODict({ |
||
| 349 | 'values': [ |
||
| 350 | { 'id':'1', 'name':'Alice', 'age':'20', 'height':'62', 'weight':'120.6', }, |
||
| 351 | { 'id':'2', 'name':'Freddie', 'age':'21', 'height':'74', 'weight':'190.6', }, |
||
| 352 | { 'id':'3', 'name':'Bob', 'age':'17', 'height':'68', 'weight':'120.0', }, |
||
| 353 | { 'id':'4', 'name':'François', 'age':'32', 'height':'75', 'weight':'110.05', }, |
||
| 354 | ], |
||
| 355 | }) |
||
| 356 | filepath = self.output_path('test_to_csv_file.csv') |
||
| 357 | d.to_csv(filepath=filepath) |
||
| 358 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 359 | self.assertEqual(d, IODict.from_csv(filepath)) |
||
| 360 | |||
| 361 | # JSON |
||
| 362 | |||
| 363 | def test_from_json_with_valid_data(self): |
||
| 364 | j = '{"a": 1, "b": 2, "c": 3}' |
||
| 365 | # static method |
||
| 366 | d = IODict.from_json(j) |
||
| 367 | self.assertTrue(isinstance(d, dict)) |
||
| 368 | self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) |
||
| 369 | # constructor |
||
| 370 | d = IODict(j, format='json') |
||
| 371 | self.assertTrue(isinstance(d, dict)) |
||
| 372 | self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) |
||
| 373 | |||
| 374 | def test_from_json_with_valid_data_list(self): |
||
| 375 | j = '[0,1,2,3,4,5]' |
||
| 376 | # static method |
||
| 377 | d = IODict.from_json(j) |
||
| 378 | self.assertTrue(isinstance(d, dict)) |
||
| 379 | self.assertEqual(d, { 'values': [0, 1, 2, 3, 4, 5] }) |
||
| 380 | # constructor |
||
| 381 | d = IODict(j, format='json') |
||
| 382 | self.assertTrue(isinstance(d, dict)) |
||
| 383 | self.assertEqual(d, { 'values': [0, 1, 2, 3, 4, 5] }) |
||
| 384 | |||
| 385 | # def test_from_json_with_valid_data_and_trailing_whitespace(self): |
||
| 386 | # j = '{"a": 1, "b": 2, "c": 3}\n\r\t\n' |
||
| 387 | # # static method |
||
| 388 | # d = IODict.from_json(j) |
||
| 389 | # self.assertTrue(isinstance(d, dict)) |
||
| 390 | # self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) |
||
| 391 | |||
| 392 | # def test_from_json_with_valid_data_and_trailing_null_chars(self): |
||
| 393 | # j = '{"a": 1, "b": 2, "c": 3}\x00\x00' |
||
| 394 | # # static method |
||
| 395 | # d = IODict.from_json(j) |
||
| 396 | # self.assertTrue(isinstance(d, dict)) |
||
| 397 | # self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) |
||
| 398 | |||
| 399 | # def test_from_json_with_valid_data_and_trailing_null_chars_and_whitespace(self): |
||
| 400 | # j = '{"a": 1, "b": 2, "c": 3}\n\x00\x00\n\t\n' |
||
| 401 | # # static method |
||
| 402 | # d = IODict.from_json(j) |
||
| 403 | # self.assertTrue(isinstance(d, dict)) |
||
| 404 | # self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, }) |
||
| 405 | |||
| 406 | def test_from_json_with_invalid_data(self): |
||
| 407 | j = 'Lorem ipsum est in ea occaecat nisi officia.' |
||
| 408 | # static method |
||
| 409 | with self.assertRaises(ValueError): |
||
| 410 | IODict.from_json(j) |
||
| 411 | # constructor |
||
| 412 | with self.assertRaises(ValueError): |
||
| 413 | IODict(j, format='json') |
||
| 414 | |||
| 415 | def test_from_json_with_valid_file_valid_content(self): |
||
| 416 | filepath = self.input_path('valid-content.json') |
||
| 417 | # static method |
||
| 418 | d = IODict.from_json(filepath) |
||
| 419 | self.assertTrue(isinstance(d, dict)) |
||
| 420 | # constructor |
||
| 421 | d = IODict(filepath, format='json') |
||
| 422 | self.assertTrue(isinstance(d, dict)) |
||
| 423 | |||
| 424 | def test_from_json_with_valid_file_valid_content_invalid_format(self): |
||
| 425 | filepath = self.input_path('valid-content.base64') |
||
| 426 | with self.assertRaises(ValueError): |
||
| 427 | IODict.from_json(filepath) |
||
| 428 | filepath = self.input_path('valid-content.qs') |
||
| 429 | with self.assertRaises(ValueError): |
||
| 430 | IODict.from_json(filepath) |
||
| 431 | filepath = self.input_path('valid-content.toml') |
||
| 432 | with self.assertRaises(ValueError): |
||
| 433 | IODict.from_json(filepath) |
||
| 434 | filepath = self.input_path('valid-content.xml') |
||
| 435 | with self.assertRaises(ValueError): |
||
| 436 | IODict.from_json(filepath) |
||
| 437 | filepath = self.input_path('valid-content.yml') |
||
| 438 | with self.assertRaises(ValueError): |
||
| 439 | IODict.from_json(filepath) |
||
| 440 | |||
| 441 | def test_from_json_with_valid_file_invalid_content(self): |
||
| 442 | filepath = self.input_path('invalid-content.json') |
||
| 443 | # static method |
||
| 444 | with self.assertRaises(ValueError): |
||
| 445 | IODict.from_json(filepath) |
||
| 446 | # constructor |
||
| 447 | with self.assertRaises(ValueError): |
||
| 448 | IODict(filepath, format='json') |
||
| 449 | |||
| 450 | def test_from_json_with_invalid_file(self): |
||
| 451 | filepath = self.input_path('invalid-file.json') |
||
| 452 | # static method |
||
| 453 | with self.assertRaises(ValueError): |
||
| 454 | IODict.from_json(filepath) |
||
| 455 | # constructor |
||
| 456 | with self.assertRaises(ValueError): |
||
| 457 | IODict(filepath, format='json') |
||
| 458 | |||
| 459 | def test_from_json_with_valid_url_valid_content(self): |
||
| 460 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.json' |
||
| 461 | # static method |
||
| 462 | d = IODict.from_json(url) |
||
| 463 | self.assertTrue(isinstance(d, dict)) |
||
| 464 | # constructor |
||
| 465 | d = IODict(url, format='json') |
||
| 466 | self.assertTrue(isinstance(d, dict)) |
||
| 467 | |||
| 468 | def test_from_json_with_valid_url_invalid_content(self): |
||
| 469 | url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 470 | # static method |
||
| 471 | with self.assertRaises(ValueError): |
||
| 472 | IODict.from_json(url) |
||
| 473 | # constructor |
||
| 474 | with self.assertRaises(ValueError): |
||
| 475 | IODict(url, format='json') |
||
| 476 | |||
| 477 | def test_from_json_with_invalid_url(self): |
||
| 478 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 479 | # static method |
||
| 480 | with self.assertRaises(ValueError): |
||
| 481 | IODict.from_json(url) |
||
| 482 | # constructor |
||
| 483 | with self.assertRaises(ValueError): |
||
| 484 | IODict(url, format='json') |
||
| 485 | |||
| 486 | def test_to_json(self): |
||
| 487 | d = IODict({ |
||
| 488 | 'x': 7, |
||
| 489 | 'y': 8, |
||
| 490 | 'z': 9, |
||
| 491 | 'a': 1, |
||
| 492 | 'b': 2, |
||
| 493 | 'c': 3, |
||
| 494 | }) |
||
| 495 | s = d.to_json(sort_keys=True) |
||
| 496 | self.assertEqual(s, '{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}') |
||
| 497 | |||
| 498 | def test_to_json_file(self): |
||
| 499 | d = IODict({ |
||
| 500 | 'x': 7, |
||
| 501 | 'y': 8, |
||
| 502 | 'z': 9, |
||
| 503 | 'a': 1, |
||
| 504 | 'b': 2, |
||
| 505 | 'c': 3, |
||
| 506 | }) |
||
| 507 | filepath = self.output_path('test_to_json_file.json') |
||
| 508 | d.to_json(filepath=filepath, sort_keys=True) |
||
| 509 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 510 | self.assertEqual(d, IODict.from_json(filepath)) |
||
| 511 | |||
| 512 | # QUERY STRING |
||
| 513 | |||
| 514 | def test_from_query_string_with_valid_data(self): |
||
| 515 | s = 'ok=1&test=2&page=3&lib=python%20benedict&author=Fabio+Caccamo&author=Fabio%20Caccamo' |
||
| 516 | r = { 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' } |
||
| 517 | # static method |
||
| 518 | d = IODict.from_query_string(s) |
||
| 519 | self.assertTrue(isinstance(d, dict)) |
||
| 520 | self.assertEqual(d, r) |
||
| 521 | # constructor |
||
| 522 | d = IODict(s, format='query_string') |
||
| 523 | self.assertTrue(isinstance(d, dict)) |
||
| 524 | self.assertEqual(d, r) |
||
| 525 | |||
| 526 | def test_from_query_string_with_invalid_data(self): |
||
| 527 | s = 'Lorem ipsum est in ea occaecat nisi officia.' |
||
| 528 | # static method |
||
| 529 | with self.assertRaises(ValueError): |
||
| 530 | IODict.from_query_string(s) |
||
| 531 | # constructor |
||
| 532 | with self.assertRaises(ValueError): |
||
| 533 | IODict(s, format='query_string') |
||
| 534 | |||
| 535 | def test_from_query_string_with_valid_file_valid_content(self): |
||
| 536 | filepath = self.input_path('valid-content.qs') |
||
| 537 | # static method |
||
| 538 | d = IODict.from_query_string(filepath) |
||
| 539 | self.assertTrue(isinstance(d, dict)) |
||
| 540 | # constructor |
||
| 541 | d = IODict(filepath, format='query_string') |
||
| 542 | self.assertTrue(isinstance(d, dict)) |
||
| 543 | |||
| 544 | def test_from_query_string_with_valid_file_valid_content_invalid_format(self): |
||
| 545 | filepath = self.input_path('valid-content.base64') |
||
| 546 | with self.assertRaises(ValueError): |
||
| 547 | IODict.from_query_string(filepath) |
||
| 548 | filepath = self.input_path('valid-content.json') |
||
| 549 | with self.assertRaises(ValueError): |
||
| 550 | IODict.from_query_string(filepath) |
||
| 551 | filepath = self.input_path('valid-content.toml') |
||
| 552 | with self.assertRaises(ValueError): |
||
| 553 | IODict.from_query_string(filepath) |
||
| 554 | filepath = self.input_path('valid-content.xml') |
||
| 555 | with self.assertRaises(ValueError): |
||
| 556 | IODict.from_query_string(filepath) |
||
| 557 | filepath = self.input_path('valid-content.yml') |
||
| 558 | with self.assertRaises(ValueError): |
||
| 559 | IODict.from_query_string(filepath) |
||
| 560 | |||
| 561 | def test_from_query_string_with_valid_file_invalid_content(self): |
||
| 562 | filepath = self.input_path('invalid-content.qs') |
||
| 563 | # static method |
||
| 564 | with self.assertRaises(ValueError): |
||
| 565 | IODict.from_query_string(filepath) |
||
| 566 | # constructor |
||
| 567 | with self.assertRaises(ValueError): |
||
| 568 | IODict(filepath, format='query_string') |
||
| 569 | |||
| 570 | def test_from_query_string_with_invalid_file(self): |
||
| 571 | filepath = self.input_path('invalid-file.qs') |
||
| 572 | # static method |
||
| 573 | with self.assertRaises(ValueError): |
||
| 574 | IODict.from_query_string(filepath) |
||
| 575 | # constructor |
||
| 576 | with self.assertRaises(ValueError): |
||
| 577 | IODict(filepath, format='query_string') |
||
| 578 | |||
| 579 | def test_from_query_string_with_valid_url_valid_content(self): |
||
| 580 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.qs' |
||
| 581 | # static method |
||
| 582 | d = IODict.from_query_string(url) |
||
| 583 | self.assertTrue(isinstance(d, dict)) |
||
| 584 | # constructor |
||
| 585 | d = IODict(url, format='query_string') |
||
| 586 | self.assertTrue(isinstance(d, dict)) |
||
| 587 | |||
| 588 | def test_from_query_string_with_valid_url_invalid_content(self): |
||
| 589 | url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 590 | # static method |
||
| 591 | with self.assertRaises(ValueError): |
||
| 592 | IODict.from_query_string(url) |
||
| 593 | # constructor |
||
| 594 | with self.assertRaises(ValueError): |
||
| 595 | IODict(url, format='query_string') |
||
| 596 | |||
| 597 | def test_from_query_string_with_invalid_url(self): |
||
| 598 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 599 | # static method |
||
| 600 | with self.assertRaises(ValueError): |
||
| 601 | IODict.from_query_string(url) |
||
| 602 | # constructor |
||
| 603 | with self.assertRaises(ValueError): |
||
| 604 | IODict(url, format='query_string') |
||
| 605 | |||
| 606 | def test_to_query_string(self): |
||
| 607 | data = { 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' } |
||
| 608 | d = IODict({ 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' }) |
||
| 609 | s = d.to_query_string() |
||
| 610 | self.assertEqual(d, IODict.from_query_string(s)) |
||
| 611 | |||
| 612 | def test_to_query_string_file(self): |
||
| 613 | d = IODict({ 'ok': '1', 'test': '2', 'page': '3', 'lib':'python benedict', 'author':'Fabio Caccamo' }) |
||
| 614 | filepath = self.output_path('test_to_query_string_file.qs') |
||
| 615 | d.to_query_string(filepath=filepath) |
||
| 616 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 617 | self.assertEqual(d, IODict.from_query_string(filepath)) |
||
| 618 | |||
| 619 | # TOML |
||
| 620 | |||
| 621 | def test_from_toml_with_valid_data(self): |
||
| 622 | j = """ |
||
| 623 | a = 1 |
||
| 624 | |||
| 625 | [b] |
||
| 626 | c = 3 |
||
| 627 | d = 4 |
||
| 628 | """ |
||
| 629 | # static method |
||
| 630 | d = IODict.from_toml(j) |
||
| 631 | self.assertTrue(isinstance(d, dict)) |
||
| 632 | self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) |
||
| 633 | # constructor |
||
| 634 | d = IODict(j, format='toml') |
||
| 635 | self.assertTrue(isinstance(d, dict)) |
||
| 636 | self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) |
||
| 637 | |||
| 638 | def test_from_toml_with_invalid_data(self): |
||
| 639 | j = 'Lorem ipsum est in ea occaecat nisi officia.' |
||
| 640 | # static method |
||
| 641 | with self.assertRaises(ValueError): |
||
| 642 | IODict.from_toml(j) |
||
| 643 | # constructor |
||
| 644 | with self.assertRaises(ValueError): |
||
| 645 | IODict(j, format='toml') |
||
| 646 | |||
| 647 | def test_from_toml_with_valid_file_valid_content(self): |
||
| 648 | filepath = self.input_path('valid-content.toml') |
||
| 649 | # static method |
||
| 650 | d = IODict.from_toml(filepath) |
||
| 651 | self.assertTrue(isinstance(d, dict)) |
||
| 652 | # constructor |
||
| 653 | d = IODict(filepath, format='toml') |
||
| 654 | self.assertTrue(isinstance(d, dict)) |
||
| 655 | |||
| 656 | def test_from_toml_with_valid_file_valid_content_invalid_format(self): |
||
| 657 | # filepath = self.input_path('valid-content.base64') |
||
| 658 | # with self.assertRaises(ValueError): |
||
| 659 | # d = IODict.from_toml(filepath) |
||
| 660 | filepath = self.input_path('valid-content.json') |
||
| 661 | with self.assertRaises(ValueError): |
||
| 662 | IODict.from_toml(filepath) |
||
| 663 | filepath = self.input_path('valid-content.qs') |
||
| 664 | with self.assertRaises(ValueError): |
||
| 665 | IODict.from_toml(filepath) |
||
| 666 | filepath = self.input_path('valid-content.xml') |
||
| 667 | with self.assertRaises(ValueError): |
||
| 668 | IODict.from_toml(filepath) |
||
| 669 | filepath = self.input_path('valid-content.yml') |
||
| 670 | with self.assertRaises(ValueError): |
||
| 671 | IODict.from_toml(filepath) |
||
| 672 | |||
| 673 | def test_from_toml_with_valid_file_invalid_content(self): |
||
| 674 | filepath = self.input_path('invalid-content.toml') |
||
| 675 | # static method |
||
| 676 | with self.assertRaises(ValueError): |
||
| 677 | IODict.from_toml(filepath) |
||
| 678 | # constructor |
||
| 679 | with self.assertRaises(ValueError): |
||
| 680 | IODict(filepath, format='toml') |
||
| 681 | |||
| 682 | def test_from_toml_with_invalid_file(self): |
||
| 683 | filepath = self.input_path('invalid-file.toml') |
||
| 684 | # static method |
||
| 685 | with self.assertRaises(ValueError): |
||
| 686 | IODict.from_toml(filepath) |
||
| 687 | # constructor |
||
| 688 | with self.assertRaises(ValueError): |
||
| 689 | IODict(filepath, format='toml') |
||
| 690 | |||
| 691 | def test_from_toml_with_valid_url_valid_content(self): |
||
| 692 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.toml' |
||
| 693 | # static method |
||
| 694 | d = IODict.from_toml(url) |
||
| 695 | self.assertTrue(isinstance(d, dict)) |
||
| 696 | # constructor |
||
| 697 | d = IODict(url, format='toml') |
||
| 698 | self.assertTrue(isinstance(d, dict)) |
||
| 699 | |||
| 700 | def test_from_toml_with_valid_url_invalid_content(self): |
||
| 701 | url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 702 | # static method |
||
| 703 | with self.assertRaises(ValueError): |
||
| 704 | IODict.from_toml(url) |
||
| 705 | # constructor |
||
| 706 | with self.assertRaises(ValueError): |
||
| 707 | IODict(url, format='toml') |
||
| 708 | |||
| 709 | def test_from_toml_with_invalid_url(self): |
||
| 710 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 711 | # static method |
||
| 712 | with self.assertRaises(ValueError): |
||
| 713 | IODict.from_toml(url) |
||
| 714 | # constructor |
||
| 715 | with self.assertRaises(ValueError): |
||
| 716 | IODict(url, format='toml') |
||
| 717 | |||
| 718 | def test_to_toml(self): |
||
| 719 | d = IODict({ |
||
| 720 | 'x': 7, |
||
| 721 | 'y': 8, |
||
| 722 | 'z': 9, |
||
| 723 | 'a': 1, |
||
| 724 | 'b': 2, |
||
| 725 | 'c': 3, |
||
| 726 | }) |
||
| 727 | s = d.to_toml() |
||
| 728 | self.assertEqual(d, IODict.from_toml(s)) |
||
| 729 | |||
| 730 | def test_to_toml_file(self): |
||
| 731 | d = IODict({ |
||
| 732 | 'x': 7, |
||
| 733 | 'y': 8, |
||
| 734 | 'z': 9, |
||
| 735 | 'a': 1, |
||
| 736 | 'b': 2, |
||
| 737 | 'c': 3, |
||
| 738 | }) |
||
| 739 | filepath = self.output_path('test_to_toml_file.toml') |
||
| 740 | d.to_toml(filepath=filepath) |
||
| 741 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 742 | self.assertEqual(d, IODict.from_toml(filepath)) |
||
| 743 | |||
| 744 | # XML |
||
| 745 | |||
| 746 | def test_from_xml_with_valid_data(self): |
||
| 747 | j = """ |
||
| 748 | <?xml version="1.0" ?> |
||
| 749 | <root> |
||
| 750 | <a>1</a> |
||
| 751 | <b> |
||
| 752 | <c>3</c> |
||
| 753 | <d>4</d> |
||
| 754 | </b> |
||
| 755 | </root> |
||
| 756 | """ |
||
| 757 | # static method |
||
| 758 | d = IODict.from_xml(j) |
||
| 759 | self.assertTrue(isinstance(d, dict)) |
||
| 760 | self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },}) |
||
| 761 | # constructor |
||
| 762 | d = IODict(j, format='xml') |
||
| 763 | self.assertTrue(isinstance(d, dict)) |
||
| 764 | self.assertEqual(d.get('root'), { 'a':'1', 'b':{ 'c':'3', 'd':'4' },}) |
||
| 765 | |||
| 766 | def test_from_xml_with_invalid_data(self): |
||
| 767 | j = 'Lorem ipsum est in ea occaecat nisi officia.' |
||
| 768 | # static method |
||
| 769 | with self.assertRaises(ValueError): |
||
| 770 | IODict.from_xml(j) |
||
| 771 | # constructor |
||
| 772 | with self.assertRaises(ValueError): |
||
| 773 | IODict(j, format='xml') |
||
| 774 | |||
| 775 | def test_from_xml_with_valid_file_valid_content(self): |
||
| 776 | filepath = self.input_path('valid-content.xml') |
||
| 777 | # static method |
||
| 778 | d = IODict.from_xml(filepath) |
||
| 779 | self.assertTrue(isinstance(d, dict)) |
||
| 780 | # constructor |
||
| 781 | d = IODict(filepath, format='xml') |
||
| 782 | self.assertTrue(isinstance(d, dict)) |
||
| 783 | |||
| 784 | def test_from_xml_with_valid_file_valid_content_invalid_format(self): |
||
| 785 | filepath = self.input_path('valid-content.base64') |
||
| 786 | with self.assertRaises(ValueError): |
||
| 787 | IODict.from_xml(filepath) |
||
| 788 | filepath = self.input_path('valid-content.json') |
||
| 789 | with self.assertRaises(ValueError): |
||
| 790 | IODict.from_xml(filepath) |
||
| 791 | filepath = self.input_path('valid-content.qs') |
||
| 792 | with self.assertRaises(ValueError): |
||
| 793 | IODict.from_xml(filepath) |
||
| 794 | filepath = self.input_path('valid-content.toml') |
||
| 795 | with self.assertRaises(ValueError): |
||
| 796 | IODict.from_xml(filepath) |
||
| 797 | filepath = self.input_path('valid-content.yml') |
||
| 798 | with self.assertRaises(ValueError): |
||
| 799 | IODict.from_xml(filepath) |
||
| 800 | |||
| 801 | def test_from_xml_with_valid_file_invalid_content(self): |
||
| 802 | filepath = self.input_path('invalid-content.xml') |
||
| 803 | # static method |
||
| 804 | with self.assertRaises(ValueError): |
||
| 805 | IODict.from_xml(filepath) |
||
| 806 | # constructor |
||
| 807 | with self.assertRaises(ValueError): |
||
| 808 | IODict(filepath, format='xml') |
||
| 809 | |||
| 810 | def test_from_xml_with_invalid_file(self): |
||
| 811 | filepath = self.input_path('invalid-file.xml') |
||
| 812 | # static method |
||
| 813 | with self.assertRaises(ValueError): |
||
| 814 | IODict.from_xml(filepath) |
||
| 815 | # constructor |
||
| 816 | with self.assertRaises(ValueError): |
||
| 817 | IODict(filepath, format='xml') |
||
| 818 | |||
| 819 | def test_from_xml_with_valid_url_valid_content(self): |
||
| 820 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.xml' |
||
| 821 | # static method |
||
| 822 | d = IODict.from_xml(url) |
||
| 823 | self.assertTrue(isinstance(d, dict)) |
||
| 824 | # constructor |
||
| 825 | d = IODict(url, format='xml') |
||
| 826 | self.assertTrue(isinstance(d, dict)) |
||
| 827 | |||
| 828 | def test_from_xml_with_valid_url_invalid_content(self): |
||
| 829 | url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 830 | # static method |
||
| 831 | with self.assertRaises(ValueError): |
||
| 832 | IODict.from_xml(url) |
||
| 833 | # constructor |
||
| 834 | with self.assertRaises(ValueError): |
||
| 835 | IODict(url, format='xml') |
||
| 836 | |||
| 837 | def test_from_xml_with_invalid_url(self): |
||
| 838 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 839 | # static method |
||
| 840 | with self.assertRaises(ValueError): |
||
| 841 | IODict.from_xml(url) |
||
| 842 | # constructor |
||
| 843 | with self.assertRaises(ValueError): |
||
| 844 | IODict(url, format='xml') |
||
| 845 | |||
| 846 | def test_to_xml(self): |
||
| 847 | d = IODict({ |
||
| 848 | 'root': { |
||
| 849 | 'x': '7', |
||
| 850 | 'y': '8', |
||
| 851 | 'z': '9', |
||
| 852 | 'a': '1', |
||
| 853 | 'b': '2', |
||
| 854 | 'c': '3', |
||
| 855 | }, |
||
| 856 | }) |
||
| 857 | s = d.to_xml() |
||
| 858 | self.assertEqual(d, IODict.from_xml(s)) |
||
| 859 | |||
| 860 | def test_to_xml_file(self): |
||
| 861 | d = IODict({ |
||
| 862 | 'root': { |
||
| 863 | 'x': '7', |
||
| 864 | 'y': '8', |
||
| 865 | 'z': '9', |
||
| 866 | 'a': '1', |
||
| 867 | 'b': '2', |
||
| 868 | 'c': '3', |
||
| 869 | }, |
||
| 870 | }) |
||
| 871 | filepath = self.output_path('test_to_xml_file.xml') |
||
| 872 | d.to_xml(filepath=filepath) |
||
| 873 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 874 | self.assertEqual(d, IODict.from_xml(filepath)) |
||
| 875 | |||
| 876 | # YAML |
||
| 877 | |||
| 878 | def test_from_yaml_with_valid_data(self): |
||
| 879 | j = """ |
||
| 880 | a: 1 |
||
| 881 | b: |
||
| 882 | c: 3 |
||
| 883 | d: 4 |
||
| 884 | """ |
||
| 885 | # static method |
||
| 886 | d = IODict.from_yaml(j) |
||
| 887 | self.assertTrue(isinstance(d, dict)) |
||
| 888 | self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) |
||
| 889 | # constructor |
||
| 890 | d = IODict(j, format='yaml') |
||
| 891 | self.assertTrue(isinstance(d, dict)) |
||
| 892 | self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) |
||
| 893 | |||
| 894 | def test_from_yaml_with_invalid_data(self): |
||
| 895 | j = 'Lorem ipsum est in ea occaecat nisi officia.' |
||
| 896 | # static method |
||
| 897 | with self.assertRaises(ValueError): |
||
| 898 | IODict.from_yaml(j) |
||
| 899 | # constructor |
||
| 900 | with self.assertRaises(ValueError): |
||
| 901 | IODict(j, format='yaml') |
||
| 902 | |||
| 903 | def test_from_yaml_with_valid_file_valid_content(self): |
||
| 904 | filepath = self.input_path('valid-content.yml') |
||
| 905 | # static method |
||
| 906 | d = IODict.from_yaml(filepath) |
||
| 907 | self.assertTrue(isinstance(d, dict)) |
||
| 908 | # constructor |
||
| 909 | d = IODict(filepath, format='yaml') |
||
| 910 | self.assertTrue(isinstance(d, dict)) |
||
| 911 | |||
| 912 | def test_from_yaml_with_valid_file_valid_content_invalid_format(self): |
||
| 913 | filepath = self.input_path('valid-content.base64') |
||
| 914 | with self.assertRaises(ValueError): |
||
| 915 | IODict.from_yaml(filepath) |
||
| 916 | # filepath = self.input_path('valid-content.json') |
||
| 917 | # with self.assertRaises(ValueError): |
||
| 918 | # IODict.from_yaml(filepath) |
||
| 919 | filepath = self.input_path('valid-content.qs') |
||
| 920 | with self.assertRaises(ValueError): |
||
| 921 | IODict.from_yaml(filepath) |
||
| 922 | filepath = self.input_path('valid-content.toml') |
||
| 923 | with self.assertRaises(ValueError): |
||
| 924 | IODict.from_yaml(filepath) |
||
| 925 | filepath = self.input_path('valid-content.xml') |
||
| 926 | with self.assertRaises(ValueError): |
||
| 927 | IODict.from_yaml(filepath) |
||
| 928 | |||
| 929 | def test_from_yaml_with_valid_file_invalid_content(self): |
||
| 930 | filepath = self.input_path('invalid-content.yml') |
||
| 931 | # static method |
||
| 932 | with self.assertRaises(ValueError): |
||
| 933 | IODict.from_yaml(filepath) |
||
| 934 | # constructor |
||
| 935 | with self.assertRaises(ValueError): |
||
| 936 | IODict(filepath, format='yaml') |
||
| 937 | |||
| 938 | def test_from_yaml_with_invalid_file(self): |
||
| 939 | filepath = self.input_path('invalid-file.yml') |
||
| 940 | # static method |
||
| 941 | with self.assertRaises(ValueError): |
||
| 942 | IODict.from_yaml(filepath) |
||
| 943 | # constructor |
||
| 944 | with self.assertRaises(ValueError): |
||
| 945 | IODict(filepath, format='yaml') |
||
| 946 | |||
| 947 | def test_from_yaml_with_valid_url_valid_content(self): |
||
| 948 | url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.yml' |
||
| 949 | # static method |
||
| 950 | d = IODict.from_yaml(url) |
||
| 951 | self.assertTrue(isinstance(d, dict)) |
||
| 952 | # constructor |
||
| 953 | d = IODict(url, format='yaml') |
||
| 954 | self.assertTrue(isinstance(d, dict)) |
||
| 955 | |||
| 956 | def test_from_yaml_with_valid_url_invalid_content(self): |
||
| 957 | url = 'https://github.com/fabiocaccamo/python-benedict' |
||
| 958 | # static method |
||
| 959 | with self.assertRaises(ValueError): |
||
| 960 | IODict.from_yaml(url) |
||
| 961 | # constructor |
||
| 962 | with self.assertRaises(ValueError): |
||
| 963 | IODict(url, format='yaml') |
||
| 964 | |||
| 965 | def test_from_yaml_with_invalid_url(self): |
||
| 966 | url = 'https://github.com/fabiocaccamo/python-benedict-invalid' |
||
| 967 | # static method |
||
| 968 | with self.assertRaises(ValueError): |
||
| 969 | IODict.from_yaml(url) |
||
| 970 | # constructor |
||
| 971 | with self.assertRaises(ValueError): |
||
| 972 | IODict(url, format='yaml') |
||
| 973 | |||
| 974 | def test_to_yaml(self): |
||
| 975 | d = IODict({ |
||
| 976 | 'x': 7, |
||
| 977 | 'y': 8, |
||
| 978 | 'z': 9, |
||
| 979 | 'a': 1, |
||
| 980 | 'b': 2, |
||
| 981 | 'c': 3, |
||
| 982 | }) |
||
| 983 | s = d.to_yaml() |
||
| 984 | self.assertEqual(d, IODict.from_yaml(s)) |
||
| 985 | |||
| 986 | def test_to_yaml_file(self): |
||
| 987 | d = IODict({ |
||
| 988 | 'x': 7, |
||
| 989 | 'y': 8, |
||
| 990 | 'z': 9, |
||
| 991 | 'a': 1, |
||
| 992 | 'b': 2, |
||
| 993 | 'c': 3, |
||
| 994 | }) |
||
| 995 | filepath = self.output_path('test_to_yaml_file.yml') |
||
| 996 | d.to_yaml(filepath=filepath) |
||
| 997 | self.assertTrue(d, os.path.isfile(filepath)) |
||
| 998 | self.assertEqual(d, IODict.from_yaml(filepath)) |
||
| 999 |