From 25a5ff1a3b32e88675bf6879c577b59dfc00658c Mon Sep 17 00:00:00 2001 From: Drew Parsons Date: Tue, 24 Aug 2021 00:07:58 +0200 Subject: [PATCH 1/3] use PymatgenTest.TEST_FILES_DIR with all tests required to enable tests to run separately from the source, for instance for CI testing of packaging for Linux distributions, see https://github.com/materialsproject/pymatgen/issues/2025 Adapted from Debian patch use_TEST_FILES_DIR.patch, https://salsa.debian.org/debichem-team/pymatgen/-/blob/f8f31f359ef846b728006e61272557e73f413b6b/debian/patches/use_TEST_FILES_DIR.patch --- pymatgen/alchemy/tests/test_materials.py | 4 +- .../tests/test_environment_nodes.py | 8 +-- .../test_coordination_geometry_finder.py | 8 +-- .../tests/test_read_write.py | 17 +----- .../tests/test_structure_environments.py | 8 +-- .../tests/test_voronoi.py | 16 +---- .../tests/test_weights.py | 8 +-- .../utils/tests/test_chemenv_config.py | 9 +-- .../tests/test_substitution_probability.py | 9 +-- .../tests/test_substitutor.py | 7 +-- pymatgen/analysis/tests/test_graphs.py | 59 ++++++------------- pymatgen/analysis/tests/test_path_finder.py | 3 +- .../analysis/tests/test_surface_analysis.py | 2 +- .../command_line/tests/test_critic2_caller.py | 7 +-- pymatgen/core/tests/test_surface.py | 2 +- pymatgen/io/abinit/tests/test_inputs.py | 2 +- pymatgen/io/abinit/tests/test_netcdf.py | 2 +- pymatgen/io/abinit/tests/test_pseudos.py | 2 +- .../tests/test_advanced_transformations.py | 2 +- 19 files changed, 44 insertions(+), 131 deletions(-) diff --git a/pymatgen/alchemy/tests/test_materials.py b/pymatgen/alchemy/tests/test_materials.py index 7c6c25f9712..67377e041ac 100644 --- a/pymatgen/alchemy/tests/test_materials.py +++ b/pymatgen/alchemy/tests/test_materials.py @@ -55,9 +55,7 @@ def test_append_filter(self): self.trans.append_filter(f3) def test_get_vasp_input(self): - SETTINGS["PMG_VASP_PSP_DIR"] = os.path.abspath( - os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files") - ) + SETTINGS["PMG_VASP_PSP_DIR"] = PymatgenTest.TEST_FILES_DIR potcar = self.trans.get_vasp_input(MPRelaxSet)["POTCAR"] self.assertEqual("Na_pv\nFe_pv\nP\nO", "\n".join([p.symbol for p in potcar])) self.assertEqual(len(self.trans.structures), 2) diff --git a/pymatgen/analysis/chemenv/connectivity/tests/test_environment_nodes.py b/pymatgen/analysis/chemenv/connectivity/tests/test_environment_nodes.py index e1cedca6e12..95c20cab140 100644 --- a/pymatgen/analysis/chemenv/connectivity/tests/test_environment_nodes.py +++ b/pymatgen/analysis/chemenv/connectivity/tests/test_environment_nodes.py @@ -20,13 +20,7 @@ bson = None json_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "json_test_files", ) diff --git a/pymatgen/analysis/chemenv/coordination_environments/tests/test_coordination_geometry_finder.py b/pymatgen/analysis/chemenv/coordination_environments/tests/test_coordination_geometry_finder.py index c5133e6988e..66dfe5c239a 100644 --- a/pymatgen/analysis/chemenv/coordination_environments/tests/test_coordination_geometry_finder.py +++ b/pymatgen/analysis/chemenv/coordination_environments/tests/test_coordination_geometry_finder.py @@ -19,13 +19,7 @@ from pymatgen.util.testing import PymatgenTest json_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "json_test_files", ) diff --git a/pymatgen/analysis/chemenv/coordination_environments/tests/test_read_write.py b/pymatgen/analysis/chemenv/coordination_environments/tests/test_read_write.py index 0d8838280bf..63b837bea34 100644 --- a/pymatgen/analysis/chemenv/coordination_environments/tests/test_read_write.py +++ b/pymatgen/analysis/chemenv/coordination_environments/tests/test_read_write.py @@ -31,26 +31,15 @@ DetailedVoronoiContainer, ) from pymatgen.core.structure import Structure +from pymatgen.util.testing import PymatgenTest json_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "json_test_files", ) se_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "structure_environments_files", ) diff --git a/pymatgen/analysis/chemenv/coordination_environments/tests/test_structure_environments.py b/pymatgen/analysis/chemenv/coordination_environments/tests/test_structure_environments.py index f164d2fb203..befda771621 100644 --- a/pymatgen/analysis/chemenv/coordination_environments/tests/test_structure_environments.py +++ b/pymatgen/analysis/chemenv/coordination_environments/tests/test_structure_environments.py @@ -23,13 +23,7 @@ from pymatgen.util.testing import PymatgenTest se_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "structure_environments_files", ) diff --git a/pymatgen/analysis/chemenv/coordination_environments/tests/test_voronoi.py b/pymatgen/analysis/chemenv/coordination_environments/tests/test_voronoi.py index df9219d7ccd..0a465fff434 100644 --- a/pymatgen/analysis/chemenv/coordination_environments/tests/test_voronoi.py +++ b/pymatgen/analysis/chemenv/coordination_environments/tests/test_voronoi.py @@ -19,24 +19,12 @@ from pymatgen.util.testing import PymatgenTest json_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "json_test_files", ) img_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "images", ) diff --git a/pymatgen/analysis/chemenv/coordination_environments/tests/test_weights.py b/pymatgen/analysis/chemenv/coordination_environments/tests/test_weights.py index 32a9debd22c..7ab498ae0a1 100644 --- a/pymatgen/analysis/chemenv/coordination_environments/tests/test_weights.py +++ b/pymatgen/analysis/chemenv/coordination_environments/tests/test_weights.py @@ -23,13 +23,7 @@ from pymatgen.util.testing import PymatgenTest se_files_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "structure_environments_files", ) diff --git a/pymatgen/analysis/chemenv/utils/tests/test_chemenv_config.py b/pymatgen/analysis/chemenv/utils/tests/test_chemenv_config.py index 1dc0c516739..8ea5c8097f7 100644 --- a/pymatgen/analysis/chemenv/utils/tests/test_chemenv_config.py +++ b/pymatgen/analysis/chemenv/utils/tests/test_chemenv_config.py @@ -11,15 +11,10 @@ from pymatgen.core import SETTINGS from pymatgen.analysis.chemenv.utils.chemenv_config import ChemEnvConfig +from pymatgen.util.testing import PymatgenTest config_file_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "chemenv", "config", ) diff --git a/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py b/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py index 4c4f06d742d..877de558ca3 100644 --- a/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py +++ b/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py @@ -13,7 +13,7 @@ SubstitutionProbability, ) from pymatgen.core.periodic_table import Species - +from pymatgen.util.testing import PymatgenTest def get_table(): """ @@ -22,12 +22,7 @@ def get_table(): default lambda table. """ data_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "struct_predictor", ) diff --git a/pymatgen/analysis/structure_prediction/tests/test_substitutor.py b/pymatgen/analysis/structure_prediction/tests/test_substitutor.py index 92f1add4781..22bc61c8454 100644 --- a/pymatgen/analysis/structure_prediction/tests/test_substitutor.py +++ b/pymatgen/analysis/structure_prediction/tests/test_substitutor.py @@ -20,12 +20,7 @@ def get_table(): default lambda table. """ data_dir = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "..", - "test_files", + PymatgenTest.TEST_FILES_DIR, "struct_predictor", ) json_file = os.path.join(data_dir, "test_lambda.json") diff --git a/pymatgen/analysis/tests/test_graphs.py b/pymatgen/analysis/tests/test_graphs.py index e1aed144513..03d418aa60a 100644 --- a/pymatgen/analysis/tests/test_graphs.py +++ b/pymatgen/analysis/tests/test_graphs.py @@ -37,7 +37,7 @@ __date__ = "August 2017" module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) -molecule_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files", "molecules") +molecule_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules") class StructureGraphTest(PymatgenTest): @@ -83,21 +83,15 @@ def setUp(self): # MoS2 example, structure graph obtained from critic2 # (not ground state, from mp-1023924, single layer) stdout_file = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/critic2/MoS2_critic2_stdout.txt", + PymatgenTest.TEST_FILES_DIR, + "critic2/MoS2_critic2_stdout.txt", ) with open(stdout_file, "r") as f: reference_stdout = f.read() self.structure = Structure.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/critic2/MoS2.cif", + PymatgenTest.TEST_FILES_DIR, + "critic2/MoS2.cif", ) ) c2o = Critic2Analysis(self.structure, reference_stdout) @@ -230,7 +224,7 @@ def test_insert_remove(self): def test_substitute(self): structure = Structure.from_file( - os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files", "Li2O.cif") + os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif") ) molecule = FunctionalGroups["methyl"] @@ -471,11 +465,8 @@ def test_from_edges(self): def test_extract_molecules(self): structure_file = os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/H6PbCI3N_mp-977013_symmetrized.cif", + PymatgenTest.TEST_FILES_DIR, + "H6PbCI3N_mp-977013_symmetrized.cif", ) s = Structure.from_file(structure_file) @@ -539,11 +530,8 @@ def setUp(self): cyclohexene = Molecule.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/graphs/cyclohexene.xyz", + PymatgenTest.TEST_FILES_DIR, + "graphs/cyclohexene.xyz", ) ) self.cyclohexene = MoleculeGraph.with_empty_graph( @@ -568,11 +556,8 @@ def setUp(self): butadiene = Molecule.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/graphs/butadiene.xyz", + PymatgenTest.TEST_FILES_DIR, + "graphs/butadiene.xyz", ) ) self.butadiene = MoleculeGraph.with_empty_graph(butadiene, edge_weight_name="strength", edge_weight_units="") @@ -588,11 +573,8 @@ def setUp(self): ethylene = Molecule.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/graphs/ethylene.xyz", + PymatgenTest.TEST_FILES_DIR, + "graphs/ethylene.xyz", ) ) self.ethylene = MoleculeGraph.with_empty_graph(ethylene, edge_weight_name="strength", edge_weight_units="") @@ -602,7 +584,7 @@ def setUp(self): self.ethylene.add_edge(1, 4, weight=1.0) self.ethylene.add_edge(1, 5, weight=1.0) - self.pc = Molecule.from_file(os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "PC.xyz")) + self.pc = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "PC.xyz")) self.pc_edges = [ [5, 10], [5, 12], @@ -619,10 +601,10 @@ def setUp(self): [6, 2], ] self.pc_frag1 = Molecule.from_file( - os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "PC_frag1.xyz") + os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "PC_frag1.xyz") ) self.pc_frag1_edges = [[0, 2], [4, 2], [2, 1], [1, 3]] - self.tfsi = Molecule.from_file(os.path.join(module_dir, "..", "..", "..", "test_files", "graphs", "TFSI.xyz")) + self.tfsi = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "TFSI.xyz")) self.tfsi_edges = ( [14, 1], [1, 4], @@ -920,11 +902,8 @@ def test_find_rings(self): def test_isomorphic(self): ethylene = Molecule.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/graphs/ethylene.xyz", + PymatgenTest.TEST_FILES_DIR, + "graphs/ethylene.xyz", ) ) # switch carbons diff --git a/pymatgen/analysis/tests/test_path_finder.py b/pymatgen/analysis/tests/test_path_finder.py index c94a02d0d1b..fbdc735f1f3 100644 --- a/pymatgen/analysis/tests/test_path_finder.py +++ b/pymatgen/analysis/tests/test_path_finder.py @@ -9,6 +9,7 @@ from pymatgen.analysis.path_finder import ChgcarPotential, NEBPathfinder from pymatgen.core.periodic_table import Element from pymatgen.io.vasp import Chgcar, Poscar +from pymatgen.util.testing import PymatgenTest __author__ = "Ziqin (Shaun) Rong" __version__ = "0.1" @@ -23,7 +24,7 @@ class PathFinderTest(unittest.TestCase): def test_image_num(self): module_dir = os.path.dirname(os.path.abspath(__file__)) - test_file_dir = os.path.join(module_dir, "..", "..", "..", "test_files", "path_finder") + test_file_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "path_finder") start_s = Poscar.from_file(os.path.join(test_file_dir, "LFP_POSCAR_s")).structure end_s = Poscar.from_file(os.path.join(test_file_dir, "LFP_POSCAR_e")).structure mid_s = start_s.interpolate(end_s, nimages=2, interpolate_lattices=False)[1] diff --git a/pymatgen/analysis/tests/test_surface_analysis.py b/pymatgen/analysis/tests/test_surface_analysis.py index 295f7debdc0..e14494e864a 100644 --- a/pymatgen/analysis/tests/test_surface_analysis.py +++ b/pymatgen/analysis/tests/test_surface_analysis.py @@ -25,7 +25,7 @@ def get_path(path_str): cwd = os.path.abspath(os.path.dirname(__file__)) - path = os.path.join(cwd, "..", "..", "..", "test_files", "surface_tests", path_str) + path = os.path.join(PymatgenTest.TEST_FILES_DIR, "surface_tests", path_str) return path diff --git a/pymatgen/command_line/tests/test_critic2_caller.py b/pymatgen/command_line/tests/test_critic2_caller.py index 5113b9b16a4..a61fa6dbc8f 100644 --- a/pymatgen/command_line/tests/test_critic2_caller.py +++ b/pymatgen/command_line/tests/test_critic2_caller.py @@ -62,11 +62,8 @@ def test_from_structure(self): # uses promolecular density structure = Structure.from_file( os.path.join( - os.path.dirname(__file__), - "..", - "..", - "..", - "test_files/critic2/MoS2.cif", + PymatgenTest.TEST_FILES_DIR, + "critic2/MoS2.cif", ) ) diff --git a/pymatgen/core/tests/test_surface.py b/pymatgen/core/tests/test_surface.py index 7eeb669be47..a0268fdf464 100644 --- a/pymatgen/core/tests/test_surface.py +++ b/pymatgen/core/tests/test_surface.py @@ -29,7 +29,7 @@ def get_path(path_str): cwd = os.path.abspath(os.path.dirname(__file__)) - path = os.path.join(cwd, "..", "..", "..", "test_files", "surface_tests", path_str) + path = os.path.join(PymatgenTest.TEST_FILES_DIR, "surface_tests", path_str) return path diff --git a/pymatgen/io/abinit/tests/test_inputs.py b/pymatgen/io/abinit/tests/test_inputs.py index 458eecf7943..e288f0f1b43 100644 --- a/pymatgen/io/abinit/tests/test_inputs.py +++ b/pymatgen/io/abinit/tests/test_inputs.py @@ -21,7 +21,7 @@ ) from pymatgen.util.testing import PymatgenTest -_test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "test_files", "abinit") +_test_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "abinit") def abiref_file(filename): diff --git a/pymatgen/io/abinit/tests/test_netcdf.py b/pymatgen/io/abinit/tests/test_netcdf.py index bda5a40d9ff..3f415f64e49 100644 --- a/pymatgen/io/abinit/tests/test_netcdf.py +++ b/pymatgen/io/abinit/tests/test_netcdf.py @@ -16,7 +16,7 @@ except ImportError: netCDF4 = None -_test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "test_files", "abinit") +_test_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "abinit") def ref_file(filename): diff --git a/pymatgen/io/abinit/tests/test_pseudos.py b/pymatgen/io/abinit/tests/test_pseudos.py index ead2d87aa89..7004545e55d 100644 --- a/pymatgen/io/abinit/tests/test_pseudos.py +++ b/pymatgen/io/abinit/tests/test_pseudos.py @@ -9,7 +9,7 @@ from pymatgen.io.abinit.pseudos import * from pymatgen.util.testing import PymatgenTest -_test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "test_files", "abinit") +_test_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "abinit") def ref_file(filename): diff --git a/pymatgen/transformations/tests/test_advanced_transformations.py b/pymatgen/transformations/tests/test_advanced_transformations.py index 43e9c29d7b1..2f625835de8 100644 --- a/pymatgen/transformations/tests/test_advanced_transformations.py +++ b/pymatgen/transformations/tests/test_advanced_transformations.py @@ -60,7 +60,7 @@ def get_table(): initialization time, and make unit tests insensitive to changes in the default lambda table. """ - data_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files", "struct_predictor") + data_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "struct_predictor") json_file = os.path.join(data_dir, "test_lambda.json") with open(json_file) as f: lambda_table = json.load(f) From 86f8981d6b1e38ed83c4dee5060a6536bef2faa6 Mon Sep 17 00:00:00 2001 From: Drew Parsons Date: Tue, 24 Aug 2021 00:59:29 +0200 Subject: [PATCH 2/3] run black to ensure PEP8 compliance --- .../tests/test_substitution_probability.py | 1 + pymatgen/analysis/tests/test_graphs.py | 8 ++------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py b/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py index 877de558ca3..41369638d7d 100644 --- a/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py +++ b/pymatgen/analysis/structure_prediction/tests/test_substitution_probability.py @@ -15,6 +15,7 @@ from pymatgen.core.periodic_table import Species from pymatgen.util.testing import PymatgenTest + def get_table(): """ Loads a lightweight lambda table for use in unit tests to reduce diff --git a/pymatgen/analysis/tests/test_graphs.py b/pymatgen/analysis/tests/test_graphs.py index 03d418aa60a..c8b7bb11699 100644 --- a/pymatgen/analysis/tests/test_graphs.py +++ b/pymatgen/analysis/tests/test_graphs.py @@ -223,9 +223,7 @@ def test_insert_remove(self): self.assertEqual(square_copy.graph.number_of_edges(), 3) def test_substitute(self): - structure = Structure.from_file( - os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif") - ) + structure = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif")) molecule = FunctionalGroups["methyl"] structure_copy = copy.deepcopy(structure) @@ -600,9 +598,7 @@ def setUp(self): [6, 0], [6, 2], ] - self.pc_frag1 = Molecule.from_file( - os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "PC_frag1.xyz") - ) + self.pc_frag1 = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "PC_frag1.xyz")) self.pc_frag1_edges = [[0, 2], [4, 2], [2, 1], [1, 3]] self.tfsi = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "graphs", "TFSI.xyz")) self.tfsi_edges = ( From 405a0b22c506077732ed0cabee82d2503ae9d0ce Mon Sep 17 00:00:00 2001 From: Shyue Ping Ong Date: Mon, 23 Aug 2021 16:46:20 -0700 Subject: [PATCH 3/3] Fix pylint. --- pymatgen/entries/computed_entries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymatgen/entries/computed_entries.py b/pymatgen/entries/computed_entries.py index 4d9657a67b5..cf77bed0650 100644 --- a/pymatgen/entries/computed_entries.py +++ b/pymatgen/entries/computed_entries.py @@ -701,7 +701,7 @@ def normalize(self, mode: str = "formula_unit") -> "ComputedStructureEntry": d = super().normalize(mode).as_dict() d["structure"] = self.structure.as_dict() entry = self.from_dict(d) - entry._composition /= factor # type: ignore + entry._composition /= factor # pylint: disable=E1101 return entry