From ec049a92183187e42b6ed220f7f26ece9858fc20 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Mon, 16 Sep 2024 19:17:07 -0400 Subject: [PATCH] add testing --- core/dbt/tracking.py | 2 +- tests/unit/test_behavior_flags.py | 59 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_behavior_flags.py diff --git a/core/dbt/tracking.py b/core/dbt/tracking.py index 1bfa9036bb0..fcae7d86028 100644 --- a/core/dbt/tracking.py +++ b/core/dbt/tracking.py @@ -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, ) diff --git a/tests/unit/test_behavior_flags.py b/tests/unit/test_behavior_flags.py new file mode 100644 index 00000000000..0188480ce02 --- /dev/null +++ b/tests/unit/test_behavior_flags.py @@ -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