|
13 | 13 | from pytest_mock.plugin import MockerFixture |
14 | 14 | from sqlglot import exp, parse_one |
15 | 15 | from sqlmesh.core import dialect as d |
| 16 | +from sqlmesh.core.environment import EnvironmentNamingInfo |
| 17 | +from sqlmesh.core.macros import RuntimeStage |
| 18 | +from sqlmesh.core.renderer import render_statements |
16 | 19 | from sqlmesh.core.audit import StandaloneAudit |
17 | 20 | from sqlmesh.core.context import Context |
18 | 21 | from sqlmesh.core.console import get_console |
@@ -1551,3 +1554,112 @@ def test_grain(): |
1551 | 1554 |
|
1552 | 1555 | model.grain = "id_a" |
1553 | 1556 | assert model.to_sqlmesh(context).grains == [exp.to_column("id_a")] |
| 1557 | + |
| 1558 | + |
| 1559 | +def test_on_run_start_end(copy_to_temp_path): |
| 1560 | + project_root = "tests/fixtures/dbt/sushi_test" |
| 1561 | + sushi_context = Context(paths=copy_to_temp_path(project_root)) |
| 1562 | + assert len(sushi_context._environment_statements) == 2 |
| 1563 | + |
| 1564 | + # Root project's on run start / on run end should be first by checking the macros |
| 1565 | + root_environment_statements = sushi_context._environment_statements[0] |
| 1566 | + assert "create_tables" in root_environment_statements.jinja_macros.root_macros |
| 1567 | + |
| 1568 | + # Validate order of execution to be correct |
| 1569 | + assert root_environment_statements.before_all == [ |
| 1570 | + "JINJA_STATEMENT_BEGIN;\nCREATE TABLE IF NOT EXISTS analytic_stats (physical_table VARCHAR, evaluation_time VARCHAR);\nJINJA_END;", |
| 1571 | + "JINJA_STATEMENT_BEGIN;\nCREATE TABLE IF NOT EXISTS to_be_executed_last (col VARCHAR);\nJINJA_END;", |
| 1572 | + """JINJA_STATEMENT_BEGIN;\nSELECT {{ var("yet_another_var") }} AS var, '{{ source("raw", "items").identifier }}' AS src, '{{ ref("waiters").identifier }}' AS ref;\nJINJA_END;""", |
| 1573 | + "JINJA_STATEMENT_BEGIN;\n{{ log_value('on-run-start') }}\nJINJA_END;", |
| 1574 | + ] |
| 1575 | + assert root_environment_statements.after_all == [ |
| 1576 | + "JINJA_STATEMENT_BEGIN;\n{{ create_tables(schemas) }}\nJINJA_END;", |
| 1577 | + "JINJA_STATEMENT_BEGIN;\nDROP TABLE to_be_executed_last;\nJINJA_END;", |
| 1578 | + ] |
| 1579 | + |
| 1580 | + assert root_environment_statements.jinja_macros.root_package_name == "sushi" |
| 1581 | + |
| 1582 | + rendered_before_all = render_statements( |
| 1583 | + root_environment_statements.before_all, |
| 1584 | + dialect=sushi_context.default_dialect, |
| 1585 | + python_env=root_environment_statements.python_env, |
| 1586 | + jinja_macros=root_environment_statements.jinja_macros, |
| 1587 | + runtime_stage=RuntimeStage.BEFORE_ALL, |
| 1588 | + ) |
| 1589 | + |
| 1590 | + rendered_after_all = render_statements( |
| 1591 | + root_environment_statements.after_all, |
| 1592 | + dialect=sushi_context.default_dialect, |
| 1593 | + python_env=root_environment_statements.python_env, |
| 1594 | + jinja_macros=root_environment_statements.jinja_macros, |
| 1595 | + snapshots=sushi_context.snapshots, |
| 1596 | + runtime_stage=RuntimeStage.AFTER_ALL, |
| 1597 | + environment_naming_info=EnvironmentNamingInfo(name="dev"), |
| 1598 | + ) |
| 1599 | + |
| 1600 | + assert rendered_before_all == [ |
| 1601 | + "CREATE TABLE IF NOT EXISTS analytic_stats (physical_table TEXT, evaluation_time TEXT)", |
| 1602 | + "CREATE TABLE IF NOT EXISTS to_be_executed_last (col TEXT)", |
| 1603 | + "SELECT 1 AS var, 'items' AS src, 'waiters' AS ref", |
| 1604 | + ] |
| 1605 | + |
| 1606 | + # The jinja macro should have resolved the schemas for this environment and generated corresponding statements |
| 1607 | + assert sorted(rendered_after_all) == sorted( |
| 1608 | + [ |
| 1609 | + "CREATE OR REPLACE TABLE schema_table_snapshots__dev AS SELECT 'snapshots__dev' AS schema", |
| 1610 | + "CREATE OR REPLACE TABLE schema_table_sushi__dev AS SELECT 'sushi__dev' AS schema", |
| 1611 | + "DROP TABLE to_be_executed_last", |
| 1612 | + ] |
| 1613 | + ) |
| 1614 | + |
| 1615 | + # Nested dbt_packages on run start / on run end |
| 1616 | + packaged_environment_statements = sushi_context._environment_statements[1] |
| 1617 | + |
| 1618 | + # Validate order of execution to be correct |
| 1619 | + assert packaged_environment_statements.before_all == [ |
| 1620 | + "JINJA_STATEMENT_BEGIN;\nCREATE TABLE IF NOT EXISTS to_be_executed_first (col VARCHAR);\nJINJA_END;", |
| 1621 | + "JINJA_STATEMENT_BEGIN;\nCREATE TABLE IF NOT EXISTS analytic_stats_packaged_project (physical_table VARCHAR, evaluation_time VARCHAR);\nJINJA_END;", |
| 1622 | + ] |
| 1623 | + assert packaged_environment_statements.after_all == [ |
| 1624 | + "JINJA_STATEMENT_BEGIN;\nDROP TABLE to_be_executed_first\nJINJA_END;", |
| 1625 | + "JINJA_STATEMENT_BEGIN;\n{{ packaged_tables(schemas) }}\nJINJA_END;", |
| 1626 | + ] |
| 1627 | + |
| 1628 | + assert "packaged_tables" in packaged_environment_statements.jinja_macros.root_macros |
| 1629 | + assert packaged_environment_statements.jinja_macros.root_package_name == "sushi" |
| 1630 | + |
| 1631 | + rendered_before_all = render_statements( |
| 1632 | + packaged_environment_statements.before_all, |
| 1633 | + dialect=sushi_context.default_dialect, |
| 1634 | + python_env=packaged_environment_statements.python_env, |
| 1635 | + jinja_macros=packaged_environment_statements.jinja_macros, |
| 1636 | + runtime_stage=RuntimeStage.BEFORE_ALL, |
| 1637 | + ) |
| 1638 | + |
| 1639 | + rendered_after_all = render_statements( |
| 1640 | + packaged_environment_statements.after_all, |
| 1641 | + dialect=sushi_context.default_dialect, |
| 1642 | + python_env=packaged_environment_statements.python_env, |
| 1643 | + jinja_macros=packaged_environment_statements.jinja_macros, |
| 1644 | + snapshots=sushi_context.snapshots, |
| 1645 | + runtime_stage=RuntimeStage.AFTER_ALL, |
| 1646 | + environment_naming_info=EnvironmentNamingInfo(name="dev"), |
| 1647 | + ) |
| 1648 | + |
| 1649 | + # Validate order of execution to match dbt's |
| 1650 | + assert rendered_before_all == [ |
| 1651 | + "CREATE TABLE IF NOT EXISTS to_be_executed_first (col TEXT)", |
| 1652 | + "CREATE TABLE IF NOT EXISTS analytic_stats_packaged_project (physical_table TEXT, evaluation_time TEXT)", |
| 1653 | + ] |
| 1654 | + |
| 1655 | + # This on run end statement should be executed first |
| 1656 | + assert rendered_after_all[0] == "DROP TABLE to_be_executed_first" |
| 1657 | + |
| 1658 | + # The table names is an indication of the rendering of the dbt_packages statements |
| 1659 | + assert sorted(rendered_after_all) == sorted( |
| 1660 | + [ |
| 1661 | + "DROP TABLE to_be_executed_first", |
| 1662 | + "CREATE OR REPLACE TABLE schema_table_snapshots__dev_nested_package AS SELECT 'snapshots__dev' AS schema", |
| 1663 | + "CREATE OR REPLACE TABLE schema_table_sushi__dev_nested_package AS SELECT 'sushi__dev' AS schema", |
| 1664 | + ] |
| 1665 | + ) |
0 commit comments