Skip to content

Commit

Permalink
Add tests for CLI yaml summary
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Mar 28, 2024
1 parent 2b11c39 commit ae58f7d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 15 deletions.
82 changes: 75 additions & 7 deletions tests/test_geomopt_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ase.io import read
import pytest
from typer.testing import CliRunner
import yaml

from janus_core.cli import app
from tests.utils import read_atoms
Expand All @@ -14,17 +15,19 @@
runner = CliRunner()


def test_geomopt_help():
def test_help():
"""Test calling `janus geomopt --help`."""
result = runner.invoke(app, ["geomopt", "--help"])
assert result.exit_code == 0
# Command is returned as "root"
assert "Usage: root geomopt [OPTIONS]" in result.stdout


def test_geomopt():
def test_geomopt(tmp_path):
"""Test geomopt calculation."""
results_path = Path("./NaCl-opt.xyz").absolute()
summary_path = tmp_path / "summary.yml"

assert not results_path.exists()

result = runner.invoke(
Expand All @@ -35,16 +38,20 @@ def test_geomopt():
DATA_PATH / "NaCl.cif",
"--max-force",
"0.2",
"--summary",
summary_path,
],
)

read_atoms(results_path)
assert result.exit_code == 0


def test_geomopt_log(tmp_path, caplog):
def test_log(tmp_path, caplog):
"""Test log correctly written for geomopt."""
results_path = tmp_path / "NaCl-opt.xyz"
log_path = tmp_path / "test.log"
summary_path = tmp_path / "summary.yml"

with caplog.at_level("INFO", logger="janus_core.geom_opt"):
result = runner.invoke(
Expand All @@ -56,18 +63,21 @@ def test_geomopt_log(tmp_path, caplog):
"--out",
results_path,
"--log",
f"{tmp_path}/test.log",
log_path,
"--summary",
summary_path,
],
)
assert result.exit_code == 0
assert "Starting geometry optimization" in caplog.text
assert "Using filter" not in caplog.text


def test_geomopt_traj(tmp_path):
def test_traj(tmp_path):
"""Test trajectory correctly written for geomopt."""
results_path = tmp_path / "NaCl-opt.xyz"
traj_path = f"{tmp_path}/test.xyz"
summary_path = tmp_path / "summary.yml"

result = runner.invoke(
app,
Expand All @@ -79,6 +89,8 @@ def test_geomopt_traj(tmp_path):
results_path,
"--traj",
traj_path,
"--summary",
summary_path,
],
)
assert result.exit_code == 0
Expand All @@ -89,6 +101,7 @@ def test_geomopt_traj(tmp_path):
def test_fully_opt(tmp_path, caplog):
"""Test passing --fully-opt without --vectors-only"""
results_path = tmp_path / "NaCl-opt.xyz"
summary_path = tmp_path / "summary.yml"

with caplog.at_level("INFO", logger="janus_core.geom_opt"):
result = runner.invoke(
Expand All @@ -100,6 +113,8 @@ def test_fully_opt(tmp_path, caplog):
"--out",
results_path,
"--fully-opt",
"--summary",
summary_path,
],
)
assert result.exit_code == 0
Expand All @@ -114,6 +129,9 @@ def test_fully_opt(tmp_path, caplog):
def test_fully_opt_and_vectors(tmp_path, caplog):
"""Test passing --fully-opt with --vectors-only."""
results_path = tmp_path / "NaCl-opt.xyz"
log_path = tmp_path / "test.log"
summary_path = tmp_path / "summary.yml"

with caplog.at_level("INFO", logger="janus_core.geom_opt"):
result = runner.invoke(
app,
Expand All @@ -126,7 +144,9 @@ def test_fully_opt_and_vectors(tmp_path, caplog):
"--out",
results_path,
"--log",
f"{tmp_path}/test.log",
log_path,
"--summary",
summary_path,
],
)
assert result.exit_code == 0
Expand All @@ -141,6 +161,8 @@ def test_fully_opt_and_vectors(tmp_path, caplog):
def test_vectors_not_fully_opt(tmp_path, caplog):
"""Test passing --vectors-only without --fully-opt."""
results_path = tmp_path / "NaCl-opt.xyz"
summary_path = tmp_path / "summary.yml"

with caplog.at_level("INFO", logger="janus_core.geom_opt"):
result = runner.invoke(
app,
Expand All @@ -151,15 +173,19 @@ def test_vectors_not_fully_opt(tmp_path, caplog):
"--out",
results_path,
"--vectors-only",
"--summary",
summary_path,
],
)
assert result.exit_code == 0
assert "hydrostatic_strain: True" in caplog.text


def duplicate_traj(tmp_path):
def test_duplicate_traj(tmp_path):
"""Test trajectory file cannot be not passed via traj_kwargs."""
traj_path = tmp_path / "NaCl-traj.xyz"
summary_path = tmp_path / "summary.yml"

result = runner.invoke(
app,
[
Expand All @@ -168,6 +194,8 @@ def duplicate_traj(tmp_path):
DATA_PATH / "NaCl.cif",
"--opt-kwargs",
f"{{'trajectory': '{str(traj_path)}'}}",
"--summary",
summary_path,
],
)
assert result.exit_code == 1
Expand All @@ -179,6 +207,7 @@ def test_restart(tmp_path):
data_path = DATA_PATH / "NaCl-deformed.cif"
restart_path = tmp_path / "NaCl-res.pkl"
results_path = tmp_path / "NaCl-opt.xyz"
summary_path = tmp_path / "summary.yml"

result = runner.invoke(
app,
Expand All @@ -192,6 +221,8 @@ def test_restart(tmp_path):
f"{{'restart': '{str(restart_path)}'}}",
"--steps",
2,
"--summary",
summary_path,
],
)
assert result.exit_code == 0
Expand All @@ -210,9 +241,46 @@ def test_restart(tmp_path):
f"{{'restart': '{str(restart_path)}'}}",
"--steps",
2,
"--summary",
summary_path,
],
)
assert result.exit_code == 0
atoms = read(results_path)
final_energy = atoms.get_potential_energy()
assert final_energy < intermediate_energy


def test_summary(tmp_path):
"""Test summary file can be read correctly."""
results_path = tmp_path / "NaCl-results.xyz"
summary_path = tmp_path / "summary.yml"

result = runner.invoke(
app,
[
"geomopt",
"--struct",
DATA_PATH / "NaCl.cif",
"--out",
results_path,
"--summary",
summary_path,
],
)

assert result.exit_code == 0

# Read geomopt summary file
with open(summary_path, encoding="utf8") as file:
md_summary = yaml.safe_load(file)

assert "command" in md_summary[0]
assert "janus geomopt" in md_summary[0]["command"]
assert "start_time" in md_summary[1]
assert "end_time" in md_summary[3]

assert "inputs" in md_summary[2]
assert "opt_kwargs" in md_summary[2]["inputs"]
assert "struct" in md_summary[2]["inputs"]
assert "n_atoms" in md_summary[2]["inputs"]["struct"]
3 changes: 2 additions & 1 deletion tests/test_md_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_md(ensemble, tmp_path):
assert isinstance(atoms, Atoms)


def test_md_log(tmp_path, caplog):
def test_log(tmp_path, caplog):
"""Test log correctly written for MD."""
file_prefix = tmp_path / "nvt-T300"
stats_path = tmp_path / "nvt-T300-stats.dat"
Expand Down Expand Up @@ -225,6 +225,7 @@ def test_summary(tmp_path):
summary = yaml.safe_load(file)

assert "command" in summary[0]
assert "janus md" in summary[0]["command"]
assert "start_time" in summary[1]
assert "inputs" in summary[2]
assert "end_time" in summary[3]
Expand Down
Loading

0 comments on commit ae58f7d

Please sign in to comment.