-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
97 lines (62 loc) · 2.66 KB
/
testing.py
File metadata and controls
97 lines (62 loc) · 2.66 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
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlmodel import Session
from src.sqlpage import paginate, PageData
DATABASE_NAME = 'test_pypagination.db'
TABLE_NAME = "test_table"
Base = declarative_base()
def create_and_populate_database():
conn = sqlite3.connect(DATABASE_NAME)
cursor = conn.cursor()
cursor.execute(f'''CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL
)''')
cursor.execute(f'SELECT COUNT(*) FROM {TABLE_NAME}')
count = cursor.fetchone()[0]
if count != 100:
for i in range(100):
cursor.execute(f"INSERT INTO {TABLE_NAME} (username, email) VALUES ('user{i}', 'user{i}@user.com')")
else:
print('Database already populated')
conn.commit()
conn.close()
def check_pagination_sqlmodel():
print("Testing for SQLModel ORM")
engine = create_engine(f"sqlite:///{DATABASE_NAME}", echo=False)
Base.metadata.create_all(engine)
with Session(engine) as session:
call_pagination_and_test(session, orm="sqlmodel")
def check_pagination_sqlalchemy():
print("Testing for SQLAlchemy ORM")
engine = create_engine(f"sqlite:///{DATABASE_NAME}", echo=False)
Base.metadata.create_all(engine)
SessionSqlAlchemy = sessionmaker(bind=engine)
session = SessionSqlAlchemy()
call_pagination_and_test(session, orm="sqlalchemy")
def call_pagination_and_test(session, orm: str):
page_size = 10
if orm == "sqlalchemy":
from tests.table_model import TestTableSqlAlchemy as TestTable
elif orm == "sqlmodel":
from tests.table_model import TestTableSqlModel as TestTable
else:
raise ValueError('Invalid ORM type. Please select either sqlalchemy or sqlmodel')
query = session.query(TestTable)
result: PageData = paginate(session, query, page_size=page_size)
print(f"Result length - {len(result.items)}")
print(f"Called with page size - {page_size}")
print(f"Next page token - {result.next_page_token}")
page_size = page_size + 20
print(f"Now querying for next page with page size {page_size}")
result: PageData = paginate(session, query, page_size=page_size, token=result.next_page_token)
print(f"Result length - {len(result.items)}")
print(f"Called with page size - {page_size}")
print(f"Next page token - {result.next_page_token}")
if __name__ == "__main__":
create_and_populate_database()
check_pagination_sqlalchemy()
check_pagination_sqlmodel()