Skip to content

Commit

Permalink
Switch test resources to pytest-resource-path
Browse files Browse the repository at this point in the history
  • Loading branch information
ralf-meyer committed Oct 20, 2023
1 parent c080e8c commit 891afd6
Show file tree
Hide file tree
Showing 454 changed files with 432 additions and 408 deletions.
1 change: 1 addition & 0 deletions devtools/conda-envs/mols.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
- pip
- pip:
- pytest
- pytest-resource-path

# Optional
# GUI / plotting
Expand Down
1 change: 1 addition & 0 deletions devtools/conda-envs/mols_minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dependencies:
- pip
- pip:
- pytest
- pytest-resource-path
3 changes: 1 addition & 2 deletions molSimplify/Classes/mol3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -2693,8 +2693,7 @@ def readfromxyz(self, filename: str, ligand_unique_id=False, read_final_optim_st
amassdict = globs.amass()
self.graph = []
self.xyzfile = filename
fname = filename.split('.xyz')[0]
with open(fname + '.xyz', 'r') as f:
with open(filename, 'r') as f:
s = f.read().splitlines()
try:
atom_count = int(s[0])
Expand Down
8 changes: 4 additions & 4 deletions molSimplify/Scripts/qcgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ def molcbasis(strfiles, basistyp):
# collect elements by rows in the periodic table
for i in range(0, len(strfiles)):
temp = mol3D()
temp.readfromxyz(strfiles[i])
temp.readfromxyz(f"{strfiles[i]}.xyz")
for atom in temp.getAtoms():
atno = atom.atno
sym = atom.symbol()
Expand Down Expand Up @@ -1365,7 +1365,7 @@ def molcras2s(strfiles):
ras2s = []
for i in range(0, len(strfiles)):
temp = mol3D()
temp.readfromxyz(strfiles[i])
temp.readfromxyz(f"{strfiles[i]}.xyz")
metal_ind = temp.findMetal()
ras2 = 0
for ind in metal_ind:
Expand Down Expand Up @@ -1399,7 +1399,7 @@ def molcnactels(strfiles, oxnum):
nactels = []
for i in range(0, len(strfiles)):
temp = mol3D()
temp.readfromxyz(strfiles[i])
temp.readfromxyz(f"{strfiles[i]}.xyz")
metal_ind = temp.findMetal()
nactel = 0
for ind in metal_ind:
Expand Down Expand Up @@ -1432,7 +1432,7 @@ def molcfrozens(strfiles):
for i in range(0, len(strfiles)):
frozen = 0
temp = mol3D()
temp.readfromxyz(strfiles[i])
temp.readfromxyz(f"{strfiles[i]}.xyz")
for atom in temp.getAtoms():
atno = atom.atno
if atno > 2 and atno <= 10:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["pre-commit"]
dev = ["pytest", "pytest-resource-path", "pre-commit"]
docs = [
"beautifulsoup4",
"sphinx",
Expand Down
97 changes: 37 additions & 60 deletions tests/helperFuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from molSimplify.Classes.globalvars import (dict_oneempty_check_st,
oneempty_angle_ref)
from molSimplify.Classes.mol3D import mol3D
from pkg_resources import resource_filename, Requirement
from typing import Dict
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -163,12 +162,13 @@ def compareOG(xyz1, xyz2, thresh: float) -> bool:
return passOG


def runtest_num_atoms_in_xyz(tmpdir, xyzfile):
def runtest_num_atoms_in_xyz(tmpdir, resource_path_root, xyzfile):
file_path = resource_path_root / "refs" / f"{xyzfile}.xyz"
xyz_file1 = mol3D()
xyz_file1.readfromxyz('tests/refs/' + xyzfile + '.xyz')
xyz_file1.readfromxyz(file_path)
xyz_file1.getNumAtoms()

with open('tests/refs/' + xyzfile + '.xyz', 'r') as f:
with open(file_path, 'r') as f:
xyz_file2 = f.readlines()
num_atoms = int(xyz_file2[0])

Expand Down Expand Up @@ -396,12 +396,11 @@ def compare_qc_input(inp, inp_ref):
return passQcInputCheck


def runtest(tmpdir, name, threshMLBL, threshLG, threshOG, seed=31415):
def runtest(tmpdir, resource_path_root, name, threshMLBL, threshLG, threshOG, seed=31415):
# Set seeds to eliminate randomness from test results
random.seed(seed)
np.random.seed(seed)
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
infile = resource_path_root / "inputs" / f"{name}.in"
newinfile, myjobdir = parse4test(infile, tmpdir)
args = ['main.py', '-i', newinfile]
with working_directory(tmpdir):
Expand All @@ -418,12 +417,9 @@ def runtest(tmpdir, name, threshMLBL, threshLG, threshOG, seed=31415):
if 'molcas' in molsim_data.lower():
output_qcin = myjobdir + '/molcas.input'

ref_xyz = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.xyz")
ref_report = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.report")
ref_qcin = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.qcin")
ref_xyz = resource_path_root / "refs" / f"{name}.xyz"
ref_report = resource_path_root / "refs" / f"{name}.report"
ref_qcin = resource_path_root / "refs" / f"{name}.qcin"

print("Test input file: ", newinfile)
print("Test output files are generated in ", myjobdir)
Expand All @@ -443,7 +439,7 @@ def runtest(tmpdir, name, threshMLBL, threshLG, threshOG, seed=31415):
return [passNumAtoms, passMLBL, passLG, passOG, pass_report, pass_qcin]


def runtest_slab(tmpdir, name, threshOG, extra_files=None):
def runtest_slab(tmpdir, resource_path_root, name, threshOG, extra_files=None):
"""
Performs test for slab builder.
Expand All @@ -456,28 +452,25 @@ def runtest_slab(tmpdir, name, threshOG, extra_files=None):
axis : threshOG
tolerance for RMSD comparison of overall geometries.
"""
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
infile = resource_path_root / "inputs" / f"{name}.in"
newinfile, _ = parse4test(infile, tmpdir)
if extra_files is not None:
for file_name in extra_files:
file_path = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{file_name}")
file_path = resource_path_root / "inputs" / f"{file_name}"
shutil.copyfile(file_path, tmpdir / file_name)
args = ['main.py', '-i', newinfile]
with working_directory(tmpdir):
startgen(args, False, False)
output_xyz = str(tmpdir / 'slab' / 'super332.xyz')
ref_xyz = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.xyz")
output_xyz = tmpdir / 'slab' / 'super332.xyz'
ref_xyz = resource_path_root / "refs" / f"{name}.xyz"
print("Output xyz file: ", output_xyz)
pass_xyz = compareGeo(output_xyz, ref_xyz, threshMLBL=0, threshLG=0,
threshOG=threshOG, slab=True)
[passNumAtoms, passOG] = pass_xyz
return [passNumAtoms, passOG]


def runtest_molecule_on_slab(tmpdir, name, threshOG, extra_files=None):
def runtest_molecule_on_slab(tmpdir, resource_path_root, name, threshOG, extra_files=None):
"""
Performs test for slab builder with a CO molecule adsorbed.
Expand All @@ -490,35 +483,29 @@ def runtest_molecule_on_slab(tmpdir, name, threshOG, extra_files=None):
axis : threshOG
tolerance for RMSD comparison of overall geometries.
"""
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
infile = resource_path_root / "inputs" / f"{name}.in"
newinfile, _ = parse4test(infile, tmpdir, extra_args={
'-unit_cell': 'slab.xyz', '-target_molecule': 'co.xyz'})
if extra_files is not None:
for file_name in extra_files:
file_path = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{file_name}")
file_path = resource_path_root / "inputs" / f"{file_name}"
shutil.copyfile(file_path, tmpdir / file_name)
args = ['main.py', '-i', newinfile]
with working_directory(tmpdir):
startgen(args, False, False)
output_xyz = str(tmpdir / 'loaded_slab' / 'loaded.xyz')
ref_xyz = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.xyz")
output_xyz = tmpdir / 'loaded_slab' / 'loaded.xyz'
ref_xyz = resource_path_root / "refs" / f"{name}.xyz"
print("Output xyz file: ", output_xyz)
pass_xyz = compareGeo(output_xyz, ref_xyz, threshMLBL=0, threshLG=0,
threshOG=threshOG, slab=True)
[passNumAtoms, passOG] = pass_xyz
return [passNumAtoms, passOG]


def runtestgeo(tmpdir, name, thresh, deleteH=True, geo_type="oct"):
initgeo = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/geocheck/{name}/init.xyz")
optgeo = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/geocheck/{name}/opt.xyz")
refjson = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/geocheck/{name}/ref.json")
def runtestgeo(tmpdir, resource_path_root, name, thresh, deleteH=True, geo_type="oct"):
initgeo = resource_path_root / "inputs" / "geocheck" / name / "init.xyz"
optgeo = resource_path_root / "inputs" / "geocheck" / name / "opt.xyz"
refjson = resource_path_root / "refs" / "geocheck" / name / "ref.json"
mymol = mol3D()
mymol.readfromxyz(optgeo)
init_mol = mol3D()
Expand All @@ -543,11 +530,9 @@ def runtestgeo(tmpdir, name, thresh, deleteH=True, geo_type="oct"):
return passGeo


def runtestgeo_optonly(tmpdir, name, thresh, deleteH=True, geo_type="oct"):
optgeo = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/geocheck/{name}/opt.xyz")
refjson = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/geocheck/{name}/ref.json")
def runtestgeo_optonly(tmpdir, resource_path_root, name, thresh, deleteH=True, geo_type="oct"):
optgeo = resource_path_root / "inputs" / "geocheck" / name / "opt.xyz"
refjson = resource_path_root / "refs" / "geocheck" / name / "ref.json"
mymol = mol3D()
mymol.readfromxyz(optgeo)
if geo_type == "oct":
Expand All @@ -561,9 +546,8 @@ def runtestgeo_optonly(tmpdir, name, thresh, deleteH=True, geo_type="oct"):
raise NotImplementedError('Only octahedral geometries supported for now')


def runtestNoFF(tmpdir, name, threshMLBL, threshLG, threshOG):
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
def runtestNoFF(tmpdir, resource_path_root, name, threshMLBL, threshLG, threshOG):
infile = resource_path_root / "inputs" / f"{name}.in"
newinfile, myjobdir = parse4testNoFF(infile, tmpdir)
[passNumAtoms, passMLBL, passLG, passOG, pass_report,
pass_qcin] = [True, True, True, True, True, True]
Expand All @@ -581,12 +565,9 @@ def runtestNoFF(tmpdir, name, threshMLBL, threshLG, threshOG):
output_qcin = myjobdir + '/orca.in'
if 'molcas' in molsim_data.lower():
output_qcin = myjobdir + '/molcas.input'
ref_xyz = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{newname}.xyz")
ref_report = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{newname}.report")
ref_qcin = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.qcin")
ref_xyz = resource_path_root / "refs" / f"{newname}.xyz"
ref_report = resource_path_root / "refs" / f"{newname}.report"
ref_qcin = resource_path_root / "refs" / f"{name}.qcin"
print("Test input file: ", newinfile)
print("Test output files are generated in ", myjobdir)
print("Output xyz file: ", output_xyz)
Expand All @@ -606,12 +587,11 @@ def runtestNoFF(tmpdir, name, threshMLBL, threshLG, threshOG):
return [passNumAtoms, passMLBL, passLG, passOG, pass_report, pass_qcin]


def runtest_reportonly(tmpdir, name, seed=31415):
def runtest_reportonly(tmpdir, resource_path_root, name, seed=31415):
# Set seeds to eliminate randomness from test results
random.seed(seed)
np.random.seed(seed)
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
infile = resource_path_root / "inputs" / f"{name}.in"
# Copy the input file to the temporary folder
shutil.copy(infile, tmpdir/f'{name}_reportonly.in')
# Add the report only flag
Expand All @@ -624,8 +604,7 @@ def runtest_reportonly(tmpdir, name, seed=31415):
with working_directory(tmpdir):
startgen(args, False, False)
output_report = myjobdir + '/' + name + '_reportonly.report'
ref_report = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}.report")
ref_report = resource_path_root / "refs" / f"{name}.report"
# Copy the reference report to the temporary folder
shutil.copy(ref_report, tmpdir/f'{name}_ref.report')
with open(tmpdir/f'{name}_ref.report', 'r') as f:
Expand All @@ -643,17 +622,15 @@ def runtest_reportonly(tmpdir, name, seed=31415):
return pass_report


def runtestMulti(tmpdir, name, threshMLBL, threshLG, threshOG):
infile = resource_filename(Requirement.parse(
"molSimplify"), f"tests/inputs/{name}.in")
def runtestMulti(tmpdir, resource_path_root, name, threshMLBL, threshLG, threshOG):
infile = resource_path_root / "inputs" / f"{name}.in"
newinfile, myjobdir = parse4test(infile, tmpdir, True)
args = ['main.py', '-i', newinfile]
with working_directory(tmpdir):
startgen(args, False, False)
print("Test input file: ", newinfile)
print("Test output files are generated in ", myjobdir)
refdir = resource_filename(Requirement.parse(
"molSimplify"), f"tests/refs/{name}")
refdir = resource_path_root / "refs" / name
[passMultiFileCheck, myfiles] = checkMultiFileGen(myjobdir, refdir)
pass_structures = []
if not passMultiFileCheck:
Expand Down
8 changes: 2 additions & 6 deletions tests/informatics/test_coulomb_analyze.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import numpy as np
from molSimplify.Classes.mol3D import mol3D
from pkg_resources import resource_filename, Requirement
from molSimplify.Informatics.coulomb_analyze import create_coulomb_matrix


def test_create_coulomb_matrix():
xyz_file = resource_filename(
Requirement.parse("molSimplify"),
"tests/inputs/cr3_f6_optimization.xyz"
)
def test_create_coulomb_matrix(resource_path_root):
xyz_file = resource_path_root / "inputs" / "cr3_f6_optimization.xyz"
mol = mol3D()
mol.readfromxyz(xyz_file)
cm = create_coulomb_matrix(mol)
Expand Down
48 changes: 0 additions & 48 deletions tests/inputs/geocheck/triplebond_linear_broken/opt.xyz

This file was deleted.

1 change: 0 additions & 1 deletion tests/refs/geocheck/compact_bonding/ref.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/refs/geocheck/triplebond_linear_broken/ref.json

This file was deleted.

17 changes: 8 additions & 9 deletions tests/runtest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from molSimplify.Scripts.generator import startgen
from pkg_resources import resource_filename, Requirement

infile = resource_filename(Requirement.parse(
"molSimplify"), "tests/inputs/example_1_noff.in")
args = ['main.py', '-i', infile]
startgen(args, False, False)
infile = resource_filename(Requirement.parse(
"molSimplify"), "tests/inputs/example_1.in")
args = ['main.py', '-i', infile]
startgen(args, False, False)

def runtest(resource_path_root):
infile = resource_path_root / "inputs" / "example_1_noff.in"
args = ['main.py', '-i', infile]
startgen(args, False, False)
infile = resource_path_root / "inputs" / "example_1.in"
args = ['main.py', '-i', infile]
startgen(args, False, False)
Loading

0 comments on commit 891afd6

Please sign in to comment.