def test_split_mongo_samples_by_version_error_raises_exception(
    mock_filtered_positive_fields_set,
    mock_helper_database_updates,
    mock_query_helper_functions,
    mock_extract_required_cp_info,
    mock_split_mongo_samples_by_version,
):
    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    mock_mongo_samples_by_date, mock_get_cherrypicked_samples_by_date = mock_query_helper_functions

    mock_filtered_positive_fields_set.return_value = False
    mock_mongo_samples_by_date.return_value = [{"plate_barcode": "1"}]
    mock_extract_required_cp_info.return_value = [["id_1"],
                                                  ["plate_barcode_1"]]
    mock_get_cherrypicked_samples_by_date.return_value = pd.DataFrame(
        {"id": ["s1", "s2"]})

    mock_split_mongo_samples_by_version.side_effect = Exception("Boom!")

    with pytest.raises(Exception):
        update_legacy_filtered_positives.run("crawler.config.integration",
                                             start_date_input, end_date_input)

    mock_update_mongo.assert_not_called()
    mock_update_mlwh.assert_not_called()
def test_update_legacy_filtered_positives_returns_early_start_datetime_after_end_datetime(
        config, mock_helper_database_updates):
    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    start_datetime = "201017_1200"
    end_datetime = "201016_1600"
    update_legacy_filtered_positives.run("crawler.config.integration",
                                         start_datetime, end_datetime)

    # ensure no database connections/updates are made
    mock_update_mongo.assert_not_called()
    mock_update_mlwh.assert_not_called()
def test_update_legacy_filtered_positives_exception_raised_if_user_enters_invalid_input(
        mock_user_input, mock_helper_database_updates,
        filtered_positive_testing_samples):
    mock_user_input.return_value = "invalid_input"
    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates

    update_legacy_filtered_positives.run("crawler.config.integration",
                                         start_date_input, end_date_input)

    mock_update_mongo.assert_not_called()
    mock_update_mlwh.assert_not_called()
Пример #4
0
def migration_update_legacy_filtered_positives():
    if not len(sys.argv) == 4:
        print(
            "Please add both start and end datetime range arguments for this migration "
            "(format YYMMDD_HHmm e.g. 200115_1200, inclusive), aborting")
        return

    s_start_datetime = sys.argv[2]
    s_end_datetime = sys.argv[3]
    print("Running update_legacy_filtered_positives migration")
    update_legacy_filtered_positives.run(s_start_datetime=s_start_datetime,
                                         s_end_datetime=s_end_datetime)
def test_update_legacy_filtered_positives_catches_error_connecting_to_mongo(
        mock_helper_database_updates, mock_filtered_positive_fields_set):
    mock_filtered_positive_fields_set.return_value = False

    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    mock_update_mongo.side_effect = Exception("Boom!")

    with pytest.raises(Exception):
        update_legacy_filtered_positives.run("crawler.config.integration",
                                             start_date_input, end_date_input)

    mock_update_mongo.assert_called_once()
    mock_update_mlwh.assert_not_called()
def test_update_legacy_filtered_positives_returns_early_datetime_post_fields_set_date(
        config, mock_helper_database_updates):
    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    start_datetime = "201016_1600"
    end_datetime = "201217_1600"

    mock_valid_datetime_string.return_value = True  # type: ignore
    update_legacy_filtered_positives.run("crawler.config.integration",
                                         start_datetime, end_datetime)

    # ensure no database connections/updates are made
    mock_update_mongo.assert_not_called()
    mock_update_mlwh.assert_not_called()
def test_update_legacy_filtered_positives_returns_early_invalid_end_datetime(
        config, mock_helper_database_updates):
    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    start_datetime = "201016_1600"
    end_datetime = "not a real datetime"

    mock_valid_datetime_string.side_effect = [True, False]  # type: ignore
    update_legacy_filtered_positives.run("crawler.config.integration",
                                         start_datetime, end_datetime)

    # ensure no database connections/updates are made
    mock_update_mongo.assert_not_called()
    mock_update_mlwh.assert_not_called()
def test_update_legacy_filtered_positives_successful_if_user_chooses_to_continue(
    config,
    freezer,
    mock_user_input,
    mock_filtered_positive_fields_set,
    mock_helper_database_updates,
    mock_query_helper_functions,
    mock_extract_required_cp_info,
    mock_split_mongo_samples_by_version,
    mock_filtered_positive_identifier_by_version,
    mock_update_filtered_positive_fields,
):
    update_timestamp = datetime.now()
    v0_samples = [{"plate_barcode": "0"}]
    v1_samples = [{"plate_barcode": "1"}]
    v2_samples = [{"plate_barcode": "2"}]

    mock_update_mongo, mock_update_mlwh = mock_helper_database_updates
    mock_mongo_samples_by_date, mock_get_cherrypicked_samples_by_date = mock_query_helper_functions
    mock_split_mongo_samples_by_version = mock_split_mongo_samples_by_version

    mock_filtered_positive_fields_set.return_value = True
    mock_user_input.return_value = "yes"
    mock_mongo_samples_by_date.return_value = [{"plate_barcode": "1"}]
    mock_extract_required_cp_info.return_value = [["id_1"],
                                                  ["plate_barcode_1"]]
    mock_get_cherrypicked_samples_by_date.return_value = pd.DataFrame(
        {"id": ["s1", "s2"]})
    mock_split_mongo_samples_by_version.return_value = {
        FILTERED_POSITIVE_VERSION_0: v0_samples,
        FILTERED_POSITIVE_VERSION_1: v1_samples,
        FILTERED_POSITIVE_VERSION_2: v2_samples,
    }
    mock_filtered_positive_identifier_by_version.side_effect = [
        identifier_v0, identifier_v1, identifier_v2
    ]
    mock_update_mongo.return_value = True
    mock_update_mlwh.return_value = True

    update_legacy_filtered_positives.run("crawler.config.test",
                                         start_date_input, end_date_input)

    assert mock_update_filtered_positive_fields.call_count == 3
    mock_update_filtered_positive_fields.assert_any_call(
        identifier_v0, v0_samples, FILTERED_POSITIVE_VERSION_0,
        update_timestamp)  # noqa: E501
    mock_update_filtered_positive_fields.assert_any_call(
        identifier_v1, v1_samples, FILTERED_POSITIVE_VERSION_1,
        update_timestamp)  # noqa: E501
    mock_update_filtered_positive_fields.assert_any_call(
        identifier_v2, v2_samples, FILTERED_POSITIVE_VERSION_2,
        update_timestamp)  # noqa: E501

    assert mock_update_mongo.call_count == 3
    mock_update_mongo.assert_any_call(config, v0_samples,
                                      FILTERED_POSITIVE_VERSION_0,
                                      update_timestamp)
    mock_update_mongo.assert_any_call(config, v1_samples,
                                      FILTERED_POSITIVE_VERSION_1,
                                      update_timestamp)
    mock_update_mongo.assert_any_call(config, v2_samples,
                                      FILTERED_POSITIVE_VERSION_2,
                                      update_timestamp)

    assert mock_update_mlwh.call_count == 3
    mock_update_mlwh.assert_any_call(config, v0_samples,
                                     FILTERED_POSITIVE_VERSION_0,
                                     update_timestamp)
    mock_update_mlwh.assert_any_call(config, v1_samples,
                                     FILTERED_POSITIVE_VERSION_1,
                                     update_timestamp)
    mock_update_mlwh.assert_any_call(config, v2_samples,
                                     FILTERED_POSITIVE_VERSION_2,
                                     update_timestamp)