Skip to content

Commit

Permalink
made fixtures file agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
wolearyc committed Aug 12, 2024
1 parent b98598e commit ae95e4c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
4 changes: 2 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@


@pytest.fixture(scope="session")
def outcar_path_fixture(request: FixtureRequest) -> Path:
def path_fixture(request: FixtureRequest) -> Path:
"""Return an outcar path."""
return Path(request.param)


@pytest.fixture(scope="session")
def outcar_file_fixture(
def file_fixture(
request: FixtureRequest,
) -> Generator[TextIO, None, None]:
"""Return an outcar file."""
Expand Down
8 changes: 4 additions & 4 deletions test/tests/test_outcar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@pytest.mark.parametrize(
"outcar_path_fixture, known_num_atoms, known_wavenumbers,"
"path_fixture, known_num_atoms, known_wavenumbers,"
"known_first_displacement, known_last_displacement",
[
(
Expand All @@ -22,17 +22,17 @@
np.array([-0.011752, 0.074105, 0.000000]) / np.sqrt(ATOMIC_WEIGHTS["O"]),
),
],
indirect=["outcar_path_fixture"],
indirect=["path_fixture"],
)
def test_read_phonons_from_outcar(
outcar_path_fixture: Path,
path_fixture: Path,
known_num_atoms: int,
known_wavenumbers: NDArray[np.float64],
known_first_displacement: NDArray[np.float64],
known_last_displacement: NDArray[np.float64],
) -> None:
"""Test read_phonons for outcar (normal)."""
phonons = rn_io.read_phonons(outcar_path_fixture, file_format="outcar")
phonons = rn_io.read_phonons(path_fixture, file_format="outcar")

known_degrees_of_freedom = known_num_atoms * 3
assert phonons.get_wavenumbers().shape == (known_degrees_of_freedom,)
Expand Down
84 changes: 40 additions & 44 deletions test/tests/test_outcar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ def test_get_atomic_symbol_from_potcar_line_exception(


@pytest.mark.parametrize(
"outcar_file_fixture, known",
"file_fixture, known",
[
("test/data/TiO2/phonons_OUTCAR", ["Ti"] * 36 + ["O"] * 72),
(
"test/data/LLZO/LLZO_OUTCAR",
["Li"] * 56 + ["La"] * 24 + ["Zr"] * 16 + ["O"] * 96,
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_atomic_symbols_from_outcar(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
known: list[str],
) -> None:
"""Test _read_atomic_symbols_from_outcar (normal)."""
atomic_symbols = vasp_outcar._read_atomic_symbols(outcar_file_fixture)
atomic_symbols = vasp_outcar._read_atomic_symbols(file_fixture)
assert atomic_symbols == known


@pytest.mark.parametrize(
"outcar_file_fixture, exception_type, in_reason",
"file_fixture, exception_type, in_reason",
[
(
"test/data/invalid_vasp/no_elements_OUTCAR",
Expand All @@ -89,47 +89,45 @@ def test_read_atomic_symbols_from_outcar(
"ion number block could not be parsed",
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_atomic_symbols_from_outcar_exception(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
exception_type: Type[Exception],
in_reason: str,
) -> None:
"""Test _read_atomic_symbols_from_outcar (exception)."""
with pytest.raises(exception_type) as error:
vasp_outcar._read_atomic_symbols(outcar_file_fixture)
vasp_outcar._read_atomic_symbols(file_fixture)
assert in_reason in str(error.value)


@pytest.mark.parametrize(
"outcar_file_fixture, known_first_position, known_last_position",
"file_fixture, known_first_position, known_last_position",
[
(
"test/data/EPS_OUTCAR",
np.array([11.82301433, 0.00141878, 11.82095340]),
np.array([7.88377093, 9.85727498, 9.86042313]),
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_cartesian_positions_from_outcar(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
known_first_position: NDArray[np.float64],
known_last_position: NDArray[np.float64],
) -> None:
"""Test _read_cartesian_positions_from_outcar (normal)."""
cartesian_positions = vasp_outcar._read_cartesian_positions(
outcar_file_fixture, 135
)
cartesian_positions = vasp_outcar._read_cartesian_positions(file_fixture, 135)

assert len(cartesian_positions) == 135
assert np.isclose(cartesian_positions[0], known_first_position).all()
assert np.isclose(cartesian_positions[-1], known_last_position).all()


@pytest.mark.parametrize(
"outcar_file_fixture, exception_type, in_reason",
"file_fixture, exception_type, in_reason",
[
(
"test/data/invalid_vasp/empty_file",
Expand All @@ -142,47 +140,45 @@ def test_read_cartesian_positions_from_outcar(
"cartesian positions could not be parsed",
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_cartesian_positions_from_outcar_exception(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
exception_type: Type[Exception],
in_reason: str,
) -> None:
"""Test _read_cartesian_positions_from_outcar (exception)."""
with pytest.raises(exception_type) as error:
vasp_outcar._read_cartesian_positions(outcar_file_fixture, 20)
vasp_outcar._read_cartesian_positions(file_fixture, 20)
assert in_reason in str(error.value)


@pytest.mark.parametrize(
"outcar_file_fixture, known_first_position, known_last_position",
"file_fixture, known_first_position, known_last_position",
[
(
"test/data/EPS_OUTCAR",
np.array([0.999995547, 0.000120001, 0.999821233]),
np.array([0.666812676, 0.833732482, 0.833998755]),
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_fractional_positions_from_outcar(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
known_first_position: NDArray[np.float64],
known_last_position: NDArray[np.float64],
) -> None:
"""Test _read_fractional_positions_from_outcar (normal)."""
fractional_positions = vasp_outcar._read_fractional_positions(
outcar_file_fixture, 135
)
fractional_positions = vasp_outcar._read_fractional_positions(file_fixture, 135)

assert len(fractional_positions) == 135
assert np.isclose(fractional_positions[0], known_first_position).all()
assert np.isclose(fractional_positions[-1], known_last_position).all()


@pytest.mark.parametrize(
"outcar_file_fixture, exception_type, in_reason",
"file_fixture, exception_type, in_reason",
[
(
"test/data/invalid_vasp/empty_file",
Expand All @@ -195,21 +191,21 @@ def test_read_fractional_positions_from_outcar(
"fractional positions could not be parsed",
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_fractional_positions_from_outcar_exception(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
exception_type: Type[Exception],
in_reason: str,
) -> None:
"""Test _read_fractional_positions_from_outcar (exception)."""
with pytest.raises(exception_type) as error:
vasp_outcar._read_fractional_positions(outcar_file_fixture, 20)
vasp_outcar._read_fractional_positions(file_fixture, 20)
assert in_reason in str(error.value)


@pytest.mark.parametrize(
"outcar_file_fixture, known_polarizability, ",
"file_fixture, known_polarizability, ",
[
(
"test/data/EPS_OUTCAR",
Expand All @@ -222,20 +218,20 @@ def test_read_fractional_positions_from_outcar_exception(
),
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_polarizability_from_outcar(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
known_polarizability: NDArray[np.float64],
) -> None:
"""Test _read_polarizability_from_outcar (normal)."""
polarizability = vasp_outcar._read_polarizability(outcar_file_fixture)
polarizability = vasp_outcar._read_polarizability(file_fixture)

assert np.isclose(polarizability, known_polarizability).all()


@pytest.mark.parametrize(
"outcar_file_fixture, exception_type, in_reason",
"file_fixture, exception_type, in_reason",
[
(
"test/data/invalid_vasp/invalid_positions_OUTCAR",
Expand All @@ -248,21 +244,21 @@ def test_read_polarizability_from_outcar(
"polarizability could not be parsed",
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_polarizability_from_outcar_exception(
outcar_file_fixture: TextIO, # pylint: disable=redefined-outer-name
file_fixture: TextIO, # pylint: disable=redefined-outer-name
exception_type: Type[Exception],
in_reason: str,
) -> None:
"""Test _read_polarizability_from_outcar (normal)."""
with pytest.raises(exception_type) as error:
vasp_outcar._read_polarizability(outcar_file_fixture)
vasp_outcar._read_polarizability(file_fixture)
assert in_reason in str(error.value)


@pytest.mark.parametrize(
"outcar_file_fixture, known_lattice",
"file_fixture, known_lattice",
[
(
"test/data/TiO2/phonons_OUTCAR",
Expand All @@ -285,18 +281,18 @@ def test_read_polarizability_from_outcar_exception(
),
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_lattice_from_outcar(
outcar_file_fixture: TextIO, known_lattice: NDArray[np.float64]
file_fixture: TextIO, known_lattice: NDArray[np.float64]
) -> None:
"""Test _read_lattice_from_outcar (normal)."""
result = vasp_outcar._read_lattice(outcar_file_fixture)
result = vasp_outcar._read_lattice(file_fixture)
assert np.isclose(result, known_lattice).all()


@pytest.mark.parametrize(
"outcar_file_fixture, exception_type, in_reason",
"file_fixture, exception_type, in_reason",
[
(
"test/data/invalid_vasp/empty_file",
Expand All @@ -309,14 +305,14 @@ def test_read_lattice_from_outcar(
"lattice could not be parsed: ",
),
],
indirect=["outcar_file_fixture"],
indirect=["file_fixture"],
)
def test_read_lattice_from_outcar_exception(
outcar_file_fixture: TextIO,
file_fixture: TextIO,
exception_type: Type[Exception],
in_reason: str,
) -> None:
"""Test _read_lattice_from_outcar (exception)."""
with pytest.raises(exception_type) as error:
vasp_outcar._read_lattice(outcar_file_fixture)
vasp_outcar._read_lattice(file_fixture)
assert in_reason in str(error.value)

0 comments on commit ae95e4c

Please sign in to comment.