|
21 | 21 | GatewayConfig, |
22 | 22 | ModelDefaultsConfig, |
23 | 23 | ) |
24 | | -from sqlmesh.core.context import Context |
| 24 | +from sqlmesh.core.context import Context, ExecutionContext |
25 | 25 | from sqlmesh.core.console import get_console |
26 | 26 | from sqlmesh.core.dialect import parse |
27 | 27 | from sqlmesh.core.engine_adapter import EngineAdapter |
@@ -2470,3 +2470,53 @@ def execute(context, start, end, execution_time, **kwargs): |
2470 | 2470 |
|
2471 | 2471 | results = ctx.test() |
2472 | 2472 | assert len(results.successes) == 30 |
| 2473 | + |
| 2474 | + |
| 2475 | +def test_python_model_upstream_table(sushi_context) -> None: |
| 2476 | + @model( |
| 2477 | + "test_upstream_table_python", |
| 2478 | + columns={"customer_id": "int", "zip": "str"}, |
| 2479 | + ) |
| 2480 | + def upstream_table_python(context, **kwargs): |
| 2481 | + demographics_external_table = context.resolve_table("memory.raw.demographics") |
| 2482 | + return context.fetchdf( |
| 2483 | + exp.select("customer_id", "zip").from_(demographics_external_table), |
| 2484 | + ) |
| 2485 | + |
| 2486 | + python_model = model.get_registry()["test_upstream_table_python"].model( |
| 2487 | + module_path=Path("."), |
| 2488 | + path=Path("."), |
| 2489 | + ) |
| 2490 | + |
| 2491 | + context = ExecutionContext(sushi_context.engine_adapter, sushi_context.snapshots, None, None) |
| 2492 | + df = list(python_model.render(context=context))[0] |
| 2493 | + |
| 2494 | + # Verify the actual model output matches the expected actual external table's values |
| 2495 | + assert df.to_dict(orient="records") == [{"customer_id": 1, "zip": "00000"}] |
| 2496 | + |
| 2497 | + # Use different input values for the test and verify the outputs |
| 2498 | + _check_successful_or_raise( |
| 2499 | + _create_test( |
| 2500 | + body=load_yaml(""" |
| 2501 | +test_test_upstream_table_python: |
| 2502 | + model: test_upstream_table_python |
| 2503 | + inputs: |
| 2504 | + memory.raw.demographics: |
| 2505 | + - customer_id: 12 |
| 2506 | + zip: "S11HA" |
| 2507 | + - customer_id: 555 |
| 2508 | + zip: "94401" |
| 2509 | + outputs: |
| 2510 | + query: |
| 2511 | + - customer_id: 12 |
| 2512 | + zip: "S11HA" |
| 2513 | + - customer_id: 555 |
| 2514 | + zip: "94401" |
| 2515 | +"""), |
| 2516 | + test_name="test_test_upstream_table_python", |
| 2517 | + model=model.get_registry()["test_upstream_table_python"].model( |
| 2518 | + module_path=Path("."), path=Path(".") |
| 2519 | + ), |
| 2520 | + context=sushi_context, |
| 2521 | + ).run() |
| 2522 | + ) |
0 commit comments