This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
conftest.py
129 lines (106 loc) · 4.17 KB
/
conftest.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import pytest
import aiohttp
import aioredis
import os
import json
from quart import Quart
from aioresponses import aioresponses
from wdreconcile.sitelink import SitelinkFetcher
from wdreconcile.itemstore import ItemStore
from wdreconcile.propertypath import PropertyFactory
from wdreconcile.typematcher import TypeMatcher
from wdreconcile.engine import ReconcileEngine
from wdreconcile.suggest import SuggestEngine
from config import redis_uri
## Environment (HTTP, redis)
@pytest.fixture
async def http_session():
async with aiohttp.ClientSession() as session:
yield session
@pytest.fixture
async def redis_client():
redis = aioredis.from_url(redis_uri, encoding='utf-8', decode_responses=True)
await redis.flushdb()
yield redis
@pytest.fixture
def mock_aioresponse():
with aioresponses() as m:
yield m
## Web app
@pytest.fixture
async def test_app():
return Quart(__name__)
## Main classes of the reconciliation service
@pytest.fixture
def sitelink_fetcher(redis_client, http_session):
return SitelinkFetcher(redis_client, http_session)
@pytest.fixture
def item_store(redis_client, http_session):
return ItemStore(redis_client, http_session)
class ItemStoreStub(ItemStore):
async def _fetch_items(self, qids):
result = {}
for qid in qids:
datapath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'tests/entities', qid+'.json')
try:
with open(datapath, 'r') as f:
itemdata = json.load(f)
if itemdata:
result[qid] = itemdata
except FileNotFoundError:
fetch_result = (await super(ItemStoreStub, self)._fetch_items([qid]))
actual_json = fetch_result.get(qid)
with open(datapath, 'w') as f:
json.dump(actual_json, f)
if actual_json:
result[qid] = actual_json
return result
@pytest.fixture
def item_store_stub(redis_client, http_session):
return ItemStoreStub(redis_client, http_session)
@pytest.fixture
def property_factory(item_store_stub):
return PropertyFactory(item_store_stub)
@pytest.fixture
def type_matcher(redis_client, http_session):
return TypeMatcher(redis_client, http_session)
class TypeMatcherStub(TypeMatcher):
async def _fetch_children(self, qid):
datapath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'tests/types', qid+'.json')
try:
with open(datapath, 'r') as f:
return json.load(f)
except FileNotFoundError:
fetched = await super(TypeMatcherStub, self)._fetch_children(qid)
with open(datapath, 'w') as f:
json.dump(fetched, f)
return fetched
class PropertyFactoryStub(PropertyFactory):
async def _fetch_unique_ids(self):
return ['P214', 'P1566']
class EngineStub(ReconcileEngine):
def __init__(self, redis_client, http_session):
super(EngineStub, self).__init__(redis_client, http_session)
self.item_store = ItemStoreStub(redis_client, http_session)
self.pf = PropertyFactoryStub(self.item_store)
self.type_matcher = TypeMatcherStub(redis_client, http_session)
async def wikibase_string_search(self, query_string, num_results, default_language):
key = '{}_{}_{}.json'.format(query_string.replace(' ', '_'), num_results, default_language)
datapath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'tests/search', key)
try:
with open(datapath, 'r') as f:
return json.load(f)
except FileNotFoundError:
fetched = await super(EngineStub, self).wikibase_string_search(query_string, num_results, default_language)
with open(datapath, 'w') as f:
json.dump(fetched, f)
return fetched
@pytest.fixture
def engine(redis_client, http_session, item_store_stub):
engine = EngineStub(redis_client, http_session)
return engine
@pytest.fixture
def suggest_engine(redis_client, http_session, item_store_stub):
suggest = SuggestEngine(redis_client, http_session)
suggest.store = item_store_stub
return suggest