-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #1554
- Loading branch information
Showing
6 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add `--quiet` option to the `query` CLI command. | ||
time: 2025-02-21T15:35:51.935247-08:00 | ||
custom: | ||
Author: plypaul | ||
Issue: "1554" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional, Sequence | ||
|
||
import click | ||
from _pytest.capture import CaptureFixture | ||
from _pytest.fixtures import FixtureRequest | ||
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration | ||
|
||
from metricflow.protocols.sql_client import SqlClient | ||
from tests_metricflow.fixtures.cli_fixtures import MetricFlowCliRunner | ||
from tests_metricflow.snapshot_utils import assert_str_snapshot_equal | ||
|
||
|
||
def run_and_check_cli_command( | ||
request: FixtureRequest, | ||
capsys: CaptureFixture, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
cli_runner: MetricFlowCliRunner, | ||
command: click.BaseCommand, | ||
args: Sequence[str], | ||
sql_client: Optional[SqlClient] = None, | ||
assert_zero_exit_code: bool = True, | ||
) -> None: | ||
"""Helper to run a CLI command and check that the output matches the stored snapshot.""" | ||
# Needed to resolve `ValueError: I/O operation on closed file` when running CLI tests individually. | ||
# See: https://github.com/pallets/click/issues/824 | ||
with capsys.disabled(): | ||
result = cli_runner.run(command, args=args) | ||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="result", | ||
snapshot_str=result.stdout, | ||
sql_engine=sql_client.sql_engine_type if sql_client else None, | ||
) | ||
if assert_zero_exit_code: | ||
assert result.exit_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
import pytest | ||
from _pytest.capture import CaptureFixture | ||
from _pytest.fixtures import FixtureRequest | ||
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration | ||
|
||
from dbt_metricflow.cli.main import query | ||
from metricflow.protocols.sql_client import SqlClient | ||
from tests_metricflow.cli.conftest import run_and_check_cli_command | ||
from tests_metricflow.fixtures.cli_fixtures import MetricFlowCliRunner | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
@pytest.mark.duckdb_only | ||
def test_query( | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
request: FixtureRequest, | ||
capsys: CaptureFixture, | ||
cli_runner: MetricFlowCliRunner, | ||
sql_client: SqlClient, | ||
) -> None: | ||
"""Test that the `--quiet` flag only shows the table when running a query.""" | ||
run_and_check_cli_command( | ||
request=request, | ||
capsys=capsys, | ||
mf_test_configuration=mf_test_configuration, | ||
cli_runner=cli_runner, | ||
command=query, | ||
args=["--metrics", "bookings", "--group-by", "metric_time", "--order", "metric_time", "--quiet"], | ||
sql_client=sql_client, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
@pytest.mark.duckdb_only | ||
def test_explain( | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
request: FixtureRequest, | ||
capsys: CaptureFixture, | ||
cli_runner: MetricFlowCliRunner, | ||
sql_client: SqlClient, | ||
) -> None: | ||
"""Test that the `--quiet` flag only shows the SQL when explaining a query.""" | ||
run_and_check_cli_command( | ||
request=request, | ||
capsys=capsys, | ||
mf_test_configuration=mf_test_configuration, | ||
cli_runner=cli_runner, | ||
command=query, | ||
args=["--metrics", "bookings", "--group-by", "metric_time", "--order", "metric_time", "--explain", "--quiet"], | ||
sql_client=sql_client, | ||
) |
17 changes: 17 additions & 0 deletions
17
tests_metricflow/snapshots/test_cli_quiet.py/str/DuckDB/test_explain__result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
test_name: test_explain | ||
test_filename: test_cli_quiet.py | ||
docstring: | ||
Test that the `--quiet` flag only shows the SQL when explaining a query. | ||
--- | ||
SELECT | ||
metric_time__day | ||
, SUM(bookings) AS bookings | ||
FROM ( | ||
SELECT | ||
DATE_TRUNC('day', ds) AS metric_time__day | ||
, 1 AS bookings | ||
FROM ***************************.fct_bookings bookings_source_src_10000 | ||
) subq_2 | ||
GROUP BY | ||
metric_time__day | ||
ORDER BY metric_time__day |
14 changes: 14 additions & 0 deletions
14
tests_metricflow/snapshots/test_cli_quiet.py/str/DuckDB/test_query__result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
test_name: test_query | ||
test_filename: test_cli_quiet.py | ||
docstring: | ||
Test that the `--quiet` flag only shows the table when running a query. | ||
--- | ||
metric_time__day bookings | ||
------------------- ---------- | ||
2019-12-01T00:00:00 1 | ||
2019-12-18T00:00:00 10 | ||
2019-12-19T00:00:00 18 | ||
2019-12-20T00:00:00 2 | ||
2020-01-01T00:00:00 5 | ||
2020-01-02T00:00:00 9 | ||
2020-01-03T00:00:00 1 |