Skip to content

Commit

Permalink
Enforce tests have the SQL-engine-test marker set if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Oct 30, 2023
1 parent 22400bd commit 0a6b559
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions metricflow/test/fixtures/setup_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def pytest_configure(config: _pytest.config.Config) -> None:
)


def check_sql_engine_snapshot_marker(request: FixtureRequest) -> None:
"""Raises an error if the given test request does not have the sql-engine-test marker set."""
mark_names = set(mark.name for mark in request.node.iter_markers(name=SQL_ENGINE_SNAPSHOT_MARKER_NAME))
if SQL_ENGINE_SNAPSHOT_MARKER_NAME not in mark_names:
raise ValueError(
f"This test needs to be marked with '{SQL_ENGINE_SNAPSHOT_MARKER_NAME}' to keep track of all tests that "
f"generate SQL-engine specific snapshots."
)


@pytest.fixture(scope="session")
def mf_test_session_state( # noqa: D
request: FixtureRequest,
Expand Down
5 changes: 4 additions & 1 deletion metricflow/test/snapshot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from metricflow.model.semantics.linkable_spec_resolver import LinkableElementSet
from metricflow.protocols.sql_client import SqlClient
from metricflow.specs.specs import InstanceSpecSet
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState, check_sql_engine_snapshot_marker

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -261,6 +261,9 @@ def assert_object_snapshot_equal( # type: ignore[misc]
sql_client: Optional[SqlClient] = None,
) -> None:
"""For tests to compare large objects, this can be used to snapshot a text representation of the object."""
if sql_client is not None:
check_sql_engine_snapshot_marker(request)

assert_snapshot_text_equal(
request=request,
mf_test_session_state=mf_test_session_state,
Expand Down
4 changes: 3 additions & 1 deletion metricflow/test/sql/compare_sql_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_plan import SqlQueryPlan, SqlQueryPlanNode, SqlSelectStatementNode
from metricflow.sql.sql_plan_to_text import sql_query_plan_as_text
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState, check_sql_engine_snapshot_marker
from metricflow.test.snapshot_utils import (
assert_plan_snapshot_text_equal,
make_schema_replacement_function,
Expand Down Expand Up @@ -58,6 +58,8 @@ def assert_rendered_sql_from_plan_equal(
sql_client: SqlClient,
) -> None:
"""Similar to assert_rendered_sql_equal, but takes in a SQL query plan."""
check_sql_engine_snapshot_marker(request)

rendered_sql = sql_client.sql_query_plan_renderer.render_sql_query_plan(sql_query_plan).sql

assert_plan_snapshot_text_equal(
Expand Down

0 comments on commit 0a6b559

Please sign in to comment.