def test_header_only_file(self): with open(self.file_path, 'w') as f: f.write('variable1') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, {'variable1': []}) with open(self.file_path, 'w') as f: f.write('variable1,variable2') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, {'variable1': [], 'variable2': []})
def test_two_line_file(self): with open(self.file_path, 'w') as f: f.write('variable1\nvalue1') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, {'variable1': ['value1']}) with open(self.file_path, 'w') as f: f.write('variable1,variable2\nvalue1,value2') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, { 'variable1': ['value1'], 'variable2': ['value2'] })
def test_separator_other_than_comma(self): with open(self.file_path, 'w') as f: f.write('variable1\nvalue1a\nvalue1b') data_dict = parse_file_to_data_dict(self.file_path, separator=';') self.assertEqual(data_dict, {'variable1': ['value1a', 'value1b']}) with open(self.file_path, 'w') as f: f.write('variable1;variable2\nvalue1a;value2a\nvalue1b;value2b') data_dict = parse_file_to_data_dict(self.file_path, separator=';') self.assertEqual( data_dict, { 'variable1': ['value1a', 'value1b'], 'variable2': ['value2a', 'value2b'] })
def test_multiple_lines(self): with open(self.file_path, 'w') as f: f.write('variable1\nvalue1a\nvalue1b') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, {'variable1': ['value1a', 'value1b']}) with open(self.file_path, 'w') as f: f.write('variable1,variable2\nvalue1a,value2a\nvalue1b,value2b') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual( data_dict, { 'variable1': ['value1a', 'value1b'], 'variable2': ['value2a', 'value2b'] })
def test_file_with_empty_value(self): with open(self.file_path, 'w') as f: f.write('variable1;variable2\nvalue1a;\n;value2b') data_dict = parse_file_to_data_dict(self.file_path, separator=';') self.assertEqual(data_dict, { 'variable1': ['value1a', ''], 'variable2': ['', 'value2b'] })
def test_file_with_double_quote_wrapped(self): with open(self.file_path, 'w') as f: f.write('"variable1";"variable2"\n"value1a";""\n"";"value2b"') data_dict = parse_file_to_data_dict(self.file_path, separator=';') self.assertEqual(data_dict, { 'variable1': ['value1a', ''], 'variable2': ['', 'value2b'] })
def test_non_existing_file(self): try: data_dict = parse_file_to_data_dict('non-existing-file.txt') except ValueError as ve: pass except Exception as e: self.fail('Expected ValueError but received: {}'.format( type(e).__name__)) else: self.fail('Expected ValueError but no exception happened')
def test_improperly_formatted_file(self): with open(self.file_path, 'w') as f: f.write('variable1;variable2\nvalue1,value2') try: data_dict = parse_file_to_data_dict(self.file_path) except ValueError as ve: pass except Exception as e: self.fail('Expected ValueError but received: {}'.format( type(e).__name__)) else: self.fail('Expected ValueError but no exception happened')
def test_empty_file(self): with open(self.file_path, 'w') as f: f.write('') data_dict = parse_file_to_data_dict(self.file_path) self.assertEqual(data_dict, dict())