Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate --event-time-start is before --event-time-end #10820

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20241003-170529.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Ensure `--event-time-start` is before `--event-time-end`
time: 2024-10-03T17:05:29.984398-05:00
custom:
Author: QMalcolm
Issue: "10786"
2 changes: 2 additions & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def cli(ctx, **kwargs):
@p.vars
@requires.postflight
@requires.preflight
@p.validate_option_interactions
@requires.profile
@requires.project
@requires.runtime_config
Expand Down Expand Up @@ -561,6 +562,7 @@ def parse(ctx, **kwargs):
@p.vars
@requires.postflight
@requires.preflight
@p.validate_option_interactions
@requires.profile
@requires.project
@requires.runtime_config
Expand Down
26 changes: 26 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from datetime import datetime
from functools import update_wrapper
from pathlib import Path

import click

from dbt.cli.option_types import YAML, ChoiceTuple, Package, WarnErrorOptionsType
from dbt.cli.options import MultiOption
from dbt.cli.resolvers import default_profiles_dir, default_project_dir
from dbt.exceptions import DbtRuntimeError
from dbt.version import get_version_information

add_package = click.option(
Expand Down Expand Up @@ -735,3 +738,26 @@
envvar="DBT_SHOW_RESOURCE_REPORT",
hidden=True,
)


def validate_option_interactions(func):
def wrapper(*args, **kwargs):
ctx = args[0]
assert isinstance(ctx, click.Context)

Check warning on line 746 in core/dbt/cli/params.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/params.py#L745-L746

Added lines #L745 - L746 were not covered by tests

event_time_start = ctx.params.get("event_time_start")
if event_time_start is not None:
event_time_end = ctx.params.get("event_time_end")

Check warning on line 750 in core/dbt/cli/params.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/params.py#L748-L750

Added lines #L748 - L750 were not covered by tests

if event_time_end is not None and event_time_start >= event_time_end:
raise DbtRuntimeError(

Check warning on line 753 in core/dbt/cli/params.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/params.py#L752-L753

Added lines #L752 - L753 were not covered by tests
"Value for `--event-time-start` must be less than `--event-time-end`"
)
elif event_time_start >= datetime.now():
raise DbtRuntimeError(

Check warning on line 757 in core/dbt/cli/params.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/params.py#L756-L757

Added lines #L756 - L757 were not covered by tests
"Value for `--event-time-start` must be less than the current time if `--event-time-end` is not specififed"
)

return func(*args, **kwargs)

Check warning on line 761 in core/dbt/cli/params.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/params.py#L761

Added line #L761 was not covered by tests

return update_wrapper(wrapper, func)
55 changes: 55 additions & 0 deletions tests/functional/cli/test_option_interaction_validations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime

import pytest
from freezegun import freeze_time

from dbt.tests.util import run_dbt


class TestEventTimeEndEventTimeStart:
@pytest.mark.parametrize(
"event_time_start,event_time_end,expect_pass",
[
("2024-10-01", "2024-10-02", True),
("2024-10-02", "2024-10-01", False),
],
)
def test_option_combo(self, project, event_time_start, event_time_end, expect_pass):
try:
run_dbt(
[
"build",
"--event-time-start",
event_time_start,
"--event-time-end",
event_time_end,
]
)
assert expect_pass
except Exception as e:
assert (
"Value for `--event-time-start` must be less than `--event-time-end`"
in e.__str__()
)
assert not expect_pass


class TestEventTimeStartCurrent_time:
@pytest.mark.parametrize(
"event_time_start,current_time,expect_pass",
[
("2024-10-01", "2024-10-02", True),
("2024-10-02", "2024-10-01", False),
],
)
def test_option_combo(self, project, event_time_start, current_time, expect_pass):
with freeze_time(datetime.fromisoformat(current_time)):
try:
run_dbt(["build", "--event-time-start", event_time_start])
assert expect_pass
except Exception as e:
assert (
"Value for `--event-time-start` must be less than the current time if `--event-time-end` is not specififed"
in e.__str__()
)
assert not expect_pass
Loading