sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

Fix: Enable unit testing in Python models with upstream tables

Open themisvaltinos opened this issue 1 year ago • 0 comments

This update allows Python models to be unit tested when they contain non-SQLMesh models, fixes: #3891

Given the following test with ext_table an upstream table:

test_python_model:
  model: test_model
  inputs:
    ext_table:
      - id: 1
      - id: 2
  outputs:
    query:
      - id: 1
      - id: 2

and this python_model which uses ext_table:

@model(
    "test_model",
    columns={"id": "int"},
)
def entrypoint(context, *args, **kwargs):
    table_ref = context.resolve_table("ext_table")
    return context.fetchdf(f"SELECT id FROM {table_ref}")

Previously resolving the table in the test would return the actual table name, preventing fixture-based mocking. This instead extends the testing table mapping to map not only sqlmesh models, but upstream references as well, instead of the actual table to the corresponding test fixture tables.

themisvaltinos avatar May 02 '25 16:05 themisvaltinos