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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
from sqlalchemy import Table, Column, String, func, MetaData, select, TypeDecorator, cast
from sqlalchemy.testing import fixtures, AssertsCompiledSQL
from sqlalchemy import testing
from sqlalchemy.testing import eq_
class _ExprFixture(object):
def _fixture(self):
class MyString(String):
def bind_expression(self, bindvalue):
return func.lower(bindvalue)
def column_expression(self, col):
return func.lower(col)
test_table = Table(
'test_table',
MetaData(), Column('x', String), Column('y', MyString)
)
return test_table
class SelectTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = 'default'
def test_select_cols(self):
table = self._fixture()
self.assert_compile(
select([table]),
"SELECT test_table.x, lower(test_table.y) AS y FROM test_table"
)
def test_anonymous_expr(self):
table = self._fixture()
self.assert_compile(
select([cast(table.c.y, String)]),
"SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table"
)
def test_select_cols_use_labels(self):
table = self._fixture()
self.assert_compile(
select([table]).apply_labels(),
"SELECT test_table.x AS test_table_x, "
"lower(test_table.y) AS test_table_y FROM test_table"
)
def test_select_cols_use_labels_result_map_targeting(self):
table = self._fixture()
compiled = select([table]).apply_labels().compile()
assert table.c.y in compiled._create_result_map()['test_table_y'][1]
assert table.c.x in compiled._create_result_map()['test_table_x'][1]
# the lower() function goes into the result_map, we don't really
# need this but it's fine
self.assert_compile(
compiled._create_result_map()['test_table_y'][1][2],
"lower(test_table.y)"
)
# then the original column gets put in there as well.
# it's not important that it's the last value.
self.assert_compile(
compiled._create_result_map()['test_table_y'][1][-1],
"test_table.y"
)
def test_insert_binds(self):
table = self._fixture()
self.assert_compile(
table.insert(),
"INSERT INTO test_table (x, y) VALUES (:x, lower(:y))"
)
self.assert_compile(
table.insert().values(y="hi"),
"INSERT INTO test_table (y) VALUES (lower(:y))"
)
def test_select_binds(self):
table = self._fixture()
self.assert_compile(
select([table]).where(table.c.y == "hi"),
"SELECT test_table.x, lower(test_table.y) AS y FROM "
"test_table WHERE test_table.y = lower(:y_1)"
)
class DerivedTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = 'default'
def test_select_from_select(self):
table = self._fixture()
self.assert_compile(
table.select().select(),
"SELECT x, lower(y) AS y FROM (SELECT test_table.x "
"AS x, test_table.y AS y FROM test_table)"
)
def test_select_from_alias(self):
table = self._fixture()
self.assert_compile(
table.select().alias().select(),
"SELECT anon_1.x, lower(anon_1.y) AS y FROM (SELECT "
"test_table.x AS x, test_table.y AS y "
"FROM test_table) AS anon_1"
)
def test_select_from_aliased_join(self):
table = self._fixture()
s1 = table.select().alias()
s2 = table.select().alias()
j = s1.join(s2, s1.c.x == s2.c.x)
s3 = j.select()
self.assert_compile(
s3, "SELECT anon_1.x, lower(anon_1.y) AS y, anon_2.x, "
"lower(anon_2.y) AS y "
"FROM (SELECT test_table.x AS x, test_table.y AS y "
"FROM test_table) AS anon_1 JOIN (SELECT "
"test_table.x AS x, test_table.y AS y "
"FROM test_table) AS anon_2 ON anon_1.x = anon_2.x")
class RoundTripTestBase(object):
def test_round_trip(self):
testing.db.execute(
self.tables.test_table.insert(),
{"x": "X1", "y": "Y1"},
{"x": "X2", "y": "Y2"},
{"x": "X3", "y": "Y3"},
)
# test insert coercion alone
eq_(
testing.db.execute(
"select * from test_table order by y").fetchall(),
[
("X1", "y1"),
("X2", "y2"),
("X3", "y3"),
]
)
# conversion back to upper
eq_(
testing.db.execute(
select([self.tables.test_table]).
order_by(self.tables.test_table.c.y)
).fetchall(),
[
("X1", "Y1"),
("X2", "Y2"),
("X3", "Y3"),
]
)
def test_targeting_no_labels(self):
testing.db.execute(
self.tables.test_table.insert(),
{"x": "X1", "y": "Y1"},
)
row = testing.db.execute(select([self.tables.test_table])).first()
eq_(
row[self.tables.test_table.c.y],
"Y1"
)
def test_targeting_by_string(self):
testing.db.execute(
self.tables.test_table.insert(),
{"x": "X1", "y": "Y1"},
)
row = testing.db.execute(select([self.tables.test_table])).first()
eq_(
row["y"],
"Y1"
)
def test_targeting_apply_labels(self):
testing.db.execute(
self.tables.test_table.insert(),
{"x": "X1", "y": "Y1"},
)
row = testing.db.execute(select([self.tables.test_table]).
apply_labels()).first()
eq_(
row[self.tables.test_table.c.y],
"Y1"
)
def test_targeting_individual_labels(self):
testing.db.execute(
self.tables.test_table.insert(),
{"x": "X1", "y": "Y1"},
)
row = testing.db.execute(select([
self.tables.test_table.c.x.label('xbar'),
self.tables.test_table.c.y.label('ybar')
])).first()
eq_(
row[self.tables.test_table.c.y],
"Y1"
)
class StringRoundTripTest(fixtures.TablesTest, RoundTripTestBase):
@classmethod
def define_tables(cls, metadata):
class MyString(String):
def bind_expression(self, bindvalue):
return func.lower(bindvalue)
def column_expression(self, col):
return func.upper(col)
Table(
'test_table',
metadata,
Column('x', String(50)),
Column('y', MyString(50)
)
)
class TypeDecRoundTripTest(fixtures.TablesTest, RoundTripTestBase):
@classmethod
def define_tables(cls, metadata):
class MyString(TypeDecorator):
impl = String
def bind_expression(self, bindvalue):
return func.lower(bindvalue)
def column_expression(self, col):
return func.upper(col)
Table(
'test_table',
metadata,
Column('x', String(50)),
Column('y', MyString(50)
)
)
class ReturningTest(fixtures.TablesTest):
__requires__ = 'returning',
@classmethod
def define_tables(cls, metadata):
class MyString(String):
def column_expression(self, col):
return func.lower(col)
Table(
'test_table',
metadata, Column('x', String(50)),
Column('y', MyString(50), server_default="YVALUE")
)
@testing.provide_metadata
def test_insert_returning(self):
table = self.tables.test_table
result = testing.db.execute(
table.insert().returning(table.c.y),
{"x": "xvalue"}
)
eq_(
result.first(),
("yvalue",)
)
|