Skip to content

Commit

Permalink
Expanding test coverage + correct some return type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
benrich37 committed Sep 24, 2024
1 parent e61a6c3 commit 407ec14
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/pymatgen/io/jdftx/jdftxoutfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ def elec_iter(self) -> int:
raise AttributeError("Property elec_iter inaccessible due to empty jstrucs class field")

@property
def elec_e(self) -> int:
def elec_e(self) -> float:
"""Return the most recent electronic energy.
Return the most recent electronic energy.
Expand Down Expand Up @@ -1082,7 +1082,7 @@ def elec_grad_k(self) -> int:
raise AttributeError("Property elec_grad_k inaccessible due to empty jstrucs class field")

@property
def elec_alpha(self) -> int:
def elec_alpha(self) -> float:
"""Return the most recent electronic alpha.
Return the most recent electronic alpha.
Expand All @@ -1096,7 +1096,7 @@ def elec_alpha(self) -> int:
raise AttributeError("Property elec_alpha inaccessible due to empty jstrucs class field")

@property
def elec_linmin(self) -> int:
def elec_linmin(self) -> float:
"""Return the most recent electronic linmin.
Return the most recent electronic linmin.
Expand Down Expand Up @@ -1171,9 +1171,11 @@ def __getattr__(self, name: str) -> Any:
The value of the attribute
"""
if name not in self.__dict__:
if not hasattr(self.slices[-1], name):
raise AttributeError(f"{self.__class__.__name__} not found: {name}")
return getattr(self.slices[-1], name)
if len(self.slices):
if not hasattr(self.slices[-1], name):
raise AttributeError(f"{self.__class__.__name__} not found: {name}")
return getattr(self.slices[-1], name)
raise AttributeError(f"Property {name} inaccessible due to empty jstrucs class field")
return self.__dict__[name]

def __dir__(self) -> list:
Expand Down
94 changes: 94 additions & 0 deletions tests/io/jdftx/test_jdftxoutfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"nSlices": 1,
"t_s": 165.87,
"iter_type": None,
"prefix": "jdft",
}

example_latmin_known = {
Expand Down Expand Up @@ -84,6 +85,7 @@
"nSlices": 7,
"t_s": 314.16,
"iter_type": "LatticeMinimize",
"prefix": None,
}

example_ionmin_known = {
Expand Down Expand Up @@ -118,6 +120,7 @@
"nSlices": 1,
"t_s": 2028.57,
"iter_type": "IonicMinimize",
"prefix": None,
}


Expand Down Expand Up @@ -177,3 +180,94 @@ def test_JDFTXOutfile_fromfile(filename: PathLike, known: dict):
assert len(jout.slices) == known["nSlices"]
assert jout.t_s == approx(known["t_s"])
assert jout.jstrucs.iter_type == known["iter_type"]
assert jout.prefix == known["prefix"]


@pytest.mark.parametrize(
("varname"),
[
("prefix"),
("jstrucs"),
("jsettings_fluid"),
("jsettings_electronic"),
("jsettings_lattice"),
("jsettings_ionic"),
("xc_func"),
("lattice_initial"),
("lattice_final"),
("lattice"),
("a"),
("b"),
("c"),
("fftgrid"),
("geom_opt"),
("geom_opt_type"),
("efermi"),
("egap"),
("emin"),
("emax"),
("homo"),
("lumo"),
("homo_filling"),
("lumo_filling"),
("is_metal"),
("etype"),
("broadening_type"),
("broadening"),
("kgrid"),
("truncation_type"),
("truncation_radius"),
("pwcut"),
("rhocut"),
("pp_type"),
("total_electrons"),
("semicore_electrons"),
("valence_electrons"),
("total_electrons_uncharged"),
("semicore_electrons_uncharged"),
("valence_electrons_uncharged"),
("nbands"),
("atom_elements"),
("atom_elements_int"),
("atom_types"),
("spintype"),
("nspin"),
("nat"),
("atom_coords_initial"),
("atom_coords_final"),
("atom_coords"),
("has_solvation"),
("fluid"),
("is_gc"),
("eiter_type"),
("elecmindata"),
("stress"),
("strain"),
("iter"),
("e"),
("grad_k"),
("alpha"),
("linmin"),
("nelectrons"),
("abs_magneticmoment"),
("tot_magneticmoment"),
("mu"),
("elec_iter"),
("elec_e"),
("elec_grad_k"),
("elec_alpha"),
("elec_linmin"),
],
)
def test_JDFTXOutfile_expected_exceptions_empty_slices(
varname: str, dummy_filename: PathLike = ex_files_dir / Path("example_sp.out")
):
jout = JDFTXOutfile.from_file(dummy_filename)
# First test that the attribute can be called
val = getattr(jout, varname)
# Next test it was properly called
assert val is not None
# Next test the error when slices is empty
jout.slices = []
with pytest.raises(AttributeError):
getattr(jout, varname)

0 comments on commit 407ec14

Please sign in to comment.