|
25 | 25 | from sqlmesh.core.macros import MacroEvaluator, macro |
26 | 26 | from sqlmesh.core.model import Model, SqlModel, load_sql_based_model, model |
27 | 27 | from sqlmesh.core.test.definition import ModelTest, PythonModelTest, SqlModelTest |
28 | | -from sqlmesh.utils.errors import ConfigError, TestError |
| 28 | +from sqlmesh.utils.errors import ConfigError, SQLMeshError, TestError |
29 | 29 | from sqlmesh.utils.yaml import dump as dump_yaml |
30 | 30 | from sqlmesh.utils.yaml import load as load_yaml |
31 | 31 |
|
@@ -1989,3 +1989,53 @@ def test_test_generation_with_recursive_ctes(tmp_path: Path) -> None: |
1989 | 1989 | } |
1990 | 1990 |
|
1991 | 1991 | _check_successful_or_raise(context.test()) |
| 1992 | + |
| 1993 | + |
| 1994 | +def test_test_with_gateway_specific_model(tmp_path: Path, mocker: MockerFixture) -> None: |
| 1995 | + init_example_project(tmp_path, dialect="duckdb") |
| 1996 | + |
| 1997 | + config = Config( |
| 1998 | + gateways={ |
| 1999 | + "main": GatewayConfig(connection=DuckDBConnectionConfig()), |
| 2000 | + "second": GatewayConfig(connection=DuckDBConnectionConfig()), |
| 2001 | + }, |
| 2002 | + default_gateway="main", |
| 2003 | + model_defaults=ModelDefaultsConfig(dialect="duckdb"), |
| 2004 | + ) |
| 2005 | + gw_model_sql_file = tmp_path / "models" / "gw_model.sql" |
| 2006 | + |
| 2007 | + # The model has a gateway specified which isn't the default |
| 2008 | + gw_model_sql_file.write_text( |
| 2009 | + "MODEL (name sqlmesh_example.gw_model, gateway second); SELECT c FROM sqlmesh_example.input_model;" |
| 2010 | + ) |
| 2011 | + input_model_sql_file = tmp_path / "models" / "input_model.sql" |
| 2012 | + input_model_sql_file.write_text( |
| 2013 | + "MODEL (name sqlmesh_example.input_model); SELECT c FROM external_table;" |
| 2014 | + ) |
| 2015 | + |
| 2016 | + context = Context(paths=tmp_path, config=config) |
| 2017 | + input_queries = {'"memory"."sqlmesh_example"."input_model"': "SELECT 5 AS c"} |
| 2018 | + mocker.patch( |
| 2019 | + "sqlmesh.core.engine_adapter.base.EngineAdapter.fetchdf", |
| 2020 | + return_value=pd.DataFrame({"c": [5]}), |
| 2021 | + ) |
| 2022 | + |
| 2023 | + assert context.engine_adapter == context._engine_adapters["main"] |
| 2024 | + with pytest.raises( |
| 2025 | + SQLMeshError, match=r"Gateway 'wrong' not found in the available engine adapters." |
| 2026 | + ): |
| 2027 | + context._get_engine_adapter("wrong") |
| 2028 | + |
| 2029 | + # Create test should use the gateway specific engine adapter |
| 2030 | + context.create_test("sqlmesh_example.gw_model", input_queries=input_queries, overwrite=True) |
| 2031 | + assert context._get_engine_adapter("second") == context._engine_adapters["second"] |
| 2032 | + assert len(context._engine_adapters) == 2 |
| 2033 | + |
| 2034 | + test = load_yaml(context.path / c.TESTS / "test_gw_model.yaml") |
| 2035 | + |
| 2036 | + assert len(test) == 1 |
| 2037 | + assert "test_gw_model" in test |
| 2038 | + assert test["test_gw_model"]["inputs"] == { |
| 2039 | + '"memory"."sqlmesh_example"."input_model"': [{"c": 5}] |
| 2040 | + } |
| 2041 | + assert test["test_gw_model"]["outputs"] == {"query": [{"c": 5}]} |
0 commit comments