aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_matching.py
blob: 17027924510837a5e58af7b0e0d59b58a6972815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
import warnings

import elasticsearch
import pytest
import requests
from dynaconf import Dynaconf
from fatcat_openapi_client import ReleaseEntity

from fuzzycat.entities import entity_from_dict
from fuzzycat.matching import anything_to_entity, match_release_fuzzy

warnings.filterwarnings("ignore") # InsecureRequestWarning: Unverified HTTPS request is being made to host ...

logger = logging.getLogger('test_matching')
logger.setLevel(logging.DEBUG)

settings = Dynaconf(envvar_prefix="FUZZYCAT")
# ad-hoc override search server with: FUZZYCAT_FATCAT_SEARCH_URL=localhost:9200 pytest ...
FATCAT_SEARCH_URL = settings.get("FATCAT_SEARCH_URL", "https://search.fatcat.wiki:443")


def is_not_reachable(url, timeout=3):
    return not is_reachable(url)

def is_reachable(url, timeout=3):
    """
    Return true, if URL is reachable and returns HTTP 200.
    """
    try:
        return requests.get(url, verify=False, timeout=timeout).ok
    except Exception:
        return False

@pytest.fixture
def es_client():
    return elasticsearch.Elasticsearch([FATCAT_SEARCH_URL])


@pytest.mark.skipif(is_not_reachable(FATCAT_SEARCH_URL),
                    reason="{} not reachable, use e.g. FUZZYCAT_FATCAT_SEARCH_URL=localhost:9200 to override".format(FATCAT_SEARCH_URL))
def test_match_release_fuzzy(es_client, caplog):
    cases = (
        ("wtv64ahbdzgwnan7rllwr3nurm", 1),
        ("eqcgtpav3na5jh56o5vjsvb4ei", 1),
    )
    for i, (ident, count) in enumerate(cases):
        entity = anything_to_entity(ident, ReleaseEntity)

        result = match_release_fuzzy(entity, es=es_client)
        logger.info("[{}] given {}, found {}".format(i, entity.title, len(result)))
        assert len(result) == count

    # Partial data.
    cases = (
        ({
            "title": "digital libraries",
            "ext_ids": {}
        }, 5),
        ({
            "title": "The Future of Digital Scholarship",
            "contribs": [{
                "raw_name": "Costantino Thanos"
            }],
            "ext_ids": {}
        }, 5),
    )
    for i, (doc, count) in enumerate(cases):
        entity = entity_from_dict(doc, ReleaseEntity)
        result = match_release_fuzzy(entity, es=es_client)
        with caplog.at_level(logging.INFO):
            logging.info("[{}] given {}, found {}, {}".format(i, entity.title, len(result),
                                                              [v.title for v in result]))
        assert len(result) == count