Skip to content

Commit 7b32b35

Browse files
Refactor mocked CLI tests
1 parent 1c0add8 commit 7b32b35

File tree

2 files changed

+103
-107
lines changed

2 files changed

+103
-107
lines changed

bio2zarr/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"-p", "--worker-processes", type=int, default=1, help="Number of worker processes"
1515
)
1616

17-
# TODO help text
17+
# Note: -l and -w were chosen when these were called "width" and "length".
18+
# possibly there are better letters now.
1819
variants_chunk_size = click.option(
1920
"-l",
2021
"--variants-chunk-size",

tests/test_cli.py

Lines changed: 101 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -4,133 +4,128 @@
44
import click.testing as ct
55

66
from bio2zarr import cli
7-
from bio2zarr import vcf
87
from bio2zarr import __main__ as main
98
from bio2zarr import provenance
109

1110

1211
class TestWithMocks:
13-
def test_vcf_explode(self):
12+
@mock.patch("bio2zarr.vcf.explode")
13+
def test_vcf_explode(self, mocked):
1414
runner = ct.CliRunner(mix_stderr=False)
15-
with mock.patch("bio2zarr.vcf.explode") as mocked:
16-
result = runner.invoke(
17-
cli.vcf2zarr, ["explode", "source", "dest"], catch_exceptions=False
18-
)
19-
assert result.exit_code == 0
20-
assert len(result.stdout) == 0
21-
assert len(result.stderr) == 0
22-
mocked.assert_called_once_with(
23-
("source",),
24-
"dest",
25-
column_chunk_size=64,
26-
worker_processes=1,
27-
show_progress=True,
28-
)
15+
result = runner.invoke(
16+
cli.vcf2zarr, ["explode", "source", "dest"], catch_exceptions=False
17+
)
18+
assert result.exit_code == 0
19+
assert len(result.stdout) == 0
20+
assert len(result.stderr) == 0
21+
mocked.assert_called_once_with(
22+
("source",),
23+
"dest",
24+
column_chunk_size=64,
25+
worker_processes=1,
26+
show_progress=True,
27+
)
2928

30-
def test_inspect(self):
29+
@mock.patch("bio2zarr.vcf.inspect")
30+
def test_inspect(self, mocked):
3131
runner = ct.CliRunner(mix_stderr=False)
32-
with mock.patch("bio2zarr.vcf.inspect", return_value={}) as mocked:
33-
result = runner.invoke(
34-
cli.vcf2zarr, ["inspect", "path"], catch_exceptions=False
35-
)
36-
assert result.exit_code == 0
37-
assert result.stdout == "\n"
38-
assert len(result.stderr) == 0
39-
mocked.assert_called_once_with("path")
32+
result = runner.invoke(
33+
cli.vcf2zarr, ["inspect", "path"], catch_exceptions=False
34+
)
35+
assert result.exit_code == 0
36+
assert result.stdout == "\n"
37+
assert len(result.stderr) == 0
38+
mocked.assert_called_once_with("path")
4039

41-
def test_mkschema(self):
40+
@mock.patch("bio2zarr.vcf.mkschema")
41+
def test_mkschema(self, mocked):
4242
runner = ct.CliRunner(mix_stderr=False)
43-
with mock.patch("bio2zarr.vcf.mkschema") as mocked:
44-
result = runner.invoke(
45-
cli.vcf2zarr, ["mkschema", "path"], catch_exceptions=False
46-
)
47-
assert result.exit_code == 0
48-
assert len(result.stdout) == 0
49-
assert len(result.stderr) == 0
50-
# TODO figure out how to test that we call it with stdout from
51-
# the CliRunner
52-
# mocked.assert_called_once_with("path", stdout)
53-
mocked.assert_called_once()
54-
55-
# @mock.patch.object(vcf, "encode", spec=vcf.encode)
56-
@mock.patch("bio2zarr.cli.vcf.encode")
43+
result = runner.invoke(
44+
cli.vcf2zarr, ["mkschema", "path"], catch_exceptions=False
45+
)
46+
assert result.exit_code == 0
47+
assert len(result.stdout) == 0
48+
assert len(result.stderr) == 0
49+
# TODO figure out how to test that we call it with stdout from
50+
# the CliRunner
51+
# mocked.assert_called_once_with("path", stdout)
52+
mocked.assert_called_once()
53+
54+
@mock.patch("bio2zarr.vcf.encode")
5755
def test_encode(self, mocked):
58-
# print("vcf", vcf)
59-
# print(vcf.encode)
6056
runner = ct.CliRunner(mix_stderr=False)
6157
result = runner.invoke(
6258
cli.vcf2zarr, ["encode", "if_path", "zarr_path"], catch_exceptions=False
6359
)
64-
# assert result.exit_code == 0
65-
# assert len(result.stdout) == 0
66-
# assert len(result.stderr) == 0
67-
# mocked.assert_called_once_with(
68-
# "if_path",
69-
# "zarr_path",
70-
# None,
71-
# variants_chunk_size=None,
72-
# samples_chunk_size=None,
73-
# max_v_chunks=None,
74-
# worker_processes=1,
75-
# max_memory=None,
76-
# show_progress=True,
77-
# )
60+
assert result.exit_code == 0
61+
assert len(result.stdout) == 0
62+
assert len(result.stderr) == 0
63+
mocked.assert_called_once_with(
64+
"if_path",
65+
"zarr_path",
66+
None,
67+
variants_chunk_size=None,
68+
samples_chunk_size=None,
69+
max_v_chunks=None,
70+
worker_processes=1,
71+
max_memory=None,
72+
show_progress=True,
73+
)
7874

79-
def test_convert_vcf(self):
75+
@mock.patch("bio2zarr.vcf.convert")
76+
def test_convert_vcf(self, mocked):
8077
runner = ct.CliRunner(mix_stderr=False)
81-
with mock.patch("bio2zarr.vcf.convert") as mocked:
82-
result = runner.invoke(
83-
cli.vcf2zarr,
84-
["convert", "vcf_path", "zarr_path"],
85-
catch_exceptions=False,
86-
)
87-
assert result.exit_code == 0
88-
assert len(result.stdout) == 0
89-
assert len(result.stderr) == 0
90-
mocked.assert_called_once_with(
91-
("vcf_path",),
92-
"zarr_path",
93-
variants_chunk_size=None,
94-
samples_chunk_size=None,
95-
worker_processes=1,
96-
show_progress=True,
97-
)
78+
result = runner.invoke(
79+
cli.vcf2zarr,
80+
["convert", "vcf_path", "zarr_path"],
81+
catch_exceptions=False,
82+
)
83+
assert result.exit_code == 0
84+
assert len(result.stdout) == 0
85+
assert len(result.stderr) == 0
86+
mocked.assert_called_once_with(
87+
("vcf_path",),
88+
"zarr_path",
89+
variants_chunk_size=None,
90+
samples_chunk_size=None,
91+
worker_processes=1,
92+
show_progress=True,
93+
)
9894

99-
def test_validate(self):
95+
@mock.patch("bio2zarr.vcf.validate")
96+
def test_validate(self, mocked):
10097
runner = ct.CliRunner(mix_stderr=False)
101-
with mock.patch("bio2zarr.vcf.validate") as mocked:
102-
result = runner.invoke(
103-
cli.vcf2zarr,
104-
["validate", "vcf_path", "zarr_path"],
105-
catch_exceptions=False,
106-
)
107-
assert result.exit_code == 0
108-
assert len(result.stdout) == 0
109-
assert len(result.stderr) == 0
110-
mocked.assert_called_once_with(
111-
"vcf_path",
112-
"zarr_path",
113-
show_progress=True,
114-
)
98+
result = runner.invoke(
99+
cli.vcf2zarr,
100+
["validate", "vcf_path", "zarr_path"],
101+
catch_exceptions=False,
102+
)
103+
assert result.exit_code == 0
104+
assert len(result.stdout) == 0
105+
assert len(result.stderr) == 0
106+
mocked.assert_called_once_with(
107+
"vcf_path",
108+
"zarr_path",
109+
show_progress=True,
110+
)
115111

116-
def test_convert_plink(self):
112+
@mock.patch("bio2zarr.plink.convert")
113+
def test_convert_plink(self, mocked):
117114
runner = ct.CliRunner(mix_stderr=False)
118-
with mock.patch("bio2zarr.plink.convert") as mocked:
119-
result = runner.invoke(
120-
cli.plink2zarr, ["convert", "in", "out"], catch_exceptions=False
121-
)
122-
assert result.exit_code == 0
123-
assert len(result.stdout) == 0
124-
assert len(result.stderr) == 0
125-
mocked.assert_called_once_with(
126-
"in",
127-
"out",
128-
worker_processes=1,
129-
samples_chunk_size=None,
130-
variants_chunk_size=None,
131-
show_progress=True,
132-
)
133-
115+
result = runner.invoke(
116+
cli.plink2zarr, ["convert", "in", "out"], catch_exceptions=False
117+
)
118+
assert result.exit_code == 0
119+
assert len(result.stdout) == 0
120+
assert len(result.stderr) == 0
121+
mocked.assert_called_once_with(
122+
"in",
123+
"out",
124+
worker_processes=1,
125+
samples_chunk_size=None,
126+
variants_chunk_size=None,
127+
show_progress=True,
128+
)
134129

135130

136131
class TestVcfPartition:

0 commit comments

Comments
 (0)