def test_historical_check_empty(barred_tac_list_importer, logger,
                                mocked_statsd, db_conn, metadata_db_conn,
                                mocked_config, tmpdir):
    """Verify that empty file import fails after importing a non empty file."""
    expect_success(barred_tac_list_importer, 20, db_conn, logger)
    with get_importer(
            BarredTacListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            BarredTacListParams(
                filename='empty_barred_tac_list_historical_check.csv')) as imp:
        expect_failure(imp, exc_message='Failed import size historic check')
def test_repeat_import(barred_tac_list_importer, logger, mocked_statsd,
                       db_conn, metadata_db_conn, mocked_config, tmpdir):
    """Verify that valid barred list data can be successfully imported into the database.

    when repeating the import of the same file.
    """
    expect_success(barred_tac_list_importer, 6, db_conn, logger)
    with get_importer(
            BarredTacListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            BarredTacListParams(
                filename='sample_barred_tac_list_v1.csv')) as imp:
        expect_success(imp, 6, db_conn, logger)
def test_historical_check_percent_fails(barred_tac_list_importer, logger,
                                        mocked_statsd, db_conn, mocked_config,
                                        metadata_db_conn, tmpdir):
    """Verify that barred list import fails historical check."""
    expect_success(barred_tac_list_importer, 20, db_conn, logger)
    with get_importer(
            BarredTacListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            BarredTacListParams(
                filename='sample_barred_tac_list_historicalcheck.csv',
                import_size_variation_percent=mocked_config.
                barred_tac_threshold_config.import_size_variation_percent,
                import_size_variation_absolute=mocked_config.
                barred_tac_threshold_config.import_size_variation_absolute)
    ) as imp:
        expect_failure(imp, exc_message='Failed import size historic check')
    # invoke cli runner
    runner = CliRunner()
    result = runner.invoke(dirbs_import_cli, ['barred_tac_list', valid_zip_file_path],
                           obj={'APP_CONFIG': mocked_config})

    # verify data in db
    with db_conn.cursor() as cursor:
        cursor.execute('SELECT tac FROM barred_tac_list ORDER BY tac')
        result_list = [res.tac for res in cursor]

    assert result.exit_code == 0
    assert result_list == ['10000110', '10000220', '10000330', '10000440', '10000550', '10000660']


@pytest.mark.parametrize('barred_tac_list_importer',
                         [BarredTacListParams(filename='barred_tac_list_missing_header.csv')],
                         indirect=True)
def test_missing_header(barred_tac_list_importer, logger, db_conn):
    """Verify that the barred list data is not imported if a header column is missing."""
    expect_failure(barred_tac_list_importer, exc_message='Metadata header, cannot find the column headers - tac, '
                                                         '10000110')


@pytest.mark.parametrize('barred_tac_list_importer',
                         [BarredTacListParams(filename='sample_barred_tac_list.csv')],
                         indirect=True)
def test_simple_import(barred_tac_list_importer, logger, db_conn):
    """Verify that the valid barred list data can be successfully imported into the db."""
    expect_success(barred_tac_list_importer, 6, db_conn, logger)