From c6f1fb635bec5222d57b6806f256fbf75da161f9 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Wed, 13 Sep 2023 17:35:03 -0700 Subject: [PATCH] Add test cases for saved queries in the CLI. --- metricflow/test/cli/test_cli.py | 44 +++++++++++++++++++ .../simple_manifest/saved_queries.yaml | 12 +++++ metricflow/test/snapshot_utils.py | 8 ++-- .../str/test_saved_query__cli_output.txt | 11 +++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 metricflow/test/fixtures/semantic_manifest_yamls/simple_manifest/saved_queries.yaml create mode 100644 metricflow/test/snapshots/test_cli.py/str/test_saved_query__cli_output.txt diff --git a/metricflow/test/cli/test_cli.py b/metricflow/test/cli/test_cli.py index cc7a12f3c3..a03944e64b 100644 --- a/metricflow/test/cli/test_cli.py +++ b/metricflow/test/cli/test_cli.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging import shutil import textwrap from contextlib import contextmanager @@ -7,6 +8,7 @@ from typing import Iterator import pytest +from _pytest.fixtures import FixtureRequest from dbt_semantic_interfaces.parsing.dir_to_model import ( parse_yaml_files_to_validation_ready_semantic_manifest, ) @@ -25,7 +27,14 @@ ) from metricflow.protocols.sql_client import SqlEngine from metricflow.test.fixtures.cli_fixtures import MetricFlowCliRunner +from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState from metricflow.test.model.example_project_configuration import EXAMPLE_PROJECT_CONFIGURATION_YAML_CONFIG_FILE +from metricflow.test.snapshot_utils import assert_object_snapshot_equal + +logger = logging.getLogger(__name__) + + +# TODO: Use snapshots to compare CLI output for all tests here. def test_query(capsys: pytest.CaptureFixture, cli_runner: MetricFlowCliRunner) -> None: # noqa: D @@ -144,3 +153,38 @@ def test_list_entities(capsys: pytest.CaptureFixture, cli_runner: MetricFlowCliR assert "guest" in resp.output assert "host" in resp.output + + +def test_saved_query( # noqa: D + request: FixtureRequest, + capsys: pytest.CaptureFixture, + mf_test_session_state: MetricFlowTestSessionState, + cli_runner: MetricFlowCliRunner, +) -> None: + # Disabling capsys to resolve error "ValueError: I/O operation on closed file". Better solution TBD. + with capsys.disabled(): + resp = cli_runner.run( + query, args=["--saved-query", "p0_booking", "--order", "metric_time__day,listing__capacity_latest"] + ) + + assert resp.exit_code == 0 + + assert_object_snapshot_equal( + request=request, mf_test_session_state=mf_test_session_state, obj_id="cli_output", obj=resp.output + ) + + +def test_saved_query_explain( # noqa: D + capsys: pytest.CaptureFixture, + mf_test_session_state: MetricFlowTestSessionState, + cli_runner: MetricFlowCliRunner, +) -> None: + # Disabling capsys to resolve error "ValueError: I/O operation on closed file". Better solution TBD. + with capsys.disabled(): + resp = cli_runner.run( + query, + args=["--explain", "--saved-query", "p0_booking", "--order", "metric_time__day,listing__capacity_latest"], + ) + + # Currently difficult to compare explain output due to randomly generated IDs. + assert resp.exit_code == 0 diff --git a/metricflow/test/fixtures/semantic_manifest_yamls/simple_manifest/saved_queries.yaml b/metricflow/test/fixtures/semantic_manifest_yamls/simple_manifest/saved_queries.yaml new file mode 100644 index 0000000000..77b4ebf832 --- /dev/null +++ b/metricflow/test/fixtures/semantic_manifest_yamls/simple_manifest/saved_queries.yaml @@ -0,0 +1,12 @@ +--- +saved_query: + name: p0_booking + description: Booking-related metrics that are of the highest priority. + metrics: + - bookings + - instant_bookings + group_bys: + - metric_time__day + - listing__capacity_latest + where: + - "{{ Dimension('listing__capacity_latest') }} > 3" diff --git a/metricflow/test/snapshot_utils.py b/metricflow/test/snapshot_utils.py index ac4fcad18f..838420c029 100644 --- a/metricflow/test/snapshot_utils.py +++ b/metricflow/test/snapshot_utils.py @@ -172,9 +172,10 @@ def assert_snapshot_text_equal( # Create parent directory for the plan text files. os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w") as snapshot_text_file: - snapshot_text_file.write(snapshot_text) # Add a new line at the end of the file so that PRSs don't show the "no newline" symbol on Github. - snapshot_text_file.write("\n") + if len(snapshot_text) > 1 and snapshot_text[-1] != "\n": + snapshot_text = snapshot_text + "\n" + snapshot_text_file.write(snapshot_text) # Throw an exception if the plan is not there. if not os.path.exists(file_path): @@ -198,8 +199,7 @@ def assert_snapshot_text_equal( # Read the existing plan from the file and compare with the actual plan with open(file_path, "r") as snapshot_text_file: - # Remove the newline that was added from above. - expected_snapshot_text = snapshot_text_file.read().rstrip() + expected_snapshot_text = snapshot_text_file.read() if exclude_line_regex: # Filter out lines that should be ignored. diff --git a/metricflow/test/snapshots/test_cli.py/str/test_saved_query__cli_output.txt b/metricflow/test/snapshots/test_cli.py/str/test_saved_query__cli_output.txt new file mode 100644 index 0000000000..534002ae25 --- /dev/null +++ b/metricflow/test/snapshots/test_cli.py/str/test_saved_query__cli_output.txt @@ -0,0 +1,11 @@ +| metric_time__day | listing__capacity_latest | bookings | instant_bookings | +|:--------------------|---------------------------:|-----------:|-------------------:| +| 2019-12-01 00:00:00 | 5.00 | 1.00 | 0.00 | +| 2019-12-18 00:00:00 | 4.00 | 4.00 | 2.00 | +| 2019-12-19 00:00:00 | 4.00 | 6.00 | 6.00 | +| 2019-12-19 00:00:00 | 5.00 | 2.00 | 0.00 | +| 2019-12-20 00:00:00 | 5.00 | 2.00 | 0.00 | +| 2020-01-01 00:00:00 | 4.00 | 2.00 | 1.00 | +| 2020-01-02 00:00:00 | 4.00 | 3.00 | 3.00 | +| 2020-01-02 00:00:00 | 5.00 | 1.00 | 0.00 | +| 2020-01-03 00:00:00 | 5.00 | 1.00 | 0.00 |