Exemplo n.º 1
0
def test_write_table_does_not_overwrite(mock_is_file, mock_open):
    mock_is_file.return_value = True
    tt = TableTransformer('file.txt')

    with pytest.raises(FileExistsError):
        tt.write_table('table', 'file')
    mock_open.assert_not_called()
Exemplo n.º 2
0
def test_write_table(mock_csv_writer, mock_open, data):
    csv_writer_instance = Mock()
    mock_csv_writer.return_value = csv_writer_instance
    file = 'file.txt'
    tt = TableTransformer('file.txt')
    tt.write_table(data, file)

    mock_open.assert_called_with(file, mode='w', encoding='utf-8', newline='')
    mock_csv_writer.assert_called()
    assert len(csv_writer_instance.writerow.call_args_list) == 2
    csv_writer_instance.writerow.assert_called_with(data[1])
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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))