You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.3 KiB
61 lines
2.3 KiB
"""Synthetic checks for source-safe local import discovery.""" |
|
|
|
from hashlib import sha256 |
|
|
|
from django.test import override_settings |
|
|
|
from ingestion.guided_imports import discover_imports |
|
|
|
|
|
HEADER = b"Data,Dependencia,Historico,Documento,Tipo,Valor\n" |
|
|
|
|
|
def test_discovery_returns_only_safe_metadata_for_direct_supported_files(tmp_path): |
|
bank = HEADER + b"01/02/2026,,SYNTHETIC BANK ROW,,,12.34\n" |
|
full = b"1 - SYNTHETIC OWNER\n" + b"15.02.2026" + b"SYNTHETIC CARD ROW".ljust(37) + b"BR 12,34 0,00\n" |
|
nested = tmp_path / "nested" |
|
nested.mkdir() |
|
(tmp_path / "bank.csv").write_bytes(bank) |
|
(tmp_path / "card.txt").write_bytes(full) |
|
(nested / "hidden.csv").write_bytes(bank) |
|
|
|
with override_settings(IMPORT_ROOT=tmp_path): |
|
rows = discover_imports() |
|
|
|
assert [(row.name, row.profile, row.sections, row.code) for row in rows] == [ |
|
("bank.csv", "BANK_CSV_V1", ("1",), None), |
|
("card.txt", "OUROCARD_FULL_V1", ("1",), None), |
|
] |
|
assert [row.document_hash for row in rows] == [sha256(bank).hexdigest(), sha256(full).hexdigest()] |
|
assert all(not hasattr(row, "source_text") for row in rows) |
|
|
|
|
|
def test_discovery_reports_safe_codes_for_direct_unsupported_or_invalid_files(tmp_path): |
|
(tmp_path / "unknown.ofx").write_bytes(b"SYNTHETIC OFX") |
|
(tmp_path / "statement.pdf").write_bytes(b"%PDF-SYNTHETIC") |
|
(tmp_path / "invalid.txt").write_bytes(b"SYNTHETIC UNRECOGNIZED TEXT") |
|
(tmp_path / "oversize.csv").write_bytes(b"x" * (10 * 1024 * 1024 + 1)) |
|
|
|
with override_settings(IMPORT_ROOT=tmp_path): |
|
rows = discover_imports() |
|
|
|
assert [(row.name, row.profile, row.sections, row.code) for row in rows] == [ |
|
("invalid.txt", None, (), "UNRECOGNIZED_LAYOUT"), |
|
("oversize.csv", None, (), "INPUT_TOO_LARGE"), |
|
("statement.pdf", None, (), "UNSUPPORTED_FILE"), |
|
("unknown.ofx", None, (), "UNSUPPORTED_FILE"), |
|
] |
|
assert all(not hasattr(row, "source_text") for row in rows) |
|
|
|
|
|
def test_discovery_ignores_symlinks_and_non_files(tmp_path): |
|
bank = HEADER + b"01/02/2026,,SYNTHETIC BANK ROW,,,12.34\n" |
|
target = tmp_path / "target.csv" |
|
target.write_bytes(bank) |
|
(tmp_path / "linked.csv").symlink_to(target) |
|
(tmp_path / "directory.csv").mkdir() |
|
|
|
with override_settings(IMPORT_ROOT=tmp_path): |
|
rows = discover_imports() |
|
|
|
assert [row.name for row in rows] == ["target.csv"]
|
|
|