|
| 1 | +import pytest |
| 2 | + |
1 | 3 | from sqlmesh.core.console import MarkdownConsole |
| 4 | +from sqlmesh.core.context_diff import ContextDiff |
| 5 | +from sqlmesh.core.dialect import parse |
| 6 | +from sqlmesh.core.environment import EnvironmentNamingInfo, EnvironmentStatements |
| 7 | +from sqlmesh.core.model import load_sql_based_model |
| 8 | +from sqlmesh.core.snapshot import SnapshotChangeCategory |
| 9 | +from sqlmesh.utils.metaprogramming import Executable |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def markdown_context_diff(make_snapshot): |
| 14 | + def make_diff(old_query, new_query, dialect="tsql"): |
| 15 | + old, new = [ |
| 16 | + make_snapshot( |
| 17 | + load_sql_based_model(parse(f"MODEL (name test.model, dialect {dialect}); {query}")) |
| 18 | + ) |
| 19 | + for query in (old_query, new_query) |
| 20 | + ] |
| 21 | + new.categorize_as(SnapshotChangeCategory.NON_BREAKING) |
| 22 | + return ContextDiff( |
| 23 | + environment="prod", |
| 24 | + is_new_environment=False, |
| 25 | + is_unfinalized_environment=False, |
| 26 | + normalize_environment_name=True, |
| 27 | + create_from="prod", |
| 28 | + create_from_env_exists=True, |
| 29 | + added=set(), |
| 30 | + removed_snapshots={}, |
| 31 | + modified_snapshots={new.name: (new, old)}, |
| 32 | + snapshots={new.snapshot_id: new}, |
| 33 | + new_snapshots={new.snapshot_id: new}, |
| 34 | + previous_plan_id=None, |
| 35 | + previously_promoted_snapshot_ids=set(), |
| 36 | + previous_finalized_snapshots=None, |
| 37 | + previous_gateway_managed_virtual_layer=False, |
| 38 | + gateway_managed_virtual_layer=False, |
| 39 | + environment_statements=[], |
| 40 | + ) |
| 41 | + |
| 42 | + return make_diff |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "dialect,old_column,new_column,table", |
| 47 | + [ |
| 48 | + ("tsql", "[old_col]", "[new_col]", "[schema].[table]"), |
| 49 | + ("tsql", "[old column]", "[new]]column]", "[my schema].[my]]table]"), |
| 50 | + ("duckdb", "old_col", "new_col", "schema_name.table_name"), |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_markdown_console_model_diff_identifiers( |
| 54 | + markdown_context_diff, dialect, old_column, new_column, table |
| 55 | +): |
| 56 | + context_diff = markdown_context_diff( |
| 57 | + f"SELECT {old_column} FROM {table}", |
| 58 | + f"SELECT {new_column} FROM {table}", |
| 59 | + dialect, |
| 60 | + ) |
| 61 | + snapshot = next(iter(context_diff.snapshots.values())) |
| 62 | + diff = context_diff.text_diff(snapshot.name) |
| 63 | + assert old_column in diff and new_column in diff and table in diff |
| 64 | + |
| 65 | + console = MarkdownConsole() |
| 66 | + console.show_model_difference_summary( |
| 67 | + context_diff, EnvironmentNamingInfo(), None, no_diff=False |
| 68 | + ) |
| 69 | + |
| 70 | + output = console.consume_captured_output() |
| 71 | + assert ( |
| 72 | + " ```diff\n" + "\n".join(f" {line}" for line in diff.split("\n")) + "\n ```\n" in output |
| 73 | + ) |
| 74 | + assert "**Directly Modified:**" in output |
| 75 | + assert "(Non-breaking)" in output |
| 76 | + assert console.consume_captured_output() == "" |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("categorized", [False, True]) |
| 80 | +@pytest.mark.parametrize("backticks", ["", "```", "`````"]) |
| 81 | +def test_markdown_console_literal_model_diffs( |
| 82 | + markdown_context_diff, mocker, capsys, categorized, backticks |
| 83 | +): |
| 84 | + literal = f"[bold] [/invalid] :smile: \\path\t{'long text ' * 20}\n{backticks}\nend" |
| 85 | + context_diff = markdown_context_diff("SELECT 1 AS [old]", f"SELECT '{literal}' AS [new]") |
| 86 | + snapshot = next(iter(context_diff.snapshots.values())) |
| 87 | + diff = context_diff.text_diff(snapshot.name) |
| 88 | + assert all(line in diff for line in literal.split("\n")) |
| 89 | + console = MarkdownConsole(width=30) |
| 90 | + console.log_success("before diff") |
| 91 | + if categorized: |
| 92 | + plan = mocker.Mock( |
| 93 | + context_diff=context_diff, |
| 94 | + categorized=[snapshot], |
| 95 | + indirectly_modified={}, |
| 96 | + environment_naming_info=EnvironmentNamingInfo(), |
| 97 | + ) |
| 98 | + console._show_categorized_snapshots(plan, None) |
| 99 | + else: |
| 100 | + console.show_model_difference_summary( |
| 101 | + context_diff, EnvironmentNamingInfo(), None, no_diff=False |
| 102 | + ) |
| 103 | + console.log_success("after diff") |
| 104 | + |
| 105 | + output = console.consume_captured_output() |
| 106 | + fence = "`" * max(3, len(backticks) + 1) |
| 107 | + indent = "" if categorized else " " |
| 108 | + block = ( |
| 109 | + f"{indent}{fence}diff\n" |
| 110 | + + "\n".join(f"{indent}{line}" for line in diff.split("\n")) |
| 111 | + + f"\n{indent}{fence}\n" |
| 112 | + ) |
| 113 | + assert block in output |
| 114 | + assert output.count(block) == 1 |
| 115 | + assert output.index("before diff") < output.index(block) < output.index("after diff") |
| 116 | + assert console.consume_captured_output() == "" |
| 117 | + visible = capsys.readouterr().out |
| 118 | + assert visible.count(f"{fence}diff") == 1 |
| 119 | + assert "[bold] [/invalid] :smile:" in visible |
| 120 | + if categorized: |
| 121 | + assert "Directly Modified:" in output |
| 122 | + assert "[bold]" not in output.split(block, 1)[1] |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("no_diff,empty_diff", [(True, False), (False, True)]) |
| 126 | +def test_markdown_console_omitted_model_diffs(markdown_context_diff, mocker, no_diff, empty_diff): |
| 127 | + context_diff = markdown_context_diff("SELECT 1 AS [old]", "SELECT 2 AS [new]") |
| 128 | + text_diff = mocker.patch.object(ContextDiff, "text_diff", return_value="") |
| 129 | + console = MarkdownConsole() |
| 130 | + console.show_model_difference_summary( |
| 131 | + context_diff, EnvironmentNamingInfo(), None, no_diff=no_diff |
| 132 | + ) |
| 133 | + output = console.consume_captured_output() |
| 134 | + assert "```" not in output |
| 135 | + assert "**Directly Modified:**" in output |
| 136 | + if empty_diff: |
| 137 | + text_diff.assert_called_once() |
| 138 | + else: |
| 139 | + text_diff.assert_not_called() |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.parametrize("no_diff", [False, True]) |
| 143 | +def test_markdown_console_environment_sql_diffs(markdown_context_diff, no_diff): |
| 144 | + context_diff = markdown_context_diff("SELECT 1", "SELECT 2") |
| 145 | + context_diff.previous_environment_statements = [ |
| 146 | + EnvironmentStatements( |
| 147 | + before_all=["SELECT [old] FROM [schema].[table]"], |
| 148 | + after_all=["SELECT [old]"], |
| 149 | + python_env={"version": Executable.value(1)}, |
| 150 | + ) |
| 151 | + ] |
| 152 | + context_diff.environment_statements = [ |
| 153 | + EnvironmentStatements( |
| 154 | + before_all=["SELECT [new], ':smile: [/invalid]\\path\t' FROM [schema].[table]"], |
| 155 | + after_all=[f"SELECT '```\n{'long text ' * 20}\n```' AS [new]"], |
| 156 | + python_env={"version": Executable.value(2)}, |
| 157 | + ) |
| 158 | + ] |
| 159 | + console = MarkdownConsole(width=35) |
| 160 | + console.show_environment_difference_summary(context_diff, no_diff=no_diff) |
| 161 | + output = console.consume_captured_output() |
| 162 | + assert "**Summary of differences from" in output and "`prod`:**" in output |
| 163 | + if no_diff: |
| 164 | + assert "Environment statements:" not in output |
| 165 | + assert "```" not in output |
| 166 | + return |
| 167 | + |
| 168 | + assert "Environment statements:" in output |
| 169 | + for language, diff in context_diff.environment_statements_diff(include_python_env=True): |
| 170 | + if language == "sql": |
| 171 | + fence = "````" if "```" in diff else "```" |
| 172 | + assert f"{fence}diff\n{diff}\n{fence}\n" in output |
| 173 | + else: |
| 174 | + assert diff in output |
| 175 | + assert output.index("before_all:") < output.index("after_all:") < output.index("dependencies:") |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.parametrize("indirect_only", [False, True]) |
| 179 | +def test_markdown_console_model_change_categories( |
| 180 | + markdown_context_diff, make_snapshot, indirect_only |
| 181 | +): |
| 182 | + context_diff = markdown_context_diff("SELECT 1 AS [old]", "SELECT 2 AS [new]") |
| 183 | + current, previous = next(iter(context_diff.modified_snapshots.values())) |
| 184 | + child_model = load_sql_based_model( |
| 185 | + parse("MODEL (name test.child, dialect tsql); SELECT * FROM test.model") |
| 186 | + ) |
| 187 | + old_child, new_child = [ |
| 188 | + make_snapshot(child_model, nodes={parent.name: parent.model}) |
| 189 | + for parent in (previous, current) |
| 190 | + ] |
| 191 | + new_child.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING) |
| 192 | + old_metadata, new_metadata = [ |
| 193 | + make_snapshot( |
| 194 | + load_sql_based_model( |
| 195 | + parse(f"MODEL (name test.metadata, owner {owner}); SELECT 1 AS value") |
| 196 | + ) |
| 197 | + ) |
| 198 | + for owner in ("old_owner", "new_owner") |
| 199 | + ] |
| 200 | + context_diff.modified_snapshots.update( |
| 201 | + { |
| 202 | + new_child.name: (new_child, old_child), |
| 203 | + new_metadata.name: (new_metadata, old_metadata), |
| 204 | + } |
| 205 | + ) |
| 206 | + context_diff.snapshots.update( |
| 207 | + {new_child.snapshot_id: new_child, new_metadata.snapshot_id: new_metadata} |
| 208 | + ) |
| 209 | + assert context_diff.indirectly_modified(new_child.name) |
| 210 | + assert context_diff.metadata_updated(new_metadata.name) |
| 211 | + if indirect_only: |
| 212 | + del context_diff.modified_snapshots[current.name] |
| 213 | + |
| 214 | + console = MarkdownConsole() |
| 215 | + console.show_model_difference_summary( |
| 216 | + context_diff, EnvironmentNamingInfo(), None, no_diff=False |
| 217 | + ) |
| 218 | + output = console.consume_captured_output() |
| 219 | + assert "**Indirectly Modified:**\n- `test.child` (Indirect Non-breaking)" in output |
| 220 | + assert "**Metadata Updated:**\n- `test.metadata`" in output |
| 221 | + if indirect_only: |
| 222 | + assert "**Directly Modified:**" not in output |
| 223 | + assert "```" not in output |
| 224 | + else: |
| 225 | + assert " Indirectly Modified Children:\n - `test.child`" in output |
| 226 | + assert output.index(" ```diff") < output.index(" Indirectly Modified Children:") |
2 | 227 |
|
3 | 228 |
|
4 | 229 | def test_markdown_console_warning_block(): |
|
0 commit comments