-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs_example_test.py
68 lines (58 loc) · 2.1 KB
/
docs_example_test.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
from sql_mapper import ModelBase
from sql_mapper.sql_executors import SQLiteSQLExecutor
class Book(ModelBase):
_tablename = "books"
id: int = "INTEGER"
title: str = "TEXT"
_additional_table_lines = "PRIMARY KEY (id)"
class Author(ModelBase):
_tablename = "authors"
id: int = "INTEGER"
name: str = "TEXT"
_additional_table_lines = "PRIMARY KEY (id)"
class BookToAuthor(ModelBase):
_tablename = "books_to_authors"
book_id: int = "INTEGER"
author_id: int = "INTEGER"
_additional_table_lines = (
"FOREIGN KEY (book_id) REFERENCES books(id),"
"FOREIGN KEY (author_id) REFERENCES authors(id)"
)
class BookNameAndAuthorName(ModelBase):
book_title: str
author_name: str
def test_example_from_docs():
sql_executor = SQLiteSQLExecutor.new(":memory:")
sql_executor.create_tables(Book, Author, BookToAuthor)
for author_name, book_names in (
("Michael Z.", ("My Great Book", "My Another Great Book")),
("Anna S.", ("BOOK 1", "BOOK 2"))
):
book_ids = []
for book_title in book_names:
sql_executor.execute(
"INSERT INTO ?", [Book(title=book_title)]
)
book_ids.append(sql_executor.cursor.lastrowid)
sql_executor.execute(
"INSERT INTO ?", [Author(name=author_name)]
)
author_id = sql_executor.cursor.lastrowid
sql_executor.execute_many(
"INSERT INTO ?", [
[BookToAuthor(book_id, author_id)]
for book_id in book_ids
]
)
sql_executor.commit()
assert list(sql_executor.execute(
"SELECT books.title, authors.name FROM books "
"INNER JOIN books_to_authors ON books_to_authors.book_id = books.id "
"INNER JOIN authors ON authors.id = books_to_authors.author_id",
model=BookNameAndAuthorName
)) == [
BookNameAndAuthorName("My Great Book", "Michael Z."),
BookNameAndAuthorName("My Another Great Book", "Michael Z."),
BookNameAndAuthorName("BOOK 1", "Anna S."),
BookNameAndAuthorName("BOOK 2", "Anna S.")
]