Skip to content

Commit

Permalink
Add io tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralf-meyer committed Nov 11, 2023
1 parent 69d5b63 commit 4528a42
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
9 changes: 1 addition & 8 deletions molSimplify/Scripts/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ def readdict(fname):


def readdict_sub(fname):
# Constructor
# @param self The object pointer
# @param subname The name of the substrate
class substrate:
def __init__(self, subname):
self.subname = subname
d = dict()
with open(fname, 'r') as f:
txt = f.read()
Expand All @@ -137,8 +131,7 @@ def __init__(self, subname):
vv.append(vvs)
else:
vv += vvs
# dict keys are instances of the substrate class
d[substrate(key)] = vv
d[key] = vv
return d

# Get ligands in dictionary
Expand Down
65 changes: 64 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import numpy as np
from molSimplify.Scripts.io import lig_load
from molSimplify.Scripts.io import (lig_load,
core_load,
printgeoms,
getsubstrates,
readdict_sub,
)
from importlib_resources import files as resource_files


def test_lig_load(resource_path_root):
Expand All @@ -18,3 +24,60 @@ def test_lig_load(resource_path_root):
print(ref.coordsvect())
np.testing.assert_allclose(mol.coordsvect(), ref.coordsvect())
assert mol.charge == ref.charge


def test_core_load():
file = str(resource_files("molSimplify").joinpath("Cores/ferrcore.xyz"))
core, emsg = core_load(file)
# Assert that the error message is empty
assert not emsg
core.convert2mol3D()
assert core.make_formula(latex=False) == "Fe1F1C10H9"

file = str(resource_files("molSimplify").joinpath("Cores/ferrocene.mol"))
core, emsg = core_load(file)
# Assert that the error message is empty
assert not emsg
core.convert2mol3D()
assert core.make_formula(latex=False) == "Fe1C10H10"


def test_printgeoms(capsys):
printgeoms()
captured = capsys.readouterr()

ref = (
"Coordination: 1, geometry: none,\t short name: no \n"
"Coordination: 2, geometry: linear,\t short name: li \n"
"Coordination: 3, geometry: trigonal_planar,\t short name: tpl \n"
"Coordination: 4, geometry: square_planar,\t short name: sqp \n"
"Coordination: 4, geometry: tetrahedral,\t short name: thd \n"
"Coordination: 5, geometry: square_pyramidal,\t short name: spy \n"
"Coordination: 5, geometry: trigonal_bipyramidal,\t short name: tbp \n"
"Coordination: 6, geometry: octahedral,\t short name: oct \n"
"Coordination: 6, geometry: trigonal_prismatic,\t short name: tpr \n"
"Coordination: 7, geometry: pentagonal_bipyramidal,\t short name: pbp \n"
"Coordination: 8, geometry: square_antiprismatic,\t short name: sqap \n\n"
)
assert captured.out == ref


def test_readdict_sub():
file = resource_files("molSimplify").joinpath("Substrates/substrates.dict")
sub_dict = readdict_sub(file)
assert sub_dict["methane"] == ['methane.xyz', 'ch4', '1', ['inter'],
['N'], ['0', '#', 'BDH', '=', '104.9(0.1)']]
assert sub_dict["ethane"] == ['ethane.xyz', 'c2h6', '2', ['inter'],
['B'], ['0', '#', 'BDH', '=', '101.1(0.4)']]


def test_getsubstrates():
subs = getsubstrates()
ref = (
"acetaldehyde acetylene benzene biphenyl bromobenzene cumene "
"cyclohexene dha diphenylmethane estrogen ethanal ethane ethene "
"ethylene fluorene formaldehyde formicacid iodobenzene methanal "
"methane methanoicacid methanol methylazide n-quinolinylbutyramidate "
"n2 phenyl propane propene propylene propyne tert-butane toluene triazole xanthene"
)
assert subs == ref

0 comments on commit 4528a42

Please sign in to comment.