Skip to content

Commit ea57a44

Browse files
committed
Simplify some FermiSolver tests
1 parent 28f6072 commit ea57a44

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

tests/test_fermisolver.py

+29-49
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import unittest
99
import warnings
10+
from copy import deepcopy
1011

1112
# Check if py_sc_fermi is available
1213
from importlib.util import find_spec
@@ -886,17 +887,14 @@ def test_parse_and_check_grid_like_chempots(self):
886887

887888
def test_parse_and_check_grid_like_chempots_invalid_chempots(self):
888889
"""
889-
Test that ValueError is raised when chempots is None.
890+
Test that ``ValueError`` is raised when ``chempots`` is ``None``.
890891
"""
891892
# Temporarily remove chempots from defect_thermodynamics
892-
original_chempots = self.defect_thermodynamics.chempots
893-
self.defect_thermodynamics.chempots = None
893+
solver = deepcopy(self.solver_doped)
894+
solver.defect_thermodynamics.chempots = None
894895

895896
with pytest.raises(ValueError):
896-
self.solver_doped._parse_and_check_grid_like_chempots()
897-
898-
# Restore original chempots
899-
self.defect_thermodynamics.chempots = original_chempots
897+
solver._parse_and_check_grid_like_chempots()
900898

901899

902900
class TestFermiSolverWithLoadedData3D(unittest.TestCase):
@@ -991,52 +989,34 @@ def test_scan_chemical_potential_grid(self, mock_tqdm):
991989
].drop_duplicates()
992990
assert len(unique_chempot_sets) > 0
993991

994-
def test_scan_chemical_potential_grid_no_chempots(self):
995-
"""
996-
Test that ``ValueError`` is raised when no chempots are provided and
997-
none are set.
998-
"""
999-
# TODO: Check if changing to deepcopy and remove is significantly slower, if not do this instead
1000-
# (less code)
1001-
# Temporarily remove chempots from defect_thermodynamics
1002-
original_chempots = self.defect_thermodynamics.chempots
1003-
self.defect_thermodynamics.chempots = None
1004-
1005-
with pytest.raises(ValueError):
1006-
self.solver_doped.scan_chemical_potential_grid(
1007-
n_points=5,
1008-
annealing_temperature=800,
1009-
quenched_temperature=300,
1010-
)
1011-
1012-
# Restore original chempots
1013-
self.defect_thermodynamics.chempots = original_chempots
1014-
1015992
def test_scan_chemical_potential_grid_wrong_chempots(self):
1016993
"""
1017-
Test the error message when chemical potentials in the wrong format
1018-
(i.e. just one user-provided chemical potential limit) are provided.
994+
Test that ``ValueError`` is raised when no chempots are provided and
995+
None are available in ``self.defect_thermodynamics``, or only a single
996+
limit is provided.
1019997
"""
1020998
# Temporarily remove chempots from defect_thermodynamics
1021-
original_chempots = self.defect_thermodynamics.chempots
1022-
self.defect_thermodynamics.chempots = None
1023-
1024-
with pytest.raises(ValueError) as exc:
1025-
self.solver_doped.scan_chemical_potential_grid(
1026-
n_points=5,
1027-
chempots={"Cu": -0.5, "Si": -1.0, "Se": 2},
1028-
annealing_temperature=800,
1029-
quenched_temperature=300,
1030-
)
1031-
print(str(exc.value))
1032-
assert (
1033-
"Only one chemical potential limit is present in "
1034-
"`chempots`/`self.defect_thermodynamics.chempots`, which makes no sense for a chemical "
1035-
"potential grid scan"
1036-
) in str(exc.value)
1037-
1038-
# Restore original chempots
1039-
self.defect_thermodynamics.chempots = original_chempots
999+
solver = deepcopy(self.solver_doped)
1000+
solver.defect_thermodynamics.chempots = None
1001+
1002+
for chempot_kwargs in [
1003+
{},
1004+
{"chempots": {"Cu": -0.5, "Si": -1.0, "Se": 2}},
1005+
]:
1006+
print(f"Testing with {chempot_kwargs}")
1007+
with pytest.raises(ValueError) as exc:
1008+
solver.scan_chemical_potential_grid(
1009+
n_points=5,
1010+
annealing_temperature=800,
1011+
quenched_temperature=300,
1012+
**chempot_kwargs,
1013+
)
1014+
print(str(exc.value))
1015+
assert (
1016+
"Only one chemical potential limit is present in "
1017+
"`chempots`/`self.defect_thermodynamics.chempots`, which makes no sense for a chemical "
1018+
"potential grid scan"
1019+
) in str(exc.value)
10401020

10411021

10421022
# TODO: Add explicit type check for `min_max_X` functions, like:

0 commit comments

Comments
 (0)