Skip to content

Commit e81a60f

Browse files
committed
Fix failing unit tests for DihedralAnalysis._get_dihedrals by:
- Instantiating a DihedralAnalysis object before calling `_get_dihedrals` - Correcting test expectations to match actual behavior: - united_atom level now asserts returned `.atoms` values - residue level tests now validate atom-group construction - residue tests with < 4 residues now expect empty list
1 parent 35cdf0b commit e81a60f

1 file changed

Lines changed: 49 additions & 28 deletions

File tree

tests/test_CodeEntropy/test_dihedral_tools.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,78 @@ class TestDihedralAnalysis(BaseTestCase):
1111

1212
def setUp(self):
1313
super().setUp()
14+
self.analysis = DihedralAnalysis()
1415

1516
def test_get_dihedrals_united_atom(self):
1617
"""
17-
Test `get_dihedrals` for 'united_atom' level.
18-
Ensures it returns the dihedrals directly from the data container.
18+
Test `_get_dihedrals` for 'united_atom' level.
19+
20+
The function should:
21+
- read dihedrals from `data_container.dihedrals`
22+
- extract `.atoms` from each dihedral
23+
- return a list of atom groups
24+
25+
Expected behavior:
26+
If dihedrals = [d1, d2, d3] and each dihedral has an `.atoms`
27+
attribute, then the returned list must be:
28+
[d1.atoms, d2.atoms, d3.atoms]
1929
"""
2030
data_container = MagicMock()
21-
mock_dihedrals = ["d1", "d2", "d3"]
22-
data_container.dihedrals = mock_dihedrals
2331

24-
result = DihedralAnalysis._get_dihedrals(data_container, level="united_atom")
25-
self.assertEqual(result, mock_dihedrals)
32+
# Mock dihedral objects with `.atoms`
33+
d1 = MagicMock()
34+
d1.atoms = "atoms1"
35+
d2 = MagicMock()
36+
d2.atoms = "atoms2"
37+
d3 = MagicMock()
38+
d3.atoms = "atoms3"
39+
40+
data_container.dihedrals = [d1, d2, d3]
41+
42+
result = self.analysis._get_dihedrals(data_container, level="united_atom")
43+
44+
self.assertEqual(result, ["atoms1", "atoms2", "atoms3"])
2645

2746
def test_get_dihedrals_residue(self):
2847
"""
29-
Test `get_dihedrals` for 'residue' level with 5 residues.
30-
Mocks bonded atom selections and verifies that dihedrals are constructed.
48+
Test `_get_dihedrals` for 'residue' level with 5 residues.
49+
50+
The implementation:
51+
- iterates over residues 4 → N
52+
- for each, selects 4 bonded atom groups
53+
- merges them using __add__ to form a single atom_group
54+
- appends to result list
55+
56+
For 5 residues (0–4), two dihedral groups should be created.
57+
Expected:
58+
- result of length 2
59+
- each item equal to the merged mock atom group
3160
"""
3261
data_container = MagicMock()
33-
data_container.residues = [0, 1, 2, 3, 4] # 5 residues
62+
data_container.residues = [0, 1, 2, 3, 4]
3463

35-
# Mock select_atoms to return atom groups with .dihedral
36-
mock_dihedral = MagicMock()
3764
mock_atom_group = MagicMock()
3865
mock_atom_group.__add__.return_value = mock_atom_group
39-
mock_atom_group.dihedral = mock_dihedral
66+
67+
# Every MDAnalysis selection returns the same mock atom group
4068
data_container.select_atoms.return_value = mock_atom_group
4169

42-
result = DihedralAnalysis._get_dihedrals(data_container, level="residue")
70+
result = self.analysis._get_dihedrals(data_container, level="residue")
4371

44-
# Should create 2 dihedrals for 5 residues (residues 0–3 and 1–4)
4572
self.assertEqual(len(result), 2)
46-
self.assertTrue(all(d == mock_dihedral for d in result))
73+
self.assertTrue(all(r is mock_atom_group for r in result))
4774

4875
def test_get_dihedrals_no_residue(self):
4976
"""
50-
Test `get_dihedrals` for 'residue' level with 3 residues.
51-
Mocks bonded atom selections and verifies that dihedrals are constructed.
52-
"""
77+
Test `_get_dihedrals` for 'residue' level when fewer than
78+
4 residues exist (here: 3 residues).
5379
80+
Expected:
81+
- The function returns an empty list.
82+
"""
5483
data_container = MagicMock()
55-
data_container.residues = [0, 1, 2] # 3 residues
56-
57-
# Mock select_atoms to return atom groups with .dihedral
58-
mock_dihedral = MagicMock()
59-
mock_atom_group = MagicMock()
60-
mock_atom_group.__add__.return_value = mock_atom_group
61-
mock_atom_group.dihedral = mock_dihedral
62-
data_container.select_atoms.return_value = mock_atom_group
84+
data_container.residues = [0, 1, 2] # Only 3 residues → too few
6385

64-
result = DihedralAnalysis._get_dihedrals(data_container, level="residue")
86+
result = self.analysis._get_dihedrals(data_container, level="residue")
6587

66-
# Should result in no residue dihedrals
6788
self.assertEqual(result, [])

0 commit comments

Comments
 (0)