Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Nov 7, 2023
1 parent 4346d3d commit cc68102
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
6 changes: 4 additions & 2 deletions posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ def match_property(property, property_values) -> bool:
override_value = property_values[key]

if operator in ("exact", "is_not"):

def compute_exact_match(value, override_value):
if isinstance(value, list):
return str(override_value).lower() in [str(val).lower() for val in value]
return str(value).lower() == str(override_value).lower()

if operator == "exact":
return compute_exact_match(value, override_value)
else:
Expand Down Expand Up @@ -298,6 +299,7 @@ def match_property_group(property_group, property_values, cohort_properties) ->
# if we get here, all matched in AND case, or none matched in OR case
return property_group_type == "AND"


def relative_date_parse_for_feature_flag_matching(value: str) -> Optional[datetime.datetime]:
regex = r"(?P<number>[0-9]+)(?P<interval>[a-z])"
match = re.search(regex, value)
Expand All @@ -320,4 +322,4 @@ def relative_date_parse_for_feature_flag_matching(value: str) -> Optional[dateti

return parsed_dt
else:
return None
return None
46 changes: 44 additions & 2 deletions posthog/test/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,7 @@ def test_match_property_date_operators(self):

self.assertTrue(match_property(property_d, {"key": "2022-04-05 11:34:11 +00:00"}))
self.assertFalse(match_property(property_d, {"key": "2022-04-05 11:34:13 +00:00"}))

@freeze_time("2022-05-01")
def test_match_property_relative_date_operators(self):
property_a = self.property(key="key", value="6h", operator="is_relative_date_before")
Expand Down Expand Up @@ -1914,7 +1914,7 @@ def test_match_property_relative_date_operators(self):

with self.assertRaises(InconclusiveMatchError):
self.assertFalse(match_property(property_c, {"key": 1}))

with self.assertRaises(InconclusiveMatchError):
self.assertFalse(match_property(property_c, {"key": "2022-05-30"}))

Expand Down Expand Up @@ -1974,6 +1974,48 @@ def test_match_property_relative_date_operators(self):
self.assertFalse(match_property(property_n, {"key": "2021-04-30 00:00:00"}))
self.assertFalse(match_property(property_n, {"key": "2021-03-01 12:13:00"}))

def test_none_property_value_with_all_operators(self):
property_a = self.property(key="key", value="none", operator="is_not")
self.assertFalse(match_property(property_a, {"key": None}))
self.assertTrue(match_property(property_a, {"key": "non"}))

property_b = self.property(key="key", value=None, operator="is_set")
self.assertTrue(match_property(property_b, {"key": None}))

property_c = self.property(key="key", value="no", operator="icontains")
self.assertTrue(match_property(property_c, {"key": None}))
self.assertFalse(match_property(property_c, {"key": "smh"}))

property_d = self.property(key="key", value="No", operator="regex")
self.assertTrue(match_property(property_d, {"key": None}))

property_d_lower_case = self.property(key="key", value="no", operator="regex")
self.assertFalse(match_property(property_d_lower_case, {"key": None}))

property_e = self.property(key="key", value=1, operator="gt")
self.assertTrue(match_property(property_e, {"key": None}))

property_f = self.property(key="key", value=1, operator="lt")
self.assertFalse(match_property(property_f, {"key": None}))

property_g = self.property(key="key", value="xyz", operator="gte")
self.assertFalse(match_property(property_g, {"key": None}))

property_h = self.property(key="key", value="Oo", operator="lte")
self.assertTrue(match_property(property_h, {"key": None}))

property_i = self.property(key="key", value="2022-05-01", operator="is_date_before")
with self.assertRaises(InconclusiveMatchError):
self.assertFalse(match_property(property_i, {"key": None}))

property_j = self.property(key="key", value="2022-05-01", operator="is_date_after")
with self.assertRaises(InconclusiveMatchError):
self.assertFalse(match_property(property_j, {"key": None}))

property_k = self.property(key="key", value="2022-05-01", operator="is_date_before")
with self.assertRaises(InconclusiveMatchError):
self.assertFalse(match_property(property_k, {"key": "random"}))


class TestCaptureCalls(unittest.TestCase):
@mock.patch.object(Client, "capture")
Expand Down

0 comments on commit cc68102

Please sign in to comment.