From 30c391fa86028a3c745634e58e1b9cd007dd57d3 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Tue, 28 Nov 2023 17:10:10 -0800 Subject: [PATCH] Adjust path generation for snapshots to replace brackets. The file path for snapshots includes the test name. For parameterized tests, this means that brackets [] will be in the file path. Since brackets are not convenient to type / auto-complete in the shell, replace them with dunders. --- metricflow/test/snapshot_utils.py | 34 +++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/metricflow/test/snapshot_utils.py b/metricflow/test/snapshot_utils.py index 274f3e73f2..76921e49d0 100644 --- a/metricflow/test/snapshot_utils.py +++ b/metricflow/test/snapshot_utils.py @@ -11,14 +11,16 @@ from _pytest.fixtures import FixtureRequest from dbt_semantic_interfaces.pretty_print import pformat_big_objects +from metricflow.collection_helpers.pretty_print import mf_pformat from metricflow.dag.mf_dag import MetricFlowDag from metricflow.dataflow.dataflow_plan import DataflowPlan from metricflow.dataflow.dataflow_plan_to_text import dataflow_plan_as_text from metricflow.execution.execution_plan import ExecutionPlan from metricflow.execution.execution_plan_to_text import execution_plan_to_text from metricflow.model.semantics.linkable_spec_resolver import LinkableElementSet +from metricflow.naming.object_builder_scheme import ObjectBuilderNamingScheme from metricflow.protocols.sql_client import SqlClient -from metricflow.specs.specs import InstanceSpecSet +from metricflow.specs.specs import InstanceSpecSet, LinkableSpecSet from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState, check_sql_engine_snapshot_marker logger = logging.getLogger(__name__) @@ -80,7 +82,17 @@ def snapshot_path_prefix( .../snapshots/test_file.py/DataflowPlan/test_name__plan1.svg """ test_name = request.node.name - snapshot_file_name = test_name + "__" + snapshot_id + + snapshot_file_name_parts = [] + # Parameterized test names look like 'test_case[some_param]'. "[" and "]" are annoying to deal with in the shell, + # so replace them with dunders. + snapshot_file_name_parts.extend(re.split(r"[\[\]]", test_name)) + # A trailing ] will produce an empty string in the list, so remove that. + snapshot_file_name_parts = [part for part in snapshot_file_name_parts if len(part) > 0] + snapshot_file_name_parts.append(snapshot_id) + + snapshot_file_name = "__".join(snapshot_file_name_parts) + path_items: List[str] = [] test_file_path_items = os.path.normpath(request.node.fspath).split(os.sep) @@ -128,6 +140,8 @@ def assert_plan_snapshot_text_equal( plans consistent when there are strings that vary between runs and shouldn't be compared. * additional_sub_directories_for_snapshots is used to specify additional sub-directories (in the automatically generated directory) where plan outputs should reside. + + TODO: Make this more generic by renaming plan -> DAG. """ assert_snapshot_text_equal( request=request, @@ -327,3 +341,19 @@ def assert_spec_set_snapshot_equal( # noqa: D obj_id=set_id, obj=sorted(spec.qualified_name for spec in spec_set.all_specs), ) + + +def assert_linkable_spec_set_snapshot_equal( # noqa: D + request: FixtureRequest, mf_test_session_state: MetricFlowTestSessionState, set_id: str, spec_set: LinkableSpecSet +) -> None: + # TODO: This will be used in a later PR and this message will be removed. + naming_scheme = ObjectBuilderNamingScheme() + assert_snapshot_text_equal( + request=request, + mf_test_session_state=mf_test_session_state, + group_id=spec_set.__class__.__name__, + snapshot_id=set_id, + snapshot_text=mf_pformat(sorted(naming_scheme.input_str(spec) for spec in spec_set.as_tuple)), + snapshot_file_extension=".txt", + additional_sub_directories_for_snapshots=(), + )