forked from catherinedevlin/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtest_generic_db_operations.py
574 lines (515 loc) Β· 16.6 KB
/
test_generic_db_operations.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import shutil
from matplotlib import pyplot as plt
import pytest
import warnings
from sql.telemetry import telemetry
from unittest.mock import ANY, Mock
import math
ALL_DATABASES = [
"ip_with_postgreSQL",
"ip_with_mySQL",
"ip_with_mariaDB",
"ip_with_SQLite",
"ip_with_duckDB",
"ip_with_MSSQL",
"ip_with_Snowflake",
]
@pytest.fixture(autouse=True)
def run_around_tests(tmpdir_factory):
# Create tmp folder
my_tmpdir = tmpdir_factory.mktemp("tmp")
yield my_tmpdir
# Destory tmp folder
shutil.rmtree(str(my_tmpdir))
@pytest.fixture
def mock_log_api(monkeypatch):
mock_log_api = Mock()
monkeypatch.setattr(telemetry, "log_api", mock_log_api)
yield mock_log_api
# Query
@pytest.mark.parametrize(
"ip_with_dynamic_db, expected",
[
("ip_with_postgreSQL", 3),
("ip_with_mySQL", 3),
("ip_with_mariaDB", 3),
("ip_with_SQLite", 3),
("ip_with_duckDB", 3),
("ip_with_Snowflake", 3),
],
)
def test_query_count(ip_with_dynamic_db, expected, request, test_table_name_dict):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
out = ip_with_dynamic_db.run_line_magic(
"sql", f"SELECT * FROM {test_table_name_dict['taxi']} LIMIT 3"
)
# Test query with --with & --save
ip_with_dynamic_db.run_cell(
f"%sql --save taxi_subset --no-execute SELECT * FROM\
{test_table_name_dict['taxi']} LIMIT 3"
)
out_query_with_save_arg = ip_with_dynamic_db.run_cell(
"%sql --with taxi_subset SELECT * FROM taxi_subset"
)
assert len(out) == expected
assert len(out_query_with_save_arg.result) == expected
# Create
@pytest.mark.parametrize(
"ip_with_dynamic_db, expected, limit",
[
("ip_with_postgreSQL", 15, 15),
("ip_with_mySQL", 15, 15),
("ip_with_mariaDB", 15, 15),
("ip_with_SQLite", 15, 15),
("ip_with_duckDB", 15, 15),
# Snowflake doesn't support index, skip that
],
)
def test_create_table_with_indexed_df(
ip_with_dynamic_db, expected, limit, request, test_table_name_dict
):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
# Clean up
ip_with_dynamic_db.run_cell(
f"%sql DROP TABLE {test_table_name_dict['new_table_from_df']}"
)
# Prepare DF
ip_with_dynamic_db.run_cell(
f"results = %sql SELECT * FROM {test_table_name_dict['taxi']}\
LIMIT {limit}"
)
# Prepare expected df
expected_df = ip_with_dynamic_db.run_cell(
f"%sql SELECT * FROM {test_table_name_dict['taxi']}\
LIMIT {limit}"
)
ip_with_dynamic_db.run_cell(
f"{test_table_name_dict['new_table_from_df']} = results.DataFrame()"
)
# Create table from DF
persist_out = ip_with_dynamic_db.run_cell(
f"%sql --persist {test_table_name_dict['new_table_from_df']}"
)
out_df = ip_with_dynamic_db.run_cell(
f"%sql SELECT * FROM {test_table_name_dict['new_table_from_df']}"
)
assert persist_out.error_in_exec is None and out_df.error_in_exec is None
assert len(out_df.result) == expected
assert expected_df.result.DataFrame().equals(
out_df.result.DataFrame().loc[:, out_df.result.DataFrame().columns != "level_0"]
)
# Connection
def get_connection_count(ip_with_dynamic_db):
out = ip_with_dynamic_db.run_line_magic("sql", "-l")
print("Current connections:", out)
connections_count = len(out)
return connections_count
# Test - Number of active connection
@pytest.mark.parametrize(
"ip_with_dynamic_db, expected",
[
("ip_with_postgreSQL", 1),
("ip_with_mySQL", 1),
("ip_with_mariaDB", 1),
("ip_with_SQLite", 1),
("ip_with_duckDB", 1),
("ip_with_MSSQL", 1),
("ip_with_Snowflake", 1),
],
)
def test_active_connection_number(ip_with_dynamic_db, expected, request):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
assert get_connection_count(ip_with_dynamic_db) == expected
@pytest.mark.parametrize(
"ip_with_dynamic_db, config_key",
[
("ip_with_postgreSQL", "postgreSQL"),
("ip_with_mySQL", "mySQL"),
("ip_with_mariaDB", "mariaDB"),
("ip_with_SQLite", "SQLite"),
("ip_with_duckDB", "duckDB"),
("ip_with_MSSQL", "MSSQL"),
("ip_with_Snowflake", "Snowflake"),
],
)
def test_close_and_connect(
ip_with_dynamic_db, config_key, request, get_database_config_helper
):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
conn_alias = get_database_config_helper.get_database_config(config_key)["alias"]
database_url = get_database_config_helper.get_database_url(config_key)
# Disconnect
ip_with_dynamic_db.run_cell("%sql -x " + conn_alias)
assert get_connection_count(ip_with_dynamic_db) == 0
# Connect, also check there is no error on re-connecting
with warnings.catch_warnings():
warnings.simplefilter("error")
ip_with_dynamic_db.run_cell("%sql " + database_url + " --alias " + conn_alias)
assert get_connection_count(ip_with_dynamic_db) == 1
# Telemetry
# Test - Number of active connection
@pytest.mark.parametrize(
"ip_with_dynamic_db, expected_dialect, expected_driver",
[
("ip_with_postgreSQL", "postgresql", "psycopg2"),
("ip_with_mySQL", "mysql", "pymysql"),
("ip_with_mariaDB", "mysql", "pymysql"),
("ip_with_SQLite", "sqlite", "pysqlite"),
("ip_with_duckDB", "duckdb", "duckdb_engine"),
("ip_with_MSSQL", "mssql", "pyodbc"),
("ip_with_Snowflake", "snowflake", "snowflake"),
],
)
def test_telemetry_execute_command_has_connection_info(
ip_with_dynamic_db, expected_dialect, expected_driver, mock_log_api, request
):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
mock_log_api.assert_called_with(
action="jupysql-execute-success",
total_runtime=ANY,
metadata={
"argv": ANY,
"connection_info": {
"dialect": expected_dialect,
"driver": expected_driver,
"server_version_info": ANY,
},
},
)
@pytest.mark.parametrize(
"cell",
[
(
"%sqlplot histogram --with plot_something_subset --table\
plot_something_subset --column x"
),
(
"%sqlplot hist --with plot_something_subset --table\
plot_something_subset --column x"
),
(
"%sqlplot histogram --with plot_something_subset --table\
plot_something_subset --column x --bins 10"
),
],
ids=[
"histogram",
"hist",
"histogram-bins",
],
)
@pytest.mark.parametrize(
"ip_with_dynamic_db",
[
("ip_with_postgreSQL"),
("ip_with_mySQL"),
("ip_with_mariaDB"),
("ip_with_SQLite"),
("ip_with_duckDB"),
pytest.param(
"ip_with_MSSQL",
marks=pytest.mark.xfail(reason="sqlglot does not support SQL server"),
),
pytest.param(
"ip_with_Snowflake",
marks=pytest.mark.xfail(
reason="Something wrong with sqlplot histogram in snowflake"
),
),
],
)
def test_sqlplot_histogram(ip_with_dynamic_db, cell, request, test_table_name_dict):
# clean current Axes
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
plt.cla()
ip_with_dynamic_db.run_cell(
f"%sql --save plot_something_subset\
--no-execute SELECT * from {test_table_name_dict['plot_something']} LIMIT 3"
)
out = ip_with_dynamic_db.run_cell(cell)
assert type(out.result).__name__ in {"Axes", "AxesSubplot"}
BOX_PLOT_FAIL_REASON = (
"Known issue, the SQL engine must support percentile_disc() SQL clause"
)
@pytest.mark.parametrize(
"cell",
[
"%sqlplot boxplot --with plot_something_subset \
--table plot_something_subset --column x",
"%sqlplot box --with plot_something_subset \
--table plot_something_subset --column x",
"%sqlplot boxplot --with plot_something_subset \
--table plot_something_subset --column x --orient h",
"%sqlplot boxplot --with plot_something_subset \
--table plot_something_subset --column x",
],
ids=[
"boxplot",
"box",
"boxplot-with-horizontal",
"boxplot-with",
],
)
@pytest.mark.parametrize(
"ip_with_dynamic_db",
[
pytest.param("ip_with_postgreSQL"),
pytest.param("ip_with_duckDB"),
pytest.param(
"ip_with_mySQL", marks=pytest.mark.xfail(reason=BOX_PLOT_FAIL_REASON)
),
pytest.param(
"ip_with_mariaDB", marks=pytest.mark.xfail(reason=BOX_PLOT_FAIL_REASON)
),
pytest.param(
"ip_with_SQLite", marks=pytest.mark.xfail(reason=BOX_PLOT_FAIL_REASON)
),
pytest.param(
"ip_with_Snowflake",
marks=pytest.mark.xfail(
reason="Something wrong with sqlplot boxplot in snowflake"
),
),
],
)
def test_sqlplot_boxplot(ip_with_dynamic_db, cell, request, test_table_name_dict):
# clean current Axes
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
plt.cla()
ip_with_dynamic_db.run_cell(
f"%sql --save plot_something_subset --no-execute\
SELECT * from {test_table_name_dict['plot_something']} LIMIT 3"
)
out = ip_with_dynamic_db.run_cell(cell)
assert type(out.result).__name__ in {"Axes", "AxesSubplot"}
@pytest.mark.parametrize(
"ip_with_dynamic_db",
[
("ip_with_postgreSQL"),
("ip_with_mySQL"),
("ip_with_mariaDB"),
("ip_with_SQLite"),
("ip_with_duckDB"),
("ip_with_MSSQL"),
("ip_with_Snowflake"),
],
)
def test_sql_cmd_magic_uno(ip_with_dynamic_db, request, capsys):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
ip_with_dynamic_db.run_cell(
"""
%%sql sqlite://
CREATE TABLE test_numbers (value);
INSERT INTO test_numbers VALUES (0);
INSERT INTO test_numbers VALUES (4);
INSERT INTO test_numbers VALUES (5);
INSERT INTO test_numbers VALUES (6);
"""
)
ip_with_dynamic_db.run_cell(
"%sqlcmd test --table test_numbers --column value" " --less-than 5 --greater 1"
)
_out = capsys.readouterr()
assert "less_than" in _out.out
assert "greater" in _out.out
@pytest.mark.parametrize(
"ip_with_dynamic_db",
[
("ip_with_postgreSQL"),
("ip_with_mySQL"),
("ip_with_mariaDB"),
("ip_with_SQLite"),
("ip_with_duckDB"),
("ip_with_MSSQL"),
pytest.param(
"ip_with_Snowflake",
marks=pytest.mark.xfail(
reason="Something wrong with test_sql_cmd_magic_dos in snowflake"
),
),
],
)
def test_sql_cmd_magic_dos(ip_with_dynamic_db, request, capsys):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
ip_with_dynamic_db.run_cell(
"""
%%sql sqlite://
CREATE TABLE test_numbers (value);
INSERT INTO test_numbers VALUES (0);
INSERT INTO test_numbers VALUES (4);
INSERT INTO test_numbers VALUES (5);
INSERT INTO test_numbers VALUES (6);
"""
)
ip_with_dynamic_db.run_cell(
"%sqlcmd test --table test_numbers --column value --greater-or-equal 3"
)
_out = capsys.readouterr()
assert "greater_or_equal" in _out.out
assert "0" in _out.out
@pytest.mark.parametrize(
"ip_with_dynamic_db, table, table_columns, expected",
[
(
"ip_with_postgreSQL",
"taxi",
["index", "taxi_driver_name"],
{
"count": [45, 45],
"mean": [22.0, math.nan],
"min": [0, "Eric Ken"],
"max": [44, "Kevin Kelly"],
"unique": [45, 3],
"freq": [1, 15],
"top": [0, "Eric Ken"],
"std": ["1.299e+01", math.nan],
"25%": [11.0, math.nan],
"50%": [22.0, math.nan],
"75%": [33.0, math.nan],
},
),
pytest.param(
"ip_with_mySQL",
"taxi",
["taxi_driver_name"],
{
"count": [45],
"mean": [0.0],
"min": ["Eric Ken"],
"max": ["Kevin Kelly"],
"unique": [3],
"freq": [15],
"top": ["Kevin Kelly"],
},
marks=pytest.mark.xfail(
reason="Need to get column names from table with a different query"
),
),
pytest.param(
"ip_with_mariaDB",
"taxi",
["taxi_driver_name"],
{
"count": [45],
"mean": [0.0],
"min": ["Eric Ken"],
"max": ["Kevin Kelly"],
"unique": [3],
"freq": [15],
"top": ["Kevin Kelly"],
},
marks=pytest.mark.xfail(
reason="Need to get column names from table with a different query"
),
),
(
"ip_with_SQLite",
"taxi",
["taxi_driver_name"],
{
"count": [45],
"mean": [0.0],
"min": ["Eric Ken"],
"max": ["Kevin Kelly"],
"unique": [3],
"freq": [15],
"top": ["Kevin Kelly"],
},
),
(
"ip_with_duckDB",
"taxi",
["index", "taxi_driver_name"],
{
"count": [45, 45],
"mean": [22.0, math.nan],
"min": [0, "Eric Ken"],
"max": [44, "Kevin Kelly"],
"unique": [45, 3],
"freq": [1, 15],
"top": [0, "Eric Ken"],
"std": ["1.299e+01", math.nan],
"25%": [11.0, math.nan],
"50%": [22.0, math.nan],
"75%": [33.0, math.nan],
},
),
(
"ip_with_MSSQL",
"taxi",
["taxi_driver_name"],
{"unique": [3], "min": ["Eric Ken"], "max": ["Kevin Kelly"], "count": [45]},
),
pytest.param(
"ip_with_Snowflake",
"taxi",
["taxi_driver_name"],
{},
marks=pytest.mark.xfail(
reason="Something wrong with test_profile_query in snowflake"
),
),
],
)
def test_profile_query(
request, ip_with_dynamic_db, table, table_columns, expected, test_table_name_dict
):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
out = ip_with_dynamic_db.run_cell(
f"""
%sqlcmd profile --table "{test_table_name_dict[table]}"
"""
).result
stats_table = out._table
assert len(stats_table.rows) == len(expected)
for row in stats_table:
criteria = row.get_string(fields=[" "], border=False).strip()
for i, column in enumerate(table_columns):
cell_value = row.get_string(
fields=[column], border=False, header=False
).strip()
assert criteria in expected
assert cell_value == str(expected[criteria][i])
@pytest.mark.parametrize(
"table",
[
"numbers",
],
)
@pytest.mark.parametrize("ip_with_dynamic_db", ALL_DATABASES)
def test_sqlcmd_tables_columns(
ip_with_dynamic_db, table, request, test_table_name_dict
):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
out = ip_with_dynamic_db.run_cell(
f"%sqlcmd columns --table {test_table_name_dict[table]}"
)
assert out.result
@pytest.mark.parametrize("ip_with_dynamic_db", ALL_DATABASES)
def test_sqlcmd_tables(ip_with_dynamic_db, request):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
out = ip_with_dynamic_db.run_cell("%sqlcmd tables")
assert out.result
@pytest.mark.parametrize(
"cell",
[
"%%sql\nSELECT * FROM numbers WHERE 0=1",
"%%sql --with subset\nSELECT * FROM subset WHERE 0=1",
"%%sql\nSELECT *\n-- %one $another\nFROM numbers WHERE 0=1",
],
ids=[
"simple-query",
"cte",
"interpolation-like-comment",
],
)
@pytest.mark.parametrize("ip_with_dynamic_db", ALL_DATABASES)
def test_sql_query(ip_with_dynamic_db, cell, request):
ip_with_dynamic_db = request.getfixturevalue(ip_with_dynamic_db)
ip_with_dynamic_db.run_cell(
"""%%sql --save subset --no-execute
SELECT * FROM numbers WHERE 1=0
"""
)
out = ip_with_dynamic_db.run_cell(cell)
assert out.error_in_exec is None