Skip to content

Commit

Permalink
refactor(insights): Don't throw pointless validation errors (#20279)
Browse files Browse the repository at this point in the history
* refactor(insights): Don't throw pointless validation errors

* Update test_insight.py
  • Loading branch information
Twixes authored Feb 12, 2024
1 parent 86b3c2c commit c1ffba5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 70 deletions.
12 changes: 0 additions & 12 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework_csv import renderers as csvrenderers
from sentry_sdk import capture_exception

from posthog import schema
from posthog.api.documentation import extend_schema
Expand Down Expand Up @@ -813,12 +812,6 @@ def retrieve(self, request, *args, **kwargs):
@action(methods=["GET", "POST"], detail=False)
def trend(self, request: request.Request, *args: Any, **kwargs: Any):
timings = HogQLTimings()

try:
serializer = TrendSerializer(request=request)
serializer.is_valid(raise_exception=True)
except Exception as e:
capture_exception(e)
try:
with timings.measure("calculate"):
result = self.calculate_trends(request)
Expand Down Expand Up @@ -905,11 +898,6 @@ def calculate_trends(self, request: request.Request) -> Dict[str, Any]:
@action(methods=["GET", "POST"], detail=False)
def funnel(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
timings = HogQLTimings()
try:
serializer = FunnelSerializer(request=request)
serializer.is_valid(raise_exception=True)
except Exception as e:
capture_exception(e)
try:
with timings.measure("calculate"):
funnel = self.calculate_funnel(request)
Expand Down
58 changes: 0 additions & 58 deletions posthog/api/test/test_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,64 +1830,6 @@ def test_insight_trends_allowed_if_project_open_and_org_member(self) -> None:
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch("posthog.api.insight.capture_exception")
def test_serializer(self, patch_capture_exception) -> None:
"""
Various regression tests for the serializer
"""
# Display
self.client.get(
f"/api/projects/{self.team.id}/insights/trend/?events={json.dumps([{'id': '$pageview'}])}&properties=%5B%5D&display=ActionsLineGraph"
)

self.assertEqual(
patch_capture_exception.call_count,
0,
patch_capture_exception.call_args_list,
)

# Properties with an array
events = [
{
"id": "$pageview",
"properties": [{"key": "something", "value": ["something"]}],
}
]
self.client.get(
f"/api/projects/{self.team.id}/insights/trend/?events={json.dumps(events)}&properties=%5B%5D&display=ActionsLineGraph"
)
self.assertEqual(
patch_capture_exception.call_count,
0,
patch_capture_exception.call_args_list,
)

# Breakdown with ints in funnels
cohort_one_id = self._create_one_person_cohort([{"key": "prop", "value": 5, "type": "person"}])
cohort_two_id = self._create_one_person_cohort([{"key": "prop", "value": 6, "type": "person"}])

events = [
{
"id": "$pageview",
"properties": [{"key": "something", "value": ["something"]}],
},
{"id": "$pageview"},
]
response = self.client.post(
f"/api/projects/{self.team.id}/insights/funnel/",
{
"events": events,
"breakdown": [cohort_one_id, cohort_two_id],
"breakdown_type": "cohort",
},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
patch_capture_exception.call_count,
0,
patch_capture_exception.call_args_list,
)

def _create_one_person_cohort(self, properties: List[Dict[str, Any]]) -> int:
Person.objects.create(team=self.team, properties=properties)
cohort_one_id = self.client.post(
Expand Down

0 comments on commit c1ffba5

Please sign in to comment.