|
| 1 | +"""E2E coverage for project variables around dollar-quoted text on every compiler engine.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from tests.e2e.src.sqlbuild.cli.commands.main.compile._test_types import ( |
| 11 | + DollarQuotedProjectVarCompileCase, |
| 12 | +) |
| 13 | +from tests.e2e.src.sqlbuild.cli.commands.shared.helpers import prepare_inline_project, run_sqb |
| 14 | + |
| 15 | +_ENGINES: tuple[str, ...] = ("python", "native", "native-preview") |
| 16 | +_PROJECT_TOML: str = 'name = "orders"\nadapter = "duckdb"\n\n[vars]\nregion = "north"\n' |
| 17 | +_MODEL_HEADER: str = "MODEL (description 'Order region labels.', materialized view);\n\n" |
| 18 | +_COMPILED_MODEL: Path = Path("target/compiled/models/order_labels.sql") |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "test_case", |
| 23 | + [ |
| 24 | + DollarQuotedProjectVarCompileCase( |
| 25 | + description=f"comment_markers_inside_dollar_quotes_are_text_{engine}", |
| 26 | + engine=engine, |
| 27 | + model_sql=( |
| 28 | + "SELECT $$--@@region$$ AS label, $tag$ it's /* $tag$ AS note, " |
| 29 | + "$$ /* $$ AS a, '@@region' AS b -- */\n" |
| 30 | + ), |
| 31 | + expected_fragment=( |
| 32 | + "SELECT $$--north$$ AS label, $tag$ it's /* $tag$ AS note, " |
| 33 | + "$$ /* $$ AS a, 'north' AS b -- */" |
| 34 | + ), |
| 35 | + ) |
| 36 | + for engine in _ENGINES |
| 37 | + ], |
| 38 | + ids=lambda case: case.description, |
| 39 | +) |
| 40 | +def test_given_dollar_quoted_model_sql_when_compiling_then_vars_substitute_inside_quotes( |
| 41 | + test_case: DollarQuotedProjectVarCompileCase, tmp_path: Path |
| 42 | +) -> None: |
| 43 | + project_dir: Path = prepare_inline_project( |
| 44 | + tmp_path=tmp_path, |
| 45 | + project_name="orders", |
| 46 | + repo_files={ |
| 47 | + "sqlbuild_project.toml": _PROJECT_TOML, |
| 48 | + "models/order_labels.sql": _MODEL_HEADER + test_case.model_sql, |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + result: subprocess.CompletedProcess[str] = run_sqb( |
| 53 | + command=("--no-color", "--compiler-engine", test_case.engine, "compile"), |
| 54 | + project_dir=project_dir, |
| 55 | + ) |
| 56 | + |
| 57 | + assert result.returncode == 0, result.stdout + result.stderr |
| 58 | + compiled_sql: str = (project_dir / _COMPILED_MODEL).read_text(encoding="utf-8") |
| 59 | + assert test_case.expected_fragment in compiled_sql |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "test_case", |
| 64 | + [ |
| 65 | + DollarQuotedProjectVarCompileCase( |
| 66 | + description=f"unclosed_dollar_quote_reports_interpolation_error_{engine}", |
| 67 | + engine=engine, |
| 68 | + model_sql="SELECT $tag$ @@region AS label\n", |
| 69 | + expected_fragment="error[P001]: SQL interpolation contains an unclosed quoted string", |
| 70 | + ) |
| 71 | + for engine in _ENGINES |
| 72 | + ], |
| 73 | + ids=lambda case: case.description, |
| 74 | +) |
| 75 | +def test_given_unclosed_dollar_quote_with_var_when_compiling_then_interpolation_reports_it( |
| 76 | + test_case: DollarQuotedProjectVarCompileCase, tmp_path: Path |
| 77 | +) -> None: |
| 78 | + project_dir: Path = prepare_inline_project( |
| 79 | + tmp_path=tmp_path, |
| 80 | + project_name="orders", |
| 81 | + repo_files={ |
| 82 | + "sqlbuild_project.toml": _PROJECT_TOML, |
| 83 | + "models/order_labels.sql": _MODEL_HEADER + test_case.model_sql, |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | + result: subprocess.CompletedProcess[str] = run_sqb( |
| 88 | + command=("--no-color", "--compiler-engine", test_case.engine, "compile"), |
| 89 | + project_dir=project_dir, |
| 90 | + ) |
| 91 | + |
| 92 | + output: str = result.stdout + result.stderr |
| 93 | + assert result.returncode == 1, output |
| 94 | + assert test_case.expected_fragment in output |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + pytest.main([__file__, "-vv"]) |
0 commit comments