-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_postgres.py
418 lines (372 loc) · 13.2 KB
/
test_postgres.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import base64
import orjson
import pandas as pd
import pytest
import sqlalchemy
from fastapi.testclient import TestClient
from testcontainers.postgres import PostgresContainer
from app.main import app
from tests.confest import file_path
pytestmark = pytest.mark.beta
client = TestClient(app)
base_url = "/v3/connector/postgres"
manifest = {
"catalog": "wren",
"schema": "public",
"models": [
{
"name": "orders",
"tableReference": {
"schema": "public",
"table": "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": "double",
},
{"name": "orderdate", "expression": "o_orderdate", "type": "date"},
{
"name": "order_cust_key",
"expression": "concat(o_orderkey, '_', o_custkey)",
"type": "varchar",
},
{
"name": "timestamp",
"expression": "cast('2024-01-01T23:59:59' as timestamp)",
"type": "timestamp",
},
{
"name": "timestamptz",
"expression": "cast('2024-01-01T23:59:59' as timestamp with time zone)",
"type": "timestamp",
},
],
"primaryKey": "orderkey",
},
{
"name": "customer",
"tableReference": {
"schema": "public",
"table": "customer",
},
"columns": [
{"name": "custkey", "expression": "c_custkey", "type": "integer"},
{"name": "name", "expression": "c_name", "type": "varchar"},
],
"primaryKey": "custkey",
},
],
}
manifest_str = base64.b64encode(orjson.dumps(manifest)).decode("utf-8")
@pytest.fixture(scope="module")
def postgres(request) -> PostgresContainer:
pg = PostgresContainer("postgres:16-alpine").start()
engine = sqlalchemy.create_engine(pg.get_connection_url())
pd.read_parquet(file_path("resource/tpch/data/orders.parquet")).to_sql(
"orders", engine, index=False
)
pd.read_parquet(file_path("resource/tpch/data/customer.parquet")).to_sql(
"customer", engine, index=False
)
request.addfinalizer(pg.stop)
return pg
def test_query(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 200
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",
820540800000,
"1_370",
1704153599000,
1704153599000,
]
assert result["dtypes"] == {
"orderkey": "int32",
"custkey": "int32",
"orderstatus": "object",
"totalprice": "object",
"orderdate": "object",
"order_cust_key": "object",
"timestamp": "datetime64[ns]",
"timestamptz": "datetime64[ns, UTC]",
}
def test_query_with_connection_url(postgres: PostgresContainer):
connection_url = to_connection_url(postgres)
response = client.post(
url=f"{base_url}/query",
json={
"connectionInfo": {"connectionUrl": connection_url},
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == len(manifest["models"][0]["columns"])
assert len(result["data"]) == 1
assert result["data"][0][0] == 1
assert result["dtypes"] is not None
def test_query_with_column_dtypes(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
"columnDtypes": {
"totalprice": "float",
"orderdate": "datetime64",
"timestamp": "datetime64",
"timestamptz": "datetime64",
},
},
)
assert response.status_code == 200
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",
]
assert result["dtypes"] == {
"orderkey": "int32",
"custkey": "int32",
"orderstatus": "object",
"totalprice": "float64",
"orderdate": "object",
"order_cust_key": "object",
"timestamp": "object",
"timestamptz": "object",
}
def test_query_with_limit(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
response = client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 10",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
def test_query_without_manifest(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"sql": "SELECT * FROM wren.public.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"
def test_query_without_sql(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = 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"
def test_query_without_connection_info():
response = client.post(
url=f"{base_url}/query",
json={
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.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"
def test_query_with_dry_run(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = client.post(
url=f"{base_url}/query",
params={"dryRun": True},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 204
def test_query_with_dry_run_and_invalid_sql(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = 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
def test_validate_with_unknown_rule(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = 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
== "The rule `unknown_rule` is not in the rules, rules: ['column_is_valid']"
)
# TODO: enable this test after supporting the default catalog and default schema
# def test_validate_rule_column_is_valid(postgres: PostgresContainer):
# connection_info = to_connection_info(postgres)
# response = 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
def test_validate_rule_column_is_valid_with_invalid_parameters(
postgres: PostgresContainer,
):
connection_info = to_connection_info(postgres)
response = 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 = 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
def test_validate_rule_column_is_valid_without_parameters(postgres: PostgresContainer):
connection_info = to_connection_info(postgres)
response = 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"
def test_validate_rule_column_is_valid_without_one_parameter(
postgres: PostgresContainer,
):
connection_info = to_connection_info(postgres)
response = 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 = 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`"
def test_dry_plan():
response = client.post(
url=f"{base_url}/dry-plan",
json={
"manifestStr": manifest_str,
"sql": "SELECT orderkey, order_cust_key FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 200
assert (
response.text
== "\"SELECT orders.orderkey, orders.order_cust_key FROM (SELECT orders.order_cust_key, orders.orderkey FROM (SELECT CONCAT(orders.o_orderkey, '_', orders.o_custkey) AS order_cust_key, orders.o_orderkey AS orderkey FROM public.orders) AS orders) AS orders LIMIT 1\""
)
def to_connection_info(pg: PostgresContainer):
return {
"host": pg.get_container_host_ip(),
"port": pg.get_exposed_port(pg.port),
"user": pg.username,
"password": pg.password,
"database": pg.dbname,
}
def to_connection_url(pg: PostgresContainer):
info = to_connection_info(pg)
return f"postgres://{info['user']}:{info['password']}@{info['host']}:{info['port']}/{info['database']}"