Skip to content

Commit b1547c7

Browse files
author
finlayclark
committed
Add more comprehensive tests
1 parent 36ebe24 commit b1547c7

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

k2dg/_print.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def _print_dg0(dg0: Quantity) -> None:
99
print(f"{dg0.to('kcal/mol').magnitude:#.3g} kcal/mol")
1010

1111

12-
def _print_kd0(kd0: float) -> None:
12+
def _print_kd0(kd0: Quantity) -> None:
1313
"""
1414
Print the dissociation constant with an appropriate prefix. Note that there
1515
is an implicit conversion from the standard dissociation constant to the
@@ -19,8 +19,11 @@ def _print_kd0(kd0: float) -> None:
1919
# (i.e. between 1e-1 and 1e2)
2020
kd = kd0.magnitude
2121
for prefix, value in K_UNITS.items():
22-
if 1e-1 < kd / value < 1e2:
22+
if 1e-1 < kd / value <= 1e2:
2323
print(f"{kd / value:#.3g} {prefix}")
24-
break
25-
# The value is less than 0.1 pM
26-
print(f"{kd / 1e-12:#.3g} pM")
24+
return
25+
# The value is less than 0.1 pM or greater than 100 M
26+
if kd < 1e-10:
27+
print(f"{kd / 1e-12:#.3g} fM")
28+
else:
29+
print(f"{kd:#.3g} M")

tests/test_cli.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from io import StringIO
2+
from unittest.mock import patch, Mock
3+
from k2dg._cli import run_cli # Replace 'your_module' with the actual module name
4+
5+
6+
def test_run_cli():
7+
# Test case 1: Convert from kd to dg
8+
test_args = ["2dg", "1.0", "uM", "-t", "298.15"]
9+
10+
with patch("sys.argv", ["k2dg"] + test_args):
11+
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
12+
run_cli()
13+
14+
# Validate the behavior of the function for this test case
15+
output = mock_stdout.getvalue()
16+
assert "-8.19 kcal/mol\n" in output
17+
18+
# Test case 2: Convert from dg to kd
19+
test_args = ["2kd", "-20.0", "kJ/mol", "-t", "310.15"]
20+
21+
with patch("sys.argv", ["k2dg"] + test_args):
22+
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
23+
run_cli()
24+
25+
# Validate the behavior of the function for this test case
26+
output = mock_stdout.getvalue()
27+
assert "0.428 mM\n" in output
28+
29+
# Test case 3: Missing subcommand
30+
test_args = []
31+
32+
with patch("argparse.ArgumentParser.print_help") as mock_print_help:
33+
with patch("sys.argv", ["k2dg"] + test_args):
34+
run_cli()
35+
36+
# Ensure that argparse.ArgumentParser.print_help was called
37+
mock_print_help.assert_called_once()

tests/test_print.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from k2dg import Q_
2+
from k2dg._print import _print_dg0, _print_kd0
3+
4+
5+
def test_print_dg0(capsys):
6+
_print_dg0(Q_(10, "kJ/mol"))
7+
captured = capsys.readouterr()
8+
assert captured.out == "2.39 kcal/mol\n"
9+
10+
_print_dg0(Q_(-20, "kcal/mol"))
11+
captured = capsys.readouterr()
12+
assert captured.out == "-20.0 kcal/mol\n"
13+
14+
15+
def test_print_kd0(capsys):
16+
_print_kd0(Q_(1e-6, "M"))
17+
captured = capsys.readouterr()
18+
assert captured.out == "1.00 uM\n"
19+
20+
_print_kd0(Q_(1e-10, "M"))
21+
captured = capsys.readouterr()
22+
assert captured.out == "100. pM\n"
23+
24+
_print_kd0(Q_(1e-2, "M"))
25+
captured = capsys.readouterr()
26+
assert captured.out == "10.0 mM\n"
27+
28+
_print_kd0(Q_(1e2, "M"))
29+
captured = capsys.readouterr()
30+
assert captured.out == "100. M\n"

0 commit comments

Comments
 (0)