|
| 1 | +from pathlib import Path |
| 2 | +from _pytest.capture import CaptureFixture |
| 3 | +from _pytest.monkeypatch import MonkeyPatch |
| 4 | + |
| 5 | +import rasa.model |
| 6 | +import rasa.cli.utils |
| 7 | + |
| 8 | + |
| 9 | +def monkeypatch_get_latest_model(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: |
| 10 | + latest_model = tmp_path / "my_test_model.tar.gz" |
| 11 | + monkeypatch.setattr(rasa.model, "get_latest_model", lambda: str(latest_model)) |
| 12 | + |
| 13 | + |
| 14 | +def test_get_sanitized_model_directory_when_not_passing_model( |
| 15 | + capsys: CaptureFixture, tmp_path: Path, monkeypatch: MonkeyPatch |
| 16 | +): |
| 17 | + from rasa.test import _get_sanitized_model_directory |
| 18 | + |
| 19 | + monkeypatch_get_latest_model(tmp_path, monkeypatch) |
| 20 | + |
| 21 | + # Create a fake model on disk so that `is_file` returns `True` |
| 22 | + latest_model = Path(rasa.model.get_latest_model()) |
| 23 | + latest_model.touch() |
| 24 | + |
| 25 | + # Input: default model file |
| 26 | + # => Should return containing directory |
| 27 | + new_modeldir = _get_sanitized_model_directory(str(latest_model)) |
| 28 | + captured = capsys.readouterr() |
| 29 | + assert not captured.out |
| 30 | + assert new_modeldir == str(latest_model.parent) |
| 31 | + |
| 32 | + |
| 33 | +def test_get_sanitized_model_directory_when_passing_model_file_explicitly( |
| 34 | + capsys: CaptureFixture, tmp_path: Path, monkeypatch: MonkeyPatch |
| 35 | +): |
| 36 | + from rasa.test import _get_sanitized_model_directory |
| 37 | + |
| 38 | + monkeypatch_get_latest_model(tmp_path, monkeypatch) |
| 39 | + |
| 40 | + other_model = tmp_path / "my_test_model1.tar.gz" |
| 41 | + assert str(other_model) != rasa.model.get_latest_model() |
| 42 | + other_model.touch() |
| 43 | + |
| 44 | + # Input: some file |
| 45 | + # => Should return containing directory and print a warning |
| 46 | + new_modeldir = _get_sanitized_model_directory(str(other_model)) |
| 47 | + captured = capsys.readouterr() |
| 48 | + assert captured.out |
| 49 | + assert new_modeldir == str(other_model.parent) |
| 50 | + |
| 51 | + |
| 52 | +def test_get_sanitized_model_directory_when_passing_other_input( |
| 53 | + capsys: CaptureFixture, tmp_path: Path, monkeypatch: MonkeyPatch |
| 54 | +): |
| 55 | + from rasa.test import _get_sanitized_model_directory |
| 56 | + |
| 57 | + monkeypatch_get_latest_model(tmp_path, monkeypatch) |
| 58 | + |
| 59 | + # Input: anything that is not an existing file |
| 60 | + # => Should return input |
| 61 | + modeldir = "random_dir" |
| 62 | + assert not Path(modeldir).is_file() |
| 63 | + new_modeldir = _get_sanitized_model_directory(modeldir) |
| 64 | + captured = capsys.readouterr() |
| 65 | + assert not captured.out |
| 66 | + assert new_modeldir == modeldir |
0 commit comments