Debian Codegraph @

tests/test_catalog.py

raw symbols SCIP
1 import pytest
2 from harlequin.catalog import CatalogSearchResult, InteractiveCatalogItem
3 from harlequin.exception import HarlequinQueryError
4
5 from harlequin_postgres.adapter import (
6 HarlequinPostgresAdapter,
7 HarlequinPostgresConnection,
8 )
9 from harlequin_postgres.catalog import (
10 ColumnCatalogItem,
11 DatabaseCatalogItem,
12 MaterializedViewCatalogItem,
13 RelationCatalogItem,
14 SchemaCatalogItem,
15 TableCatalogItem,
16 ViewCatalogItem,
17 )
18
19
20 @pytest.fixture
21 def connection_with_objects(
22 connection: HarlequinPostgresConnection,
23 ) -> HarlequinPostgresConnection:
24 connection.execute("create schema one")
25 connection.execute("create table one.foo as select 1 as a, '2' as b")
26 connection.execute("create table one.bar as select 1 as a, '2' as b")
27 connection.execute("create table one.baz as select 1 as a, '2' as b")
28 connection.execute("create schema two")
29 connection.execute("create view two.qux as select * from one.foo")
30 connection.execute("create schema three")
31 connection.execute("create schema four")
32 connection.execute("create materialized view four.foo as select * from one.foo")
33 # the original connection fixture will clean this up.
34 return connection
35
36
37 def test_catalog(connection_with_objects: HarlequinPostgresConnection) -> None:
38 conn = connection_with_objects
39
40 catalog = conn.get_catalog()
41
42 # at least two databases, postgres and test
43 assert len(catalog.items) >= 2
44
45 [test_db_item] = filter(lambda item: item.label == "test", catalog.items)
46 assert isinstance(test_db_item, InteractiveCatalogItem)
47 assert isinstance(test_db_item, DatabaseCatalogItem)
48 assert not test_db_item.children
49 assert not test_db_item.loaded
50
51 schema_items = test_db_item.fetch_children()
52 assert all(isinstance(item, SchemaCatalogItem) for item in schema_items)
53
54 [schema_one_item] = filter(lambda item: item.label == "one", schema_items)
55 assert isinstance(schema_one_item, SchemaCatalogItem)
56 assert not schema_one_item.children
57 assert not schema_one_item.loaded
58
59 table_items = schema_one_item.fetch_children()
60 assert all(isinstance(item, RelationCatalogItem) for item in table_items)
61
62 [foo_item] = filter(lambda item: item.label == "foo", table_items)
63 assert isinstance(foo_item, TableCatalogItem)
64 assert not foo_item.children
65 assert not foo_item.loaded
66
67 foo_column_items = foo_item.fetch_children()
68 assert all(isinstance(item, ColumnCatalogItem) for item in foo_column_items)
69
70 [schema_two_item] = filter(lambda item: item.label == "two", schema_items)
71 assert isinstance(schema_two_item, SchemaCatalogItem)
72 assert not schema_two_item.children
73 assert not schema_two_item.loaded
74
75 view_items = schema_two_item.fetch_children()
76 assert all(isinstance(item, ViewCatalogItem) for item in view_items)
77
78 [qux_item] = filter(lambda item: item.label == "qux", view_items)
79 assert isinstance(qux_item, ViewCatalogItem)
80 assert not qux_item.children
81 assert not qux_item.loaded
82
83 qux_column_items = qux_item.fetch_children()
84 assert all(isinstance(item, ColumnCatalogItem) for item in qux_column_items)
85
86 assert [item.label for item in foo_column_items] == [
87 item.label for item in qux_column_items
88 ]
89
90 # ensure calling fetch_children on cols doesn't raise
91 children_items = foo_column_items[0].fetch_children()
92 assert not children_items
93
94 [schema_three_item] = filter(lambda item: item.label == "three", schema_items)
95 assert isinstance(schema_two_item, SchemaCatalogItem)
96 assert not schema_two_item.children
97 assert not schema_two_item.loaded
98
99 three_children = schema_three_item.fetch_children()
100 assert not three_children
101
102 [schema_four_item] = filter(lambda item: item.label == "four", schema_items)
103 assert isinstance(schema_four_item, SchemaCatalogItem)
104 assert not schema_four_item.children
105 assert not schema_four_item.loaded
106
107 mview_items = schema_four_item.fetch_children()
108 assert all(isinstance(item, MaterializedViewCatalogItem) for item in mview_items)
109
110 [foo_mv_item] = filter(lambda item: item.label == "foo", mview_items)
111 assert isinstance(foo_mv_item, MaterializedViewCatalogItem)
112 assert not foo_mv_item.children
113 assert not foo_mv_item.loaded
114
115 foo_mv_cols = foo_mv_item.fetch_children()
116 assert foo_mv_cols
117 assert all(isinstance(item, ColumnCatalogItem) for item in foo_mv_cols)
118
119
120 def test_catalog_items_carry_type_names(
121 connection_with_objects: HarlequinPostgresConnection,
122 ) -> None:
123 """Every item the catalog walk builds names its full type, not just a label."""
124 conn = connection_with_objects
125
126 [db_item] = [
127 i
128 for i in conn.get_catalog().items
129 if isinstance(i, DatabaseCatalogItem) and i.label == "test"
130 ]
131 schema_items = db_item.fetch_children()
132 [schema_one_item] = [i for i in schema_items if i.label == "one"]
133 [schema_two_item] = [i for i in schema_items if i.label == "two"]
134 [schema_four_item] = [i for i in schema_items if i.label == "four"]
135
136 [table_item] = [i for i in schema_one_item.fetch_children() if i.label == "foo"]
137 [view_item] = [i for i in schema_two_item.fetch_children() if i.label == "qux"]
138 [matview_item] = [i for i in schema_four_item.fetch_children() if i.label == "foo"]
139
140 assert table_item.type_name == "BASE TABLE"
141 assert view_item.type_name == "VIEW"
142 assert matview_item.type_name == "MATERIALIZED VIEW"
143
144 # a column's type_name is the full type its type_label is shortened from
145 assert [
146 (i.label, i.type_label, i.type_name) for i in table_item.fetch_children()
147 ] == [
148 ("a", "#", "integer"),
149 ("b", "s", "text"),
150 ]
151 assert [
152 (i.label, i.type_label, i.type_name) for i in matview_item.fetch_children()
153 ] == [
154 ("a", "#", "integer"),
155 ("b", "s", "text"),
156 ]
157
158
159 @pytest.fixture
160 def connection_for_search(
161 connection_with_objects: HarlequinPostgresConnection,
162 ) -> HarlequinPostgresConnection:
163 conn = connection_with_objects
164 conn.execute("create table one.orders as select 1 as customer_id, 2 as amount")
165 # a schema, a relation, and a column that all match the same term, to check
166 # that a search reports a parent before the items under it
167 conn.execute("create schema five")
168 conn.execute("create table five.five_a as select 1 as five_col")
169 # names holding LIKE metacharacters, which a term must not be able to use
170 conn.execute('create table one."a_b" as select 1 as a')
171 conn.execute('create table one."axb" as select 1 as a')
172 conn.execute('create table one."pct%tbl" as select 1 as a')
173 # the original connection fixture will clean this up.
174 return conn
175
176
177 def _labeled(results: list[CatalogSearchResult], label: str) -> CatalogSearchResult:
178 [result] = [r for r in results if r.item.label == label]
179 return result
180
181
182 def test_implements_catalog_search() -> None:
183 assert HarlequinPostgresAdapter.IMPLEMENTS_CATALOG_SEARCH is True
184
185
186 def test_search_catalog_relations(
187 connection_for_search: HarlequinPostgresConnection,
188 ) -> None:
189 conn = connection_for_search
190
191 results = conn.search_catalog("foo", kind="relations")
192
193 assert [(r.item.label, r.parents) for r in results] == [
194 ("foo", ("test", "four")),
195 ("foo", ("test", "one")),
196 ]
197 matview_result, table_result = results
198 assert isinstance(matview_result.item, MaterializedViewCatalogItem)
199 assert isinstance(table_result.item, TableCatalogItem)
200 # a relation is built by the class fetch_children() would have used for it,
201 # so its query name is the one --catalog shows for the same object
202 assert table_result.item.query_name == '"one"."foo"'
203 assert (table_result.item.type_label, table_result.item.type_name) == (
204 "t",
205 "BASE TABLE",
206 )
207 assert (matview_result.item.type_label, matview_result.item.type_name) == (
208 "mv",
209 "MATERIALIZED VIEW",
210 )
211
212
213 def test_search_catalog_relations_finds_views(
214 connection_for_search: HarlequinPostgresConnection,
215 ) -> None:
216 results = connection_for_search.search_catalog("qux", kind="relations")
217
218 [result] = results
219 assert isinstance(result.item, ViewCatalogItem)
220 assert (result.item.type_label, result.item.type_name) == ("v", "VIEW")
221 assert result.parents == ("test", "two")
222
223
224 def test_search_catalog_columns(
225 connection_for_search: HarlequinPostgresConnection,
226 ) -> None:
227 results = connection_for_search.search_catalog("customer", kind="columns")
228
229 [result] = results
230 assert isinstance(result.item, ColumnCatalogItem)
231 assert result.item.label == "customer_id"
232 assert result.parents == ("test", "one", "orders")
233 assert result.item.query_name == '"customer_id"'
234 # type_name is the full type the label is shortened from
235 assert (result.item.type_label, result.item.type_name) == ("#", "integer")
236
237
238 def test_search_catalog_columns_finds_matview_columns(
239 connection_for_search: HarlequinPostgresConnection,
240 ) -> None:
241 # four.foo is a materialized view, which information_schema does not have
242 results = connection_for_search.search_catalog("b", kind="columns")
243
244 matview_results = [r for r in results if r.parents == ("test", "four", "foo")]
245 [result] = matview_results
246 assert isinstance(result.item, ColumnCatalogItem)
247 assert result.item.label == "b"
248 assert (result.item.type_label, result.item.type_name) == ("s", "text")
249
250
251 def test_search_catalog_kinds_are_scoped(
252 connection_for_search: HarlequinPostgresConnection,
253 ) -> None:
254 conn = connection_for_search
255
256 relations = conn.search_catalog("five", kind="relations")
257 assert [r.item.label for r in relations] == ["five_a"]
258
259 columns = conn.search_catalog("five", kind="columns")
260 assert [r.item.label for r in columns] == ["five_col"]
261
262
263 def test_search_catalog_all_returns_every_level(
264 connection_for_search: HarlequinPostgresConnection,
265 ) -> None:
266 results = connection_for_search.search_catalog("five", kind="all")
267
268 # a parent arrives before the items under it
269 assert [(r.item.label, r.parents) for r in results] == [
270 ("five", ("test",)),
271 ("five_a", ("test", "five")),
272 ("five_col", ("test", "five", "five_a")),
273 ]
274 schema_result, relation_result, column_result = results
275 assert isinstance(schema_result.item, SchemaCatalogItem)
276 assert isinstance(relation_result.item, TableCatalogItem)
277 assert isinstance(column_result.item, ColumnCatalogItem)
278
279
280 def test_search_catalog_matches_databases_by_name(
281 connection_for_search: HarlequinPostgresConnection,
282 ) -> None:
283 conn = connection_for_search
284
285 results = conn.search_catalog("postgres", kind="all")
286
287 # the pool is bound to one database, but the others can still be matched
288 # by name, which is all the catalog's top level shows for them
289 result = _labeled(results, "postgres")
290 assert isinstance(result.item, DatabaseCatalogItem)
291 assert result.parents == ()
292 assert result.item.query_name == '"postgres"'
293
294 # a database is not a relation, so the narrower kinds do not report it
295 assert not conn.search_catalog("postgres", kind="relations")
296
297
298 def test_search_catalog_is_case_insensitive(
299 connection_for_search: HarlequinPostgresConnection,
300 ) -> None:
301 conn = connection_for_search
302
303 assert [(r.item.label, r.parents) for r in conn.search_catalog("FOO")] == [
304 (r.item.label, r.parents) for r in conn.search_catalog("foo")
305 ]
306
307
308 def test_search_catalog_matches_a_substring(
309 connection_for_search: HarlequinPostgresConnection,
310 ) -> None:
311 results = connection_for_search.search_catalog("rder", kind="relations")
312
313 assert [r.item.label for r in results] == ["orders"]
314
315
316 @pytest.mark.parametrize(
317 "term,expected",
318 [
319 ("a_b", ["a_b"]),
320 ("%tbl", ["pct%tbl"]),
321 ("pct%", ["pct%tbl"]),
322 ],
323 )
324 def test_search_catalog_escapes_like_metacharacters(
325 connection_for_search: HarlequinPostgresConnection,
326 term: str,
327 expected: list[str],
328 ) -> None:
329 results = connection_for_search.search_catalog(term, kind="relations")
330
331 assert [r.item.label for r in results] == expected
332
333
334 def test_search_catalog_excludes_system_schemas(
335 connection_for_search: HarlequinPostgresConnection,
336 ) -> None:
337 conn = connection_for_search
338
339 # information_schema.columns and pg_catalog.pg_statistic exist, but the
340 # catalog tree does not show them, so a search must not report them either
341 assert not conn.search_catalog("information_schema", kind="all")
342 assert not conn.search_catalog("pg_statistic", kind="relations")
343
344
345 def test_search_catalog_without_a_match_is_empty(
346 connection_for_search: HarlequinPostgresConnection,
347 ) -> None:
348 assert connection_for_search.search_catalog("no-such-object") == []
349
350
351 def test_search_catalog_raises_query_error(
352 connection_for_search: HarlequinPostgresConnection,
353 monkeypatch: pytest.MonkeyPatch,
354 ) -> None:
355 from harlequin_postgres import adapter
356
357 monkeypatch.setitem(adapter._SEARCH_SQL, "all", "select not_a_column")
358
359 with pytest.raises(HarlequinQueryError):
360 connection_for_search.search_catalog("foo")
361
362 # the connection went back to the pool, so the next search still works
363 monkeypatch.undo()
364 assert connection_for_search.search_catalog("foo")
365
366
367 def test_search_catalog_items_match_fetch_children(
368 connection_for_search: HarlequinPostgresConnection,
369 ) -> None:
370 conn = connection_for_search
371
372 [db_item] = [
373 i
374 for i in conn.get_catalog().items
375 if isinstance(i, DatabaseCatalogItem) and i.label == "test"
376 ]
377 [schema_item] = [i for i in db_item.fetch_children() if i.label == "one"]
378 [walked_relation] = [i for i in schema_item.fetch_children() if i.label == "orders"]
379 [walked_column] = [
380 i for i in walked_relation.fetch_children() if i.label == "customer_id"
381 ]
382
383 [searched_relation] = conn.search_catalog("orders", kind="relations")
384 [searched_column] = conn.search_catalog("customer_id", kind="columns")
385
386 for walked, searched in (
387 (walked_relation, searched_relation.item),
388 (walked_column, searched_column.item),
389 ):
390 assert type(searched) is type(walked)
391 assert searched.label == walked.label
392 assert searched.query_name == walked.query_name
393 assert searched.qualified_identifier == walked.qualified_identifier
394 assert searched.type_label == walked.type_label
395 assert searched.type_name == walked.type_name