Skip to content

Fix: Allow python models to call resolve_table() on themselves within a unit test#4967

Merged
erindru merged 3 commits intomainfrom
erin/parameterized-table-name-in-test
Jul 15, 2025
Merged

Fix: Allow python models to call resolve_table() on themselves within a unit test#4967
erindru merged 3 commits intomainfrom
erin/parameterized-table-name-in-test

Conversation

@erindru
Copy link
Collaborator

@erindru erindru commented Jul 15, 2025

Prior to this PR, having a Python model defined something like:

    @model(
        name="sushi.foo",
        columns={
            "id": "int",
            "name": "varchar",
        },
        dialect="snowflake",
    )
    def execute(
        context: ExecutionContext,
        **kwargs: t.Any,
    ) -> pd.DataFrame:
        self_table = context.resolve_table("sushi.foo")
        return context.fetchdf(f"select <some self-referential query> from {self_table}")

would work fine in the normal sense if you ran sqlmesh plan.

However, if you tried to create a unit test for it, you might get a KeyError on the resolve_table() call like:

KeyError: '"ERIN"."SUSHI"."FOO"'

The cause is a subtle bug introduced in PR #4302 that switched the model-to-table mapping to be keyed by model name instead of normalized model fqn. The resolve_table() call normalizes the argument that gets passed to it and expects it to be present in the mapping.

However, things mostly kept working because that PR also added the contents of model.depends_on to the mapping, which contains a list of normalized names. Since resolve_table() is usually called on upstream tables and not the current table, it would still resolve correctly.

This PR switches the TestExecutionContext table mapping to be keyed by the original key, which is the same key that Context.models uses.

@erindru erindru changed the title Chore: Add extra tests for the model test framework Fix: Allow python models to call resolve_table() on themselves within a unit test Jul 15, 2025
@erindru erindru merged commit 4ee6cb3 into main Jul 15, 2025
27 checks passed
@erindru erindru deleted the erin/parameterized-table-name-in-test branch July 15, 2025 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants