Skip to content

Commit

Permalink
Remove recursion, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macobo committed Oct 4, 2021
1 parent e507b54 commit a899225
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ee/clickhouse/queries/test/test_trends.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ def test_filter_test_accounts(self):
"date_to": "2020-01-12T00:00:00Z",
"events": [{"id": "$pageview", "type": "events", "order": 0}],
"filter_test_accounts": "true",
}
},
team=self.team,
)
result = ClickhouseTrends().run(filter, self.team,)
self.assertEqual(result[0]["count"], 1)
Expand Down
2 changes: 1 addition & 1 deletion posthog/models/filters/base_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, data: Optional[Dict[str, Any]] = None, request: Optional[Http
self._data = data
self.kwargs = kwargs

if "team" in kwargs and hasattr(self, "simplify"):
if "team" in kwargs and hasattr(self, "simplify") and not getattr(self, "is_simplified", False):
simplified_filter = getattr(self, "simplify")(kwargs["team"])
self._data = simplified_filter._data

Expand Down
2 changes: 1 addition & 1 deletion posthog/models/filters/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ def __init__(
self._data = data
self.kwargs = kwargs

if "team" in kwargs:
if "team" in kwargs and not self.is_simplified:
simplified_filter = self.simplify(kwargs["team"])
self._data = simplified_filter._data
10 changes: 7 additions & 3 deletions posthog/models/filters/mixins/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@


class SimplifyFilterMixin:
def simplify(self: T, team: "Team") -> T: # type: ignore
def simplify(self: T, team: "Team") -> T:
"""
Expands this filter to not refer to external resources of the team.
Actions taken:
- if filter.filter_test_accounts, adds property filters to `filter.properties`
"""

result = self.with_data({"is_simplified": True})
result = self.with_data({"is_simplified": True}) # type: ignore
if getattr(self, "filter_test_accounts", False):
result = result.with_data(
{"properties": result.properties + team.test_account_filters, "filter_test_accounts": False}
)
) # type: ignore

return result

@property
def is_simplified(self) -> bool:
return self._data.get("is_simplified", False) # type: ignore
9 changes: 5 additions & 4 deletions posthog/queries/test/test_trends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2147,9 +2147,9 @@ def test_filter_test_accounts(self):
"events": [{"id": "$pageview", "type": "events", "order": 0}],
"filter_test_accounts": "true",
}
filter = Filter(data=data)
filter_2 = Filter(data={**data, "filter_test_accounts": "false",})
filter_3 = Filter(data={**data, "breakdown": "key"})
filter = Filter(data=data, team=self.team)
filter_2 = Filter(data={**data, "filter_test_accounts": "false",}, team=self.team)
filter_3 = Filter(data={**data, "breakdown": "key"}, team=self.team)
result = trends().run(filter, self.team,)
self.assertEqual(result[0]["count"], 1)
result = trends().run(filter_2, self.team,)
Expand All @@ -2175,7 +2175,8 @@ def test_filter_test_accounts_cohorts(self):
self.team.save()

response = trends().run(
Filter(data={"events": [{"id": "event_name"}], "filter_test_accounts": True}), self.team,
Filter(data={"events": [{"id": "event_name"}], "filter_test_accounts": True}, team=self.team),
self.team,
)

self.assertEqual(response[0]["count"], 2)
Expand Down

0 comments on commit a899225

Please sign in to comment.