Skip to content

Commit 7becd02

Browse files
committed
test(core): add atomic pytest unit tests for the molecules module
1 parent 2ae41d7 commit 7becd02

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import logging
2+
from unittest.mock import MagicMock
3+
4+
import pytest
5+
6+
from CodeEntropy.molecules.grouping import MoleculeGrouper
7+
8+
9+
def _universe_with_fragments(fragments):
10+
u = MagicMock()
11+
u.atoms.fragments = fragments
12+
return u
13+
14+
15+
def _fragment(names):
16+
f = MagicMock()
17+
f.names = names
18+
return f
19+
20+
21+
def test_get_strategy_returns_each_callable():
22+
g = MoleculeGrouper()
23+
fn = g._get_strategy("each")
24+
assert callable(fn)
25+
assert fn == g._group_each
26+
27+
28+
def test_get_strategy_returns_molecules_callable():
29+
g = MoleculeGrouper()
30+
fn = g._get_strategy("molecules")
31+
assert callable(fn)
32+
assert fn == g._group_by_signature
33+
34+
35+
def test_get_strategy_raises_value_error_for_unknown():
36+
g = MoleculeGrouper()
37+
with pytest.raises(ValueError, match="Unknown grouping strategy"):
38+
g._get_strategy("nope")
39+
40+
41+
def test_fragments_returns_universe_fragments():
42+
g = MoleculeGrouper()
43+
frags = [MagicMock(), MagicMock()]
44+
u = _universe_with_fragments(frags)
45+
assert g._fragments(u) is frags
46+
47+
48+
def test_num_molecules_counts_fragments_length():
49+
g = MoleculeGrouper()
50+
u = _universe_with_fragments([MagicMock(), MagicMock(), MagicMock()])
51+
assert g._num_molecules(u) == 3
52+
53+
54+
def test_group_each_returns_one_group_per_molecule():
55+
g = MoleculeGrouper()
56+
u = _universe_with_fragments([MagicMock(), MagicMock(), MagicMock()])
57+
assert g._group_each(u) == {0: [0], 1: [1], 2: [2]}
58+
59+
60+
def test_signature_uses_atom_count_and_ordered_names():
61+
g = MoleculeGrouper()
62+
frag = _fragment(["H", "O", "H"])
63+
assert g._signature(frag) == (3, ("H", "O", "H"))
64+
65+
66+
def test_representative_id_first_seen_sets_rep_and_returns_candidate():
67+
g = MoleculeGrouper()
68+
cache = {}
69+
sig = (3, ("H", "O", "H"))
70+
rep = g._representative_id(cache, sig, candidate_id=5)
71+
assert rep == 5
72+
assert cache[sig] == 5
73+
74+
75+
def test_representative_id_returns_existing_rep_when_seen_before():
76+
g = MoleculeGrouper()
77+
sig = (3, ("H", "O", "H"))
78+
cache = {sig: 2}
79+
rep = g._representative_id(cache, sig, candidate_id=9)
80+
assert rep == 2
81+
assert cache[sig] == 2
82+
83+
84+
def test_group_by_signature_groups_identical_signatures_and_uses_first_id_as_group_id():
85+
g = MoleculeGrouper()
86+
f0 = _fragment(["H", "O", "H"])
87+
f1 = _fragment(["H", "O", "H"]) # same signature as f0
88+
f2 = _fragment(["C", "C", "H", "H"]) # different signature
89+
u = _universe_with_fragments([f0, f1, f2])
90+
91+
out = g._group_by_signature(u)
92+
93+
assert out == {0: [0, 1], 2: [2]}
94+
95+
96+
def test_group_by_signature_is_deterministic_for_first_seen_representative():
97+
g = MoleculeGrouper()
98+
f0 = _fragment(["B"])
99+
f1 = _fragment(["A"])
100+
f2 = _fragment(["B"])
101+
u = _universe_with_fragments([f0, f1, f2])
102+
103+
out = g._group_by_signature(u)
104+
105+
assert out[0] == [0, 2]
106+
assert out[1] == [1]
107+
108+
109+
def test_grouping_molecules_dispatches_each_and_logs_summary(caplog):
110+
g = MoleculeGrouper()
111+
u = _universe_with_fragments([MagicMock(), MagicMock()])
112+
113+
caplog.set_level(logging.INFO)
114+
out = g.grouping_molecules(u, "each")
115+
116+
assert out == {0: [0], 1: [1]}
117+
assert any("Number of molecule groups" in rec.message for rec in caplog.records)
118+
119+
120+
def test_grouping_molecules_dispatches_molecules_strategy():
121+
g = MoleculeGrouper()
122+
f0 = _fragment(["H", "O", "H"])
123+
f1 = _fragment(["H", "O", "H"])
124+
u = _universe_with_fragments([f0, f1])
125+
126+
out = g.grouping_molecules(u, "molecules")
127+
128+
assert out == {0: [0, 1]}

0 commit comments

Comments
 (0)