Skip to content

Commit ae161fe

Browse files
authored
Unskip and fix packmol tests (#3195)
* don't use tempfile.TemporaryDirectory() in tests, prefer PymatgenTest.tmp_path auto-used fixture * fix broken test_files paths in TestPackmolSet * skip failing TestPackmolSet.test_random_seed due to openbabel not installed pymatgen/io/tests/test_packmol.py:188: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ args = (<pymatgen.analysis.molecule_matcher.MoleculeMatcher object at 0x2aecc7bd0>,) kwargs = {} @functools.wraps(_callable) def decorated(*args, **kwargs): if not self.condition: > raise RuntimeError(self.message) E RuntimeError: BabelMolAdaptor requires openbabel to be installed with Python bindings. Please get it at http://openbabel.org (version >=3.0.0). * only skip pymatgen/io/tests/test_packmol.py if packmol not installed was set to always skip
1 parent 0b7607a commit ae161fe

File tree

5 files changed

+265
-307
lines changed

5 files changed

+265
-307
lines changed

pymatgen/core/tests/test_structure.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import warnings
77
from pathlib import Path
88
from shutil import which
9-
from tempfile import TemporaryDirectory
109
from unittest import skipIf
1110

1211
import numpy as np
@@ -1467,8 +1466,7 @@ def test_relax_ase_return_traj(self):
14671466
def test_relax_ase_opt_kwargs(self):
14681467
pytest.importorskip("ase")
14691468
structure = self.cu_structure
1470-
tmp_dir = TemporaryDirectory()
1471-
traj_file = f"{tmp_dir.name}/testing.traj"
1469+
traj_file = f"{self.tmp_path}/testing.traj"
14721470
relaxed, traj = structure.relax(
14731471
calculator=EMT(), fmax=0.01, steps=2, return_trajectory=True, opt_kwargs={"trajectory": traj_file}
14741472
)
@@ -2005,8 +2003,7 @@ def test_relax_ase_mol(self):
20052003

20062004
def test_relax_ase_mol_return_traj(self):
20072005
pytest.importorskip("ase")
2008-
tmp_dir = TemporaryDirectory()
2009-
traj_file = f"{tmp_dir.name}/testing.traj"
2006+
traj_file = f"{self.tmp_path}/testing.traj"
20102007
relaxed, traj = self.mol.relax(
20112008
calculator=EMT(), fmax=0.01, steps=2, return_trajectory=True, opt_kwargs={"trajectory": traj_file}
20122009
)

pymatgen/io/lammps/tests/test_inputs.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import os
55
import re
66
import shutil
7-
import tempfile
87
import unittest
9-
from pathlib import Path
108

119
import pandas as pd
1210
import pytest
@@ -671,45 +669,43 @@ def tearDownClass(cls):
671669

672670
class LammpsTemplateGenTest(PymatgenTest):
673671
def test_write_inputs(self):
674-
with tempfile.TemporaryDirectory() as tmp_dir:
675-
# simple script without data file
676-
lis = LammpsTemplateGen().get_input_set(
677-
script_template=f"{PymatgenTest.TEST_FILES_DIR}/lammps/kappa.txt",
678-
settings={"method": "heat"},
679-
data=None,
680-
data_filename="data.peptide",
681-
)
682-
tmp_dir = Path(tmp_dir)
683-
assert len(lis) == 1
684-
lis.write_input(tmp_dir / "heat")
685-
686-
with open(tmp_dir / "heat" / "in.lammps") as f:
687-
kappa_script = f.read()
688-
fix_hot = re.search(r"fix\s+hot\s+all\s+([^\s]+)\s+", kappa_script)
689-
# placeholders supposed to be filled
690-
assert fix_hot.group(1) == "heat"
691-
fix_cold = re.search(r"fix\s+cold\s+all\s+([^\s]+)\s+", kappa_script)
692-
assert fix_cold.group(1) == "heat"
693-
lattice = re.search(r"lattice\s+fcc\s+(.*)\n", kappa_script)
694-
# parentheses not supposed to be filled
695-
assert lattice.group(1) == "${rho}"
696-
pair_style = re.search(r"pair_style\slj/cut\s+(.*)\n", kappa_script)
697-
assert pair_style.group(1) == "${rc}"
698-
699-
# script with data file
700-
obj = LammpsData.from_file(
701-
os.path.join(PymatgenTest.TEST_FILES_DIR, "lammps", "data.quartz"), atom_style="atomic"
702-
)
703-
lis = LammpsTemplateGen().get_input_set(
704-
script_template=f"{PymatgenTest.TEST_FILES_DIR}/lammps/in.peptide",
705-
settings=None,
706-
data=obj,
707-
data_filename="data.peptide",
708-
)
709-
assert len(lis) == 2
710-
assert isinstance(lis["data.peptide"], LammpsData)
711-
lis.write_input(tmp_dir / "obj")
712-
713-
obj_read = LammpsData.from_file(str(tmp_dir / "obj" / "data.peptide"), atom_style="atomic")
714-
pd.testing.assert_frame_equal(obj_read.masses, obj.masses)
715-
pd.testing.assert_frame_equal(obj_read.atoms, obj.atoms)
672+
# simple script without data file
673+
lis = LammpsTemplateGen().get_input_set(
674+
script_template=f"{PymatgenTest.TEST_FILES_DIR}/lammps/kappa.txt",
675+
settings={"method": "heat"},
676+
data=None,
677+
data_filename="data.peptide",
678+
)
679+
assert len(lis) == 1
680+
lis.write_input(self.tmp_path / "heat")
681+
682+
with open(self.tmp_path / "heat" / "in.lammps") as f:
683+
kappa_script = f.read()
684+
fix_hot = re.search(r"fix\s+hot\s+all\s+([^\s]+)\s+", kappa_script)
685+
# placeholders supposed to be filled
686+
assert fix_hot.group(1) == "heat"
687+
fix_cold = re.search(r"fix\s+cold\s+all\s+([^\s]+)\s+", kappa_script)
688+
assert fix_cold.group(1) == "heat"
689+
lattice = re.search(r"lattice\s+fcc\s+(.*)\n", kappa_script)
690+
# parentheses not supposed to be filled
691+
assert lattice.group(1) == "${rho}"
692+
pair_style = re.search(r"pair_style\slj/cut\s+(.*)\n", kappa_script)
693+
assert pair_style.group(1) == "${rc}"
694+
695+
# script with data file
696+
obj = LammpsData.from_file(
697+
os.path.join(PymatgenTest.TEST_FILES_DIR, "lammps", "data.quartz"), atom_style="atomic"
698+
)
699+
lis = LammpsTemplateGen().get_input_set(
700+
script_template=f"{PymatgenTest.TEST_FILES_DIR}/lammps/in.peptide",
701+
settings=None,
702+
data=obj,
703+
data_filename="data.peptide",
704+
)
705+
assert len(lis) == 2
706+
assert isinstance(lis["data.peptide"], LammpsData)
707+
lis.write_input(self.tmp_path / "obj")
708+
709+
obj_read = LammpsData.from_file(str(self.tmp_path / "obj" / "data.peptide"), atom_style="atomic")
710+
pd.testing.assert_frame_equal(obj_read.masses, obj.masses)
711+
pd.testing.assert_frame_equal(obj_read.atoms, obj.atoms)

0 commit comments

Comments
 (0)