def test_read_data(mock_csv_reader, mock_open, data): mock_csv_reader.return_value = data tt = TableTransformer('file.txt') output = tt.read_data() mock_open.assert_called() mock_csv_reader.assert_called() assert output == data
def test_read_data_path_is_dir(mock_open): mock_open.side_effect = IsADirectoryError() tt = TableTransformer('file.txt') tt.logger = Mock() output = tt.read_data() mock_open.assert_called() tt.logger.error.assert_called() assert output is None
def test_read_data_file_not_found(mock_open): mock_open.side_effect = FileNotFoundError() tt = TableTransformer('file.txt') tt.logger = Mock() output = tt.read_data() mock_open.assert_called() tt.logger.error.assert_called() assert output is None
def test_run_catches_FileExistsError(): tt = TableTransformer('file.txt') tt.extractors = [Mock(), Mock()] tt.logger = Mock() tt.read_data = Mock() tt.write_table = Mock(side_effect=FileExistsError('error')) tt.run() assert len(tt.logger.error.call_args_list) == len(tt.extractors)
def test_run_does_nothing_if_no_extractor(): tt = TableTransformer('file.txt') tt.extractors = [] tt.logger = Mock() tt.read_data = Mock() tt.write_table = Mock() tt.run() tt.read_data.assert_not_called() tt.write_table.assert_not_called() tt.logger.error.assert_called()
def test_run_does_nothing_if_no_data(): tt = TableTransformer('file.txt') tt.extractors = [Mock(), Mock()] tt.read_data = Mock(return_value=None) tt.write_table = Mock() tt.run() tt.read_data.assert_called() tt.write_table.assert_not_called() for e in tt.extractors: e.extract.assert_not_called() e.create_table.assert_not_called()
def test_run(): tt = TableTransformer('file.txt') tt.extractors = [Mock(), Mock()] tt.logger = Mock() tt.read_data = Mock() tt.write_table = Mock() tt.output_template = Mock() tt.run() for e in tt.extractors: e.extract.assert_called_once() e.create_table.assert_called_once() tt.read_data.assert_called_once() assert len(tt.write_table.call_args_list) == len(tt.extractors) assert (len(tt.output_template.substitute.call_args_list) == len( tt.extractors))