-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
204 lines (167 loc) · 5.56 KB
/
app.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
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
from curses import meta
from sqlalchemy import event
from sqlalchemy import Column, MetaData, String, create_engine, Integer, ForeignKey
from sqlalchemy.orm import (
declarative_base,
sessionmaker,
relationship,
contains_eager,
configure_mappers,
joinedload,
)
DB_URI = "sqlite://"
engine = create_engine(DB_URI)
metadata = MetaData()
Base = declarative_base(metadata=metadata)
Session = sessionmaker(engine)
Session.configure(bind=engine)
class DBStatementCounter(object):
"""
https://stackoverflow.com/questions/19073099/how-to-count-sqlalchemy-queries-in-unit-tests
Use as a context manager to count the number of execute()'s performed
against the given sqlalchemy connection.
Usage:
with DBStatementCounter(conn) as ctr:
conn.execute("SELECT 1")
conn.execute("SELECT 1")
assert ctr.get_count() == 2
"""
def __init__(self, conn):
self.conn = conn
self.count = 0
# Will have to rely on this since sqlalchemy 0.8 does not support
# removing event listeners
self.do_count = False
event.listen(conn, "after_execute", self.callback)
def __enter__(self):
self.do_count = True
return self
def __exit__(self, *_):
self.do_count = False
def get_count(self):
return self.count
def callback(self, *_):
if self.do_count:
self.count += 1
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
user = relationship("User", uselist=False)
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, autoincrement=True)
person_id = Column(Integer, ForeignKey("person.id"))
person = relationship("Person")
my_accounts = relationship("UserAccount")
class Company(Base):
__tablename__ = "company"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
class Account(Base):
__tablename__ = "account"
id = Column(Integer, primary_key=True, autoincrement=True)
status = Column(String)
company_id = Column(Integer, ForeignKey("company.id"))
company = relationship("Company")
class UserAccount(Base):
__tablename__ = "user_account"
account_id = Column(Integer, ForeignKey("account.id"), primary_key=True)
account = relationship("Account")
user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
user = relationship("User")
def populate_db(n=10):
users = []
with Session() as session:
for i in range(n):
person = Person(name=f"test{i}")
session.add(person)
session.flush()
user = User(person=person)
session.add(user)
session.flush()
company = Company(name=f"company{i}")
session.add(company)
session.flush()
account = Account(status="x{i}", company=company)
session.add(account)
session.flush()
user_account = UserAccount(user=user, account=account)
session.add(user_account)
session.flush()
session.commit()
configure_mappers()
metadata.drop_all(engine)
metadata.create_all(engine)
populate_db()
def get_query(session, options=None):
if not options:
options = []
return (
session.query(Person)
.join("user", "my_accounts", "account", "company")
.options(*options)
.filter(
Person.name.ilike("test%"),
Account.status.ilike("x%"),
Company.name.ilike("company%"),
)
)
def simple_query():
print("--------")
print("simple")
with Session() as session:
with DBStatementCounter(session.connection()) as ctr:
result = get_query(session)
print("Query")
print(result)
person = result.first()
if person:
a = person.user.my_accounts[0].account.company.name
print("Statements")
print(ctr.count)
def joinedload_query():
print("--------")
print("joinedload")
with Session() as session:
with DBStatementCounter(session.connection()) as ctr:
result = get_query(
session,
options=[
joinedload("user"),
joinedload("user", "my_accounts"),
joinedload("user", "my_accounts", "account"),
joinedload("user", "my_accounts", "account", "company"),
],
)
print("Query")
print(result)
person = result.first()
if person:
a = person.user.my_accounts[0].account.company.name
print("Statements")
print(ctr.count)
def eager_query():
print("--------")
print("joinedload")
with Session() as session:
with DBStatementCounter(session.connection()) as ctr:
result = get_query(
session,
options=[
contains_eager("user"),
contains_eager("user", "my_accounts"),
contains_eager("user", "my_accounts", "account"),
contains_eager("user", "my_accounts", "account", "company"),
],
)
print("Query")
print(result)
person = result.first()
if person:
a = person.user.my_accounts[0].account.company.name
print("Statements")
print(ctr.count)
simple_query()
joinedload_query()
eager_query()