-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3_test.py
More file actions
242 lines (187 loc) · 7.11 KB
/
sqlite3_test.py
File metadata and controls
242 lines (187 loc) · 7.11 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
"""Using the `sqlite3` module to access SQLite databases."""
import sqlite3
from datetime import date, datetime
from pathlib import Path
from typing import Generator, List, NamedTuple, Tuple
import pytest
@pytest.fixture(name="connect_memory_db")
def fixture_connect_memory_db() -> Generator[sqlite3.Connection, None, None]:
"""Connect to an in-memory database, pre-populated with data."""
conn = sqlite3.connect(":memory:")
create_schema(conn)
insert_data(conn)
yield conn
conn.close()
@pytest.fixture(name="connect_file_db")
def fixture_connect_file_db(
tmp_path: Path,
) -> Generator[sqlite3.Connection, None, None]:
"""Connect to a file-based database, pre-populated with data."""
db_path = tmp_path.joinpath("sqlite3.db")
db_exists = db_path.exists()
conn = sqlite3.connect(db_path)
if not db_exists:
create_schema(conn)
insert_data(conn)
yield conn
conn.close()
def create_schema(conn: sqlite3.Connection) -> None:
"""Create the database schema using `executescript()`."""
with conn:
cursor: sqlite3.Cursor = conn.cursor()
cursor.executescript(
"""\
create table tasks(
id integer primary key autoincrement not null,
priority integer default 1,
details text,
deadline date
);"""
)
def insert_data(conn: sqlite3.Connection) -> None:
"""Insert data into a database using `executemany()`."""
data = [
(2, "Task 1", "2020-07-08"),
(1, "Task 2", "2020-07-11"),
]
with conn:
cursor: sqlite3.Cursor = conn.cursor()
cursor.executemany(
"insert into tasks (priority, details, deadline) values (?, ?, ?);", data
)
def test_context_manager_commit() -> None:
"""Use connection as a context manager to automatically commit changes.
Uses qmark style placeholders for `execute()`.
"""
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
try:
create_schema(conn)
with conn:
cursor.execute(
"insert into tasks (details, deadline) values (?, ?);",
("Task 1", "2020-07-11"),
)
# Still in transaction, not committed yet.
assert conn.in_transaction
# Change automatically committed - no uncommitted changes.
assert not conn.in_transaction
with conn:
cursor.execute("select * from tasks;")
assert cursor.fetchone() == (1, 1, "Task 1", "2020-07-11")
finally:
conn.close()
def test_context_manager_rollback() -> None:
"""Use connection as a context manager to automatically rollback changes.
Uses named style placeholders for `execute()`.
"""
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
try:
create_schema(conn)
with conn:
cursor.execute(
"insert into tasks (details, deadline) values (:det, :dline);",
{"det": "Task 1", "dline": "2020-07-11"},
)
assert conn.in_transaction
raise RuntimeError
except RuntimeError:
assert not conn.in_transaction
# Change automatically rolled back.
with conn:
cursor.execute("select * from tasks;")
assert cursor.fetchone() is None
finally:
conn.close()
def test_retrieve_iterator(connect_file_db: sqlite3.Connection) -> None:
"""Retrieve data using the cursor as an iterator."""
with connect_file_db as conn:
cursor = conn.cursor()
rows = list(cursor.execute("select * from tasks order by priority"))
assert rows == [(2, 1, "Task 2", "2020-07-11"), (1, 2, "Task 1", "2020-07-08")]
def test_retrieve_fetch_one(connect_memory_db: sqlite3.Connection) -> None:
"""Retrieve data one row at a time."""
with connect_memory_db as conn:
cursor = conn.cursor()
cursor.execute("select * from tasks")
assert cursor.fetchone() == (1, 2, "Task 1", "2020-07-08")
assert cursor.fetchone() == (2, 1, "Task 2", "2020-07-11")
assert cursor.fetchone() is None
def test_retrieve_fetch_all(connect_memory_db: sqlite3.Connection) -> None:
"""Retrieve all rows in one go."""
with connect_memory_db as conn:
cursor = conn.cursor()
cursor.execute("select details from tasks")
assert cursor.fetchall() == [("Task 1",), ("Task 2",)]
assert cursor.fetchall() == []
def test_retrieve_row(connect_memory_db: sqlite3.Connection) -> None:
"""Retrieve rows as `Row` objects.
Uses non-standard `execute()` shortcut.
"""
with connect_memory_db as conn:
conn.row_factory = sqlite3.Row
row: sqlite3.Row = conn.execute("select * from tasks;").fetchone()
assert len(row) == 4
assert row.keys() == ["id", "priority", "details", "deadline"]
assert row[1] == row["priority"] == 2
assert tuple(row) == (1, 2, "Task 1", "2020-07-08")
assert list(row) == [1, 2, "Task 1", "2020-07-08"]
def test_retrieve_object(connect_memory_db: sqlite3.Connection) -> None:
"""Retrieve rows as custom objects."""
class Task(NamedTuple):
"""A task stored as a row in the database."""
id: int
priority: int
details: str
deadline: date
def task_factory(cursor: sqlite3.Cursor, row: Tuple[int, int, str, str]) -> Task:
"""Create `Task` objects out of rows."""
return Task(
row[0], row[1], row[2], datetime.strptime(row[3], "%Y-%m-%d").date()
)
with connect_memory_db as conn:
conn.row_factory = task_factory
tasks: List[Task] = conn.execute("select * from tasks;").fetchall()
assert len(tasks) == 2
assert tasks[0] == Task(1, 2, "Task 1", date(2020, 7, 8))
assert tasks[1] == Task(2, 1, "Task 2", date(2020, 7, 11))
def test_default_date_adapter_converter() -> None:
"""Using the default date (`datetime.date`) adapter and converter."""
conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
try:
create_schema(conn)
with conn:
cursor.execute(
"insert into tasks (priority, details, deadline) values (?, ?, ?);",
(2, "Task 1", date(2020, 7, 8)),
)
cursor.execute("select * from tasks;")
assert cursor.fetchone() == (1, 2, "Task 1", date(2020, 7, 8))
finally:
conn.close()
def test_save_database(connect_memory_db: sqlite3.Connection, tmp_path: Path) -> None:
"""Save a database in an SQL text format."""
dump_path = tmp_path.joinpath("dump.sql")
with open(dump_path, mode="w") as dump_file:
for line in connect_memory_db.iterdump():
dump_file.write(f"{line}\n")
with open(dump_path) as dump_read:
assert (
dump_read.read()
== """\
BEGIN TRANSACTION;
DELETE FROM "sqlite_sequence";
INSERT INTO "sqlite_sequence" VALUES('tasks',2);
CREATE TABLE tasks(
id integer primary key autoincrement not null,
priority integer default 1,
details text,
deadline date
);
INSERT INTO "tasks" VALUES(1,2,'Task 1','2020-07-08');
INSERT INTO "tasks" VALUES(2,1,'Task 2','2020-07-11');
COMMIT;
"""
)