Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare committed Sep 16, 2024
1 parent b3bf1a1 commit ec049a9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/dbt/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def track_behavior_deprecation_warn(msg: EventMsg) -> None:
track(
active_user,
category="dbt",
action="behavior_deprecation",
action=msg.info.name,
label=get_invocation_id(),
context=context,
)
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/test_behavior_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from dbt.tracking import (
disable_tracking,
initialize_from_flags,
track_behavior_deprecation_warn,
)
from dbt_common.behavior_flags import Behavior
from dbt_common.events.event_manager_client import (
add_callback_to_manager,
cleanup_event_logger,
)


@pytest.fixture
def snowplow_tracker(mocker):
# initialize `active_user` without writing the cookie to disk
initialize_from_flags(True, "")
mocker.patch("dbt.tracking.User.set_cookie").return_value = {"id": 42}

# add the relevant callback to the event manager
add_callback_to_manager(track_behavior_deprecation_warn)

# don't make a call, catch the request
snowplow_tracker = mocker.patch("dbt.tracking.tracker.track_struct_event")

yield snowplow_tracker

# teardown
cleanup_event_logger()
disable_tracking()


def test_false_evaluation_triggers_snowplow_tracking(snowplow_tracker):
behavior = Behavior([{"name": "my_flag", "default": False}], {})
if behavior.my_flag:
# trigger a False evaluation
assert False, "This flag should evaluate to false and skip this line"
assert snowplow_tracker.called


def test_true_evaluation_does_not_trigger_snowplow_tracking(snowplow_tracker):
behavior = Behavior([{"name": "my_flag", "default": True}], {})
if behavior.my_flag:
pass
else:
# trigger a True evaluation
assert False, "This flag should evaluate to false and skip this line"
assert not snowplow_tracker.called


def test_false_evaluation_does_not_trigger_snowplow_tracking_when_disabled(snowplow_tracker):
disable_tracking()

behavior = Behavior([{"name": "my_flag", "default": False}], {})
if behavior.my_flag:
# trigger a False evaluation
assert False, "This flag should evaluate to false and skip this line"
assert not snowplow_tracker.called

0 comments on commit ec049a9

Please sign in to comment.