Skip to content

Commit

Permalink
Fix TestPotcar.test_write polluting git repo on failure (#3347)
Browse files Browse the repository at this point in the history
* improve StructureError msg on species/coord length mismatch

* fix TestPotcar.test_write polluting git repo on failure
  • Loading branch information
janosh authored Sep 23, 2023
1 parent 7aa84e2 commit 5c16854
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ def __init__(
lost when converting to other formats.
"""
if len(species) != len(coords):
raise StructureError("atomic species and fractional coordinates must have same length")
raise StructureError(f"{len(species)=} != {len(coords)=}")

self._lattice = lattice if isinstance(lattice, Lattice) else Lattice(lattice)

Expand Down
2 changes: 1 addition & 1 deletion pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ def __init__(self, data, symbol=None):
has_sh256, hash_check_passed = self.verify_potcar()
if not has_sh256 and not hash_check_passed:
warnings.warn(
f"POTCAR data with symbol { self.symbol} does not match any VASP "
f"POTCAR data with symbol {self.symbol} does not match any VASP "
"POTCAR known to pymatgen. There is a possibility your "
"POTCAR is corrupted or that the pymatgen database is incomplete.",
UnknownPotcarWarning,
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ def test_set_item(self):
assert struct.formula == "Si1.25 C0.125"

def test_init_error(self):
with pytest.raises(StructureError, match="atomic species and fractional coordinates must have same length"):
with pytest.raises(StructureError, match=r"len\(species\)=1 != len\(coords\)=2"):
Structure(Lattice.cubic(3), ["Si"], [[0, 0, 0], [0.5, 0.5, 0.5]])

def test_from_sites(self):
Expand Down
20 changes: 8 additions & 12 deletions tests/io/vasp/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ def test_repr(self):
)


class TestPotcar(unittest.TestCase):
class TestPotcar(PymatgenTest):
def setUp(self):
if "PMG_VASP_PSP_DIR" not in SETTINGS:
SETTINGS["PMG_VASP_PSP_DIR"] = str(TEST_FILES_DIR)
Expand Down Expand Up @@ -1125,24 +1125,20 @@ def test_as_from_dict(self):
assert potcar.symbols == ["Fe", "P", "O"]

def test_write(self):
tempfname = Path("POTCAR.testing")
self.potcar.write_file(tempfname)
p = Potcar.from_file(tempfname)
tmp_file = f"{self.tmp_path}/POTCAR.testing"
self.potcar.write_file(tmp_file)
p = Potcar.from_file(tmp_file)
assert p.symbols == self.potcar.symbols

# check line by line
with open(self.filepath) as f_ref, open(tempfname) as f_new:
with open(self.filepath) as f_ref, open(tmp_file) as f_new:
ref_potcar = f_ref.readlines()
new_potcar = f_new.readlines()

if len(ref_potcar) != len(new_potcar):
raise AssertionError("POTCAR file lengths are not equal")
assert len(ref_potcar) == len(new_potcar), f"wrong POTCAR line count: {len(ref_potcar)} != {len(new_potcar)}"

# check line by line
for line1, line2 in zip(ref_potcar, new_potcar):
if line1.strip() != line2.strip():
raise AssertionError("POTCAR contents are not")

tempfname.unlink()
assert line1.strip() == line2.strip(), f"wrong POTCAR line: {line1} != {line2}"

def test_set_symbol(self):
assert self.potcar.symbols == ["Fe", "P", "O"]
Expand Down

0 comments on commit 5c16854

Please sign in to comment.