-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_orm.py
73 lines (56 loc) · 1.99 KB
/
test_orm.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
import sqlite3
# tests
def test_create_db(db):
assert isinstance(db.conn, sqlite3.Connection)
assert db.tables == []
def test_define_tables(Author, Book):
assert Author.name.type == str
assert Book.author.table == Author
assert Author.name.sql_type == "TEXT"
assert Author.age.sql_type == "INTEGER"
def test_create_tables(db, Author, Book):
db.create(Author)
db.create(Book)
assert Author._get_create_sql() == "CREATE TABLE IF NOT EXISTS author (id INTEGER PRIMARY KEY AUTOINCREMENT, age INTEGER, name TEXT);"
assert Book._get_create_sql() == "CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER, published INTEGER, title TEXT);"
for table in ("author", "book"):
assert table in db.tables
def test_create_author_instance(db, Author):
db.create(Author)
john = Author(name="John Doe", age=35)
assert john.name == "John Doe"
assert john.age == 35
assert john.id is None
def test_save_author_instances(db, Author):
db.create(Author)
john = Author(name="John Doe", age=23)
db.save(john)
assert john._get_insert_sql() == (
"INSERT INTO author (age, name) VALUES (?, ?);",
[23, "John Doe"]
)
assert john.id == 1
man = Author(name="Man Harsh", age = 28)
db.save(man)
assert man.id == 2
vik = Author(name="Vik Star", age=43)
db.save(vik)
assert vik.id == 3
jack = Author(name="Jack Ma", age=39)
db.save(jack)
assert jack.id == 4
def test_query_all_authors(db, Author):
db.create(Author)
john = Author(name="John Doe", age=23)
vik = Author(name="Vik Star", age=43)
db.save(john)
db.save(vik)
authors = db.all(Author)
assert Author._get_select_all_sql() == (
"SELECT id, age, name FROM author;",
["id", "age", "name"]
)
assert len(authors) == 2
assert type(authors[0]) == Author
assert {a.age for a in authors} == {23, 43}
assert {a.name for a in authors} == {"John Doe", "Vik Star"}