Skip to content

Commit 02a18bb

Browse files
committed
test(core): add atomic pytest unit tests for the results module
1 parent 7becd02 commit 02a18bb

1 file changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
import json
2+
from types import SimpleNamespace
3+
from unittest.mock import MagicMock
4+
5+
import numpy as np
6+
7+
import CodeEntropy.results.reporter as reporter
8+
from CodeEntropy.results.reporter import ResultsReporter
9+
10+
11+
class _FakeTable:
12+
"""Tiny Table stand-in: records columns and rows added."""
13+
14+
def __init__(self, *args, **kwargs):
15+
self.args = args
16+
self.kwargs = kwargs
17+
self.columns = []
18+
self.rows = []
19+
20+
def add_column(self, *args, **kwargs):
21+
self.columns.append((args, kwargs))
22+
23+
def add_row(self, *cells):
24+
self.rows.append(cells)
25+
26+
27+
def test_clean_residue_name_removes_dash_like_characters():
28+
assert ResultsReporter.clean_residue_name("A-LA") == "ALA"
29+
assert ResultsReporter.clean_residue_name("A–LA") == "ALA"
30+
assert ResultsReporter.clean_residue_name("A—LA") == "ALA"
31+
assert ResultsReporter.clean_residue_name(123) == "123"
32+
33+
34+
def test_add_results_data_appends_molecule_tuple():
35+
rr = ResultsReporter()
36+
rr.add_results_data(group_id=7, level="residue", entropy_type="vib", value=1.23)
37+
38+
assert rr.molecule_data == [(7, "residue", "vib", 1.23)]
39+
40+
41+
def test_add_residue_data_cleans_resname_and_converts_ndarray_count_to_list():
42+
rr = ResultsReporter()
43+
44+
rr.add_residue_data(
45+
group_id=1,
46+
resname="A-LA",
47+
level="residue",
48+
entropy_type="conf",
49+
frame_count=np.array([1, 2, 3], dtype=int),
50+
value=9.0,
51+
)
52+
53+
assert rr.residue_data == [[1, "ALA", "residue", "conf", [1, 2, 3], 9.0]]
54+
55+
56+
def test_add_residue_data_keeps_non_ndarray_count_as_is():
57+
rr = ResultsReporter()
58+
59+
rr.add_residue_data(
60+
group_id=2,
61+
resname="GLY",
62+
level="residue",
63+
entropy_type="conf",
64+
frame_count=5,
65+
value=3.14,
66+
)
67+
68+
assert rr.residue_data == [[2, "GLY", "residue", "conf", 5, 3.14]]
69+
70+
71+
def test_add_group_label_stores_metadata_with_optionals():
72+
rr = ResultsReporter()
73+
rr.add_group_label(group_id="G0", label="protein", residue_count=10, atom_count=100)
74+
75+
assert rr.group_labels["G0"] == {
76+
"label": "protein",
77+
"residue_count": 10,
78+
"atom_count": 100,
79+
}
80+
81+
82+
def test_save_dataframes_as_json_writes_expected_payload(tmp_path):
83+
rr = ResultsReporter()
84+
85+
molecule_df = MagicMock()
86+
residue_df = MagicMock()
87+
88+
molecule_df.to_dict.return_value = [{"a": 1}]
89+
residue_df.to_dict.return_value = [{"b": 2}]
90+
91+
out = tmp_path / "out.json"
92+
rr.save_dataframes_as_json(
93+
molecule_df=molecule_df, residue_df=residue_df, output_file=str(out)
94+
)
95+
96+
data = json.loads(out.read_text())
97+
assert data == {"molecule_data": [{"a": 1}], "residue_data": [{"b": 2}]}
98+
99+
molecule_df.to_dict.assert_called_once_with(orient="records")
100+
residue_df.to_dict.assert_called_once_with(orient="records")
101+
102+
103+
def test_log_tables_calls_each_internal_table_renderer(monkeypatch):
104+
rr = ResultsReporter()
105+
106+
mol_spy = MagicMock()
107+
res_spy = MagicMock()
108+
grp_spy = MagicMock()
109+
110+
monkeypatch.setattr(rr, "_log_molecule_table", mol_spy)
111+
monkeypatch.setattr(rr, "_log_residue_table", res_spy)
112+
monkeypatch.setattr(rr, "_log_group_label_table", grp_spy)
113+
114+
rr.log_tables()
115+
116+
mol_spy.assert_called_once()
117+
res_spy.assert_called_once()
118+
grp_spy.assert_called_once()
119+
120+
121+
def test_log_molecule_table_returns_early_when_no_data(monkeypatch):
122+
rr = ResultsReporter()
123+
124+
fake_console = SimpleNamespace(print=MagicMock())
125+
monkeypatch.setattr(reporter, "console", fake_console)
126+
monkeypatch.setattr(reporter, "Table", _FakeTable)
127+
128+
rr._log_molecule_table()
129+
130+
fake_console.print.assert_not_called()
131+
132+
133+
def test_log_residue_table_returns_early_when_no_data(monkeypatch):
134+
rr = ResultsReporter()
135+
136+
fake_console = SimpleNamespace(print=MagicMock())
137+
monkeypatch.setattr(reporter, "console", fake_console)
138+
monkeypatch.setattr(reporter, "Table", _FakeTable)
139+
140+
rr._log_residue_table()
141+
142+
fake_console.print.assert_not_called()
143+
144+
145+
def test_log_group_label_table_returns_early_when_no_labels(monkeypatch):
146+
rr = ResultsReporter()
147+
148+
fake_console = SimpleNamespace(print=MagicMock())
149+
monkeypatch.setattr(reporter, "console", fake_console)
150+
monkeypatch.setattr(reporter, "Table", _FakeTable)
151+
152+
rr._log_group_label_table()
153+
154+
fake_console.print.assert_not_called()
155+
156+
157+
def test_log_molecule_table_builds_rows_and_prints_table(monkeypatch):
158+
rr = ResultsReporter()
159+
rr.molecule_data = [
160+
(1, "residue", "conf", 1.0),
161+
(2, "polymer", "vib", 2.0),
162+
]
163+
164+
fake_console = SimpleNamespace(print=MagicMock())
165+
monkeypatch.setattr(reporter, "console", fake_console)
166+
monkeypatch.setattr(reporter, "Table", _FakeTable)
167+
168+
rr._log_molecule_table()
169+
170+
fake_console.print.assert_called_once()
171+
table = fake_console.print.call_args.args[0]
172+
173+
assert isinstance(table, _FakeTable)
174+
# 4 columns defined, 2 rows added
175+
assert len(table.columns) == 4
176+
assert len(table.rows) == 2
177+
# cells were stringified
178+
assert table.rows[0] == ("1", "residue", "conf", "1.0")
179+
180+
181+
def test_log_residue_table_builds_rows_and_prints_table(monkeypatch):
182+
rr = ResultsReporter()
183+
rr.residue_data = [
184+
[1, "ALA", "residue", "conf", [1, 2], 9.0],
185+
]
186+
187+
fake_console = SimpleNamespace(print=MagicMock())
188+
monkeypatch.setattr(reporter, "console", fake_console)
189+
monkeypatch.setattr(reporter, "Table", _FakeTable)
190+
191+
rr._log_residue_table()
192+
193+
fake_console.print.assert_called_once()
194+
table = fake_console.print.call_args.args[0]
195+
196+
assert isinstance(table, _FakeTable)
197+
# 6 columns defined, 1 row added
198+
assert len(table.columns) == 6
199+
assert len(table.rows) == 1
200+
assert table.rows[0] == ("1", "ALA", "residue", "conf", "[1, 2]", "9.0")
201+
202+
203+
def test_log_group_label_table_adds_rows_for_each_label_and_prints(monkeypatch):
204+
rr = ResultsReporter()
205+
rr.group_labels = {
206+
7: {"label": "protein", "residue_count": 10, "atom_count": 100},
207+
8: {"label": "water", "residue_count": None, "atom_count": None},
208+
}
209+
210+
fake_console = SimpleNamespace(print=MagicMock())
211+
monkeypatch.setattr(reporter, "console", fake_console)
212+
monkeypatch.setattr(reporter, "Table", _FakeTable)
213+
214+
rr._log_group_label_table()
215+
216+
fake_console.print.assert_called_once()
217+
table = fake_console.print.call_args.args[0]
218+
219+
assert isinstance(table, _FakeTable)
220+
assert len(table.columns) == 4
221+
assert len(table.rows) == 2
222+
223+
assert table.rows[0] == ("7", "protein", "10", "100")
224+
assert table.rows[1] == ("8", "water", "None", "None")

0 commit comments

Comments
 (0)