forked from ChelseaKR/ceqa-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_synth.py
More file actions
81 lines (63 loc) · 3.12 KB
/
Copy pathtest_synth.py
File metadata and controls
81 lines (63 loc) · 3.12 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
76
77
78
79
80
81
"""Synthetic package generation and end-to-end defect detection."""
from __future__ import annotations
from pathlib import Path
import pytest
from ceqa_preflight.checker import check_package
from ceqa_preflight.manifest import load_manifest
from ceqa_preflight.models import FilingType
from ceqa_preflight.synth import SyntheticDefect, write_synthetic_package
def _statuses_by_rule(directory: Path, filing_type: FilingType) -> dict[str, set[str]]:
manifest = load_manifest(directory / "package.yaml")
report, _ = check_package(directory, filing_type, manifest=manifest, include_experimental=True)
statuses: dict[str, set[str]] = {}
for finding in [*report.findings, *report.manual_review]:
statuses.setdefault(finding.rule_id, set()).add(finding.status.value)
return statuses
def test_clean_synthetic_package_passes_all_automated_checks(tmp_path: Path) -> None:
directory = tmp_path / "clean"
created = write_synthetic_package(directory, FilingType.NOE, [])
assert (directory / "package.yaml") in created
banner = (directory / "NOE_Fictional_Example_Project_form.pdf").read_bytes()
assert b"Synthetic CEQA Preflight test data" in banner
manifest = load_manifest(directory / "package.yaml")
report, exit_code = check_package(
directory, FilingType.NOE, manifest=manifest, include_experimental=True
)
assert exit_code == 0
assert all(finding.status.value == "pass" for finding in report.findings)
def test_refuses_non_empty_directories(tmp_path: Path) -> None:
occupied = tmp_path / "occupied"
occupied.mkdir()
(occupied / "existing.txt").write_text("existing", encoding="utf-8")
blocked = tmp_path / "blocked.txt"
blocked.write_text("not a directory", encoding="utf-8")
with pytest.raises(FileExistsError):
write_synthetic_package(occupied, FilingType.NOE, [])
with pytest.raises(ValueError, match="must be a directory"):
write_synthetic_package(blocked, FilingType.NOE, [])
def test_each_seeded_defect_is_detected_by_its_rule(tmp_path: Path) -> None:
directory = tmp_path / "defects"
write_synthetic_package(
directory,
FilingType.NOD,
[
SyntheticDefect.ENCRYPTED,
SyntheticDefect.UNREADABLE,
SyntheticDefect.SCANNED,
SyntheticDefect.FILLABLE_FORM,
SyntheticDefect.DUPLICATE,
SyntheticDefect.NON_PDF,
SyntheticDefect.BAD_SIGNATURE,
SyntheticDefect.WEAK_FILENAME,
SyntheticDefect.MISSING_MANIFEST_REFERENCE,
],
)
statuses = _statuses_by_rule(directory, FilingType.NOD)
assert "failure" in statuses["PDF-001"] # bad signature
assert "failure" in statuses["PDF-002"] # encrypted and unreadable
assert "warning" in statuses["PDF-003"] # scanned, no searchable text
assert "warning" in statuses["PDF-007"] # fillable form
assert "warning" in statuses["FILE-001"] # weak filename
assert "warning" in statuses["FILE-002"] # duplicate hashes
assert "warning" in statuses["FILE-003"] # non-PDF document
assert "failure" in statuses["MAN-001"] # missing manifest reference