def test_dump(self): """Test dump_to_csv() can write data to CSV file.""" # Create paths to files. log_file = os.path.join(LOG_PATH, 'UnitTestMessageA.log') csv_file = os.path.join(TMP_PATH, 'data.csv') # Dump data to CSV file. keys = ['name', 'data', 'timestamp'] dump_to_csv(log_file, csv_file, keys) # Read data from CSV file and reference. with open(csv_file, 'r') as f: write_data = f.read() with open(os.path.join(LOG_PATH, 'UnitTestMessageA.csv'), 'r') as f: expected_data = f.read() # Ensure CSV data is in the expected format. self.assertEqual(write_data, expected_data)
def test_bad_input(self): """Test dump_to_csv() bad input.""" bad_path = os.path.join(LOG_PATH, 'UnitTestMessageC.log') log_file = os.path.join(LOG_PATH, 'UnitTestMessageA.log') csv_file = os.path.join(TMP_PATH, 'data.csv') # Catch invalid path. with self.assertRaises(IOError): dump_to_csv(bad_path, csv_file, ['timestamp', 'data']) # Catch non iterable keys. with self.assertRaises(TypeError): dump_to_csv(log_file, csv_file, 5) # Catch non-string key. with self.assertRaises(TypeError): dump_to_csv(log_file, csv_file, ['error', 5]) # Catch non-existent key with self.assertRaises(KeyError): dump_to_csv(log_file, csv_file, ['error'])
def test_mixed_type(self): """Test dump_to_csv() mixed types.""" csv_file = os.path.join(TMP_PATH, 'data.csv') with self.assertRaises(TypeError): dump_to_csv(LOG_PATH, csv_file, ['timestamp', 'data'])