-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_compare_cli.py
More file actions
75 lines (54 loc) · 2.55 KB
/
Copy pathtest_compare_cli.py
File metadata and controls
75 lines (54 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import numpy as np
import pytest
from pyspatialml import compare_cli
def test_compare_paths_identical_files_pass(capsys, tmp_path):
expected = tmp_path / "expected.npy"
actual = tmp_path / "actual.npy"
np.save(expected, np.array([1.0, 2.0], dtype=np.float32))
np.save(actual, np.array([1.0, 2.0], dtype=np.float32))
assert compare_cli.compare_paths(expected, actual) == 0
captured = capsys.readouterr()
assert "passed: yes" in captured.out
assert "max_abs_diff: 0" in captured.out
def test_compare_paths_mismatch_returns_compare_exit(capsys, tmp_path):
expected = tmp_path / "expected.npy"
actual = tmp_path / "actual.npy"
np.save(expected, np.array([1.0, 2.0], dtype=np.float32))
np.save(actual, np.array([1.0, 3.0], dtype=np.float32))
assert compare_cli.compare_paths(expected, actual, rtol=1e-6, atol=1e-6) == 4
captured = capsys.readouterr()
assert "passed: no" in captured.out
assert "max_abs_diff: 1" in captured.out
def test_compare_paths_json_output(capsys, tmp_path):
expected = tmp_path / "expected.npy"
actual = tmp_path / "actual.npy"
np.save(expected, np.array([1.0, 2.0], dtype=np.float32))
np.save(actual, np.array([1.0, 2.001], dtype=np.float32))
assert compare_cli.compare_paths(expected, actual, rtol=1e-2, atol=1e-2, as_json=True) == 0
payload = json.loads(capsys.readouterr().out)
assert payload["passed"] is True
assert payload["comparisons"][0]["name"] == "expected.npy"
assert payload["comparisons"][0]["max_abs_diff"] > 0
def test_compare_directories_report_missing_and_extra(capsys, tmp_path):
expected = tmp_path / "expected"
actual = tmp_path / "actual"
expected.mkdir()
actual.mkdir()
np.save(expected / "a.npy", np.array([1], dtype=np.float32))
np.save(expected / "missing.npy", np.array([1], dtype=np.float32))
np.save(actual / "a.npy", np.array([1], dtype=np.float32))
np.save(actual / "extra.npy", np.array([1], dtype=np.float32))
assert compare_cli.compare_paths(expected, actual) == 4
captured = capsys.readouterr()
assert "Missing actual:" in captured.out
assert "missing.npy" in captured.out
assert "Extra actual:" in captured.out
assert "extra.npy" in captured.out
def test_compare_rejects_non_npy_file(tmp_path):
expected = tmp_path / "expected.bin"
actual = tmp_path / "actual.bin"
expected.write_bytes(b"1")
actual.write_bytes(b"1")
with pytest.raises(compare_cli.CompareCliError, match="Only .npy files"):
compare_cli.compare_paths(expected, actual)