-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_models.py
More file actions
355 lines (301 loc) · 11.3 KB
/
test_models.py
File metadata and controls
355 lines (301 loc) · 11.3 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Tests for Pydantic models and parsing."""
from encode_connector.client.models import (
DownloadResult,
ExperimentDetail,
ExperimentSummary,
FileSummary,
_extract_organism,
_human_size,
)
class TestHumanSize:
def test_zero(self):
assert _human_size(0) == "0 B"
def test_bytes(self):
assert _human_size(500) == "500.0 B"
def test_kilobytes(self):
assert _human_size(1024) == "1.0 KB"
def test_megabytes(self):
assert _human_size(1024 * 1024) == "1.0 MB"
def test_gigabytes(self):
assert _human_size(1024**3) == "1.0 GB"
def test_terabytes(self):
assert _human_size(1024**4) == "1.0 TB"
class TestExtractOrganism:
def test_direct_dict(self):
data = {"organism": {"scientific_name": "Homo sapiens"}}
assert _extract_organism(data) == "Homo sapiens"
def test_direct_string_path(self):
data = {"organism": "/organisms/human/"}
assert _extract_organism(data) == "human"
def test_from_replicates(self):
data = {
"replicates": [{"library": {"biosample": {"donor": {"organism": {"scientific_name": "Mus musculus"}}}}}]
}
assert _extract_organism(data) == "Mus musculus"
def test_empty(self):
assert _extract_organism({}) == ""
def test_none_organism(self):
assert _extract_organism({"organism": None}) == ""
class TestExperimentSummary:
def test_from_api_basic(self):
data = {
"accession": "ENCSR133RZO",
"assay_title": "Histone ChIP-seq",
"status": "released",
"description": "Test experiment",
"date_released": "2024-01-15",
}
exp = ExperimentSummary.from_api(data)
assert exp.accession == "ENCSR133RZO"
assert exp.assay_title == "Histone ChIP-seq"
assert exp.status == "released"
def test_from_api_with_target_dict(self):
data = {
"accession": "ENCSR133RZO",
"target": {"label": "H3K27me3"},
}
exp = ExperimentSummary.from_api(data)
assert exp.target == "H3K27me3"
def test_from_api_with_target_string(self):
data = {
"accession": "ENCSR133RZO",
"target": "/targets/H3K27me3-human/",
}
exp = ExperimentSummary.from_api(data)
assert exp.target == "H3K27me3-human"
def test_from_api_with_lab_dict(self):
data = {
"accession": "ENCSR133RZO",
"lab": {"title": "Bernstein Lab"},
}
exp = ExperimentSummary.from_api(data)
assert exp.lab == "Bernstein Lab"
def test_from_api_with_biosample_ontology(self):
data = {
"accession": "ENCSR133RZO",
"biosample_ontology": {
"classification": "tissue",
"organ_slims": ["pancreas"],
},
}
exp = ExperimentSummary.from_api(data)
assert exp.biosample_type == "tissue"
assert exp.organ == "pancreas"
def test_from_api_empty(self):
exp = ExperimentSummary.from_api({})
assert exp.accession == ""
def test_from_api_assembly_extraction(self):
data = {
"accession": "ENCSR133RZO",
"files": [
{"assembly": "GRCh38"},
{"assembly": "GRCh38"},
{"assembly": "hg19"},
{"assembly": ""}, # empty should be excluded
],
}
exp = ExperimentSummary.from_api(data)
assert exp.assembly == ["GRCh38", "hg19"]
def test_from_api_assembly_empty(self):
data = {"accession": "ENCSR133RZO"}
exp = ExperimentSummary.from_api(data)
assert exp.assembly == []
def test_from_api_audit_counts(self):
data = {
"accession": "ENCSR133RZO",
"audit": {
"ERROR": [{"category": "error1"}, {"category": "error2"}],
"WARNING": [{"category": "warn1"}],
},
}
exp = ExperimentSummary.from_api(data)
assert exp.audit_error_count == 2
assert exp.audit_warning_count == 1
def test_from_api_audit_counts_empty(self):
data = {"accession": "ENCSR133RZO"}
exp = ExperimentSummary.from_api(data)
assert exp.audit_error_count == 0
assert exp.audit_warning_count == 0
def test_from_api_dbxrefs(self):
data = {
"accession": "ENCSR133RZO",
"dbxrefs": ["GEO:GSE123456", "PMID:12345"],
}
exp = ExperimentSummary.from_api(data)
assert exp.dbxrefs == ["GEO:GSE123456", "PMID:12345"]
def test_from_api_dbxrefs_empty(self):
data = {"accession": "ENCSR133RZO"}
exp = ExperimentSummary.from_api(data)
assert exp.dbxrefs == []
def test_from_api_url_generated(self):
data = {"accession": "ENCSR133RZO"}
exp = ExperimentSummary.from_api(data)
assert exp.url == "https://www.encodeproject.org/experiments/ENCSR133RZO/"
def test_from_api_url_empty_accession(self):
data = {}
exp = ExperimentSummary.from_api(data)
assert exp.url == ""
class TestFileSummary:
def test_from_api_basic(self):
data = {
"accession": "ENCFF635JIA",
"file_format": "bed",
"file_type": "bed narrowPeak",
"output_type": "IDR thresholded peaks",
"file_size": 1048576,
"status": "released",
"href": "/files/ENCFF635JIA/@@download/ENCFF635JIA.bed.gz",
"md5sum": "abc123",
}
f = FileSummary.from_api(data)
assert f.accession == "ENCFF635JIA"
assert f.file_format == "bed"
assert f.file_size == 1048576
assert f.file_size_human == "1.0 MB"
assert "encodeproject.org" in f.download_url
assert f.md5sum == "abc123"
def test_from_api_with_dataset_string(self):
data = {
"accession": "ENCFF635JIA",
"dataset": "/experiments/ENCSR133RZO/",
}
f = FileSummary.from_api(data)
assert f.experiment_accession == "ENCSR133RZO"
def test_from_api_with_dataset_dict(self):
data = {
"accession": "ENCFF635JIA",
"dataset": {"accession": "ENCSR133RZO"},
}
f = FileSummary.from_api(data)
assert f.experiment_accession == "ENCSR133RZO"
def test_from_api_zero_size(self):
data = {"accession": "ENCFF635JIA", "file_size": 0}
f = FileSummary.from_api(data)
assert f.file_size == 0
assert f.file_size_human == "0 B"
def test_from_api_none_size(self):
data = {"accession": "ENCFF635JIA", "file_size": None}
f = FileSummary.from_api(data)
assert f.file_size == 0
class TestExperimentDetail:
def test_from_api_with_organism(self):
data = {
"accession": "ENCSR133RZO",
"organism": {"scientific_name": "Homo sapiens"},
"assay_title": "ChIP-seq",
}
detail = ExperimentDetail.from_api(data)
assert detail.organism == "Homo sapiens"
def test_from_api_with_controls(self):
data = {
"accession": "ENCSR133RZO",
"possible_controls": [
{"accession": "ENCSR000ABC"},
"/experiments/ENCSR000DEF/",
],
}
detail = ExperimentDetail.from_api(data)
assert "ENCSR000ABC" in detail.possible_controls
assert "ENCSR000DEF" in detail.possible_controls
def test_from_api_with_files(self):
files = [
{"accession": "ENCFF001", "file_format": "bed", "file_size": 100},
{"accession": "ENCFF002", "file_format": "bam", "file_size": 200},
]
detail = ExperimentDetail.from_api({"accession": "ENCSR133RZO"}, files=files)
assert len(detail.files) == 2
def test_url_generated(self):
detail = ExperimentDetail.from_api({"accession": "ENCSR133RZO"})
assert "ENCSR133RZO" in detail.url
class TestExperimentSummaryEdgeCases:
def test_from_api_with_lab_string(self):
"""Cover line 49: lab as string path."""
data = {
"accession": "ENCSR133RZO",
"lab": "/labs/bernstein/",
}
exp = ExperimentSummary.from_api(data)
assert exp.lab == "bernstein"
def test_from_api_with_biosample_ontology_string(self):
"""Cover lines 62-63: biosample_ontology as string."""
data = {
"accession": "ENCSR133RZO",
"biosample_ontology": "tissue",
}
exp = ExperimentSummary.from_api(data)
assert exp.biosample_type == "tissue"
def test_from_api_files_with_non_dict_entries(self):
"""Assembly extraction filters out non-dict file entries."""
data = {
"accession": "ENCSR133RZO",
"files": [
{"assembly": "GRCh38"},
"/files/ENCFF001/", # string entry, not dict
],
}
exp = ExperimentSummary.from_api(data)
assert exp.assembly == ["GRCh38"]
class TestExperimentDetailEdgeCases:
def test_from_api_with_target_string(self):
"""Cover line 230: target as string path in ExperimentDetail."""
data = {
"accession": "ENCSR133RZO",
"target": "/targets/H3K27me3-human/",
}
detail = ExperimentDetail.from_api(data)
assert detail.target == "H3K27me3-human"
def test_from_api_with_lab_string(self):
"""Cover line 239: lab as string path in ExperimentDetail."""
data = {
"accession": "ENCSR133RZO",
"lab": "/labs/bernstein/",
}
detail = ExperimentDetail.from_api(data)
assert detail.lab == "bernstein"
def test_from_api_with_award_string(self):
"""Cover line 248: award as string path."""
data = {
"accession": "ENCSR133RZO",
"award": "/awards/U54HG007004/",
}
detail = ExperimentDetail.from_api(data)
assert detail.award == "U54HG007004"
def test_from_api_with_award_dict(self):
"""Cover line 250: award as dict with project."""
data = {
"accession": "ENCSR133RZO",
"award": {"project": "ENCODE"},
}
detail = ExperimentDetail.from_api(data)
assert detail.award == "ENCODE"
def test_from_api_with_controls_string_no_path(self):
"""Possible control as string without path separators."""
data = {
"accession": "ENCSR133RZO",
"possible_controls": ["ENCSR000ABC"],
}
detail = ExperimentDetail.from_api(data)
assert "ENCSR000ABC" in detail.possible_controls
def test_from_api_empty_controls(self):
"""No possible_controls key."""
data = {"accession": "ENCSR133RZO"}
detail = ExperimentDetail.from_api(data)
assert detail.possible_controls == []
class TestDownloadResult:
def test_default_values(self):
r = DownloadResult(accession="ENCFF001")
assert r.success is False
assert r.error == ""
assert r.md5_verified is False
def test_with_all_fields(self):
r = DownloadResult(
accession="ENCFF001",
success=True,
file_path="/data/file.bed",
file_size=1024,
file_size_human="1.0 KB",
md5_verified=True,
error="",
)
assert r.success is True
assert r.file_size == 1024