-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_canner.py
373 lines (325 loc) · 11.9 KB
/
test_canner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import base64
import os
import pytest
from orjson import orjson
from app.model.validator import rules
"""
The Canner Enterprise must setup below:
- A user with PAT
- A data source with TPCH Tiny
- A workspace
- A TPCH table `orders` in the workspace
- The table `orders` must be with a description `This is a table comment`
- The table `orders` must have a column `o_comment` with a description `This is a comment`
"""
pytestmark = pytest.mark.canner
base_url = "/v2/connector/canner"
connection_info = {
"host": os.getenv("CANNER_HOST", default="localhost"),
"port": os.getenv("CANNER_PORT", default="7432"),
"user": os.getenv("CANNER_USER", default="canner"),
"pat": os.getenv("CANNER_PAT", default="PAT"),
"workspace": os.getenv("CANNER_WORKSPACE", default="ws"),
}
manifest = {
"catalog": "my_catalog",
"schema": "my_schema",
"models": [
{
"name": "Orders",
"refSql": f"select * from canner.{connection_info['workspace']}.orders",
"columns": [
{"name": "orderkey", "expression": "o_orderkey", "type": "integer"},
{"name": "custkey", "expression": "o_custkey", "type": "integer"},
{
"name": "orderstatus",
"expression": "o_orderstatus",
"type": "varchar",
},
{"name": "totalprice", "expression": "o_totalprice", "type": "float"},
{"name": "orderdate", "expression": "o_orderdate", "type": "date"},
{
"name": "order_cust_key",
"expression": "o_orderkey || '_' || o_custkey",
"type": "varchar",
},
{
"name": "timestamp",
"expression": "cast('2024-01-01 23:59:59' as timestamp)",
"type": "timestamp",
},
{
"name": "timestamptz",
"expression": "cast('2024-01-01 23:59:59 UTC' as timestamp with time zone)",
"type": "timestamp",
},
{
"name": "test_null_time",
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
],
"primaryKey": ["orderkey"],
},
],
}
@pytest.fixture(scope="module")
def manifest_str():
return base64.b64encode(orjson.dumps(manifest)).decode("utf-8")
async def test_query(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders" ORDER BY orderkey LIMIT 1',
},
)
assert response.status_code == 200, response.text
result = response.json()
assert len(result["columns"]) == len(manifest["models"][0]["columns"])
assert len(result["data"]) == 1
assert result["data"][0] == [
1,
370,
"O",
"172799.49",
"1996-01-02 00:00:00.000000",
"1_370",
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
]
assert result["dtypes"] == {
"orderkey": "int64",
"custkey": "int64",
"orderstatus": "object",
"totalprice": "float64",
"orderdate": "object",
"order_cust_key": "object",
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
}
async def test_query_with_connection_url(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": {"connectionUrl": _to_connection_url()},
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders" LIMIT 1',
},
)
assert response.status_code == 200, response.text
result = response.json()
assert len(result["columns"]) == len(manifest["models"][0]["columns"])
assert len(result["data"]) == 1
assert result["dtypes"] is not None
async def test_query_with_limit(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders"',
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
response = await client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders" LIMIT 10',
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
async def test_query_without_manifest(client):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"sql": 'SELECT * FROM "Orders" LIMIT 1',
},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "manifestStr"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_without_sql(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
json={"connectionInfo": connection_info, "manifestStr": manifest_str},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "sql"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_without_connection_info(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
json={
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders" LIMIT 1',
},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "connectionInfo"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_with_dry_run(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
params={"dryRun": True},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "Orders" LIMIT 1',
},
)
assert response.status_code == 204, response.text
async def test_query_with_dry_run_and_invalid_sql(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
params={"dryRun": True},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM X",
},
)
assert response.status_code == 422
assert response.text is not None
async def test_validate_with_unknown_rule(client, manifest_str):
response = await client.post(
url=f"{base_url}/validate/unknown_rule",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"modelName": "Orders", "columnName": "orderkey"},
},
)
assert response.status_code == 422
assert (
response.text == f"The rule `unknown_rule` is not in the rules, rules: {rules}"
)
async def test_validate_rule_column_is_valid(client, manifest_str):
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"modelName": "Orders", "columnName": "orderkey"},
},
)
assert response.status_code == 204, response.text
async def test_validate_rule_column_is_valid_with_invalid_parameters(
client, manifest_str
):
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"modelName": "X", "columnName": "orderkey"},
},
)
assert response.status_code == 422
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"modelName": "Orders", "columnName": "X"},
},
)
assert response.status_code == 422
async def test_validate_rule_column_is_valid_without_parameters(client, manifest_str):
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={"connectionInfo": connection_info, "manifestStr": manifest_str},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "parameters"]
assert result["detail"][0]["msg"] == "Field required"
async def test_validate_rule_column_is_valid_without_one_parameter(
client, manifest_str
):
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"modelName": "Orders"},
},
)
assert response.status_code == 422
assert response.text == "Missing required parameter: `columnName`"
response = await client.post(
url=f"{base_url}/validate/column_is_valid",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"parameters": {"columnName": "orderkey"},
},
)
assert response.status_code == 422
assert response.text == "Missing required parameter: `modelName`"
async def test_dry_plan(client, manifest_str):
response = await client.post(
url=f"{base_url}/dry-plan",
json={
"manifestStr": manifest_str,
"sql": 'SELECT orderkey, order_cust_key FROM "Orders" LIMIT 1',
},
)
assert response.status_code == 200
assert response.text is not None
async def test_metadata_list_tables(client):
response = await client.post(
url=f"{base_url}/metadata/tables",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
result = next(filter(lambda x: x["name"] == "orders", response.json()))
assert result["name"] == "orders"
assert result["primaryKey"] is not None
assert result["description"] == "This is a table comment"
assert result["properties"]["catalog"] == "canner"
assert result["properties"]["schema"] == "tpch_tiny"
assert result["properties"]["table"] == "orders"
assert len(result["columns"]) == 9
comment_column = next(filter(lambda x: x["name"] == "o_comment", result["columns"]))
assert comment_column["description"] == "This is a comment"
async def test_metadata_list_constraints(client):
response = await client.post(
url=f"{base_url}/metadata/constraints",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
assert response.json() == []
async def test_metadata_db_version(client):
response = await client.post(
url=f"{base_url}/metadata/version",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
assert response.text is not None
def _to_connection_url():
info = connection_info
return f"postgres://{info['user']}:{info['pat']}@{info['host']}:{info['port']}/{info['workspace']}"