Skip to content

Commit

Permalink
test: Add a unit test to cover cycles created via updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias committed Aug 28, 2024
1 parent 9dbe068 commit d4dced4
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions posthog/warehouse/models/test/test_modeling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from django.db.utils import ProgrammingError

from posthog.test.base import BaseTest
from posthog.warehouse.models.datawarehouse_saved_query import DataWarehouseSavedQuery
Expand Down Expand Up @@ -219,3 +220,39 @@ def test_creating_cycles_raises_exception(self):
with pytest.raises(UnknownParentError):
DataWarehouseModelPath.objects.create_from_saved_query(grand_child_saved_query)
DataWarehouseModelPath.objects.create_from_saved_query(cycling_child_saved_query)

def test_creating_cycles_via_updates_raises_exception(self):
"""Test cycles cannot be created just by updating queries that select from each other."""
parent_query = """\
select
events.event,
persons.properties
from events
left join persons on events.person_id = persons.id
where events.event = 'login' and person.pdi != 'some_distinct_id'
"""
parent_saved_query = DataWarehouseSavedQuery.objects.create(
team=self.team,
name="my_model",
query={"query": parent_query},
)
child_saved_query = DataWarehouseSavedQuery.objects.create(
team=self.team,
name="my_model_child",
query={"query": "select * from my_model"},
)
grand_child_saved_query = DataWarehouseSavedQuery.objects.create(
team=self.team,
name="my_model_grand_child",
query={"query": "select * from my_model_child"},
)

DataWarehouseModelPath.objects.create_from_saved_query(parent_saved_query)
DataWarehouseModelPath.objects.create_from_saved_query(child_saved_query)
DataWarehouseModelPath.objects.create_from_saved_query(grand_child_saved_query)

child_saved_query.query = {"query": "select * from my_model union all select * from my_model_grand_child"}
child_saved_query.save()

with pytest.raises(ProgrammingError):
DataWarehouseModelPath.objects.update_from_saved_query(child_saved_query)

0 comments on commit d4dced4

Please sign in to comment.