|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | import sqlitecloud |
12 | | -from sqlitecloud.exceptions import SQLiteCloudException, SQLiteCloudProgrammingError |
| 12 | +from sqlitecloud.exceptions import SQLiteCloudProgrammingError |
13 | 13 | from tests.conftest import ( |
14 | 14 | close_generator, |
15 | 15 | get_sqlite3_connection, |
|
18 | 18 |
|
19 | 19 |
|
20 | 20 | class TestSQLiteFeatureParity: |
21 | | - def test_connection_close(self, sqlitecloud_dbapi2_connection, sqlite3_connection): |
22 | | - sqlitecloud_connection = sqlitecloud_dbapi2_connection |
| 21 | + @pytest.mark.parametrize( |
| 22 | + "connection, expected", |
| 23 | + [ |
| 24 | + ("sqlitecloud_dbapi2_connection", SQLiteCloudProgrammingError), |
| 25 | + ("sqlite3_connection", sqlite3.ProgrammingError), |
| 26 | + ], |
| 27 | + ) |
| 28 | + def test_connection_close(self, connection, expected, request): |
| 29 | + connection = request.getfixturevalue(connection) |
23 | 30 |
|
24 | | - sqlitecloud_connection.close() |
25 | | - sqlite3_connection.close() |
| 31 | + connection.close() |
26 | 32 |
|
27 | | - with pytest.raises(SQLiteCloudException) as e: |
28 | | - sqlitecloud_connection.execute("SELECT 1") |
| 33 | + with pytest.raises(expected) as e: |
| 34 | + connection.execute("SELECT 1") |
29 | 35 |
|
30 | | - assert isinstance(e.value, SQLiteCloudException) |
| 36 | + assert isinstance(e.value, expected) |
31 | 37 |
|
32 | | - with pytest.raises(sqlite3.ProgrammingError) as e: |
33 | | - sqlite3_connection.execute("SELECT 1") |
| 38 | + @pytest.mark.parametrize( |
| 39 | + "connection, expected", |
| 40 | + [ |
| 41 | + ("sqlitecloud_dbapi2_connection", SQLiteCloudProgrammingError), |
| 42 | + ("sqlite3_connection", sqlite3.ProgrammingError), |
| 43 | + ], |
| 44 | + ) |
| 45 | + def test_cursor_close(self, connection, expected, request): |
| 46 | + connection = request.getfixturevalue(connection) |
34 | 47 |
|
35 | | - assert isinstance(e.value, sqlite3.ProgrammingError) |
| 48 | + cursor = connection.cursor() |
36 | 49 |
|
37 | | - def test_ping_select(self, sqlitecloud_dbapi2_connection, sqlite3_connection): |
38 | | - sqlitecloud_connection = sqlitecloud_dbapi2_connection |
| 50 | + cursor.close() |
39 | 51 |
|
40 | | - sqlitecloud_cursor = sqlitecloud_connection.execute("SELECT 1") |
41 | | - sqlite3_cursor = sqlite3_connection.execute("SELECT 1") |
| 52 | + with pytest.raises(expected) as e: |
| 53 | + cursor.execute("SELECT 1") |
42 | 54 |
|
43 | | - sqlitecloud_cursor = sqlitecloud_cursor.fetchall() |
44 | | - sqlite3_cursor = sqlite3_cursor.fetchall() |
| 55 | + assert isinstance(e.value, expected) |
45 | 56 |
|
46 | | - assert sqlitecloud_cursor == sqlite3_cursor |
| 57 | + @pytest.mark.parametrize( |
| 58 | + "connection", ["sqlitecloud_dbapi2_connection", "sqlite3_connection"] |
| 59 | + ) |
| 60 | + def test_ping_select(self, connection, request): |
| 61 | + connection = request.getfixturevalue(connection) |
| 62 | + |
| 63 | + cursor = connection.execute("SELECT 1") |
| 64 | + |
| 65 | + cursor = cursor.fetchall() |
| 66 | + |
| 67 | + assert cursor == [(1,)] |
47 | 68 |
|
48 | 69 | @pytest.mark.parametrize( |
49 | 70 | "connection", ["sqlitecloud_dbapi2_connection", "sqlite3_connection"] |
50 | 71 | ) |
51 | 72 | def test_create_table_and_insert_many(self, connection, request): |
52 | 73 | connection = request.getfixturevalue(connection) |
53 | 74 |
|
54 | | - create_table_query = "CREATE TABLE IF NOT EXISTS sqlitetest (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)" |
55 | | - connection.execute(create_table_query) |
56 | | - |
57 | | - truncate_table_query = "DELETE FROM sqlitetest" |
58 | | - connection.execute(truncate_table_query) |
| 75 | + table = "sqlitetest" + str(random.randint(0, 99999)) |
| 76 | + try: |
| 77 | + create_table_query = f"CREATE TABLE IF NOT EXISTS {table} (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)" |
| 78 | + connection.execute(create_table_query) |
59 | 79 |
|
60 | | - insert_query = "INSERT INTO sqlitetest (name, age) VALUES (?, ?)" |
61 | | - params = [("Alice", 25), ("Bob", 30)] |
62 | | - connection.executemany(insert_query, params) |
| 80 | + insert_query = f"INSERT INTO {table} (name, age) VALUES (?, ?)" |
| 81 | + params = [("Alice", 25), ("Bob", 30)] |
| 82 | + connection.executemany(insert_query, params) |
63 | 83 |
|
64 | | - select_query = "SELECT * FROM sqlitetest" |
65 | | - cursor = connection.execute(select_query) |
| 84 | + select_query = f"SELECT * FROM {table}" |
| 85 | + cursor = connection.execute(select_query) |
66 | 86 |
|
67 | | - results = cursor.fetchall() |
| 87 | + results = cursor.fetchall() |
68 | 88 |
|
69 | | - assert len(results) == 2 |
70 | | - assert results[0][1] == "Alice" |
71 | | - assert results[0][2] == 25 |
72 | | - assert results[1][1] == "Bob" |
73 | | - assert results[1][2] == 30 |
| 89 | + assert len(results) == 2 |
| 90 | + assert results[0][1] == "Alice" |
| 91 | + assert results[0][2] == 25 |
| 92 | + assert results[1][1] == "Bob" |
| 93 | + assert results[1][2] == 30 |
| 94 | + finally: |
| 95 | + connection.execute(f"DROP TABLE IF EXISTS {table}") |
74 | 96 |
|
75 | 97 | @pytest.mark.parametrize( |
76 | 98 | "connection", ["sqlitecloud_dbapi2_connection", "sqlite3_connection"] |
@@ -175,9 +197,6 @@ def test_executemany_with_named_param_style(self, connection, request): |
175 | 197 |
|
176 | 198 | assert connection.total_changes == 2 |
177 | 199 |
|
178 | | - @pytest.mark.skip( |
179 | | - reason="Rowcount does not contain the number of inserted rows yet" |
180 | | - ) |
181 | 200 | def test_insert_result(self, sqlitecloud_dbapi2_connection, sqlite3_connection): |
182 | 201 | sqlitecloud_connection = sqlitecloud_dbapi2_connection |
183 | 202 |
|
@@ -375,13 +394,18 @@ def test_cursor_description_with_explicit_decltype( |
375 | 394 | """Since py3.7 the parsed of `[decltype]` disabled when PARSE_COLNAMES. |
376 | 395 | See bpo-39652 https://github.com/python/cpython/issues/83833""" |
377 | 396 | if parse_colnames: |
378 | | - connection = next(connection(module.PARSE_COLNAMES)) |
| 397 | + connection_gen = connection(module.PARSE_COLNAMES) |
379 | 398 | else: |
380 | | - connection = next(connection()) |
| 399 | + connection_gen = connection() |
381 | 400 |
|
382 | | - cursor = connection.execute(f"SELECT {value}") |
| 401 | + connection = next(connection_gen) |
| 402 | + |
| 403 | + try: |
| 404 | + cursor = connection.execute(f"SELECT {value}") |
383 | 405 |
|
384 | | - assert cursor.description[0][0] == expected |
| 406 | + assert cursor.description[0][0] == expected |
| 407 | + finally: |
| 408 | + close_generator(connection_gen) |
385 | 409 |
|
386 | 410 | def test_fetch_one(self, sqlitecloud_dbapi2_connection, sqlite3_connection): |
387 | 411 | sqlitecloud_connection = sqlitecloud_dbapi2_connection |
@@ -508,7 +532,7 @@ def test_explicit_transaction_to_commit( |
508 | 532 | sqlitecloud_dbapi2_connection: sqlitecloud.Connection, |
509 | 533 | sqlite3_connection: sqlite3.Connection, |
510 | 534 | ): |
511 | | - seed = str(int(time.time())) |
| 535 | + seed = str(uuid.uuid4()) |
512 | 536 |
|
513 | 537 | sqlitecloud_conn_gen = get_sqlitecloud_dbapi2_connection() |
514 | 538 | sqlite_conn_gen = get_sqlite3_connection() |
@@ -878,7 +902,9 @@ def adapt_point(point): |
878 | 902 | ], |
879 | 903 | ) |
880 | 904 | def test_parse_decltypes(self, connection, module): |
881 | | - connection = next(connection(module.PARSE_DECLTYPES)) |
| 905 | + connection_gen = connection(module.PARSE_DECLTYPES) |
| 906 | + |
| 907 | + connection = next(connection_gen) |
882 | 908 |
|
883 | 909 | class Point: |
884 | 910 | def __init__(self, x, y): |
@@ -909,6 +935,7 @@ def convert_point(s): |
909 | 935 | assert result[0].y == p.y |
910 | 936 | finally: |
911 | 937 | connection.execute(f"DROP TABLE IF EXISTS {tableName}") |
| 938 | + close_generator(connection_gen) |
912 | 939 |
|
913 | 940 | @pytest.mark.parametrize( |
914 | 941 | "connection, module", |
|
0 commit comments