From d4dced4a33733a10e2b5a8fdaa5f3054d6381700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Far=C3=ADas=20Santana?= Date: Tue, 27 Aug 2024 23:04:50 +0200 Subject: [PATCH] test: Add a unit test to cover cycles created via updates --- .../warehouse/models/test/test_modeling.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/posthog/warehouse/models/test/test_modeling.py b/posthog/warehouse/models/test/test_modeling.py index 5c7e2460a9a1bd..9ab2ab2e60616c 100644 --- a/posthog/warehouse/models/test/test_modeling.py +++ b/posthog/warehouse/models/test/test_modeling.py @@ -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 @@ -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)