From 7a58def845f060f2390f8c0a1b7711a6d86c05d0 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Fri, 26 Apr 2024 20:16:08 +0000 Subject: [PATCH] feat(analytics): add tests for custom properties for UTM code information --- tests/pytest/core/test_analytics.py | 54 ++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/tests/pytest/core/test_analytics.py b/tests/pytest/core/test_analytics.py index 49065c557..e343b55f1 100644 --- a/tests/pytest/core/test_analytics.py +++ b/tests/pytest/core/test_analytics.py @@ -1,7 +1,41 @@ import pytest import benefits.core.analytics -from benefits.core.analytics import Event +from benefits.core.analytics import Event, ViewedPageEvent +from django.contrib.sessions.middleware import SessionMiddleware +from django.middleware.locale import LocaleMiddleware + + +@pytest.fixture +def app_request_utm(rf): + """ + Fixture creates and initializes a new Django request object similar to a real application request with UTM codes. + """ + # create a request for the path with UTM codes, initialize + app_request = rf.get( + "/some/arbitrary/path?utm_campaign=campaign&utm_source=source&utm_medium=medium&utm_content=content&utm_id=id" + ) + + # https://stackoverflow.com/a/55530933/358804 + middleware = [SessionMiddleware(lambda x: x), LocaleMiddleware(lambda x: x)] + for m in middleware: + m.process_request(app_request) + + app_request.session.save() + benefits.core.analytics.session.reset(app_request) + + return app_request + + +@pytest.fixture +def utm_code_data(): + return { + "utm_campaign": "campaign", + "utm_source": "source", + "utm_medium": "medium", + "utm_content": "content", + "utm_id": "id", + } @pytest.mark.django_db @@ -80,3 +114,21 @@ def test_Event_update_user_properties(app_request): assert key in event.user_properties assert event.user_properties[key] == value + + +@pytest.mark.django_db +def test_ViewedPageEvent_with_UTM(app_request_utm, utm_code_data): + event = ViewedPageEvent(app_request_utm) + + for key, value in utm_code_data.items(): + assert event.event_properties[key] == value + assert event.user_properties[key] == value + + +@pytest.mark.django_db +def test_ViewedPageEvent_without_UTM(app_request, utm_code_data): + event = ViewedPageEvent(app_request) + + for key in utm_code_data: + assert event.event_properties[key] is None + assert event.user_properties[key] is None