def test_from_csv_with_invalid_url(self): url = 'https://github.com/fabiocaccamo/python-benedict-invalid' # static method with self.assertRaises(ValueError): IODict.from_csv(url) # constructor with self.assertRaises(ValueError): IODict(url, format='csv')
def test_from_csv_with_invalid_file(self): filepath = self.input_path('invalid-file.csv') # static method with self.assertRaises(ValueError): IODict.from_csv(filepath) # constructor with self.assertRaises(ValueError): IODict(filepath, format='csv')
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_csv_with_valid_data(self): s = """id,name,age,height,weight 1,Alice,20,62,120.6 2,Freddie,21,74,190.6 3,Bob,17,68,120.0 4,François,32,75,110.05 """ r = { 'values': [ { 'id': '1', 'name': 'Alice', 'age': '20', 'height': '62', 'weight': '120.6', }, { 'id': '2', 'name': 'Freddie', 'age': '21', 'height': '74', 'weight': '190.6', }, { 'id': '3', 'name': 'Bob', 'age': '17', 'height': '68', 'weight': '120.0', }, { 'id': '4', 'name': 'François', 'age': '32', 'height': '75', 'weight': '110.05', }, ], } # static method d = IODict.from_csv(s) self.assertTrue(isinstance(d, dict)) self.assertEqual(d, r) # constructor d = IODict(s, format='csv') self.assertTrue(isinstance(d, dict)) self.assertEqual(d, r)
def test_to_csv_file(self): d = IODict({ 'values': [ { 'id': '1', 'name': 'Alice', 'age': '20', 'height': '62', 'weight': '120.6', }, { 'id': '2', 'name': 'Freddie', 'age': '21', 'height': '74', 'weight': '190.6', }, { 'id': '3', 'name': 'Bob', 'age': '17', 'height': '68', 'weight': '120.0', }, { 'id': '4', 'name': 'François', 'age': '32', 'height': '75', 'weight': '110.05', }, ], }) filepath = self.output_path('test_to_csv_file.csv') d.to_csv(filepath=filepath) self.assertFileExists(filepath) self.assertEqual(d, IODict.from_csv(filepath))