Skip to content

Commit 3bfcfd1

Browse files
Fixup tests
1 parent 7437862 commit 3bfcfd1

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

bio2zarr/vcf.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,9 @@ def check_field_clobbering(icf_metadata):
988988

989989
@dataclasses.dataclass
990990
class IcfWriteSummary:
991-
partitions: int
992-
samples: int
993-
variants: int
991+
num_partitions: int
992+
num_samples: int
993+
num_variants: int
994994

995995
def asdict(self):
996996
return dataclasses.asdict(self)
@@ -1056,9 +1056,9 @@ def init(
10561056
with open(self.wip_path / "metadata.json", "w") as f:
10571057
json.dump(self.metadata.asdict(), f, indent=4)
10581058
return IcfWriteSummary(
1059-
partitions=self.num_partitions,
1060-
variants=icf_metadata.num_records,
1061-
samples=icf_metadata.num_samples,
1059+
num_partitions=self.num_partitions,
1060+
num_variants=icf_metadata.num_records,
1061+
num_samples=icf_metadata.num_samples,
10621062
)
10631063

10641064
def mkdirs(self):
@@ -1667,10 +1667,10 @@ def fromdict(d):
16671667

16681668
@dataclasses.dataclass
16691669
class VcfZarrWriteSummary:
1670-
partitions: int
1671-
samples: int
1672-
variants: int
1673-
chunks: int
1670+
num_partitions: int
1671+
num_samples: int
1672+
num_variants: int
1673+
num_chunks: int
16741674
max_encoding_memory: str
16751675

16761676
def asdict(self):
@@ -1765,10 +1765,10 @@ def init(
17651765
json.dump(self.metadata.asdict(), f, indent=4)
17661766

17671767
return VcfZarrWriteSummary(
1768-
variants=self.icf.num_records,
1769-
samples=self.icf.num_samples,
1770-
partitions=self.num_partitions,
1771-
chunks=total_chunks,
1768+
num_variants=self.icf.num_records,
1769+
num_samples=self.icf.num_samples,
1770+
num_partitions=self.num_partitions,
1771+
num_chunks=total_chunks,
17721772
max_encoding_memory=display_size(self.get_max_encoding_memory()),
17731773
)
17741774

tests/test_cli.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dataclasses
12
import json
23
from unittest import mock
34

@@ -47,6 +48,17 @@
4748
DEFAULT_DENCODE_FINALISE_ARGS = dict(show_progress=True)
4849

4950

51+
@dataclasses.dataclass
52+
class FakeWorkSummary:
53+
num_partitions: int
54+
55+
def asdict(self):
56+
return dataclasses.asdict(self)
57+
58+
def asjson(self):
59+
return json.dumps(self.asdict())
60+
61+
5062
class TestWithMocks:
5163
vcf_path = "tests/data/vcf/sample.vcf.gz"
5264

@@ -263,7 +275,7 @@ def test_vcf_explode_missing_and_existing_vcf(self, mocked, tmp_path):
263275
assert "'no_such_file' does not exist" in result.stderr
264276
mocked.assert_not_called()
265277

266-
@mock.patch("bio2zarr.vcf.explode_init", return_value=5)
278+
@mock.patch("bio2zarr.vcf.explode_init", return_value=FakeWorkSummary(5))
267279
def test_vcf_dexplode_init(self, mocked, tmp_path):
268280
runner = ct.CliRunner(mix_stderr=False)
269281
icf_path = tmp_path / "icf"
@@ -274,7 +286,7 @@ def test_vcf_dexplode_init(self, mocked, tmp_path):
274286
)
275287
assert result.exit_code == 0
276288
assert len(result.stderr) == 0
277-
assert result.stdout == "5\n"
289+
assert list(result.stdout.split()) == ["num_partitions", "5"]
278290
mocked.assert_called_once_with(
279291
str(icf_path),
280292
(self.vcf_path,),
@@ -416,7 +428,7 @@ def test_encode(self, mocked, tmp_path):
416428
**DEFAULT_ENCODE_ARGS,
417429
)
418430

419-
@mock.patch("bio2zarr.vcf.encode_init", return_value=(10, 1024))
431+
@mock.patch("bio2zarr.vcf.encode_init", return_value=FakeWorkSummary(10))
420432
def test_dencode_init(self, mocked, tmp_path):
421433
icf_path = tmp_path / "icf"
422434
icf_path.mkdir()
@@ -428,7 +440,7 @@ def test_dencode_init(self, mocked, tmp_path):
428440
catch_exceptions=False,
429441
)
430442
assert result.exit_code == 0
431-
assert result.stdout == "10\t1 KiB\n"
443+
assert list(result.stdout.split()) == ["num_partitions", "10"]
432444
assert len(result.stderr) == 0
433445
mocked.assert_called_once_with(
434446
str(icf_path),
@@ -534,7 +546,7 @@ def test_dexplode(self, tmp_path, one_based):
534546
catch_exceptions=False,
535547
)
536548
assert result.exit_code == 0
537-
assert json.loads(result.stdout)["partitions"] == 3
549+
assert json.loads(result.stdout)["num_partitions"] == 3
538550

539551
for j in range(3):
540552
if one_based:
@@ -603,7 +615,7 @@ def test_dencode(self, tmp_path, one_based):
603615
catch_exceptions=False,
604616
)
605617
assert result.exit_code == 0
606-
assert json.loads(result.stdout)["partitions"] == 3
618+
assert json.loads(result.stdout)["num_partitions"] == 3
607619

608620
for j in range(3):
609621
if one_based:

tests/test_icf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_init_paths(self, tmp_path):
106106
icf_path = tmp_path / "x.icf"
107107
assert not icf_path.exists()
108108
summary = vcf.explode_init(icf_path, [self.data_path])
109-
assert summary.partitions == 3
109+
assert summary.num_partitions == 3
110110
assert icf_path.exists()
111111
wip_path = icf_path / "wip"
112112
assert wip_path.exists()
@@ -120,7 +120,7 @@ def test_finalise_paths(self, tmp_path):
120120
wip_path = icf_path / "wip"
121121
summary = vcf.explode_init(icf_path, [self.data_path])
122122
assert icf_path.exists()
123-
for j in range(summary.partitions):
123+
for j in range(summary.num_partitions):
124124
vcf.explode_partition(icf_path, j)
125125
assert wip_path.exists()
126126
vcf.explode_finalise(icf_path)
@@ -271,7 +271,7 @@ def run_explode(self, tmp_path, **kwargs):
271271
def run_dexplode(self, tmp_path, **kwargs):
272272
icf_path = tmp_path / "icf"
273273
summary = vcf.explode_init(icf_path, [self.data_path], **kwargs)
274-
for j in range(summary.partitions):
274+
for j in range(summary.num_partitions):
275275
vcf.explode_partition(icf_path, j)
276276
vcf.explode_finalise(icf_path)
277277
return vcf.IntermediateColumnarFormat(icf_path)

tests/test_vcf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def test_init_paths(self, icf_path, tmp_path):
421421
zarr_path = tmp_path / "x.zarr"
422422
assert not zarr_path.exists()
423423
summary = vcf.encode_init(icf_path, zarr_path, 7, variants_chunk_size=3)
424-
assert summary.partitions == 3
424+
assert summary.num_partitions == 3
425425
assert zarr_path.exists()
426426
wip_path = zarr_path / "wip"
427427
assert wip_path.exists()
@@ -443,7 +443,7 @@ def test_finalise_paths(self, icf_path, tmp_path):
443443
summary = vcf.encode_init(icf_path, zarr_path, 7, variants_chunk_size=3)
444444
wip_path = zarr_path / "wip"
445445
assert wip_path.exists()
446-
for j in range(summary.partitions):
446+
for j in range(summary.num_partitions):
447447
vcf.encode_partition(zarr_path, j)
448448
assert (wip_path / "partitions" / f"p{j}").exists()
449449
vcf.encode_finalise(zarr_path)
@@ -527,7 +527,7 @@ def generate_vcf(self, path, info_field=None, format_field=None, num_rows=1):
527527
pos = str(k + 1)
528528
print("\t".join(["1", pos, "A", "T", ".", ".", ".", "."]), file=out)
529529

530-
print(open(path).read())
530+
# print(open(path).read())
531531
# This also compresses the input file
532532
pysam.tabix_index(str(path), preset="vcf")
533533

tests/test_vcf_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,12 @@ def test_split_explode(tmp_path):
873873
]
874874
out = tmp_path / "test.explode"
875875
work_summary = vcf.explode_init(out, paths, target_num_partitions=15)
876-
assert work_summary.partitions == 3
876+
assert work_summary.num_partitions == 3
877877

878878
with pytest.raises(FileNotFoundError):
879879
pcvcf = vcf.IntermediateColumnarFormat(out)
880880

881-
for j in range(work_summary.partitions):
881+
for j in range(work_summary.num_partitions):
882882
vcf.explode_partition(out, j)
883883
vcf.explode_finalise(out)
884884
pcvcf = vcf.IntermediateColumnarFormat(out)

0 commit comments

Comments
 (0)