|
| 1 | +from freezegun import freeze_time |
| 2 | +from posthog.test.base import BaseTest |
| 3 | + |
| 4 | +from parameterized import parameterized |
| 5 | + |
| 6 | +from products.data_modeling.backend.models import CycleDetectionError, Edge, Node |
| 7 | +from products.data_warehouse.backend.models import DataWarehouseSavedQuery |
| 8 | + |
| 9 | +LINKED_LIST_DAG_ID = "linked_list" |
| 10 | +BALANCED_TREE_DAG_ID = "balanced_tree" |
| 11 | + |
| 12 | + |
| 13 | +def _basic_saved_query_with_label(label: str): |
| 14 | + return f"""SELECT '{label}'""" |
| 15 | + |
| 16 | + |
| 17 | +class LinkedListCycleDetectionTest(BaseTest): |
| 18 | + @classmethod |
| 19 | + def setUpTestData(cls): |
| 20 | + super().setUpTestData() |
| 21 | + with freeze_time("2025-01-01T12:00:00.000Z"): |
| 22 | + ll_queries = [ |
| 23 | + DataWarehouseSavedQuery.objects.create( |
| 24 | + name=f"ll_{i}", |
| 25 | + team=cls.team, |
| 26 | + query=_basic_saved_query_with_label(str(i)), |
| 27 | + ) |
| 28 | + for i in range(25) |
| 29 | + ] |
| 30 | + ll_nodes = [ |
| 31 | + Node.objects.create(team=cls.team, dag_id=LINKED_LIST_DAG_ID, saved_query=query, name=f"ll_{i}") |
| 32 | + for i, query in enumerate(ll_queries) |
| 33 | + ] |
| 34 | + for i in range(len(ll_nodes) - 1): |
| 35 | + Edge.objects.create( |
| 36 | + team=cls.team, |
| 37 | + dag_id=LINKED_LIST_DAG_ID, |
| 38 | + source=ll_nodes[i], |
| 39 | + target=ll_nodes[i + 1], |
| 40 | + ) |
| 41 | + |
| 42 | + @parameterized.expand( |
| 43 | + [ |
| 44 | + # low index to high index is always fine |
| 45 | + ("0", "24", False), |
| 46 | + ("0", "5", False), |
| 47 | + ("0", "2", False), # note 0, 1 would be a duplicate edge and would fail |
| 48 | + # self cycle case |
| 49 | + ("0", "0", True), |
| 50 | + # high index to low always causes a cycle |
| 51 | + ("1", "0", True), |
| 52 | + ("5", "0", True), |
| 53 | + ("24", "0", True), |
| 54 | + ], |
| 55 | + ) |
| 56 | + def test_linked_list_dag(self, source_label, target_label, should_raise): |
| 57 | + source = Node.objects.get(saved_query__name=f"ll_{source_label}") |
| 58 | + target = Node.objects.get(saved_query__name=f"ll_{target_label}") |
| 59 | + if should_raise: |
| 60 | + with self.assertRaises(Exception): |
| 61 | + Edge.objects.create(team=source.team, dag_id=LINKED_LIST_DAG_ID, source=source, target=target) |
| 62 | + else: |
| 63 | + edge = Edge.objects.create(team=source.team, dag_id=LINKED_LIST_DAG_ID, source=source, target=target) |
| 64 | + edge.delete() |
| 65 | + |
| 66 | + |
| 67 | +class TreeCycleDetectionTest(BaseTest): |
| 68 | + @classmethod |
| 69 | + def setUpTestData(cls): |
| 70 | + super().setUpTestData() |
| 71 | + with freeze_time("2025-01-01T12:00:00.000Z"): |
| 72 | + bt_root = [ |
| 73 | + DataWarehouseSavedQuery.objects.create( |
| 74 | + name="bt_root", |
| 75 | + team=cls.team, |
| 76 | + query=_basic_saved_query_with_label("root"), |
| 77 | + ) |
| 78 | + ] |
| 79 | + bt_children = [ |
| 80 | + DataWarehouseSavedQuery.objects.create( |
| 81 | + name=f"bt_child_{i}", |
| 82 | + team=cls.team, |
| 83 | + query=_basic_saved_query_with_label(f"child_{i}"), |
| 84 | + ) |
| 85 | + for i in range(5) |
| 86 | + ] |
| 87 | + bt_grandchildren = [ |
| 88 | + DataWarehouseSavedQuery.objects.create( |
| 89 | + name=f"bt_child_{i}_child_{j}", |
| 90 | + team=cls.team, |
| 91 | + query=_basic_saved_query_with_label(f"child_{i}_child_{j}"), |
| 92 | + ) |
| 93 | + for i in range(5) |
| 94 | + for j in range(5) |
| 95 | + ] |
| 96 | + bt_nodes = [ |
| 97 | + Node.objects.create(team=cls.team, dag_id=BALANCED_TREE_DAG_ID, saved_query=query, name=query.name) |
| 98 | + for query in bt_root + bt_children + bt_grandchildren |
| 99 | + ] |
| 100 | + root = bt_nodes[0] |
| 101 | + children = bt_nodes[1:6] |
| 102 | + for i, child in enumerate(children): |
| 103 | + Edge.objects.create(team=cls.team, dag_id=BALANCED_TREE_DAG_ID, source=root, target=child) |
| 104 | + for j in range(5): |
| 105 | + grandchild = bt_nodes[6 + i * 5 + j] |
| 106 | + Edge.objects.create( |
| 107 | + team=cls.team, |
| 108 | + dag_id=BALANCED_TREE_DAG_ID, |
| 109 | + source=child, |
| 110 | + target=grandchild, |
| 111 | + ) |
| 112 | + |
| 113 | + @parameterized.expand( |
| 114 | + [ |
| 115 | + # root to any grandchild is always fine |
| 116 | + ("root", "child_0_child_0", False), |
| 117 | + ("root", "child_2_child_0", False), |
| 118 | + ("root", "child_4_child_0", False), |
| 119 | + # child to any grandchild is always fine |
| 120 | + ("child_0", "child_1_child_0", False), |
| 121 | + ("child_0", "child_2_child_0", False), |
| 122 | + ("child_0", "child_4_child_0", False), |
| 123 | + # self cycle case |
| 124 | + ("root", "root", True), |
| 125 | + # child to root always causes a cycle |
| 126 | + ("child_0", "root", True), |
| 127 | + ("child_2", "root", True), |
| 128 | + ("child_4", "root", True), |
| 129 | + # grandchild to root always causes a cycle |
| 130 | + ("child_0_child_0", "root", True), |
| 131 | + ("child_2_child_0", "root", True), |
| 132 | + ("child_4_child_0", "root", True), |
| 133 | + # grandchild to any child not its parent is always fine (i.e. root -> 0 -> 0_0 -> 1 + root -> 1 = no cycle) |
| 134 | + ("child_1_child_0", "child_0", False), |
| 135 | + ("child_2_child_0", "child_0", False), |
| 136 | + ("child_4_child_0", "child_0", False), |
| 137 | + # any grandchild to its parent always causes a cycle |
| 138 | + ("child_0_child_0", "child_0", True), |
| 139 | + ("child_0_child_2", "child_0", True), |
| 140 | + ("child_0_child_4", "child_0", True), |
| 141 | + # any child to any other child is always fine |
| 142 | + ("child_0", "child_1", False), |
| 143 | + ("child_0", "child_2", False), |
| 144 | + ("child_0", "child_4", False), |
| 145 | + # any grandchild to any other grandchild is always fine |
| 146 | + ("child_0_child_0", "child_1_child_1", False), |
| 147 | + ("child_0_child_0", "child_2_child_2", False), |
| 148 | + ("child_0_child_0", "child_4_child_4", False), |
| 149 | + ], |
| 150 | + ) |
| 151 | + def test_tree_like_dag(self, source_label, target_label, should_raise): |
| 152 | + source = Node.objects.get(saved_query__name=f"bt_{source_label}") |
| 153 | + target = Node.objects.get(saved_query__name=f"bt_{target_label}") |
| 154 | + if should_raise: |
| 155 | + with self.assertRaises(CycleDetectionError): |
| 156 | + Edge.objects.create(team=source.team, dag_id=BALANCED_TREE_DAG_ID, source=source, target=target) |
| 157 | + else: |
| 158 | + edge = Edge.objects.create(team=source.team, dag_id=BALANCED_TREE_DAG_ID, source=source, target=target) |
| 159 | + edge.delete() |
| 160 | + |
| 161 | + def test_disallowed_object_functions(self): |
| 162 | + test_team = self.team |
| 163 | + test_node = Node.objects.get(name="bt_root") |
| 164 | + bt_edges = Edge.objects.filter(dag_id=BALANCED_TREE_DAG_ID) |
| 165 | + disallowed = ("dag_id", "source", "source_id", "target", "target_id", "team", "team_id") |
| 166 | + for key in disallowed: |
| 167 | + # test update disallowed for each key |
| 168 | + with self.assertRaises(NotImplementedError): |
| 169 | + if key.endswith("id"): |
| 170 | + bt_edges.update(**{key: "test"}) |
| 171 | + elif key == "source": |
| 172 | + bt_edges.update(source=test_node) |
| 173 | + elif key == "target": |
| 174 | + bt_edges.update(target=test_node) |
| 175 | + elif key == "team": |
| 176 | + bt_edges.update(team=test_team) |
| 177 | + # test bulk_update disallowed for each key |
| 178 | + mock_edges = [Edge(source=test_node, target=test_node, team=test_team, dag_id="test") for _ in range(3)] |
| 179 | + for edge in mock_edges: |
| 180 | + if key.endswith("id"): |
| 181 | + setattr(edge, key, "test") |
| 182 | + elif key in ("source", "target"): |
| 183 | + setattr(edge, key, test_node) |
| 184 | + elif key == "team": |
| 185 | + setattr(edge, key, test_team) |
| 186 | + with self.assertRaises(NotImplementedError): |
| 187 | + Edge.objects.bulk_update(mock_edges, [key]) |
| 188 | + # test bulk_create disallowed |
| 189 | + with self.assertRaises(NotImplementedError): |
| 190 | + Edge.objects.bulk_create(bt_edges) |
0 commit comments