From c9097ec05958ddf9647215b9e0d3e1817ad50b55 Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 26 Dec 2023 09:19:32 +0000 Subject: [PATCH] chore: Add tests for relative date parsing --- posthog/queries/test/test_base.py | 132 +++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) diff --git a/posthog/queries/test/test_base.py b/posthog/queries/test/test_base.py index e856bb315800b..6cfb1472af7cc 100644 --- a/posthog/queries/test/test_base.py +++ b/posthog/queries/test/test_base.py @@ -1,5 +1,6 @@ import datetime import re +import unittest from unittest.mock import patch from dateutil import parser, tz @@ -10,7 +11,7 @@ from posthog.models.filters.path_filter import PathFilter from posthog.models.property.property import Property -from posthog.queries.base import match_property, sanitize_property_key +from posthog.queries.base import match_property, relative_date_parse_for_feature_flag_matching, sanitize_property_key from posthog.test.base import APIBaseTest @@ -406,3 +407,132 @@ def test_sanitize_keys(key, expected): sanitized_key = sanitize_property_key(key) assert sanitized_key == expected + + +class TestRelativeDateParsing(unittest.TestCase): + def test_hour_parsing(self): + with freeze_time("2020-01-01T12:01:20.1340Z"): + assert relative_date_parse_for_feature_flag_matching("1h") == datetime.datetime( + 2020, 1, 1, 11, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2h") == datetime.datetime( + 2020, 1, 1, 10, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("24h") == datetime.datetime( + 2019, 12, 31, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("30h") == datetime.datetime( + 2019, 12, 31, 6, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("48h") == datetime.datetime( + 2019, 12, 30, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + + assert relative_date_parse_for_feature_flag_matching( + "24h" + ) == relative_date_parse_for_feature_flag_matching("1d") + assert relative_date_parse_for_feature_flag_matching( + "48h" + ) == relative_date_parse_for_feature_flag_matching("2d") + + def test_day_parsing(self): + with freeze_time("2020-01-01T12:01:20.1340Z"): + assert relative_date_parse_for_feature_flag_matching("1d") == datetime.datetime( + 2019, 12, 31, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2d") == datetime.datetime( + 2019, 12, 30, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("7d") == datetime.datetime( + 2019, 12, 25, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("14d") == datetime.datetime( + 2019, 12, 18, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("30d") == datetime.datetime( + 2019, 12, 2, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + + assert relative_date_parse_for_feature_flag_matching("7d") == relative_date_parse_for_feature_flag_matching( + "1w" + ) + + def test_week_parsing(self): + with freeze_time("2020-01-01T12:01:20.1340Z"): + assert relative_date_parse_for_feature_flag_matching("1w") == datetime.datetime( + 2019, 12, 25, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2w") == datetime.datetime( + 2019, 12, 18, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("4w") == datetime.datetime( + 2019, 12, 4, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("8w") == datetime.datetime( + 2019, 11, 6, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + + assert relative_date_parse_for_feature_flag_matching("1m") == datetime.datetime( + 2019, 12, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("4w") != relative_date_parse_for_feature_flag_matching( + "1m" + ) + + def test_month_parsing(self): + with freeze_time("2020-01-01T12:01:20.1340Z"): + assert relative_date_parse_for_feature_flag_matching("1m") == datetime.datetime( + 2019, 12, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2m") == datetime.datetime( + 2019, 11, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("4m") == datetime.datetime( + 2019, 9, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("8m") == datetime.datetime( + 2019, 5, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + + assert relative_date_parse_for_feature_flag_matching("1y") == datetime.datetime( + 2019, 1, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching( + "12m" + ) == relative_date_parse_for_feature_flag_matching("1y") + + with freeze_time("2020-04-03T00:00:00"): + assert relative_date_parse_for_feature_flag_matching("1m") == datetime.datetime( + 2020, 3, 3, 0, 0, 0, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2m") == datetime.datetime( + 2020, 2, 3, 0, 0, 0, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("4m") == datetime.datetime( + 2019, 12, 3, 0, 0, 0, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("8m") == datetime.datetime( + 2019, 8, 3, 0, 0, 0, tzinfo=tz.gettz("UTC") + ) + + assert relative_date_parse_for_feature_flag_matching("1y") == datetime.datetime( + 2019, 4, 3, 0, 0, 0, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching( + "12m" + ) == relative_date_parse_for_feature_flag_matching("1y") + + def test_year_parsing(self): + with freeze_time("2020-01-01T12:01:20.1340Z"): + assert relative_date_parse_for_feature_flag_matching("1y") == datetime.datetime( + 2019, 1, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("2y") == datetime.datetime( + 2018, 1, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("4y") == datetime.datetime( + 2016, 1, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + ) + assert relative_date_parse_for_feature_flag_matching("8y") == datetime.datetime( + 2012, 1, 1, 12, 1, 20, 134000, tzinfo=tz.gettz("UTC") + )