Debian Codegraph @

tests/test_catalog.py

raw symbols SCIP
1 import pytest
2 from harlequin.catalog import CatalogSearchKind, CatalogSearchResult
3
4 from harlequin_mysql.adapter import HarlequinMySQLConnection
5 from harlequin_mysql.catalog import (
6 ColumnCatalogItem,
7 DatabaseCatalogItem,
8 RelationCatalogItem,
9 TableCatalogItem,
10 ViewCatalogItem,
11 )
12
13
14 @pytest.fixture
15 def connection_with_objects(
16 connection: HarlequinMySQLConnection,
17 ) -> HarlequinMySQLConnection:
18 connection.execute("create database one")
19 connection.execute("create table one.foo as select 1 as a, '2' as b")
20 connection.execute("create table one.bar as select 1 as a, '2' as b")
21 connection.execute("create table one.baz as select 1 as a, '2' as b")
22 connection.execute("create database two")
23 connection.execute("create view two.qux as select * from one.foo")
24 connection.execute("create database three")
25 # the original connection fixture will clean this up.
26 return connection
27
28
29 def test_catalog(connection_with_objects: HarlequinMySQLConnection) -> None:
30 conn = connection_with_objects
31
32 catalog = conn.get_catalog()
33
34 # five databases: dev, test, one, two, and three.
35 assert len(catalog.items) == 5
36
37 database_items = catalog.items
38 assert all(isinstance(item, DatabaseCatalogItem) for item in database_items)
39
40 [database_one_item] = filter(lambda item: item.label == "one", database_items)
41 assert isinstance(database_one_item, DatabaseCatalogItem)
42 assert not database_one_item.children
43 assert not database_one_item.loaded
44
45 table_items = database_one_item.fetch_children()
46 assert all(isinstance(item, RelationCatalogItem) for item in table_items)
47
48 [foo_item] = filter(lambda item: item.label == "foo", table_items)
49 assert isinstance(foo_item, TableCatalogItem)
50 assert not foo_item.children
51 assert not foo_item.loaded
52
53 foo_column_items = foo_item.fetch_children()
54 assert all(isinstance(item, ColumnCatalogItem) for item in foo_column_items)
55
56 [database_two_item] = filter(lambda item: item.label == "two", database_items)
57 assert isinstance(database_two_item, DatabaseCatalogItem)
58 assert not database_two_item.children
59 assert not database_two_item.loaded
60
61 view_items = database_two_item.fetch_children()
62 assert all(isinstance(item, ViewCatalogItem) for item in view_items)
63
64 [qux_item] = filter(lambda item: item.label == "qux", view_items)
65 assert isinstance(qux_item, ViewCatalogItem)
66 assert not qux_item.children
67 assert not qux_item.loaded
68
69 qux_column_items = qux_item.fetch_children()
70 assert all(isinstance(item, ColumnCatalogItem) for item in qux_column_items)
71
72 assert [item.label for item in foo_column_items] == [
73 item.label for item in qux_column_items
74 ]
75
76 # ensure calling fetch_children on cols doesn't raise
77 children_items = foo_column_items[0].fetch_children()
78 assert not children_items
79
80 [database_three_item] = filter(lambda item: item.label == "three", database_items)
81 assert isinstance(database_three_item, DatabaseCatalogItem)
82 assert not database_three_item.children
83 assert not database_three_item.loaded
84
85 three_children = database_three_item.fetch_children()
86 assert not three_children
87
88
89 @pytest.fixture
90 def connection_with_typed_objects(
91 connection: HarlequinMySQLConnection,
92 ) -> HarlequinMySQLConnection:
93 connection.execute("create database one")
94 connection.execute(
95 "create table one.customers ("
96 " customer_id int,"
97 " amount decimal(18, 2),"
98 " name varchar(20),"
99 " customerxid int,"
100 " secret int invisible"
101 ")"
102 )
103 connection.execute("create database two")
104 connection.execute("create view two.customers_v as select * from one.customers")
105 # the original connection fixture will clean this up.
106 return connection
107
108
109 def _result(
110 results: list[CatalogSearchResult],
111 label: str,
112 parents: tuple[str, ...] | None = None,
113 ) -> CatalogSearchResult | None:
114 """The one result for an item, identified by its label and its path."""
115 matches = [
116 result
117 for result in results
118 if result.item.label == label and (parents is None or result.parents == parents)
119 ]
120 assert len(matches) <= 1, f"{label} matched more than once"
121 return matches[0] if matches else None
122
123
124 def test_catalog_type_names(
125 connection_with_typed_objects: HarlequinMySQLConnection,
126 ) -> None:
127 conn = connection_with_typed_objects
128
129 database_items = conn.get_catalog().items
130 [database_one_item] = filter(lambda item: item.label == "one", database_items)
131 assert database_one_item.type_name == "database"
132
133 assert isinstance(database_one_item, DatabaseCatalogItem)
134 [table_item] = database_one_item.fetch_children()
135 assert table_item.type_name == "BASE TABLE"
136
137 column_items = table_item.fetch_children()
138 # the full type, not the shortened type_label, and not the invisible column
139 assert [(item.label, item.type_label, item.type_name) for item in column_items] == [
140 ("customer_id", "##", "int"),
141 ("amount", "#.#", "decimal(18,2)"),
142 ("name", "s", "varchar(20)"),
143 ("customerxid", "##", "int"),
144 ]
145
146 [database_two_item] = filter(lambda item: item.label == "two", database_items)
147 assert isinstance(database_two_item, DatabaseCatalogItem)
148 [view_item] = database_two_item.fetch_children()
149 assert view_item.type_name == "VIEW"
150
151
152 def test_search_catalog_finds_every_level(
153 connection_with_typed_objects: HarlequinMySQLConnection,
154 ) -> None:
155 conn = connection_with_typed_objects
156
157 results = conn.search_catalog("customer")
158
159 # the database "one" does not contain the term, but its relation and
160 # columns do
161 assert _result(results, "one") is None
162
163 table_result = _result(results, "customers")
164 assert table_result is not None
165 assert isinstance(table_result.item, TableCatalogItem)
166 assert table_result.parents == ("one",)
167 assert table_result.item.type_name == "BASE TABLE"
168
169 view_result = _result(results, "customers_v")
170 assert view_result is not None
171 assert isinstance(view_result.item, ViewCatalogItem)
172 assert view_result.parents == ("two",)
173
174 column_result = _result(results, "customer_id", ("one", "customers"))
175 assert column_result is not None
176 assert isinstance(column_result.item, ColumnCatalogItem)
177 assert column_result.item.type_label == "##"
178 assert column_result.item.type_name == "int"
179
180 # the same column of the view is its own result, under its own path
181 assert _result(results, "customer_id", ("two", "customers_v")) is not None
182
183
184 def test_search_catalog_matches_databases(
185 connection_with_typed_objects: HarlequinMySQLConnection,
186 ) -> None:
187 conn = connection_with_typed_objects
188
189 results = conn.search_catalog("ne")
190
191 database_result = _result(results, "one")
192 assert database_result is not None
193 assert isinstance(database_result.item, DatabaseCatalogItem)
194 assert database_result.parents == ()
195 assert database_result.item.type_name == "database"
196 assert database_result.item.query_name == "`one`"
197
198
199 def test_search_catalog_builds_the_items_the_catalog_shows(
200 connection_with_typed_objects: HarlequinMySQLConnection,
201 ) -> None:
202 conn = connection_with_typed_objects
203
204 database_items = conn.get_catalog().items
205 [database_one_item] = filter(lambda item: item.label == "one", database_items)
206 assert isinstance(database_one_item, DatabaseCatalogItem)
207 [table_item] = database_one_item.fetch_children()
208 [column_item, *_] = table_item.fetch_children()
209
210 table_result = _result(
211 conn.search_catalog("customers", "relations"), "customers", ("one",)
212 )
213 assert table_result is not None
214 assert table_result.item == table_item
215
216 column_result = _result(
217 conn.search_catalog("customer_id", "columns"),
218 "customer_id",
219 ("one", "customers"),
220 )
221 assert column_result is not None
222 assert column_result.item == column_item
223
224
225 @pytest.mark.parametrize("term", ["CUSTOMERS", "Customers", "customers"])
226 def test_search_catalog_is_case_insensitive(
227 connection_with_typed_objects: HarlequinMySQLConnection, term: str
228 ) -> None:
229 conn = connection_with_typed_objects
230
231 results = conn.search_catalog(term, "relations")
232
233 assert [result.item.label for result in results] == ["customers", "customers_v"]
234
235
236 @pytest.mark.parametrize(
237 "kind,expected",
238 [
239 ("relations", ["customers", "customers_v"]),
240 ("columns", ["customer_id", "customerxid", "customer_id", "customerxid"]),
241 (
242 "all",
243 [
244 "customers",
245 "customer_id",
246 "customerxid",
247 "customers_v",
248 "customer_id",
249 "customerxid",
250 ],
251 ),
252 ],
253 )
254 def test_search_catalog_kinds(
255 connection_with_typed_objects: HarlequinMySQLConnection,
256 kind: CatalogSearchKind,
257 expected: list[str],
258 ) -> None:
259 conn = connection_with_typed_objects
260
261 results = conn.search_catalog("customer", kind)
262
263 # a parent arrives before its own children
264 assert [result.item.label for result in results] == expected
265
266
267 def test_search_catalog_escapes_like_metacharacters(
268 connection_with_typed_objects: HarlequinMySQLConnection,
269 ) -> None:
270 conn = connection_with_typed_objects
271
272 # the underscore is a character to match, not a wildcard
273 results = conn.search_catalog("customer_id", "columns")
274 assert {result.item.label for result in results} == {"customer_id"}
275
276 assert conn.search_catalog("%", "all") == []
277 assert conn.search_catalog("!", "all") == []
278 assert conn.search_catalog("\\", "all") == []
279
280
281 def test_search_catalog_excludes_system_databases(
282 connection_with_typed_objects: HarlequinMySQLConnection,
283 ) -> None:
284 conn = connection_with_typed_objects
285
286 # information_schema.tables and mysql.user exist, but the catalog does not
287 # show the databases that hold them
288 results = conn.search_catalog("user")
289
290 assert all(result.parents[:1] != ("mysql",) for result in results)
291 assert _result(results, "mysql") is None
292 assert _result(results, "information_schema") is None
293
294
295 def test_search_catalog_excludes_invisible_columns(
296 connection_with_typed_objects: HarlequinMySQLConnection,
297 ) -> None:
298 conn = connection_with_typed_objects
299
300 assert conn.search_catalog("secret", "columns") == []
301
302
303 def test_search_catalog_no_matches(
304 connection_with_typed_objects: HarlequinMySQLConnection,
305 ) -> None:
306 conn = connection_with_typed_objects
307
308 assert conn.search_catalog("no_such_object_anywhere") == []
309
310
311 def test_search_catalog_returns_its_connection_to_the_pool(
312 connection_with_typed_objects: HarlequinMySQLConnection,
313 ) -> None:
314 conn = connection_with_typed_objects
315
316 for _ in range(conn._pool.pool_size * 2):
317 assert conn.search_catalog("customers", "relations")