-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_opensearch.py
More file actions
238 lines (179 loc) · 6.72 KB
/
test_opensearch.py
File metadata and controls
238 lines (179 loc) · 6.72 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from datetime import date, datetime
from types import SimpleNamespace
import pytest
import harvester.opensearch as opensearch
from harvester.opensearch import (
DEFAULT_DELETE_REQUEST_TIMEOUT_SECONDS,
ConnectionTimeout,
OpenSearchInterface,
)
class DummyOrg:
def to_dict(self):
return {"id": "org-1", "name": "Test Org"}
class FakeIndices:
def __init__(self, exists=True):
self._exists = exists
self.created = None
self.refreshed = []
def exists(self, index):
return self._exists
def create(self, index, body):
self.created = {"index": index, "body": body}
return {"acknowledged": True}
def refresh(self, index, request_timeout):
self.refreshed.append((index, request_timeout))
return {"result": "refreshed"}
class FakeClient:
def __init__(self, exists=True):
self.indices = FakeIndices(exists=exists)
self.deleted = []
def delete(self, index, id, ignore, request_timeout):
self.deleted.append((index, id, ignore, request_timeout))
return {"result": "deleted"}
@pytest.fixture()
def sample_dataset():
return SimpleNamespace(
id="dataset-1",
slug="dataset-1",
dcat={
"title": "Dataset Title",
"description": "Dataset description",
"publisher": {"name": "Publisher"},
"keyword": ["kw-1"],
"theme": ["theme-1"],
"identifier": "id-1",
"spatial": "POINT(1 2)",
"modified": date(2024, 1, 2),
},
last_harvested_date=datetime(2024, 1, 3, 4, 5, 6),
translated_spatial={"type": "Point", "coordinates": [1, 2]},
organization=DummyOrg(),
popularity=7,
harvest_record_id="hr-1",
)
def test_normalize_dcat_dates():
dcat = {
"modified": date(2024, 1, 2),
"issued": datetime(2024, 1, 3, 4, 5, 6),
"temporal": 123,
}
normalized = OpenSearchInterface._normalize_dcat_dates(dcat)
assert normalized["modified"] == "2024-01-02"
assert normalized["issued"].startswith("2024-01-03T04:05:06")
assert normalized["temporal"] == "123"
def test_geometry_centroid_returns_average():
geometry = {"type": "MultiPoint", "coordinates": [[0, 0], [2, 2]]}
centroid = OpenSearchInterface._geometry_centroid(geometry)
assert centroid == {"lat": 1.0, "lon": 1.0}
def test_dataset_to_document(sample_dataset):
iface = OpenSearchInterface.__new__(OpenSearchInterface)
document = iface.dataset_to_document(sample_dataset)
assert document["_index"] == OpenSearchInterface.INDEX_NAME
assert document["_id"] == sample_dataset.id
assert document["title"] == "Dataset Title"
assert document["publisher"] == "Publisher"
assert document["has_spatial"] is True
assert document["harvest_record"] == "/harvest_record/hr-1"
assert document["spatial_centroid"] == {"lat": 2.0, "lon": 1.0}
def test_init_creates_index_when_missing(monkeypatch):
fake_client = FakeClient(exists=False)
monkeypatch.setattr(
OpenSearchInterface,
"_create_test_opensearch_client",
staticmethod(lambda host: fake_client),
)
OpenSearchInterface(test_host="localhost")
created = fake_client.indices.created
assert created is not None
assert created["index"] == OpenSearchInterface.INDEX_NAME
assert created["body"]["mappings"] == OpenSearchInterface.MAPPINGS
assert created["body"]["settings"] == OpenSearchInterface.SETTINGS
def test_from_environment_uses_aws_client(monkeypatch):
fake_client = FakeClient(exists=True)
captured = {}
def fake_aws_client(host):
captured["host"] = host
return fake_client
monkeypatch.setenv("OPENSEARCH_HOST", "search.example.es.amazonaws.com")
monkeypatch.setattr(
OpenSearchInterface,
"_create_aws_opensearch_client",
staticmethod(fake_aws_client),
)
monkeypatch.setattr(
OpenSearchInterface,
"_create_test_opensearch_client",
staticmethod(lambda host: FakeClient(exists=True)),
)
iface = OpenSearchInterface.from_environment()
assert iface.client is fake_client
assert captured["host"] == "search.example.es.amazonaws.com"
def test_from_environment_requires_host(monkeypatch):
monkeypatch.delenv("OPENSEARCH_HOST", raising=False)
with pytest.raises(ValueError):
OpenSearchInterface.from_environment()
def test_run_with_timeout_retry(monkeypatch):
iface = OpenSearchInterface.__new__(OpenSearchInterface)
calls = {"count": 0}
sleeps = []
def action():
calls["count"] += 1
if calls["count"] < 3:
raise ConnectionTimeout("timeout")
return "ok"
def fake_sleep(seconds):
sleeps.append(seconds)
monkeypatch.setattr(opensearch.time, "sleep", fake_sleep)
result = iface._run_with_timeout_retry(
action,
action_name="test",
timeout_retries=3,
timeout_backoff_base=2.0,
)
assert result == "ok"
assert calls["count"] == 3
assert sleeps == [2.0, 4.0]
def test_index_datasets_counts_errors(monkeypatch, sample_dataset):
iface = OpenSearchInterface.__new__(OpenSearchInterface)
iface.client = FakeClient(exists=True)
def fake_streaming_bulk(client, documents, raise_on_error, max_retries):
assert client is iface.client
assert len(documents) == 1
yield True, {"index": {"result": "created", "_id": "dataset-1"}}
yield False, {
"index": {
"status": 400,
"error": {
"type": "mapper_parsing_exception",
"reason": "bad",
"caused_by": {"type": "illegal_argument_exception"},
},
}
}
monkeypatch.setattr(opensearch.helpers, "streaming_bulk", fake_streaming_bulk)
succeeded, failed, errors = iface.index_datasets(
[sample_dataset],
refresh_after=False,
)
assert succeeded == 1
assert failed == 1
assert len(errors) == 2
def test_delete_dataset_by_id_calls_client():
iface = OpenSearchInterface.__new__(OpenSearchInterface)
iface.client = FakeClient(exists=True)
result = iface.delete_dataset_by_id("dataset-1", refresh_after=False)
assert result is True
assert iface.client.deleted == [
(
OpenSearchInterface.INDEX_NAME,
"dataset-1",
[404],
DEFAULT_DELETE_REQUEST_TIMEOUT_SECONDS,
)
]
def test_delete_dataset_by_id_no_id():
iface = OpenSearchInterface.__new__(OpenSearchInterface)
iface.client = FakeClient(exists=True)
result = iface.delete_dataset_by_id("", refresh_after=False)
assert result is False
assert iface.client.deleted == []