|
| 1 | +"""The file picker only falls back for unavailable toolbox data.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from machinevisiontoolbox.base import imageio |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def picker(monkeypatch): |
| 14 | + dialog = Mock(return_value="selected.png") |
| 15 | + root = Mock() |
| 16 | + tk = SimpleNamespace( |
| 17 | + Tk=Mock(return_value=root), filedialog=SimpleNamespace(askopenfilename=dialog) |
| 18 | + ) |
| 19 | + monkeypatch.setitem(__import__("sys").modules, "tkinter", tk) |
| 20 | + return dialog, root |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "error", [ModuleNotFoundError("mvtbdata"), ValueError("missing images")] |
| 25 | +) |
| 26 | +def test_missing_data_uses_home(monkeypatch, picker, error): |
| 27 | + monkeypatch.setattr(imageio, "mvtb_path_to_datafile", Mock(side_effect=error)) |
| 28 | + dialog, root = picker |
| 29 | + assert imageio._pick_imagefile() == Path("selected.png") |
| 30 | + assert dialog.call_args.kwargs["initialdir"] == str(Path.home()) |
| 31 | + root.destroy.assert_called_once() |
| 32 | + |
| 33 | + |
| 34 | +def test_data_directory_is_used(monkeypatch, picker, tmp_path): |
| 35 | + monkeypatch.setattr(imageio, "mvtb_path_to_datafile", Mock(return_value=tmp_path)) |
| 36 | + dialog, _ = picker |
| 37 | + assert imageio._pick_imagefile() == Path("selected.png") |
| 38 | + assert dialog.call_args.kwargs["initialdir"] == str(tmp_path) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "error", [RuntimeError("broken resolver"), KeyboardInterrupt()] |
| 43 | +) |
| 44 | +def test_unexpected_resolver_errors_propagate(monkeypatch, picker, error): |
| 45 | + monkeypatch.setattr(imageio, "mvtb_path_to_datafile", Mock(side_effect=error)) |
| 46 | + with pytest.raises(type(error)): |
| 47 | + imageio._pick_imagefile() |
| 48 | + picker[0].assert_not_called() |
0 commit comments