def test_historical_check_empty(monitoring_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(monitoring_list_importer, 20, db_conn, logger)
    with get_importer(
            MonitoringListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            MonitoringListParams(
                filename='empty_monitoringlist_historical_check.csv')) as imp:
        expect_failure(imp, exc_message='Failed import size historic check')
def test_repeat_import(monitoring_list_importer, logger, mocked_statsd,
                       db_conn, metadata_db_conn, mocked_config, tmpdir):
    """Verify that valid monitoring list data can be successfully imported into the database.

    when repeating the import of the same file.
    """
    expect_success(monitoring_list_importer, 21, db_conn, logger)
    with get_importer(
            MonitoringListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            MonitoringListParams(
                filename='sample_monitoring_list_v1.csv')) as imp:
        expect_success(imp, 21, db_conn, logger)
def test_historical_check_percent_fails(monitoring_list_importer, logger,
                                        mocked_statsd, db_conn, mocked_config,
                                        metadata_db_conn, tmpdir):
    """Verify that monitoring list import fails historical check."""
    expect_success(monitoring_list_importer, 20, db_conn, logger)
    with get_importer(
            MonitoringListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            MonitoringListParams(
                filename='sample_monitoring_list_historicalcheck.csv',
                import_size_variation_percent=mocked_config.
                barred_threshold_config.import_size_variation_percent,
                import_size_variation_absolute=mocked_config.
                barred_threshold_config.import_size_variation_absolute)
    ) as imp:
        expect_failure(imp, exc_message='Failed import size historic check')
def test_malformed_imeis(monitoring_list_importer, logger, mocked_statsd,
                         db_conn, metadata_db_conn, mocked_config, tmpdir):
    """Verify that the monitoring list data file is accepted.

    And imported if the data contains IMEIs with #, and *.
    """
    expect_success(monitoring_list_importer, 20, db_conn, logger)
    # attempting to import stolen list file containing symbol not allowed '%'.
    with get_importer(
            MonitoringListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            MonitoringListParams(
                filename='monitoring_list_hexpound_bad_symbol.csv')) as imp:
        expect_failure(imp,
                       exc_message='regex("^[0-9A-Fa-f\\\\*\\\\#]{1,16}$") '
                       'fails for line: 1, column: imei, value: '
                       '"62%222222222222"\\nFAIL')
def test_historical_check_percent_succeeds(monitoring_list_importer, logger,
                                           mocked_statsd, db_conn,
                                           mocked_config, metadata_db_conn,
                                           tmpdir):
    """Verify that a local monitoring data is successfully imported.

    After having imported two files where the second file has 80% size of the first one and the threshold value is 75.
    """
    expect_success(monitoring_list_importer, 100, db_conn, logger)
    with get_importer(
            MonitoringListImporter, db_conn, metadata_db_conn,
            mocked_config.db_config, tmpdir, logger, mocked_statsd,
            MonitoringListParams(
                filename=data_file_to_test(80,
                                           imei_custom_header='imei',
                                           imei_imsi=False),
                import_size_variation_percent=mocked_config.
                barred_threshold_config.import_size_variation_percent,
                import_size_variation_absolute=mocked_config.
                barred_threshold_config.import_size_variation_absolute)
    ) as imp:
        expect_success(imp, 80, db_conn, logger)
    # verify data in db
    with db_conn.cursor() as cursor:
        cursor.execute(
            'SELECT imei_norm FROM monitoring_list ORDER BY imei_norm')
        result_list = [res.imei_norm for res in cursor]

    assert result.exit_code == 0
    assert result_list == [
        '10000110000006', '10000220000007', '10000330000008', '10000440000009',
        '10000550000000', '10000660000001'
    ]


@pytest.mark.parametrize(
    'monitoring_list_importer',
    [MonitoringListParams(filename='monitoring_list_missing_header.csv')],
    indirect=True)
def test_missing_header(monitoring_list_importer, logger, db_conn):
    """Verify that the monitoring list data is not imported if a header column is missing."""
    expect_failure(
        monitoring_list_importer,
        exc_message='Metadata header, cannot find the column headers - imei, '
        '642222222222222')


@pytest.mark.parametrize(
    'monitoring_list_importer',
    [MonitoringListParams(filename='sample_monitoring_list_normalize.csv')],
    indirect=True)
def test_matching_normalization(monitoring_list_importer, logger, db_conn):
    """Verify that IMEIs that normalize to the same value are successfully imported into the database."""