-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtest_mysql.py
490 lines (434 loc) · 16.1 KB
/
test_mysql.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import base64
import orjson
import pandas as pd
import pymysql
import pytest
import sqlalchemy
from MySQLdb import OperationalError
from sqlalchemy import text
from testcontainers.mysql import MySqlContainer
from app.model import SSLMode
from app.model.validator import rules
from tests.conftest import file_path
pytestmark = pytest.mark.mysql
base_url = "/v2/connector/mysql"
manifest = {
"catalog": "my_catalog",
"schema": "my_schema",
"models": [
{
"name": "Orders",
"refSql": "select * from 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": "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",
},
{
"name": "test_null_time",
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": ["orderkey"],
},
{
"name": "Customer",
"refSql": "select * from customer",
"columns": [
{"name": "custkey", "expression": "c_custkey", "type": "integer"},
{"name": "name", "expression": "c_name", "type": "varchar"},
],
"primaryKey": ["custkey"],
},
],
}
@pytest.fixture(scope="module")
def manifest_str():
return base64.b64encode(orjson.dumps(manifest)).decode("utf-8")
@pytest.fixture(scope="module")
def mysql(request) -> MySqlContainer:
mysql = MySqlContainer(image="mysql:8.0.40", dialect="pymysql").start()
connection_url = mysql.get_connection_url()
engine = sqlalchemy.create_engine(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
)
with engine.begin() as conn:
conn.execute(
text("""
ALTER TABLE orders
ADD PRIMARY KEY (o_orderkey),
COMMENT = 'This is a table comment',
MODIFY COLUMN o_comment VARCHAR(255) COMMENT 'This is a comment';
""")
)
conn.execute(text("ALTER TABLE customer Add PRIMARY KEY(c_custkey);"))
conn.execute(
text(
"ALTER TABLE orders ADD FOREIGN KEY (o_custkey) REFERENCES customer(c_custkey);"
)
)
request.addfinalizer(mysql.stop)
return mysql
@pytest.fixture(scope="module")
def mysql_ssl_off(request) -> MySqlContainer:
mysql = MySqlContainer(image="mysql:8.0.40").with_command("--ssl=0").start()
# We disable SSL for this container to test SSLMode.ENABLED.
# However, Mysql use caching_sha2_password as default authentication plugin which requires the connection to use SSL.
# MysqlDB used by ibis supports caching_sha2_password only with SSL. So, we need to change the authentication plugin to mysql_native_password.
# Before changing the authentication plugin, we need to connect to the database using caching_sha2_password. That's why we use pymysql to connect to the database.
# pymsql supports caching_sha2_password without SSL.
conn = pymysql.connect(
host="127.0.0.1",
user="root",
passwd="test",
port=int(mysql.get_exposed_port(mysql.port)),
)
cur = conn.cursor()
cur.execute(
"ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'test';"
)
cur.execute("FLUSH PRIVILEGES;")
conn.commit()
conn.close()
request.addfinalizer(mysql.stop)
return mysql
async def test_query(client, manifest_str, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "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",
"1996-01-02 00:00:00.000000",
"1_370",
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int32",
"custkey": "int32",
"orderstatus": "object",
"totalprice": "object",
"orderdate": "object",
"order_cust_key": "object",
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}
async def test_query_with_connection_url(client, manifest_str, mysql: MySqlContainer):
connection_url = _to_connection_url(mysql)
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": {"connectionUrl": connection_url},
"manifestStr": manifest_str,
"sql": 'SELECT * FROM "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
async def test_query_without_manifest(client, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
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, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
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, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
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
async def test_query_with_dry_run_and_invalid_sql(
client, manifest_str, mysql: MySqlContainer
):
connection_info = _to_connection_info(mysql)
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, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
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 == 404
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, mysql: MySqlContainer
):
connection_info = _to_connection_info(mysql)
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
async def test_validate_rule_column_is_valid_with_invalid_parameters(
client, manifest_str, mysql: MySqlContainer
):
connection_info = _to_connection_info(mysql)
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, mysql: MySqlContainer
):
connection_info = _to_connection_info(mysql)
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, mysql: MySqlContainer
):
connection_info = _to_connection_info(mysql)
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_metadata_list_tables(client, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
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"] == "test.orders", response.json()))
assert result["name"] == "test.orders"
assert result["primaryKey"] is not None
assert result["description"] == "This is a table comment"
assert result["properties"] == {
"catalog": "",
"schema": "test",
"table": "orders",
"path": None,
}
assert len(result["columns"]) == 9
o_comment_column = next(
filter(lambda x: x["name"] == "o_comment", result["columns"])
)
assert o_comment_column == {
"name": "o_comment",
"nestedColumns": None,
"type": "VARCHAR",
"notNull": False,
"description": "This is a comment",
"properties": None,
}
async def test_metadata_list_constraints(client, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
response = await client.post(
url=f"{base_url}/metadata/constraints",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
result = response.json()[0]
assert result["constraintName"] is not None
assert result["constraintType"] is not None
assert result["constraintTable"] is not None
assert result["constraintColumn"] is not None
assert result["constraintedTable"] is not None
assert result["constraintedColumn"] is not None
async def test_metadata_db_version(client, mysql: MySqlContainer):
connection_info = _to_connection_info(mysql)
response = await client.post(
url=f"{base_url}/metadata/version",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
assert response.text == '"8.0.40"'
@pytest.mark.parametrize(
"ssl_mode, expected_exception, expected_error",
[
(
SSLMode.ENABLED,
OperationalError,
"SSL connection error: SSL is required but the server doesn't support it",
),
(
SSLMode.VERIFY_CA,
ValueError,
"SSL CA must be provided when SSL mode is VERIFY CA",
),
],
)
async def test_connection_invalid_ssl_mode(
client, mysql_ssl_off: MySqlContainer, ssl_mode, expected_exception, expected_error
):
connection_info = _to_connection_info(mysql_ssl_off)
connection_info["sslMode"] = ssl_mode
with pytest.raises(expected_exception) as excinfo:
await client.post(
url=f"{base_url}/metadata/version",
json={"connectionInfo": connection_info},
)
assert expected_error in str(excinfo.value)
async def test_connection_valid_ssl_mode(client, mysql_ssl_off: MySqlContainer):
connection_info = _to_connection_info(mysql_ssl_off)
connection_info["sslMode"] = SSLMode.DISABLED
response = await client.post(
url=f"{base_url}/metadata/version",
json={"connectionInfo": connection_info},
)
assert response.status_code == 200
assert response.text == '"8.0.40"'
def _to_connection_info(mysql: MySqlContainer):
return {
"host": mysql.get_container_host_ip(),
"port": mysql.get_exposed_port(mysql.port),
"user": mysql.username,
"password": mysql.password,
"database": mysql.dbname,
}
def _to_connection_url(mysql: MySqlContainer):
info = _to_connection_info(mysql)
return f"mysql://{info['user']}:{info['password']}@{info['host']}:{info['port']}/{info['database']}"