diff options
author | bnewbold <bnewbold@archive.org> | 2020-12-18 02:13:47 +0000 |
---|---|---|
committer | bnewbold <bnewbold@archive.org> | 2020-12-18 02:13:47 +0000 |
commit | 443243e8cccba3e779b7c56d0cdb6dcd992a3100 (patch) | |
tree | b279887d9038daa19b72e53509658f016eaec452 /python/tests/import_common.py | |
parent | 7d90a0404e3ecb44c7d0ca93b2c32a0f66b8d88a (diff) | |
parent | 5eeb7a9d61beb8cb40fd89bd91fcd9dd820035aa (diff) | |
download | fatcat-443243e8cccba3e779b7c56d0cdb6dcd992a3100.tar.gz fatcat-443243e8cccba3e779b7c56d0cdb6dcd992a3100.zip |
Merge branch 'bnewbold-doaj-fuzzy' into 'master'
DOAJ import fuzzy match filter
See merge request webgroup/fatcat!92
Diffstat (limited to 'python/tests/import_common.py')
-rw-r--r-- | python/tests/import_common.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/python/tests/import_common.py b/python/tests/import_common.py new file mode 100644 index 00000000..d0db014e --- /dev/null +++ b/python/tests/import_common.py @@ -0,0 +1,78 @@ + +import json +import datetime +from typing import Any + +import pytest +import elasticsearch +import fatcat_openapi_client +from fatcat_openapi_client import ReleaseEntity, ReleaseExtIds +import fuzzycat.matching + +from fatcat_tools.importers import EntityImporter +from fatcat_tools.transforms import entity_to_dict +from fixtures import * + + +@pytest.fixture(scope="function") +def entity_importer(api, mocker) -> Any: + es_client = elasticsearch.Elasticsearch("mockbackend") + mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + yield EntityImporter(api, es_client=es_client) + +def test_fuzzy_match_none(entity_importer, mocker) -> None: + """ + Simple ES-mocked test for "no search results" case + """ + + es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request') + es_raw.side_effect = [ + (200, {}, json.dumps(ES_RELEASE_EMPTY_RESP)), + (200, {}, json.dumps(ES_RELEASE_EMPTY_RESP)), + ] + + release = ReleaseEntity( + title="some long title which should not match anything because it is for testing", + ext_ids=ReleaseExtIds(), + ) + + resp = entity_importer.match_existing_release_fuzzy(release) + assert resp == None + +def test_fuzzy_match_different(entity_importer, mocker) -> None: + """ + Simple fuzzycat-mocked test for "strong match" case + """ + + r1 = ReleaseEntity( + title="example title: novel work", + contribs=[ReleaseContrib(raw_name="robin hood")], + ext_ids=ReleaseExtIds(doi="10.1234/abcdefg"), + ) + r2 = ReleaseEntity( + title="Example Title: Novel Work?", + contribs=[ReleaseContrib(raw_name="robin hood")], + ext_ids=ReleaseExtIds(), + ) + r3 = ReleaseEntity( + title="entirely different", + contribs=[ReleaseContrib(raw_name="king tut")], + ext_ids=ReleaseExtIds(), + ) + + match_raw = mocker.patch('fatcat_tools.importers.common.match_release_fuzzy') + match_raw.side_effect = [[r3, r2, r3, r2]] + resp = entity_importer.match_existing_release_fuzzy(r1) + assert (resp[0], resp[2]) == ("STRONG", r2) + + match_raw.side_effect = [[r2, r2, r3, r1]] + resp = entity_importer.match_existing_release_fuzzy(r1) + assert (resp[0], resp[2]) == ("EXACT", r1) + + match_raw.side_effect = [[r3]] + resp = entity_importer.match_existing_release_fuzzy(r1) + assert resp == None + + match_raw.side_effect = [[]] + resp = entity_importer.match_existing_release_fuzzy(r1) + assert resp == None |