-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_dbapi2.py
More file actions
458 lines (333 loc) · 14.1 KB
/
test_dbapi2.py
File metadata and controls
458 lines (333 loc) · 14.1 KB
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
import os
import uuid
import pytest
import sqlitecloud
from sqlitecloud.datatypes import SQLiteCloudAccount
from sqlitecloud.exceptions import SQLiteCloudError, SQLiteCloudProgrammingError
class TestDBAPI2:
def test_connect_with_account(self):
account = SQLiteCloudAccount(
os.getenv("SQLITE_USER"),
os.getenv("SQLITE_PASSWORD"),
os.getenv("SQLITE_HOST"),
os.getenv("SQLITE_DB"),
int(os.getenv("SQLITE_PORT")),
)
connection = sqlitecloud.connect(account)
connection.close()
assert isinstance(connection, sqlitecloud.Connection)
def test_connect_with_connection_string(self):
connection_str = f"{os.getenv('SQLITE_CONNECTION_STRING')}/{os.getenv('SQLITE_DB')}?apikey={os.getenv('SQLITE_API_KEY')}"
connection = sqlitecloud.connect(connection_str)
connection.close()
assert isinstance(connection, sqlitecloud.Connection)
def test_disconnect(self):
account = SQLiteCloudAccount(
os.getenv("SQLITE_USER"),
os.getenv("SQLITE_PASSWORD"),
os.getenv("SQLITE_HOST"),
os.getenv("SQLITE_DB"),
int(os.getenv("SQLITE_PORT")),
)
connection = sqlitecloud.connect(account)
connection.close()
assert isinstance(connection, sqlitecloud.Connection)
with pytest.raises(SQLiteCloudProgrammingError) as e:
connection.execute("SELECT 1")
assert e.value.errcode == 1
assert e.value.errmsg == "The cursor is closed."
def test_select(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.cursor()
cursor.execute("SELECT 'Hello'")
result = cursor.fetchone()
assert result == ("Hello",)
def test_connection_execute(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT 'Hello'")
result = cursor.fetchone()
assert result == ("Hello",)
def test_column_not_found(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
with pytest.raises(SQLiteCloudError) as e:
connection.execute("SELECT not_a_column FROM albums")
assert e.value.errcode == 1
assert e.value.errmsg == "no such column: not_a_column"
def test_rowset_data(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT AlbumId FROM albums LIMIT 2")
assert cursor.rowcount == 2
assert len(cursor.description) == 1
def test_fetch_one_row(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT * FROM albums")
row = cursor.fetchone()
assert len(row) == 3
assert row == (1, "For Those About To Rock We Salute You", 1)
def test_select_utf8_value_and_column_name(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT 'Minha História'")
assert cursor.rowcount == 1
assert len(cursor.description) == 1
assert "Minha História" == cursor.fetchone()[0]
assert "'Minha História'" == cursor.description[0][0]
def test_column_name(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT * FROM albums")
assert "AlbumId" == cursor.description[0][0]
assert "Title" == cursor.description[1][0]
def test_integer(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("TEST INTEGER")
assert cursor.rowcount == -1
assert cursor.fetchone() is None
def test_error(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
with pytest.raises(SQLiteCloudError) as e:
connection.execute("TEST ERROR")
assert e.value.errcode == 66666
assert e.value.errmsg == "This is a test error message with a devil error code."
def test_execute_with_named_placeholder(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute(
"SELECT * FROM albums WHERE AlbumId = :id and Title like :title",
{"id": 1, "title": "For Those About%"},
)
assert cursor.rowcount == 1
assert cursor.fetchone() == (1, "For Those About To Rock We Salute You", 1)
def test_execute_with_named_placeholder_and_a_fake_one_which_is_not_given(
self, sqlitecloud_dbapi2_connection
):
""" "Expect the converter from name to qmark placeholder to not be fooled by the
fake name with the colon in it."""
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute(
"SELECT * FROM albums WHERE AlbumId = :id and Title != 'special:name'",
{"id": 1},
)
assert cursor.rowcount == 1
assert cursor.fetchone() == (1, "For Those About To Rock We Salute You", 1)
def test_execute_with_qmarks(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute(
"SELECT * FROM albums WHERE AlbumId = ? and Title like ?",
(
1,
"For Those About%",
),
)
assert cursor.rowcount == 1
assert cursor.fetchone() == (1, "For Those About To Rock We Salute You", 1)
def test_execute_two_updates_using_the_cursor(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.cursor()
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
genreId = 2
cursor.execute(
"UPDATE genres SET Name = ? WHERE GenreId = ?;",
(
new_name1,
genreId,
),
)
cursor.execute(
"UPDATE genres SET Name = ? WHERE GenreId = ?;",
(
new_name2,
genreId,
),
)
cursor.execute(
"SELECT Name, GenreID FROM genres WHERE GenreId = :id", {"id": genreId}
)
assert cursor.fetchone() == (
new_name2,
genreId,
)
def test_executemany_updates(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
genreId = 2
cursor = connection.executemany(
"UPDATE genres SET Name = ? WHERE GenreId = ?;",
[(new_name1, genreId), (new_name2, genreId)],
)
cursor.execute(
"SELECT Name, GenreID FROM genres WHERE GenreId = :id", {"id": genreId}
)
assert cursor.fetchone() == (
new_name2,
genreId,
)
def test_executemany_updates_using_the_cursor(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.cursor()
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
genreId = 2
cursor.executemany(
"UPDATE genres SET Name = ? WHERE GenreId = ?;",
[(new_name1, genreId), (new_name2, genreId)],
)
cursor.execute(
"SELECT Name, GenreID FROM genres WHERE GenreId = :id", {"id": genreId}
)
assert cursor.fetchone() == (
new_name2,
genreId,
)
def test_row_factory(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
connection.row_factory = lambda cursor, row: {
description[0]: row[i] for i, description in enumerate(cursor.description)
}
cursor = connection.execute("SELECT * FROM albums")
row = cursor.fetchone()
assert row["AlbumId"] == 1
assert row["Title"] == "For Those About To Rock We Salute You"
assert row["ArtistId"] == 1
def test_row_object_for_factory_string_representation(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
connection.row_factory = sqlitecloud.Row
cursor = connection.execute('SELECT "foo" as Bar, "john" Doe')
row = cursor.fetchone()
assert str(row) == "Bar: foo\nDoe: john"
def test_last_rowid_and_rowcount_with_select(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.execute("SELECT * FROM genres LIMIT 3")
assert cursor.fetchone() is not None
assert cursor.lastrowid is None
assert cursor.rowcount == 3
def test_last_rowid_and_rowcount_with_execute_update(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name = "Jazz" + str(uuid.uuid4())
genreId = 2
cursor = connection.execute(
"UPDATE genres SET Name = ? WHERE GenreId = ?",
(new_name, genreId),
)
assert cursor.fetchone() is None
assert cursor.lastrowid is None
assert cursor.rowcount == 1
def test_last_rowid_and_rowcount_with_execute_insert(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name = "Jazz" + str(uuid.uuid4())
cursor = connection.execute(
"INSERT INTO genres (Name) VALUES (?)",
(new_name,),
)
last_result = connection.execute(
"SELECT GenreId FROM genres WHERE Name = ?", (new_name,)
)
assert cursor.fetchone() is None
assert cursor.lastrowid == last_result.fetchone()[0]
assert cursor.rowcount == 1
def test_last_rowid_and_rowcount_with_execute_delete(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name = "Jazz" + str(uuid.uuid4())
cursor_select = connection.execute(
"INSERT INTO genres (Name) VALUES (?)",
(new_name,),
)
cursor = connection.execute("DELETE FROM genres WHERE Name = ?", (new_name,))
assert cursor.fetchone() is None
assert cursor.lastrowid == cursor_select.lastrowid
assert cursor.rowcount == 1
def test_last_rowid_and_rowcount_with_multiple_updates(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name = "Jazz" + str(uuid.uuid4())
cursor = connection.execute(
"UPDATE genres SET Name = ? WHERE GenreId = ? or GenreId = ?",
(new_name, 2, 3),
)
assert cursor.fetchone() is None
assert cursor.lastrowid is None
assert cursor.rowcount == 2
def test_last_rowid_and_rowcount_with_multiple_deletes(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
cursor = connection.executemany(
"INSERT INTO genres (Name) VALUES (?)",
[(new_name1,), (new_name2,)],
)
cursor = connection.execute(
"DELETE FROM genres WHERE Name = ? or Name = ?", (new_name1, new_name2)
)
assert cursor.fetchone() is None
assert cursor.lastrowid > 0
assert cursor.rowcount == 2
def test_last_rowid_and_rowcount_with_executemany_updates(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
genreId = 2
cursor = connection.executemany(
"UPDATE genres SET Name = ? WHERE GenreId = ?;",
[(new_name1, genreId), (new_name2, genreId)],
)
assert cursor.fetchone() is None
assert cursor.lastrowid is None
assert cursor.rowcount == 1
def test_last_rowid_and_rowcount_with_executemany_inserts(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
cursor = connection.executemany(
"INSERT INTO genres (Name) VALUES (?)",
[(new_name1,), (new_name2,)],
)
last_result = connection.execute(
"SELECT GenreId FROM genres WHERE Name = ?", (new_name2,)
)
assert cursor.fetchone() is None
assert cursor.lastrowid == last_result.fetchone()[0]
assert cursor.rowcount == 1
def test_last_rowid_and_rowcount_with_executemany_deletes(
self, sqlitecloud_dbapi2_connection
):
connection = sqlitecloud_dbapi2_connection
new_name1 = "Jazz" + str(uuid.uuid4())
new_name2 = "Jazz" + str(uuid.uuid4())
cursor_insert = connection.executemany(
"INSERT INTO genres (Name) VALUES (?)",
[(new_name1,), (new_name2,)],
)
cursor = connection.executemany(
"DELETE FROM genres WHERE Name = ?", [(new_name1,), (new_name2,)]
)
assert cursor.fetchone() is None
assert cursor.lastrowid == cursor_insert.lastrowid
assert cursor.rowcount == 1
def test_connection_is_connected(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
assert connection.is_connected()
connection.close()
assert not connection.is_connected()
def test_fetchall_returns_right_nrows_number(self, sqlitecloud_dbapi2_connection):
connection = sqlitecloud_dbapi2_connection
cursor = connection.cursor()
cursor.execute("SELECT * FROM Genres LIMIT 3")
assert len(cursor.fetchall()) == 3
assert cursor.rowcount == 3
cursor.execute("SELECT * FROM Albums LIMIT 4")
assert len(cursor.fetchall()) == 4
assert cursor.rowcount == 4