Skip to content

Commit cb213d9

Browse files
committed
test error messages
1 parent 56b4936 commit cb213d9

File tree

12 files changed

+63
-65
lines changed

12 files changed

+63
-65
lines changed

pymatgen/analysis/xas/tests/test_spectrum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ def test_site_weighted_spectrum(self):
126126
assert weighted_spectrum.y[0] == approx((4 * self.site1_xanes.y[0] + 2 * self.site2_xanes.y[0]) / 6, abs=1e-2)
127127
assert min(weighted_spectrum.x) == max(min(self.site1_xanes.x), min(self.site2_xanes.x))
128128
self.site2_xanes.absorbing_index = self.site1_xanes.absorbing_index
129-
with pytest.raises(ValueError):
129+
with pytest.raises(ValueError, match="Need at least two site-wise spectra to perform site-weighting"):
130130
site_weighted_spectrum([self.site1_xanes, self.site2_xanes])

pymatgen/command_line/enumlib_caller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def get_sg_info(ss):
266266
# enumeration. See Cu7Te5.cif test file.
267267
base *= 10
268268

269-
# base = ndisordered # 10 ** int(math.ceil(math.log10(ndisordered)))
269+
# base = n_disordered # 10 ** int(math.ceil(math.log10(n_disordered)))
270270
# To get a reasonable number of structures, we fix concentrations to the
271271
# range expected in the original structure.
272272
total_amounts = sum(index_amounts)
@@ -298,7 +298,7 @@ def _run_multienum(self):
298298
timer.cancel()
299299

300300
if timed_out:
301-
raise TimeoutError("Enumeration took too long.")
301+
raise TimeoutError("Enumeration took too long")
302302

303303
else:
304304
output = p.communicate()[0].decode("utf-8")

pymatgen/command_line/tests/test_enumlib_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ def test_timeout(self):
128128
SpacegroupAnalyzer(struct, 0.1)
129129
struct["Al3+"] = {"Al3+": 0.5, "Ga3+": 0.5}
130130
adaptor = EnumlibAdaptor(struct, 1, 1, enum_precision_parameter=0.01, timeout=0.0000000000001)
131-
with pytest.raises(TimeoutError):
131+
with pytest.raises(TimeoutError, match="Enumeration took too long"):
132132
adaptor._run_multienum()

pymatgen/command_line/tests/test_mcsqs_caller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_mcsqs_caller_supercell(self):
3333
sqs = run_mcsqs(struct, {2: 6, 3: 4}, scaling=[2, 1, 1], search_time=0.01, instances=1)
3434

3535
matches = [sqs.bestsqs.matches(s) for s in self.pzt_structs]
36-
assert True in matches
36+
assert any(matches)
3737

3838
assert isinstance(sqs.bestsqs, Structure)
3939

@@ -52,15 +52,15 @@ def test_mcsqs_caller_total_atoms(self):
5252
sqs = run_mcsqs(struct, {2: 6, 3: 4}, scaling=2, search_time=0.01, instances=1)
5353

5454
matches = [sqs.bestsqs.matches(s) for s in self.pzt_structs2]
55-
assert True in matches
55+
assert any(matches)
5656

5757
def test_mcsqs_caller_total_atoms_auto_instances(self):
5858
struct = self.struct.copy()
5959
struct.replace_species({"Ti": {"Ti": 0.5, "Zr": 0.5}, "Zr": {"Ti": 0.5, "Zr": 0.5}})
6060
sqs = run_mcsqs(struct, {2: 6, 3: 4}, scaling=2, search_time=0.01, instances=None)
6161

6262
matches = [sqs.bestsqs.matches(s) for s in self.pzt_structs2]
63-
assert True in matches
63+
assert any(matches)
6464

6565
def test_mcsqs_caller_parallel(self):
6666
# explicitly test with four instances
@@ -70,7 +70,7 @@ def test_mcsqs_caller_parallel(self):
7070
sqs = run_mcsqs(struct, {2: 6, 3: 4}, scaling=2, search_time=0.01, instances=4)
7171

7272
matches = [sqs.bestsqs.matches(s) for s in self.pzt_structs2]
73-
assert True in matches
73+
assert any(matches)
7474

7575
def test_mcsqs_perfect_match_error(self):
7676
scale = 32 / self.perfect_match_zzn_rs.num_sites
@@ -101,5 +101,5 @@ def test_mcsqs_caller_runtime_error(self):
101101
struct.replace_species({"Ti": {"Ti": 0.5, "Zr": 0.5}, "Zr": {"Ti": 0.5, "Zr": 0.5}})
102102
struct.replace_species({"Pb": {"Ti": 0.2, "Pb": 0.8}})
103103
struct.replace_species({"O": {"F": 0.8, "O": 0.2}})
104-
with pytest.raises(RuntimeError):
104+
with pytest.raises(RuntimeError, match="mcsqs exited before timeout reached"):
105105
run_mcsqs(struct, {2: 6, 3: 4}, 10, 0.000001)

pymatgen/core/tests/test_periodic_table.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,12 @@ def test_sort(self):
545545

546546
def test_immutable(self):
547547
sp = Species("Fe", 2, spin=5)
548-
with pytest.raises(AttributeError, match="property 'spin' of 'Species' object has no setter"):
548+
with pytest.raises(AttributeError) as exc:
549549
sp.spin = 6
550+
551+
assert "can't set attribute" in str(exc.value) or "property 'spin' of 'Species' object has no setter" in str(
552+
exc.value
553+
) # 'can't set attribute' on Linux, for some reason different message on Windows and Mac
550554
sp.properties["spin"] = 7
551555
assert sp.spin == 5
552556

pymatgen/electronic_structure/tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class OrbitalTest(unittest.TestCase):
2828
def test_init(self):
2929
for orb in Orbital:
3030
assert Orbital(orb.value) == orb
31-
with pytest.raises(ValueError):
31+
with pytest.raises(ValueError, match="100 is not a valid Orbital"):
3232
Orbital(100)
3333

3434
def test_cached(self):

pymatgen/ext/tests/test_matproj.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,15 @@ def test_get_structure_by_material_id(self):
169169
assert s1.formula == "Cs1"
170170

171171
# requesting via task-id instead of mp-id
172-
with pytest.warns(Warning):
172+
with pytest.warns(
173+
Warning,
174+
match="calculation task mp-698856 is mapped to canonical mp-id mp-1394, so structure for mp-1394 returned",
175+
):
173176
self.rester.get_structure_by_material_id("mp-698856")
174177

175178
# requesting unknown mp-id
176-
with pytest.raises(MPRestError):
179+
# TODO (janosh) this seems like the wrong error message for this case
180+
with pytest.raises(MPRestError, match="'id' is not a valid Element"):
177181
self.rester.get_structure_by_material_id("id-does-not-exist")
178182

179183
def test_get_entry_by_material_id(self):

pymatgen/io/babel.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
from pymatgen.core.structure import IMolecule, Molecule
1616

1717
try:
18-
from openbabel import openbabel
19-
from openbabel import pybel as pb
18+
from openbabel import openbabel, pybel
2019
except Exception:
2120
openbabel = None
2221

@@ -75,7 +74,7 @@ def __init__(self, mol):
7574
self._obmol = ob_mol
7675
elif isinstance(mol, openbabel.OBMol):
7776
self._obmol = mol
78-
elif isinstance(mol, pb.Molecule):
77+
elif isinstance(mol, pybel.Molecule):
7978
self._obmol = mol.OBMol
8079

8180
@property
@@ -102,9 +101,9 @@ def localopt(self, forcefield="mmff94", steps=500):
102101
'mmff94', 'mmff94s', and 'uff'.
103102
steps: Default is 500.
104103
"""
105-
pbmol = pb.Molecule(self._obmol)
106-
pbmol.localopt(forcefield=forcefield, steps=steps)
107-
self._obmol = pbmol.OBMol
104+
pybelmol = pybel.Molecule(self._obmol)
105+
pybelmol.localopt(forcefield=forcefield, steps=steps)
106+
self._obmol = pybelmol.OBMol
108107

109108
def make3d(self, forcefield="mmff94", steps=50):
110109
"""
@@ -125,9 +124,9 @@ def make3d(self, forcefield="mmff94", steps=50):
125124
'mmff94', 'mmff94s', and 'uff'.
126125
steps: Default is 50.
127126
"""
128-
pbmol = pb.Molecule(self._obmol)
129-
pbmol.make3D(forcefield=forcefield, steps=steps)
130-
self._obmol = pbmol.OBMol
127+
pybelmol = pybel.Molecule(self._obmol)
128+
pybelmol.make3D(forcefield=forcefield, steps=steps)
129+
self._obmol = pybelmol.OBMol
131130

132131
def add_hydrogen(self):
133132
"""Add hydrogens (make all hydrogen explicit)."""
@@ -288,7 +287,7 @@ def confab_conformers(
288287
@property
289288
def pybel_mol(self):
290289
"""Returns Pybel's Molecule object."""
291-
return pb.Molecule(self._obmol)
290+
return pybel.Molecule(self._obmol)
292291

293292
def write_file(self, filename, file_format="xyz"):
294293
"""
@@ -298,7 +297,7 @@ def write_file(self, filename, file_format="xyz"):
298297
filename: Filename of file to output
299298
file_format: String specifying any OpenBabel supported formats.
300299
"""
301-
mol = pb.Molecule(self._obmol)
300+
mol = pybel.Molecule(self._obmol)
302301
return mol.write(file_format, filename, overwrite=True)
303302

304303
@staticmethod
@@ -316,7 +315,7 @@ def from_file(filename, file_format="xyz", return_all_molecules=False):
316315
Returns:
317316
BabelMolAdaptor object or list thereof
318317
"""
319-
mols = pb.readfile(str(file_format), str(filename))
318+
mols = pybel.readfile(str(file_format), str(filename))
320319
if return_all_molecules:
321320
return [BabelMolAdaptor(mol.OBMol) for mol in mols]
322321

@@ -348,5 +347,5 @@ def from_string(string_data, file_format="xyz"):
348347
Returns:
349348
BabelMolAdaptor object
350349
"""
351-
mols = pb.readstring(str(file_format), str(string_data))
350+
mols = pybel.readstring(str(file_format), str(string_data))
352351
return BabelMolAdaptor(mols.OBMol)

pymatgen/io/lammps/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from pymatgen.util.coord import get_angle
1919

2020
try:
21-
from openbabel import pybel as pb
21+
from openbabel import pybel
2222
except ImportError:
23-
pb = None
23+
pybel = None
2424

2525
__author__ = "Kiran Mathew, Brandon Wood, Michael Humbert"
2626
__email__ = "[email protected]"
@@ -276,7 +276,7 @@ def _write_input(self, input_dir="."):
276276
# all other filetypes
277277
else:
278278
a = BabelMolAdaptor(mol)
279-
pm = pb.Molecule(a.openbabel_mol)
279+
pm = pybel.Molecule(a.openbabel_mol)
280280
pm.write(
281281
self.control_params["filetype"],
282282
filename=filename,
@@ -330,7 +330,7 @@ def write_pdb(mol, filename, name=None, num=None):
330330
name = name or f"ml{num}"
331331

332332
# bma = BabelMolAdaptor(mol)
333-
pbm = pb.Molecule(bma._obmol)
333+
pbm = pybel.Molecule(bma._obmol)
334334
for x in pbm.residues:
335335
x.OBResidue.SetName(name)
336336
x.OBResidue.SetNum(num)
@@ -353,7 +353,7 @@ def convert_obatoms_to_molecule(self, atoms, residue_name=None, site_property="f
353353
354354
Args:
355355
atoms ([OBAtom]): list of OBAtom objects
356-
residue_name (str): the key in self.map_residue_to_mol. Usec to
356+
residue_name (str): the key in self.map_residue_to_mol. Used to
357357
restore the site properties in the final packed molecule.
358358
site_property (str): the site property to be restored.
359359
@@ -407,7 +407,7 @@ def restore_site_properties(self, site_property="ff_map", filename=None):
407407

408408
filename = filename or self.control_params["output"]
409409
bma = BabelMolAdaptor.from_file(filename, "pdb")
410-
pbm = pb.Molecule(bma._obmol)
410+
pbm = pybel.Molecule(bma._obmol)
411411

412412
assert len(pbm.residues) == sum(x["number"] for x in self.param_list)
413413

pymatgen/io/tests/test_babel.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pymatgen.io.xyz import XYZ
1616
from pymatgen.util.testing import PymatgenTest
1717

18-
pytest.importorskip("openbabel")
18+
pybel = pytest.importorskip("openbabel.pybel")
1919

2020

2121
class BabelMolAdaptorTest(unittest.TestCase):
@@ -81,18 +81,14 @@ def test_localopt(self):
8181
assert site.distance(optmol[0]) == approx(1.09216, abs=1e-1)
8282

8383
def test_make3d(self):
84-
from openbabel import pybel as pb
85-
86-
mol_0d = pb.readstring("smi", "CCCC").OBMol
84+
mol_0d = pybel.readstring("smi", "CCCC").OBMol
8785
adaptor = BabelMolAdaptor(mol_0d)
8886
adaptor.make3d()
8987
assert mol_0d.GetDimension() == 3
9088

9189
def add_hydrogen(self):
92-
from openbabel import pybel as pb
93-
94-
mol_0d = pb.readstring("smi", "CCCC").OBMol
95-
assert len(pb.Molecule(mol_0d).atoms) == 2
90+
mol_0d = pybel.readstring("smi", "CCCC").OBMol
91+
assert len(pybel.Molecule(mol_0d).atoms) == 2
9692
adaptor = BabelMolAdaptor(mol_0d)
9793
adaptor.add_hydrogen()
9894
assert len(adaptor.pymatgen_mol) == 14
@@ -126,9 +122,7 @@ def test_rotor_search_rrs(self):
126122
assert site.distance(optmol[0]) == approx(1.09216, abs=1e-1)
127123

128124
def test_confab_conformers(self):
129-
from openbabel import pybel as pb
130-
131-
mol = pb.readstring("smi", "CCCC").OBMol
125+
mol = pybel.readstring("smi", "CCCC").OBMol
132126
adaptor = BabelMolAdaptor(mol)
133127
adaptor.make3d()
134128
conformers = adaptor.confab_conformers()

0 commit comments

Comments
 (0)