def test_fuzzy_matched_goes_trough_the_workflow(
    mocked_is_pdf_link,
    mocked_download_arxiv,
    mocked_api_request_beard,
    mocked_api_request_magpie,
    workflow_app,
    mocked_external_services,
    record_from_db,
    enable_fuzzy_matcher,
):
    """Test update article fuzzy matched.

    In this test the `WorkflowObject.continue_workflow` is mocked because in
    the test suite celery is run in eager mode. This prevents celery to switch
    back from the api context to the app context, generating errors during
    the workflow execution. The patched version of `continue_workflow` uses
    the correct application context to run the workflow.
    """
    def continue_wf_patched_context(workflow_app):
        workflow_object_class._custom_continue = workflow_object_class.continue_workflow

        def custom_continue_workflow(self, *args, **kwargs):
            with workflow_app.app_context():
                self._custom_continue(*args, **kwargs)

        return custom_continue_workflow

    es_query = {
        'algorithm': [
            {
                'queries': [
                    {
                        'path': 'report_numbers.value',
                        'search_path': 'report_numbers.value.raw',
                        'type': 'exact',
                    },
                ],
            },
        ],
        'doc_type': 'hep',
        'index': 'records-hep',
    }

    record = record_from_db
    expected_brief = _get_hep_record_brief(record)
    del record['arxiv_eprints']
    rec_id = record['control_number']

    with mock.patch.dict(workflow_app.config['FUZZY_MATCH'], es_query):
        eng_uuid = start('article', [record])

    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.status == ObjectStatus.HALTED  # for matching approval
    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.extra_data['already-in-holding-pen'] is False
    assert obj.extra_data['holdingpen_matches'] == []
    assert obj.extra_data['matches']['fuzzy'][0] == expected_brief

    WorkflowObject.continue_workflow = continue_wf_patched_context(workflow_app)
    do_resolve_matching(workflow_app, obj.id, rec_id)

    obj = workflow_object_class.get(obj_id)
    assert obj.extra_data['matches']['approved'] == rec_id
    assert obj.extra_data['fuzzy-matched']
    assert obj.extra_data['is-update']
    assert obj.extra_data['approved']
    assert obj.status == ObjectStatus.COMPLETED
def test_fuzzy_matched_goes_trough_the_workflow(
    mocked_is_pdf_link,
    mocked_download_arxiv,
    mocked_api_request_beard,
    mocked_api_request_magpie,
    workflow_app,
    mocked_external_services,
    record_from_db,
    enable_fuzzy_matcher,
):
    """Test update article fuzzy matched.

    In this test the `WorkflowObject.continue_workflow` is mocked because in
    the test suite celery is run in eager mode. This prevents celery to switch
    back from the api context to the app context, generating errors during
    the workflow execution. The patched version of `continue_workflow` uses
    the correct application context to run the workflow.
    """
    def continue_wf_patched_context(workflow_app):
        workflow_object_class._custom_continue = workflow_object_class.continue_workflow

        def custom_continue_workflow(self, *args, **kwargs):
            with workflow_app.app_context():
                self._custom_continue(*args, **kwargs)

        return custom_continue_workflow

    es_query = {
        "algorithm": [{
            "queries": [{
                "path": "report_numbers.value",
                "search_path": "report_numbers.value.raw",
                "type": "exact",
            }]
        }],
        "doc_type":
        "hep",
        "index":
        "records-hep",
    }

    record = record_from_db
    expected_brief = _get_hep_record_brief(record)
    del record["arxiv_eprints"]
    rec_id = record["control_number"]

    with mock.patch.dict(workflow_app.config["FUZZY_MATCH"], es_query):
        workflow_id = build_workflow(record).id
        eng_uuid = start("article", object_id=workflow_id)

    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.status == ObjectStatus.HALTED  # for matching approval
    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.extra_data["holdingpen_matches"] == []
    assert obj.extra_data["matches"]["fuzzy"][0] == expected_brief

    WorkflowObject.continue_workflow = continue_wf_patched_context(
        workflow_app)
    do_resolve_matching(workflow_app, obj.id, rec_id)

    obj = workflow_object_class.get(obj_id)
    assert obj.extra_data["matches"]["approved"] == rec_id
    assert obj.extra_data["fuzzy-matched"]
    assert obj.extra_data["is-update"]
    assert obj.extra_data["approved"]

    obj = workflow_object_class.get(obj_id)
    assert obj.status == ObjectStatus.COMPLETED
def test_fuzzy_matched_goes_trough_the_workflow(
    mocked_is_pdf_link,
    mocked_download_arxiv,
    mocked_api_request_beard,
    mocked_api_request_magpie,
    workflow_app,
    mocked_external_services,
    record_from_db,
    enable_fuzzy_matcher,
):
    """Test update article fuzzy matched.

    In this test the `WorkflowObject.continue_workflow` is mocked because in
    the test suite celery is run in eager mode. This prevents celery to switch
    back from the api context to the app context, generating errors during
    the workflow execution. The patched version of `continue_workflow` uses
    the correct application context to run the workflow.
    """

    def continue_wf_patched_context(workflow_app):
        workflow_object_class._custom_continue = workflow_object_class.continue_workflow

        def custom_continue_workflow(self, *args, **kwargs):
            with workflow_app.app_context():
                self._custom_continue(*args, **kwargs)

        return custom_continue_workflow

    es_query = {
        "algorithm": [
            {
                "queries": [
                    {
                        "path": "report_numbers.value",
                        "search_path": "report_numbers.value.raw",
                        "type": "exact",
                    }
                ]
            }
        ],
        "doc_type": "hep",
        "index": "records-hep",
    }

    record = record_from_db
    expected_brief = _get_hep_record_brief(record)
    del record["arxiv_eprints"]
    rec_id = record["control_number"]

    with mock.patch.dict(workflow_app.config["FUZZY_MATCH"], es_query):
        workflow_id = build_workflow(record).id
        eng_uuid = start("article", object_id=workflow_id)

    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.status == ObjectStatus.HALTED  # for matching approval
    obj_id = WorkflowEngine.from_uuid(eng_uuid).objects[0].id
    obj = workflow_object_class.get(obj_id)

    assert obj.extra_data["holdingpen_matches"] == []
    assert obj.extra_data["matches"]["fuzzy"][0] == expected_brief

    WorkflowObject.continue_workflow = continue_wf_patched_context(workflow_app)
    do_resolve_matching(workflow_app, obj.id, rec_id)

    obj = workflow_object_class.get(obj_id)
    assert obj.extra_data["matches"]["approved"] == rec_id
    assert obj.extra_data["fuzzy-matched"]
    assert obj.extra_data["is-update"]
    assert obj.extra_data["approved"]

    obj = workflow_object_class.get(obj_id)
    assert obj.status == ObjectStatus.COMPLETED