-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_statement_params.py
More file actions
202 lines (169 loc) · 6.5 KB
/
Copy pathtest_statement_params.py
File metadata and controls
202 lines (169 loc) · 6.5 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
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
import random
from sqlalchemy import testing
from sqlalchemy.schema import Column
from sqlalchemy.sql import bindparam
from sqlalchemy.sql import column
from sqlalchemy.sql import dml
from sqlalchemy.sql import func
from sqlalchemy.sql import select
from sqlalchemy.sql import text
from sqlalchemy.sql import tstring
from sqlalchemy.sql.base import ExecutableStatement
from sqlalchemy.sql.elements import literal
from sqlalchemy.testing import eq_
from sqlalchemy.testing import expect_deprecated
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_not
from sqlalchemy.testing import ne_
from sqlalchemy.testing.schema import Table
from sqlalchemy.types import Integer
from sqlalchemy.types import Text
from sqlalchemy.util.compat import Template
from sqlalchemy.util.langhelpers import class_hierarchy
class BasicTests(fixtures.TestBase):
def _all_subclasses(self, cls_):
return dict.fromkeys(
s
for s in class_hierarchy(cls_)
# class_hierarchy may return values that
# aren't subclasses of cls
if issubclass(s, cls_)
)
@staticmethod
def _relevant_impls():
return (
text("select 1 + 2"),
tstring(Template("select 1 + 2")),
text("select 42 as q").columns(column("q", Integer)),
func.max(42),
select(1, 2).union(select(3, 4)),
select(1, 2),
)
def test_params_impl(self):
exclude = (dml.UpdateBase,)
visit_names = set()
for cls_ in self._all_subclasses(ExecutableStatement):
if not issubclass(cls_, exclude):
if "__visit_name__" in cls_.__dict__:
visit_names.add(cls_.__visit_name__)
eq_(cls_.params, ExecutableStatement.params, cls_)
else:
ne_(cls_.params, ExecutableStatement.params, cls_)
for other in exclude:
if issubclass(cls_, other):
eq_(cls_.params, other.params, cls_)
break
else:
assert False
extra = {"orm_from_statement"}
eq_(
visit_names - extra,
{i.__visit_name__ for i in self._relevant_impls()},
)
@testing.combinations(*_relevant_impls())
def test_compile_params(self, impl):
new = impl.params(foo=5, bar=10)
is_not(new, impl)
eq_(impl.compile()._collected_params, {})
eq_(new.compile()._collected_params, {"foo": 5, "bar": 10})
eq_(new._generate_cache_key()[2], {"foo": 5, "bar": 10})
class CacheTests(fixtures.TablesTest):
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
Table("a", metadata, Column("data", Integer))
Table("b", metadata, Column("data", Text))
@classmethod
def insert_data(cls, connection):
connection.execute(
cls.tables.a.insert(),
[{"data": i} for i in range(1, 11)],
)
connection.execute(
cls.tables.b.insert(),
[{"data": "row %d" % i} for i in range(1, 11)],
)
def test_plain_select(self, connection):
a = self.tables.a
cs = connection.scalars
for _ in range(3):
x1 = random.randint(1, 10)
eq_(cs(select(a).where(a.c.data == x1)).all(), [x1])
stmt = select(a).where(a.c.data == bindparam("x", x1))
eq_(cs(stmt).all(), [x1])
x1 = random.randint(1, 10)
eq_(cs(stmt.params({"x": x1})).all(), [x1])
x1 = random.randint(1, 10)
eq_(cs(stmt, {"x": x1}).all(), [x1])
x1 = random.randint(1, 10)
x2 = random.randint(1, 10)
eq_(cs(stmt.params({"x": x1}), {"x": x2}).all(), [x2])
stmt2 = stmt.params(x=6).subquery().select()
eq_(cs(stmt2).all(), [6])
eq_(cs(stmt2.params({"x": 2})).all(), [2])
with expect_deprecated(
r"The params\(\) and unique_params\(\) "
"methods on non-statement"
):
# NOTE: can't mix and match the two params styles here
stmt3 = stmt.params(x=6).subquery().params(x=8).select()
eq_(cs(stmt3).all(), [6])
eq_(cs(stmt3.params({"x": 9})).all(), [9])
def test_union(self, connection):
a = self.tables.a
cs = connection.scalars
for _ in range(3):
x1 = random.randint(1, 10)
x2 = random.randint(1, 10)
eq_(
cs(
select(a)
.where(a.c.data == x1)
.union_all(select(a).where(a.c.data == x2))
.order_by(a.c.data)
).all(),
sorted([x1, x2]),
)
x1 = random.randint(1, 10)
x2 = random.randint(1, 10)
stmt = (
select(a, literal(1).label("ord"))
.where(a.c.data == bindparam("x", x1))
.union_all(
select(a, literal(2)).where(a.c.data == bindparam("y", x2))
)
.order_by("ord")
)
eq_(cs(stmt).all(), [x1, x2])
x1a = random.randint(1, 10)
eq_(cs(stmt.params({"x": x1a})).all(), [x1a, x2])
x2 = random.randint(1, 10)
eq_(cs(stmt, {"y": x2}).all(), [x1, x2])
x1 = random.randint(1, 10)
x2 = random.randint(1, 10)
eq_(cs(stmt.params({"x": x1}), {"y": x2}).all(), [x1, x2])
x1 = random.randint(1, 10)
x2 = random.randint(1, 10)
stmt2 = (
stmt.params(x=x1)
.subquery()
.select()
.params(y=x2)
.order_by("ord")
)
eq_(cs(stmt2).all(), [x1, x2])
eq_(cs(stmt2.params({"x": x1}).params({"y": x2})).all(), [x1, x2])
def test_text(self, connection):
a = self.tables.a
cs = connection.scalars
for _ in range(3):
x0 = random.randint(1, 10)
stmt = text("select data from a where data = :x").params(x=x0)
eq_(cs(stmt).all(), [x0])
x1 = random.randint(1, 10)
eq_(cs(stmt.params({"x": x1})).all(), [x1])
x2 = random.randint(1, 10)
stmt2 = stmt.columns(a.c.data).params(x=x2)
eq_(cs(stmt2).all(), [x2])
eq_(cs(stmt2, {"x": 1}).all(), [1])
eq_(cs(stmt2.params(x=1)).all(), [1])