Skip to content

Commit

Permalink
Fix the tests such that it no longer require the working directory to…
Browse files Browse the repository at this point in the history
… be the root directory (#28)

Co-authored-by: William (Zhiyi) Wu <zwu@exscientia.co.uk>
  • Loading branch information
xiki-tempula and xiki-tempula authored Sep 19, 2023
1 parent d5e28e8 commit 20da673
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 56 deletions.
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Align/test_decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from BioSimSpace.Sandpit.Exscientia.Align._decouple import decouple
import BioSimSpace.Sandpit.Exscientia as BSS
from tests.conftest import root_fp

# Store the tutorial URL.
url = BSS.tutorialUrl()
Expand All @@ -14,7 +15,9 @@
@pytest.fixture(scope="session")
def mol():
# Alanin-dipeptide.
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])[0]
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)[0]


def test_sanity(mol):
Expand Down
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Align/test_make_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import BioSimSpace.Sandpit.Exscientia as BSS
from BioSimSpace.Sandpit.Exscientia.Align import make_ml
from tests.conftest import root_fp

# Store the tutorial URL.
url = BSS.tutorialUrl()


@pytest.fixture
def mol():
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])[0]
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)[0]


@pytest.fixture
Expand Down
3 changes: 2 additions & 1 deletion tests/Sandpit/Exscientia/Align/test_squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sire.maths import Vector

import BioSimSpace.Sandpit.Exscientia as BSS
from tests.conftest import root_fp

# Make sure AMBER is installed.
if BSS._amber_home is not None:
Expand Down Expand Up @@ -60,7 +61,7 @@ def dual_topology_system():
@pytest.fixture
def perturbed_tripeptide():
return pickle.load(
open("tests/Sandpit/Exscientia/input/merged_tripeptide.pickle", "rb")
open(f"{root_fp}/Sandpit/Exscientia/input/merged_tripeptide.pickle", "rb")
)


Expand Down
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Convert/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from sire.legacy import Mol as SireMol

import BioSimSpace.Sandpit.Exscientia as BSS
from tests.conftest import root_fp


@pytest.fixture(scope="session")
def system():
return BSS.IO.readMolecules(["tests/input/ala.crd", "tests/input/ala.top"])
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.crd", f"{root_fp}/input/ala.top"]
)


def test_system(system):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
has_gromacs,
url,
)
from tests.conftest import root_fp

import BioSimSpace.Sandpit.Exscientia as BSS
from BioSimSpace.Sandpit.Exscientia.Protocol import FreeEnergyEquilibration
Expand Down Expand Up @@ -133,7 +134,9 @@ class TestAnalysePARQUET:
@pytest.fixture(scope="class")
def data(tmp_path_factory):
outdir = tmp_path_factory.mktemp("out")
shutil.copytree("tests/Sandpit/Exscientia/input/parquet", outdir / "parquet")
shutil.copytree(
f"{root_fp}/Sandpit/Exscientia/input/parquet", outdir / "parquet"
)
return str(outdir / "parquet")

def test_analyse(self, data):
Expand All @@ -153,7 +156,9 @@ class Test_gmx_ABFE:
@staticmethod
@pytest.fixture(scope="class")
def freenrg():
m = BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])[0]
m = BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)[0]
decouple_m = decouple(m)
solvated = BSS.Solvent.tip3p(
molecule=decouple_m, box=3 * [3 * BSS.Units.Length.nanometer]
Expand Down
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Gateway/test_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from BioSimSpace.Sandpit.Exscientia.Gateway import File
from tests.conftest import root_fp


def test_no_arguments():
Expand All @@ -10,7 +11,9 @@ def test_no_arguments():
f = File()


@pytest.mark.parametrize("value", ["tests/input/ala.crd", "tests/input/ala.top"])
@pytest.mark.parametrize(
"value", [f"{root_fp}/input/ala.crd", f"{root_fp}/input/ala.top"]
)
def test_value(value):
"""Test whether object is initialised correctly and value is set."""

Expand Down
11 changes: 6 additions & 5 deletions tests/Sandpit/Exscientia/Gateway/test_fileset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from BioSimSpace.Sandpit.Exscientia.Gateway import FileSet
from tests.conftest import root_fp


def test_no_arguments():
Expand All @@ -13,11 +14,11 @@ def test_no_arguments():
@pytest.mark.parametrize(
"value",
[
["tests/input/ala.crd", "tests/input/ala.top"],
[f"{root_fp}/input/ala.crd", f"{root_fp}/input/ala.top"],
[
"tests/input/alanin.pdb",
"tests/input/alanin.psf",
"tests/input/alanin.params",
f"{root_fp}/input/alanin.pdb",
f"{root_fp}/input/alanin.psf",
f"{root_fp}/input/alanin.params",
],
],
)
Expand Down Expand Up @@ -58,7 +59,7 @@ def test_missing_files():
# One file missing.
with pytest.raises(IOError):
f = FileSet(help="Help!")
f.setValue(["tests/input/amber/ala/ala.crd", "missing2.txt"])
f.setValue([f"{root_fp}/input/amber/ala/ala.crd", "missing2.txt"])


@pytest.mark.parametrize("optional", [True, False])
Expand Down
3 changes: 2 additions & 1 deletion tests/Sandpit/Exscientia/Gateway/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import sys

import BioSimSpace.Sandpit.Exscientia as BSS
from tests.conftest import root_fp

# Store the name of the test script.
script_name = "tests/Gateway/node.py"
script_name = f"{root_fp}/Gateway/node.py"

# Store the name of the python interpreter.
exe = sys.executable
Expand Down
3 changes: 2 additions & 1 deletion tests/Sandpit/Exscientia/IO/test_file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import BioSimSpace.Sandpit.Exscientia as BSS

from tests.Sandpit.Exscientia.conftest import has_amber, has_openff
from tests.conftest import root_fp


def test_file_cache():
Expand All @@ -18,7 +19,7 @@ def test_file_cache():
BSS.IO._file_cache._cache = BSS.IO._file_cache._FixedSizeOrderedDict()

# Load the molecular system.
s = BSS.IO.readMolecules(["tests/input/ala.crd", "tests/input/ala.top"])
s = BSS.IO.readMolecules([f"{root_fp}/input/ala.crd", f"{root_fp}/input/ala.top"])

# Create a temporary working directory.
tmp_dir = tempfile.TemporaryDirectory()
Expand Down
3 changes: 2 additions & 1 deletion tests/Sandpit/Exscientia/IO/test_tuple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BioSimSpace.Sandpit.Exscientia as BSS
from tests.conftest import root_fp


def test_tuple():
"""Check that we can read from a tuple of files."""
BSS.IO.readMolecules(("tests/input/ala.crd", "tests/input/ala.top"))
BSS.IO.readMolecules((f"{root_fp}/input/ala.crd", f"{root_fp}/input/ala.top"))
11 changes: 7 additions & 4 deletions tests/Sandpit/Exscientia/MD/test_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import BioSimSpace.Sandpit.Exscientia as BSS

from tests.Sandpit.Exscientia.conftest import url, has_amber, has_gromacs, has_namd
from tests.conftest import root_fp


@pytest.mark.skipif(has_amber is False, reason="Requires AMBER to be installed.")
Expand All @@ -13,7 +14,9 @@ def test_amber():
protocol = BSS.Protocol.Minimisation(steps=100)

# Load the molecular system.
system = BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])
system = BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)

# Initialise the AMBER process.
process = BSS.MD.run(system, protocol, name="test")
Expand Down Expand Up @@ -55,9 +58,9 @@ def test_namd():
# Load the molecular system.
system = BSS.IO.readMolecules(
[
"tests/input/alanin.psf",
"tests/input/alanin.pdb",
"tests/input/alanin.params",
f"{root_fp}/input/alanin.psf",
f"{root_fp}/input/alanin.pdb",
f"{root_fp}/input/alanin.params",
]
)

Expand Down
18 changes: 9 additions & 9 deletions tests/Sandpit/Exscientia/Process/test_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import BioSimSpace.Sandpit.Exscientia as BSS

from tests.Sandpit.Exscientia.conftest import url, has_amber, has_pyarrow
from tests.conftest import root_fp


@pytest.fixture(scope="session")
def system():
"""Re-use the same molecuar system for each test."""
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)


@pytest.mark.skipif(
Expand Down Expand Up @@ -280,9 +283,9 @@ def test_parse_fep_output(system, protocol):

# Assign the path to the output file.
if isinstance(protocol, BSS.Protocol.FreeEnergy):
out_file = "tests/Sandpit/Exscientia/output/amber_fep.out"
out_file = f"{root_fp}/Sandpit/Exscientia/output/amber_fep.out"
else:
out_file = "tests/Sandpit/Exscientia/output/amber_fep_min.out"
out_file = f"{root_fp}/Sandpit/Exscientia/output/amber_fep_min.out"

# Copy the existing output file into the working directory.
shutil.copyfile(out_file, process.workDir() + "/amber.out")
Expand Down Expand Up @@ -320,7 +323,6 @@ def test_parse_fep_output(system, protocol):
assert len(records_sc1) != 0



class TestsaveMetric:
@staticmethod
@pytest.fixture()
Expand All @@ -334,7 +336,6 @@ def alchemical_system(system):
system_copy.updateMolecule(0, mol)
return system_copy


@staticmethod
@pytest.fixture()
def setup(alchemical_system):
Expand All @@ -344,7 +345,7 @@ def setup(alchemical_system):
BSS.Protocol.FreeEnergy(temperature=298 * BSS.Units.Temperature.kelvin),
)
shutil.copyfile(
"tests/Sandpit/Exscientia/output/amber_fep.out",
f"{root_fp}/Sandpit/Exscientia/output/amber_fep.out",
process.workDir() + "/amber.out",
)
process.saveMetric()
Expand All @@ -357,10 +358,9 @@ def test_error_alchemlyb_extract(self, alchemical_system):
BSS.Protocol.FreeEnergy(temperature=298 * BSS.Units.Temperature.kelvin),
)
process.wait()
with open(process.workDir() + '/amber.err', 'r') as f:
with open(process.workDir() + "/amber.err", "r") as f:
text = f.read()
assert 'Exception Information' in text

assert "Exception Information" in text

def test_metric_parquet_exist(self, setup):
assert Path(f"{setup.workDir()}/metric.parquet").exists()
Expand Down
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Process/test_dummy_protocol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from tests.Sandpit.Exscientia.conftest import has_amber, has_gromacs
from tests.conftest import root_fp

import BioSimSpace.Sandpit.Exscientia as BSS
from BioSimSpace.Sandpit.Exscientia.Process._process import Process
Expand All @@ -9,7 +10,9 @@
@pytest.fixture(scope="session")
def system():
"""Re-use the same molecuar system for each test."""
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)


@pytest.mark.skipif(
Expand Down
16 changes: 10 additions & 6 deletions tests/Sandpit/Exscientia/Process/test_gromacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
has_openff,
has_pyarrow,
)
from tests.conftest import root_fp


@pytest.fixture(scope="session")
def system():
"""Re-use the same molecuar system for each test."""
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -254,7 +257,7 @@ def setup(perturbable_system):
)
process = BSS.Process.Gromacs(perturbable_system, protocol)
shutil.copyfile(
"tests/Sandpit/Exscientia/output/gromacs.edr",
f"{root_fp}/Sandpit/Exscientia/output/gromacs.edr",
process.workDir() + "/gromacs.edr",
)
shutil.copyfile(
Expand Down Expand Up @@ -343,17 +346,18 @@ def test_u_nk_parquet(self, setup):

def test_error_alchemlyb_extract(self, perturbable_system, monkeypatch):
def extract(*args):
raise ValueError('alchemlyb.parsing.gmx.extract failed.')
monkeypatch.setattr('alchemlyb.parsing.gmx.extract', extract)
raise ValueError("alchemlyb.parsing.gmx.extract failed.")

monkeypatch.setattr("alchemlyb.parsing.gmx.extract", extract)
# Create a process using any system and the protocol.
process = BSS.Process.Gromacs(
perturbable_system,
BSS.Protocol.FreeEnergy(temperature=298 * BSS.Units.Temperature.kelvin),
)
process.wait()
with open(process.workDir() + '/gromacs.err', 'r') as f:
with open(process.workDir() + "/gromacs.err", "r") as f:
text = f.read()
assert 'Exception Information' in text
assert "Exception Information" in text


@pytest.mark.skipif(
Expand Down
7 changes: 4 additions & 3 deletions tests/Sandpit/Exscientia/Process/test_namd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import BioSimSpace.Sandpit.Exscientia as BSS

from tests.Sandpit.Exscientia.conftest import url, has_namd
from tests.conftest import root_fp


@pytest.fixture(scope="session")
def system():
"""Re-use the same molecuar system for each test."""
return BSS.IO.readMolecules(
[
"tests/input/alanin.psf",
f"tests/input/alanin.pdb",
f"tests/input/alanin.params",
f"{root_fp}/input/alanin.psf",
f"{root_fp}/input/alanin.pdb",
f"{root_fp}/input/alanin.params",
]
)

Expand Down
5 changes: 4 additions & 1 deletion tests/Sandpit/Exscientia/Process/test_openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import BioSimSpace.Sandpit.Exscientia as BSS

from tests.Sandpit.Exscientia.conftest import url, has_amber, has_gromacs, has_openff
from tests.conftest import root_fp


@pytest.fixture(scope="session")
def system():
"""Re-use the same molecuar system for each test."""
return BSS.IO.readMolecules(["tests/input/ala.top", "tests/input/ala.crd"])
return BSS.IO.readMolecules(
[f"{root_fp}/input/ala.top", f"{root_fp}/input/ala.crd"]
)


@pytest.mark.parametrize("restraint", ["backbone", "heavy", "all", "none"])
Expand Down
Loading

0 comments on commit 20da673

Please sign in to comment.