Repository navigation
Expand file tree
/
Copy pathtest_database.py
More file actions
67 lines (54 loc) · 1.98 KB
/
Copy pathtest_database.py
File metadata and controls
67 lines (54 loc) · 1.98 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
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from database import create_table, get_connection
def test_table_exists():
create_table()
connection = get_connection()
rows = connection.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='opportunities'"
).fetchall()
connection.close()
if len(rows) == 1:
print("PASS: opportunities table exists")
else:
print("FAIL: opportunities table not found")
def test_columns():
connection = get_connection()
rows = connection.execute("PRAGMA table_info(opportunities)").fetchall()
connection.close()
columns = [row["name"] for row in rows]
print("Columns:", columns)
if columns == ["id", "title", "category"]:
print("PASS: columns are correct")
else:
print("FAIL: unexpected columns")
def test_row_factory():
connection = get_connection()
cursor = connection.execute(
"INSERT INTO opportunities (title, category) VALUES (?, ?)",
("Data Analyst Intern", "Data"),
)
connection.commit()
# Read back the row we just inserted. Looking it up by its id (not with
# "LIMIT 1") matters: the table already holds other rows, so LIMIT 1 would
# return the first row of the table instead of the new one.
new_id = cursor.lastrowid
row = connection.execute(
"SELECT title, category FROM opportunities WHERE id = ?",
(new_id,)
).fetchone()
# Remove the test row again so running this file does not fill the
# database with "Data Analyst Intern" copies.
connection.execute("DELETE FROM opportunities WHERE id = ?", (new_id,))
connection.commit()
connection.close()
print("Row as dict:", dict(row))
if row["title"] == "Data Analyst Intern":
print("PASS: row_factory works (row['title'])")
else:
print("FAIL: row_factory not working")
if __name__ == "__main__":
test_table_exists()
test_columns()
test_row_factory()