Skip to content

Commit d45d258

Browse files
committed
adding tests for chempy models
1 parent bb36778 commit d45d258

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from chempy import Atom, Bond
2+
import pytest
3+
4+
from chempy.models import Indexed, Connected
5+
6+
7+
@pytest.fixture
8+
def connected_model() -> Connected:
9+
connected_model = Connected()
10+
11+
for i in range(3):
12+
atom = Atom()
13+
atom.symbol = "H" if i == 0 else "He"
14+
atom.chain = "AB" if i == 0 else "BA"
15+
16+
atom.coord = [float(i), 0., 0.]
17+
connected_model.add_atom(atom)
18+
19+
for i in range(len(connected_model.atom)):
20+
connected_model.bond.append([])
21+
22+
bond = Bond()
23+
bond.index = [i, i + 1]
24+
bond.order = i
25+
# note two refs to same object
26+
connected_model.bond[bond.index[0]].append(bond)
27+
connected_model.bond[bond.index[1]].append(bond)
28+
return connected_model
29+
30+
31+
def test_base_model_methods(connected_model):
32+
assert connected_model.nAtom == 3
33+
assert connected_model.nBond == 6
34+
assert connected_model.get_residues() == [(0, 1), (1, 3)]
35+
assert connected_model.get_coord_list() == [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]]
36+
assert connected_model.get_mass() == 9.013144
37+
38+
assert connected_model.index is None
39+
connected_model.update_index()
40+
assert sorted(list(connected_model.index.values())) == [0, 1, 2]
41+
42+
43+
def test_add_atom(connected_model):
44+
new_atom = Atom()
45+
new_atom.symbol = "Ne"
46+
47+
assert len(connected_model.atom) == 3
48+
connected_model.update_index()
49+
previos_last_atom = connected_model.atom[0]
50+
51+
connected_model.add_atom(new_atom)
52+
assert connected_model.atom[-1] is new_atom
53+
assert connected_model.atom[-2] is not previos_last_atom
54+
55+
assert len(connected_model.atom) == 4
56+
57+
58+
def test_convert_to_indexed(connected_model):
59+
indexed_model = connected_model.convert_to_indexed()
60+
assert isinstance(indexed_model, Indexed)
61+
assert len(indexed_model.atom) == 3
62+
assert len(indexed_model.atom) == 3
63+
# maybe a bug ?
64+
65+
assert connected_model.atom == []
66+
assert connected_model.bond == []
67+
assert connected_model.index == None
68+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from chempy import Atom, Bond
2+
import pytest
3+
4+
from chempy.models import Indexed, Connected
5+
6+
7+
@pytest.fixture
8+
def indexed_model() -> Indexed:
9+
indexed_model = Indexed()
10+
11+
for i in range(3):
12+
atom = Atom()
13+
atom.symbol = "H" if i == 0 else "He"
14+
atom.chain = "AB" if i == 0 else "BA"
15+
16+
atom.coord = [float(i), 0., 0.]
17+
indexed_model.add_atom(atom)
18+
19+
bond = Bond()
20+
bond.index = [0, i]
21+
bond.order = i
22+
indexed_model.add_bond(bond)
23+
24+
return indexed_model
25+
26+
27+
def test_base_model_methods(indexed_model):
28+
assert indexed_model.nAtom == 3
29+
assert indexed_model.nBond == 3
30+
assert indexed_model.get_residues() == [(0, 1), (1, 3)]
31+
assert indexed_model.get_coord_list() == [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]]
32+
assert indexed_model.get_mass() == 9.013144
33+
34+
assert indexed_model.index is None
35+
indexed_model.update_index()
36+
assert sorted(list(indexed_model.index.values())) == [0, 1, 2]
37+
38+
39+
def test_indexed_merge(indexed_model):
40+
other_indexed_model = Indexed()
41+
42+
atom = Atom()
43+
atom.symbol = "Ne"
44+
atom.chain = "F"
45+
46+
atom.coord = [0., 0., 7.]
47+
other_indexed_model.add_atom(atom)
48+
49+
bond = Bond()
50+
bond.index = [1, 1]
51+
bond.order = 1
52+
other_indexed_model.add_bond(bond)
53+
54+
indexed_model.update_index()
55+
assert sorted(list(indexed_model.index.values())) == [0, 1, 2]
56+
57+
indexed_model.merge(other_indexed_model)
58+
59+
assert sorted(list(indexed_model.index.values())) == [0, 1, 2, 3]
60+
assert len(indexed_model.atom) == 4
61+
assert len(indexed_model.bond) == 4
62+
63+
assert other_indexed_model.index is None
64+
assert other_indexed_model.bond == []
65+
assert other_indexed_model.bond == []
66+
67+
68+
def test_add_atom(indexed_model):
69+
new_atom = Atom()
70+
new_atom.symbol = "Ne"
71+
72+
assert len(indexed_model.atom) == 3
73+
indexed_model.update_index()
74+
previos_last_atom = indexed_model.atom[0]
75+
76+
indexed_model.add_atom(new_atom)
77+
assert indexed_model.atom[-1] is new_atom
78+
assert indexed_model.atom[-2] is not previos_last_atom
79+
80+
assert len(indexed_model.atom) == 4
81+
82+
83+
def test_convert_to_connected(indexed_model):
84+
connected_model = indexed_model.convert_to_connected()
85+
assert isinstance(connected_model, Connected)
86+
assert len(connected_model.bond) == 3
87+
assert connected_model.bond[0] is connected_model.bond[0]
88+
# maybe a bug ?
89+
90+
assert indexed_model.atom == []
91+
assert indexed_model.bond == []
92+
assert indexed_model.index == None
93+
94+
95+
def test_get_internal_tuples(indexed_model):
96+
assert indexed_model.get_internal_tuples() == [(0,), (0, 0), (1, 0, 0)]
97+
98+

0 commit comments

Comments
 (0)