Skip to content

Commit

Permalink
Clean up conftest.py imports (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-S-Rosen committed Feb 29, 2024
1 parent 87d78ad commit 3f1f38c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 27 deletions.
14 changes: 9 additions & 5 deletions tests/core/recipes/gaussian_recipes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
from pathlib import Path
from shutil import copy

import pytest
from ase.calculators.calculator import FileIOCalculator
from ase.calculators.gaussian import Gaussian
from ase.calculators.lj import LennardJones
from ase.io import read

FILE_DIR = Path(__file__).parent
GAUSSIAN_DIR = Path(FILE_DIR, "gaussian_run")


def mock_execute(self, *args, **kwargs):
from shutil import copy

copy(GAUSSIAN_DIR / "Gaussian.log.gz", "Gaussian.log.gz")


@pytest.fixture(autouse=True)
def patch_execute(monkeypatch):
from ase.calculators.calculator import FileIOCalculator

monkeypatch.setattr(FileIOCalculator, "execute", mock_execute)


def mock_read_results(self, *args, **kwargs):
from ase.calculators.lj import LennardJones
from ase.io import read

atoms = read("Gaussian.com")
atoms.calc = LennardJones()
atoms.get_potential_energy()
Expand All @@ -30,4 +32,6 @@ def mock_read_results(self, *args, **kwargs):

@pytest.fixture(autouse=True)
def patch_read_results(monkeypatch):
from ase.calculators.gaussian import Gaussian

monkeypatch.setattr(Gaussian, "read_results", mock_read_results)
16 changes: 11 additions & 5 deletions tests/core/recipes/gulp_recipes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import os
from pathlib import Path
from shutil import copy
from unittest import mock

import pytest
from ase.calculators.calculator import FileIOCalculator
from ase.calculators.gulp import GULP
from ase.calculators.lj import LennardJones
from ase.io import read

FILE_DIR = Path(__file__).parent
GULP_DIR = Path(FILE_DIR, "gulp_run")
Expand All @@ -20,17 +15,24 @@ def mock_settings_env_vars():


def mock_execute(self, *args, **kwargs):
from shutil import copy

copy(GULP_DIR / "gulp.got.gz", "gulp.got.gz")
copy(GULP_DIR / "gulp.cif.gz", "gulp.cif.gz")
copy(GULP_DIR / "gulp.xyz.gz", "gulp.xyz.gz")


@pytest.fixture(autouse=True)
def patch_execute(monkeypatch):
from ase.calculators.calculator import FileIOCalculator

monkeypatch.setattr(FileIOCalculator, "execute", mock_execute)


def mock_read_results(self, *args, **kwargs):
from ase.calculators.lj import LennardJones
from ase.io import read

atoms = read("gulp.xyz.gz")
atoms.calc = LennardJones()
atoms.get_potential_energy()
Expand All @@ -40,6 +42,8 @@ def mock_read_results(self, *args, **kwargs):

@pytest.fixture(autouse=True)
def patch_read_results(monkeypatch):
from ase.calculators.gulp import GULP

monkeypatch.setattr(GULP, "read_results", mock_read_results)


Expand All @@ -54,6 +58,8 @@ def mock_get_opt_state(self, **kwargs):

@pytest.fixture(autouse=True)
def patch_get_opt_state(monkeypatch):
from ase.calculators.gulp import GULP

# Monkeypatch the .get_opt_state() method of the GULP calculator object so
# we aren't fetching the actual state
monkeypatch.setattr(GULP, "get_opt_state", mock_get_opt_state)
10 changes: 7 additions & 3 deletions tests/core/recipes/onetep_recipes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import pytest
from ase.calculators.emt import EMT
from ase.calculators.onetep import OnetepTemplate
from ase.io import read


def mock_execute(self, *args, **kwargs):
Expand All @@ -10,10 +7,15 @@ def mock_execute(self, *args, **kwargs):

@pytest.fixture(autouse=True)
def patch_execute(monkeypatch):
from ase.calculators.onetep import OnetepTemplate

monkeypatch.setattr(OnetepTemplate, "execute", mock_execute)


def mock_read_results(self, directory, *args, **kwargs):
from ase.calculators.emt import EMT
from ase.io import read

atoms = read(directory / "onetep.dat")
atoms.calc = EMT()
atoms.get_potential_energy()
Expand All @@ -22,4 +24,6 @@ def mock_read_results(self, directory, *args, **kwargs):

@pytest.fixture(autouse=True)
def patch_read_results(monkeypatch):
from ase.calculators.onetep import OnetepTemplate

monkeypatch.setattr(OnetepTemplate, "read_results", mock_read_results)
15 changes: 10 additions & 5 deletions tests/core/recipes/orca_recipes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
from pathlib import Path
from shutil import copy

import pytest
from ase.calculators.lj import LennardJones
from ase.calculators.orca import OrcaTemplate
from ase.io import write
from ase.io.orca import read_geom_orcainp

FILE_DIR = Path(__file__).parent
ORCA_DIR = Path(FILE_DIR, "orca_run")


def mock_execute(self, *args, **kwargs):
from shutil import copy

copy(ORCA_DIR / "orca.out.gz", "orca.out.gz")


@pytest.fixture(autouse=True)
def patch_execute(monkeypatch):
from ase.calculators.orca import OrcaTemplate

monkeypatch.setattr(OrcaTemplate, "execute", mock_execute)


def mock_read_results(self, directory, *args, **kwargs):
from ase.calculators.lj import LennardJones
from ase.io import write
from ase.io.orca import read_geom_orcainp

atoms = read_geom_orcainp(directory / "orca.inp")
write(directory / "orca.xyz", atoms)
atoms.calc = LennardJones()
Expand All @@ -30,4 +33,6 @@ def mock_read_results(self, directory, *args, **kwargs):

@pytest.fixture(autouse=True)
def patch_read_results(monkeypatch):
from ase.calculators.orca import OrcaTemplate

monkeypatch.setattr(OrcaTemplate, "read_results", mock_read_results)
11 changes: 8 additions & 3 deletions tests/core/recipes/vasp_recipes/mocked/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from pathlib import Path

import pytest
from ase.calculators.emt import EMT
from ase.io import read, write
from emmet.core.tasks import TaskDoc
from monty.os.path import zpath

FILE_DIR = Path(__file__).parent
PSEUDO_DIR = FILE_DIR / "fake_pseudos"
Expand All @@ -13,6 +10,8 @@


def mock_run(self, *args, **kwargs):
from ase.io import write

write(Path(self.directory) / "CONTCAR", self.atoms)


Expand All @@ -25,6 +24,8 @@ def patch_run(monkeypatch):


def mock_read_results(self, *args, **kwargs):
from ase.calculators.emt import EMT

atoms = self.atoms
atoms.calc = EMT()
atoms.get_potential_energy()
Expand All @@ -39,6 +40,9 @@ def patch_read_results(monkeypatch):


def mock_taskdoc(*args, **kwargs):
from ase.io import read
from monty.os.path import zpath

from quacc.atoms.core import check_is_metal

MOCK_TASKDOC.output.bandgap = 0.0 if check_is_metal(read(zpath("CONTCAR"))) else 0.5
Expand All @@ -47,4 +51,5 @@ def mock_taskdoc(*args, **kwargs):

@pytest.fixture(autouse=True)
def patch_taskdoc(monkeypatch):

monkeypatch.setattr("quacc.schemas.vasp.TaskDoc.from_directory", mock_taskdoc)
6 changes: 4 additions & 2 deletions tests/dask/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
from pathlib import Path
from shutil import rmtree

TEST_RESULTS_DIR = Path(__file__).parent / "_test_results"
TEST_SCRATCH_DIR = Path(__file__).parent / "_test_scratch"
Expand All @@ -15,6 +13,8 @@
if has_import:

def pytest_sessionstart():
import os

from dask.distributed import Client, get_client

file_dir = Path(__file__).parent
Expand All @@ -28,6 +28,8 @@ def pytest_sessionstart():
Client()

def pytest_sessionfinish(exitstatus):
from shutil import rmtree

if exitstatus == 0:
from dask.distributed import default_client

Expand Down
3 changes: 2 additions & 1 deletion tests/jobflow/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from pathlib import Path

TEST_RESULTS_DIR = Path(__file__).parent / "_test_results"
TEST_SCRATCH_DIR = Path(__file__).parent / "_test_scratch"


def pytest_sessionstart():
import os

file_dir = Path(__file__).parent
os.environ["QUACC_CONFIG_FILE"] = str(file_dir / "quacc.yaml")
os.environ["QUACC_RESULTS_DIR"] = str(TEST_RESULTS_DIR)
Expand Down
3 changes: 2 additions & 1 deletion tests/parsl/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

TEST_RESULTS_DIR = Path(__file__).parent / "_test_results"
Expand All @@ -12,6 +11,8 @@


def pytest_sessionstart():
import os

if parsl:
parsl.load()
file_dir = Path(__file__).parent
Expand Down
3 changes: 2 additions & 1 deletion tests/prefect/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

import pytest
Expand All @@ -14,6 +13,8 @@
if prefect:

def pytest_sessionstart():
import os

from prefect.testing.utilities import prefect_test_harness

file_dir = Path(__file__).parent
Expand Down
3 changes: 2 additions & 1 deletion tests/redun/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from pathlib import Path

TEST_RESULTS_DIR = Path(__file__).parent / "_test_results"
TEST_SCRATCH_DIR = Path(__file__).parent / "_test_scratch"


def pytest_sessionstart():
import os

file_dir = Path(__file__).parent
os.environ["QUACC_CONFIG_FILE"] = str(file_dir / "quacc.yaml")
os.environ["QUACC_RESULTS_DIR"] = str(TEST_RESULTS_DIR)
Expand Down

0 comments on commit 3f1f38c

Please sign in to comment.