|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import tabulate |
| 6 | +from _pytest.fixtures import FixtureRequest |
| 7 | +from dbt_semantic_interfaces.naming.keywords import DUNDER |
| 8 | +from dbt_semantic_interfaces.protocols import SemanticManifest |
| 9 | +from metricflow_semantics.collection_helpers.mf_type_aliases import AnyLengthTuple |
| 10 | +from metricflow_semantics.experimental.dsi.manifest_object_lookup import ManifestObjectLookup |
| 11 | +from metricflow_semantics.experimental.mf_graph.path_finding.pathfinder import MetricflowPathfinder |
| 12 | +from metricflow_semantics.experimental.semantic_graph.attribute_resolution.recipe_writer_path import ( |
| 13 | + AttributeRecipeWriterPath, |
| 14 | + RecipeWriterPathfinder, |
| 15 | +) |
| 16 | +from metricflow_semantics.experimental.semantic_graph.attribute_resolution.recipe_writer_weight import ( |
| 17 | + AttributeRecipeWriterWeightFunction, |
| 18 | +) |
| 19 | +from metricflow_semantics.experimental.semantic_graph.builder.graph_builder import SemanticGraphBuilder |
| 20 | +from metricflow_semantics.experimental.semantic_graph.nodes.node_labels import GroupByAttributeLabel, MeasureLabel |
| 21 | +from metricflow_semantics.mf_logging.pretty_print import mf_pformat |
| 22 | +from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration |
| 23 | +from metricflow_semantics.test_helpers.snapshot_helpers import ( |
| 24 | + assert_str_snapshot_equal, |
| 25 | +) |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def test_recipe_writer_path( |
| 31 | + request: FixtureRequest, |
| 32 | + mf_test_configuration: MetricFlowTestConfiguration, |
| 33 | + sg_02_single_join_manifest: SemanticManifest, |
| 34 | +) -> None: |
| 35 | + """Test generating recipes by traversing the semantic graph.""" |
| 36 | + semantic_graph = SemanticGraphBuilder(ManifestObjectLookup(sg_02_single_join_manifest)).build() |
| 37 | + path_finder: RecipeWriterPathfinder = MetricflowPathfinder() |
| 38 | + |
| 39 | + # Find all valid paths from the `booking_count` measure to any group-by attribute node. |
| 40 | + source_node = semantic_graph.node_with_label(MeasureLabel.get_instance("booking_count")) |
| 41 | + target_nodes = semantic_graph.nodes_with_labels(GroupByAttributeLabel.get_instance()) |
| 42 | + |
| 43 | + # found_paths = tuple( |
| 44 | + # path_finder.find_paths_dfs( |
| 45 | + # graph=semantic_graph, |
| 46 | + # initial_path=AttributeRecipeWriterPath.create(source_node), |
| 47 | + # target_nodes=target_nodes, |
| 48 | + # weight_function=AttributeRecipeWriterWeightFunction(), |
| 49 | + # max_path_weight=2, |
| 50 | + # ) |
| 51 | + # ) |
| 52 | + found_paths: list[AttributeRecipeWriterPath] = [] |
| 53 | + for path in path_finder.find_paths_dfs( |
| 54 | + graph=semantic_graph, |
| 55 | + initial_path=AttributeRecipeWriterPath.create(source_node), |
| 56 | + target_nodes=target_nodes, |
| 57 | + weight_function=AttributeRecipeWriterWeightFunction(), |
| 58 | + max_path_weight=2, |
| 59 | + ): |
| 60 | + found_paths.append(path.copy()) |
| 61 | + |
| 62 | + # Produce a table showing how the path relates to the dunder name and the recipe. |
| 63 | + table_headers = ("Path", "Dunder Name", "Recipe") |
| 64 | + table_rows: list[AnyLengthTuple[str]] = [] |
| 65 | + |
| 66 | + for path in found_paths: |
| 67 | + path_str = "\n-> ".join(node.node_descriptor.node_name for node in path.nodes) |
| 68 | + dunder_name = DUNDER.join(path.latest_recipe.indexed_dunder_name) |
| 69 | + table_rows.append((path_str, dunder_name, mf_pformat(path.latest_recipe))) |
| 70 | + |
| 71 | + assert_str_snapshot_equal( |
| 72 | + request=request, |
| 73 | + snapshot_configuration=mf_test_configuration, |
| 74 | + snapshot_str=tabulate.tabulate( |
| 75 | + headers=table_headers, |
| 76 | + tabular_data=table_rows, |
| 77 | + ), |
| 78 | + ) |
0 commit comments