GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DataBUS/tests/test_valid_sequence.py at main · NeotomaDB/DataBUS

Expand file tree

/

test_valid_sequence.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
336 lines (275 loc) · 12 KB
/

test_valid_sequence.py

Copy path

File metadata and controls

336 lines (275 loc) · 12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""Tests for the valid_sequence validator and its extraction helpers.
``valid_sequence`` runs after ``valid_data``. For every DNA entry in the
template it inserts one ``ndb.sequences`` row, one ``ndb.sequencedata`` row per
dataid the taxon produced, and one ``ndb.aednamodels`` row carrying the taxon
call. The tests drive it with a routing cursor because each of those steps is a
different statement.
"""
import pytest
import DataBUS.neotomaValidator as nv
from DataBUS import Response
from DataBUS.neotomaValidator.valid_sequence import (
_build_key_to_dna_map,
_extract_dna_entries,
_extract_model_info,
)
class RoutingCursor:
"""Answers fetchone() based on a substring match against the last query."""
def __init__(self, routes=None, rowcount=0):
self.routes = routes or {}
self.calls = []
self.rowcount = rowcount
self._next = None
def execute(self, query, params=None):
self.calls.append((query, params))
self._next = None
for marker, value in self.routes.items():
if marker in query:
self._next = value
break
def fetchone(self):
return self._next
def fetchall(self):
return []
def queries_matching(self, marker):
return [p for q, p in self.calls if marker in q]
def _dna_entry(taxonname, dnasequence, asv, column=None):
return {
"neotoma": "ndb.data.value",
"taxonname": taxonname,
"dnasequence": dnasequence,
"asv": asv,
"column": column or asv,
}
def _seq_yml(entries=None, model="DADA2", superseeds=None):
metadata = list(entries if entries is not None else [_dna_entry("Picea", "ACGTACGT", "ASV1")])
if model is not None:
entry = {"neotoma": "ndb.aednamodels.modelid", "value": model}
if superseeds is not None:
entry["superseeds"] = superseeds
metadata.append(entry)
return {"metadata": metadata}
def _databus(id_dict, datasetid=74655):
datasets, data = Response(), Response()
datasets.id_int = datasetid
data.id_dict = id_dict
return {"datasets": datasets, "data": data}
# Sequence insert -> 500, model insert -> 900, taxonid lookup -> 42.
BASE_ROUTES = {
"INSERT INTO ndb.sequences": (500,),
"INSERT INTO ndb.aednamodels": (900,),
"SELECT v.taxonid": (42,),
}
class TestExtractDnaEntries:
def test_no_entries(self):
assert _extract_dna_entries({"metadata": []}) == []
def test_ignores_entries_without_both_dnasequence_and_asv(self):
yml = {
"metadata": [
{"neotoma": "ndb.data.value", "taxonname": "Picea", "asv": "ASV1"},
{"neotoma": "ndb.data.value", "taxonname": "Picea", "dnasequence": "ACGT"},
{"neotoma": "ndb.sites.sitename", "dnasequence": "ACGT", "asv": "ASV1"},
]
}
assert _extract_dna_entries(yml) == []
def test_collects_complete_entries(self):
yml = {"metadata": [_dna_entry("Picea", "ACGTACGT", "ASV1", column="col_a")]}
assert _extract_dna_entries(yml) == [
{
"taxonname": "Picea",
"dnasequence": "ACGTACGT",
"asv": "ASV1",
"column": "col_a",
}
]
class TestExtractModelInfo:
def test_absent_model(self):
assert _extract_model_info({"metadata": []}) == (None, [])
def test_model_without_superseeds(self):
yml = {"metadata": [{"neotoma": "ndb.aednamodels.modelid", "value": "DADA2"}]}
assert _extract_model_info(yml) == ("DADA2", [])
def test_superseeds_string_is_split_and_stripped(self):
yml = {
"metadata": [
{
"neotoma": "ndb.aednamodels.modelid",
"value": "DADA2",
"superseeds": "OBITools, mothur ",
}
]
}
assert _extract_model_info(yml) == ("DADA2", ["OBITools", "mothur"])
def test_superseeds_list_is_kept(self):
yml = {
"metadata": [
{
"neotoma": "ndb.aednamodels.modelid",
"value": "DADA2",
"superseeds": ["OBITools"],
}
]
}
assert _extract_model_info(yml) == ("DADA2", ["OBITools"])
class TestBuildKeyToDnaMap:
def test_prefers_compound_key(self):
entries = _extract_dna_entries(_seq_yml())
mapping = _build_key_to_dna_map(entries, {"Picea::ASV1": [1], "Picea": [2]})
assert list(mapping) == ["Picea::ASV1"]
def test_falls_back_to_plain_taxon_key(self):
entries = _extract_dna_entries(_seq_yml())
mapping = _build_key_to_dna_map(entries, {"Picea": [2]})
assert mapping == {"Picea": {"dnasequence": "ACGTACGT", "asv": "ASV1"}}
def test_unmatched_entry_is_dropped(self):
entries = _extract_dna_entries(_seq_yml())
assert _build_key_to_dna_map(entries, {"Abies": [1]}) == {}
def test_two_asvs_for_one_taxon_keep_separate_compound_keys(self):
yml = _seq_yml(
entries=[
_dna_entry("Picea", "ACGT", "ASV1"),
_dna_entry("Picea", "TGCA", "ASV2"),
]
)
entries = _extract_dna_entries(yml)
mapping = _build_key_to_dna_map(entries, {"Picea::ASV1": [1], "Picea::ASV2": [2]})
assert mapping["Picea::ASV1"]["dnasequence"] == "ACGT"
assert mapping["Picea::ASV2"]["dnasequence"] == "TGCA"
def test_plain_key_taken_only_once(self):
"""With no compound keys, the first entry wins and the second is dropped."""
yml = _seq_yml(
entries=[
_dna_entry("Picea", "ACGT", "ASV1"),
_dna_entry("Picea", "TGCA", "ASV2"),
]
)
entries = _extract_dna_entries(yml)
mapping = _build_key_to_dna_map(entries, {"Picea": [1]})
assert mapping == {"Picea": {"dnasequence": "ACGT", "asv": "ASV1"}}
class TestValidSequence:
def test_no_dna_entries_is_valid(self):
cur = RoutingCursor()
response = nv.valid_sequence(
cur, {"metadata": []}, csv_file=None, databus=_databus({"Picea": [1]})
)
assert response.validAll
assert response.message == ["? No aeDNA sequence entries found in template."]
assert cur.calls == []
def test_missing_dataset_id_aborts(self):
response = nv.valid_sequence(RoutingCursor(), _seq_yml(), csv_file=None, databus={})
assert not response.validAll
assert any("Dataset ID not available" in m for m in response.message)
def test_missing_data_ids_aborts(self):
datasets = Response()
datasets.id_int = 74655
response = nv.valid_sequence(
RoutingCursor(), _seq_yml(), csv_file=None, databus={"datasets": datasets}
)
assert not response.validAll
assert any("Data IDs not available" in m for m in response.message)
def test_no_matching_data_entries_is_valid(self):
response = nv.valid_sequence(
RoutingCursor(), _seq_yml(), csv_file=None, databus=_databus({"Abies": [1]})
)
assert response.validAll
assert any("Could not match any DNA entries" in m for m in response.message)
def test_full_insert_chain(self):
cur = RoutingCursor(dict(BASE_ROUTES))
response = nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": [11, 12]})
)
assert response.validAll
assert response.id_dict == {"Picea::ASV1": {"sequenceid": 500, "modelid": 900}}
assert cur.queries_matching("INSERT INTO ndb.sequences") == [
{"datasetid": 74655, "sequence": "ACGTACGT", "asv": "ASV1", "primername": None}
]
def test_one_sequencedata_row_per_dataid(self):
cur = RoutingCursor(dict(BASE_ROUTES))
nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": [11, 12, 13]})
)
assert cur.queries_matching("INSERT INTO ndb.sequencedata") == [
{"dataid": 11, "sequenceid": 500},
{"dataid": 12, "sequenceid": 500},
{"dataid": 13, "sequenceid": 500},
]
def test_model_row_carries_taxonid_from_first_dataid(self):
cur = RoutingCursor(dict(BASE_ROUTES))
nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": [11, 12]})
)
assert cur.queries_matching("SELECT v.taxonid") == [{"dataid": 11}]
(model,) = cur.queries_matching("INSERT INTO ndb.aednamodels")
assert model["sequenceid"] == 500
assert model["taxonid"] == 42
assert model["model"] == "DADA2"
def test_no_model_entry_skips_aednamodels(self):
cur = RoutingCursor(dict(BASE_ROUTES))
response = nv.valid_sequence(
cur,
_seq_yml(model=None),
csv_file=None,
databus=_databus({"Picea::ASV1": [11]}),
)
assert any("No aeDNA model entry" in m for m in response.message)
assert cur.queries_matching("INSERT INTO ndb.aednamodels") == []
assert response.id_dict == {"Picea::ASV1": {"sequenceid": 500, "modelid": None}}
def test_missing_taxonid_leaves_model_none(self):
routes = dict(BASE_ROUTES)
routes["SELECT v.taxonid"] = None
cur = RoutingCursor(routes)
response = nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": [11]})
)
assert not response.validAll
assert any("Could not retrieve taxonid for dataid 11" in m for m in response.message)
assert response.id_dict == {"Picea::ASV1": {"sequenceid": 500, "modelid": None}}
def test_superseded_models_are_updated(self):
cur = RoutingCursor(dict(BASE_ROUTES), rowcount=2)
response = nv.valid_sequence(
cur,
_seq_yml(superseeds="OBITools"),
csv_file=None,
databus=_databus({"Picea::ASV1": [11]}),
)
updates = cur.queries_matching("UPDATE ndb.aednamodels")
assert updates == [{"new_modelid": 900, "taxonid": 42, "old_model": "OBITools"}]
assert any("2 previous model(s) marked as superseded" in m for m in response.message)
def test_empty_dataid_list_is_skipped(self):
cur = RoutingCursor(dict(BASE_ROUTES))
response = nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": []})
)
assert response.validAll
assert any("No DNA data entries matched" in m for m in response.message)
assert cur.queries_matching("INSERT INTO ndb.sequences") == []
def test_sequence_insert_failure_skips_the_entry(self):
class Failing(RoutingCursor):
def execute(self, query, params=None):
super().execute(query, params)
if "INSERT INTO ndb.sequences" in query:
raise RuntimeError("duplicate key")
cur = Failing(dict(BASE_ROUTES))
response = nv.valid_sequence(
cur, _seq_yml(), csv_file=None, databus=_databus({"Picea::ASV1": [11]})
)
assert not response.validAll
assert any("Sequence cannot be inserted" in m for m in response.message)
assert cur.queries_matching("INSERT INTO ndb.sequencedata") == []
assert response.id_dict == {}
def test_two_taxa_each_get_their_own_sequence(self):
yml = _seq_yml(
entries=[
_dna_entry("Picea", "ACGT", "ASV1"),
_dna_entry("Abies", "TGCA", "ASV2"),
]
)
cur = RoutingCursor(dict(BASE_ROUTES))
response = nv.valid_sequence(
cur,
yml,
csv_file=None,
databus=_databus({"Picea::ASV1": [11], "Abies::ASV2": [22]}),
)
assert response.validAll
assert set(response.id_dict) == {"Picea::ASV1", "Abies::ASV2"}
sequences = cur.queries_matching("INSERT INTO ndb.sequences")
assert [s["asv"] for s in sequences] == ["ASV1", "ASV2"]
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.