|
| 1 | +"""Failure messages show authored text as written and escape only raw path names.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from sqlbuild.compiler.discovery.models import DiscoveryFileFault |
| 11 | +from tests.integration.src.sqlbuild.compiler.discovery._test_types import ( |
| 12 | + FailureTextTestCase, |
| 13 | + TolerantFailureTextTestCase, |
| 14 | +) |
| 15 | +from tests.integration.src.sqlbuild.compiler.discovery.helpers import ( |
| 16 | + stage_outcome, |
| 17 | + tolerant_declaration_outcome, |
| 18 | + write_project, |
| 19 | +) |
| 20 | + |
| 21 | +_ENGINES: tuple[str, ...] = ("python", "native", "native-preview") |
| 22 | +_MODEL: tuple[str, bytes] = ("models/orders.sql", b"MODEL ();\nSELECT 1 AS order_id") |
| 23 | +_HOOK: tuple[str, bytes] = ("hooks/sql/refresh.sql", b"HOOK (descri\x00ption 'x');\nSELECT 1") |
| 24 | +_CONSTANT: tuple[str, bytes] = ("constants/limits.sql", b"CONSTANT (name cap, valu\x00e 1);") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "test_case", |
| 29 | + [ |
| 30 | + FailureTextTestCase( |
| 31 | + description="a NUL in a model header key", |
| 32 | + files=(("models/orders.sql", b"MODEL (descri\x00ption 'x');\nSELECT 1"),), |
| 33 | + expected_error_type="ModelSqlParseError", |
| 34 | + expected_message_suffix="has unsupported keys: descri\x00ption", |
| 35 | + ), |
| 36 | + FailureTextTestCase( |
| 37 | + description="a NUL in a SQL hook header key", |
| 38 | + files=(_MODEL, _HOOK), |
| 39 | + expected_error_type="SqlHookParseError", |
| 40 | + expected_message_suffix="has unsupported keys: descri\x00ption", |
| 41 | + ), |
| 42 | + FailureTextTestCase( |
| 43 | + description="a NUL in a constant key", |
| 44 | + files=(_MODEL, _CONSTANT), |
| 45 | + expected_error_type="DeclarationParseError", |
| 46 | + expected_message_suffix="constant has unknown keys: valu\x00e", |
| 47 | + ), |
| 48 | + FailureTextTestCase( |
| 49 | + description="a NUL in an audit header key", |
| 50 | + files=( |
| 51 | + _MODEL, |
| 52 | + ("audits/generic/positive.sql", b"AUDIT (sever\x00ity warn);\nSELECT 1"), |
| 53 | + ), |
| 54 | + expected_error_type="SqlAuditParseError", |
| 55 | + expected_message_suffix="has unsupported keys: sever\x00ity", |
| 56 | + ), |
| 57 | + FailureTextTestCase( |
| 58 | + description="a NUL in a SQL function header key", |
| 59 | + files=( |
| 60 | + _MODEL, |
| 61 | + ( |
| 62 | + "functions/sql/cents.sql", |
| 63 | + b'FUNCTION (arguments (), returns INTEGER, descri\x00ption "x");\nSELECT 1', |
| 64 | + ), |
| 65 | + ), |
| 66 | + expected_error_type="ModelSqlParseError", |
| 67 | + expected_message_suffix="has unsupported keys: descri\x00ption", |
| 68 | + ), |
| 69 | + FailureTextTestCase( |
| 70 | + description="a raw entry name in a declaration group", |
| 71 | + files=(_MODEL, (os.fsdecode(b"models/orders/_sqlbuild/caf\xe9.txt"), b"x")), |
| 72 | + expected_error_type="DeclarationParseError", |
| 73 | + expected_message_suffix="unsupported entries: models/orders/_sqlbuild/caf\\xe9.txt", |
| 74 | + ), |
| 75 | + ], |
| 76 | + ids=lambda case: case.description, |
| 77 | +) |
| 78 | +def test_given_failing_project_when_discovering_then_every_engine_shows_authored_text( |
| 79 | + test_case: FailureTextTestCase, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 80 | +) -> None: |
| 81 | + write_project(project_dir=tmp_path, files=test_case.files) |
| 82 | + |
| 83 | + outcomes: list[tuple[object, ...]] = [ |
| 84 | + stage_outcome(project_dir=tmp_path, engine=engine, monkeypatch=monkeypatch) |
| 85 | + for engine in _ENGINES |
| 86 | + ] |
| 87 | + |
| 88 | + assert {outcome[1:] for outcome in outcomes} == {outcomes[0][1:]} |
| 89 | + assert outcomes[0][1] == test_case.expected_error_type |
| 90 | + assert str(outcomes[0][2]).endswith(test_case.expected_message_suffix) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "test_case", |
| 95 | + [ |
| 96 | + TolerantFailureTextTestCase( |
| 97 | + description="NUL header keys are faulted with their authored text", |
| 98 | + files=(_MODEL, _HOOK, _CONSTANT), |
| 99 | + expected_fault_keys=("descri\x00ption", "valu\x00e"), |
| 100 | + ) |
| 101 | + ], |
| 102 | + ids=lambda case: case.description, |
| 103 | +) |
| 104 | +def test_given_nul_header_keys_when_discovering_tolerantly_then_faults_match_python( |
| 105 | + test_case: TolerantFailureTextTestCase, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 106 | +) -> None: |
| 107 | + write_project(project_dir=tmp_path, files=test_case.files) |
| 108 | + python: tuple[object, tuple[DiscoveryFileFault, ...], tuple[DiscoveryFileFault, ...]] = ( |
| 109 | + tolerant_declaration_outcome(project_dir=tmp_path, engine="python", monkeypatch=monkeypatch) |
| 110 | + ) |
| 111 | + |
| 112 | + native: tuple[object, tuple[DiscoveryFileFault, ...], tuple[DiscoveryFileFault, ...]] = ( |
| 113 | + tolerant_declaration_outcome( |
| 114 | + project_dir=tmp_path, engine="native-preview", monkeypatch=monkeypatch |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + assert native == python |
| 119 | + assert ( |
| 120 | + tuple(sorted(fault.message.rsplit("keys: ", 1)[1] for fault in (*native[1], *native[2]))) |
| 121 | + == test_case.expected_fault_keys |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + pytest.main([__file__, "-vv"]) |
0 commit comments