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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
from sqlalchemy import cast
from sqlalchemy import Column
from sqlalchemy import func
from sqlalchemy import MetaData
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy import TypeDecorator
from sqlalchemy import union
from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL
from sqlalchemy.sql.type_api import UserDefinedType
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
class _ExprFixture:
def _test_table(self, type_):
test_table = Table(
"test_table", MetaData(), Column("x", String), Column("y", type_)
)
return test_table
def _fixture(self):
class MyString(String):
# supersedes any processing that might be on
# String
def bind_expression(self, bindvalue):
return func.lower(bindvalue)
def column_expression(self, col):
return func.lower(col)
return self._test_table(MyString)
def _type_decorator_outside_fixture(self):
class MyString(TypeDecorator):
impl = String
cache_ok = True
def bind_expression(self, bindvalue):
return func.outside_bind(bindvalue)
def column_expression(self, col):
return func.outside_colexpr(col)
return self._test_table(MyString)
def _type_decorator_inside_fixture(self):
class MyInsideString(String):
def bind_expression(self, bindvalue):
return func.inside_bind(bindvalue)
def column_expression(self, col):
return func.inside_colexpr(col)
class MyString(TypeDecorator):
impl = MyInsideString
cache_ok = True
return self._test_table(MyString)
def _type_decorator_both_fixture(self):
class MyDialectString(String):
def bind_expression(self, bindvalue):
return func.inside_bind(bindvalue)
def column_expression(self, col):
return func.inside_colexpr(col)
class MyString(TypeDecorator):
impl = String
cache_ok = True
# this works because when the compiler calls dialect_impl(),
# a copy of MyString is created which has just this impl
# as self.impl
def load_dialect_impl(self, dialect):
return MyDialectString()
# user-defined methods need to invoke explicitly on the impl
# for now...
def bind_expression(self, bindvalue):
return func.outside_bind(self.impl.bind_expression(bindvalue))
def column_expression(self, col):
return func.outside_colexpr(self.impl.column_expression(col))
return self._test_table(MyString)
def _variant_fixture(self, inner_fixture):
type_ = inner_fixture.c.y.type
variant = String(30).with_variant(type_, "default")
return self._test_table(variant)
def _dialect_level_fixture(self):
class ImplString(String):
def bind_expression(self, bindvalue):
return func.dialect_bind(bindvalue)
def column_expression(self, col):
return func.dialect_colexpr(col)
from sqlalchemy.engine import default
dialect = default.DefaultDialect()
dialect.colspecs = {String: ImplString}
return dialect
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 y FROM test_table",
)
def test_select_cols_use_labels(self):
table = self._fixture()
self.assert_compile(
select(table).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL),
"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)
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.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][3],
"lower(test_table.y)",
)
# then the original column gets put in there as well.
# as of 1.1 it's important that it is first as this is
# taken as significant by the result processor.
self.assert_compile(
compiled._create_result_map()["test_table_y"][1][0], "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)",
)
@testing.variation(
"compile_opt", ["plain", "postcompile", "literal_binds"]
)
def test_in_binds(self, compile_opt):
table = self._fixture()
stmt = select(table).where(
table.c.y.in_(["hi", "there", "some", "expr"])
)
if compile_opt.plain:
self.assert_compile(
stmt,
"SELECT test_table.x, lower(test_table.y) AS y FROM "
"test_table WHERE test_table.y IN "
"(__[POSTCOMPILE_y_1~~lower(~~REPL~~)~~])",
render_postcompile=False,
)
elif compile_opt.postcompile:
self.assert_compile(
stmt,
"SELECT test_table.x, lower(test_table.y) AS y FROM "
"test_table WHERE test_table.y IN "
"(lower(:y_1_1), lower(:y_1_2), lower(:y_1_3), lower(:y_1_4))",
render_postcompile=True,
)
elif compile_opt.literal_binds:
self.assert_compile(
stmt,
"SELECT test_table.x, lower(test_table.y) AS y FROM "
"test_table WHERE test_table.y IN "
"(lower('hi'), lower('there'), lower('some'), lower('expr'))",
literal_binds=True,
)
def test_dialect(self):
table = self._fixture()
dialect = self._dialect_level_fixture()
# 'x' is straight String
self.assert_compile(
select(table.c.x).where(table.c.x == "hi"),
"SELECT dialect_colexpr(test_table.x) AS x "
"FROM test_table WHERE test_table.x = dialect_bind(:x_1)",
dialect=dialect,
)
def test_type_decorator_inner(self):
table = self._type_decorator_inside_fixture()
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT test_table.x, inside_colexpr(test_table.y) AS y "
"FROM test_table WHERE test_table.y = inside_bind(:y_1)",
)
def test_type_decorator_inner_plus_dialect(self):
table = self._type_decorator_inside_fixture()
dialect = self._dialect_level_fixture()
# for "inner", the MyStringImpl is a subclass of String, #
# so a dialect-level
# implementation supersedes that, which is the same as with other
# processor functions
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT dialect_colexpr(test_table.x) AS x, "
"dialect_colexpr(test_table.y) AS y FROM test_table "
"WHERE test_table.y = dialect_bind(:y_1)",
dialect=dialect,
)
def test_type_decorator_outer(self):
table = self._type_decorator_outside_fixture()
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT test_table.x, outside_colexpr(test_table.y) AS y "
"FROM test_table WHERE test_table.y = outside_bind(:y_1)",
)
def test_type_decorator_outer_plus_dialect(self):
table = self._type_decorator_outside_fixture()
dialect = self._dialect_level_fixture()
# for "outer", the MyString isn't calling the "impl" functions,
# so we don't get the "impl"
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT dialect_colexpr(test_table.x) AS x, "
"outside_colexpr(test_table.y) AS y "
"FROM test_table WHERE test_table.y = outside_bind(:y_1)",
dialect=dialect,
)
def test_type_decorator_both(self):
table = self._type_decorator_both_fixture()
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT test_table.x, "
"outside_colexpr(inside_colexpr(test_table.y)) AS y "
"FROM test_table WHERE "
"test_table.y = outside_bind(inside_bind(:y_1))",
)
def test_type_decorator_both_plus_dialect(self):
table = self._type_decorator_both_fixture()
dialect = self._dialect_level_fixture()
# for "inner", the MyStringImpl is a subclass of String,
# so a dialect-level
# implementation supersedes that, which is the same as with other
# processor functions
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT dialect_colexpr(test_table.x) AS x, "
"outside_colexpr(dialect_colexpr(test_table.y)) AS y "
"FROM test_table WHERE "
"test_table.y = outside_bind(dialect_bind(:y_1))",
dialect=dialect,
)
def test_type_decorator_both_w_variant(self):
table = self._variant_fixture(self._type_decorator_both_fixture())
self.assert_compile(
select(table).where(table.c.y == "hi"),
"SELECT test_table.x, "
"outside_colexpr(inside_colexpr(test_table.y)) AS y "
"FROM test_table WHERE "
"test_table.y = outside_bind(inside_bind(:y_1))",
)
def test_compound_select(self):
table = self._fixture()
s1 = select(table).where(table.c.y == "hi")
s2 = select(table).where(table.c.y == "there")
self.assert_compile(
union(s1, s2),
"SELECT test_table.x, lower(test_table.y) AS y "
"FROM test_table WHERE test_table.y = lower(:y_1) "
"UNION SELECT test_table.x, lower(test_table.y) AS y "
"FROM test_table WHERE test_table.y = lower(:y_2)",
)
def test_select_of_compound_select(self):
table = self._fixture()
s1 = select(table).where(table.c.y == "hi")
s2 = select(table).where(table.c.y == "there")
self.assert_compile(
union(s1, s2).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 WHERE test_table.y = lower(:y_1) "
"UNION SELECT test_table.x AS x, test_table.y AS y "
"FROM test_table WHERE test_table.y = lower(:y_2)) AS anon_1",
)
class DerivedTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = "default"
def test_select_from_select(self):
table = self._fixture()
self.assert_compile(
table.select().subquery().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 AS x_1, "
"lower(anon_2.y) AS y_1 "
"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:
@testing.requires.insertmanyvalues
def test_insertmanyvalues_returning(self, connection):
tt = self.tables.test_table
result = connection.execute(
tt.insert().returning(tt.c["x", "y"]),
[
{"x": "X1", "y": "Y1"},
{"x": "X2", "y": "Y2"},
{"x": "X3", "y": "Y3"},
],
)
eq_(
result.all(),
[("X1", "Y1"), ("X2", "Y2"), ("X3", "Y3")],
)
def test_round_trip(self, connection):
connection.execute(
self.tables.test_table.insert(),
[
{"x": "X1", "y": "Y1"},
{"x": "X2", "y": "Y2"},
{"x": "X3", "y": "Y3"},
],
)
# test insert coercion alone
eq_(
connection.exec_driver_sql(
"select * from test_table order by y"
).fetchall(),
[("X1", "y1"), ("X2", "y2"), ("X3", "y3")],
)
# conversion back to upper
eq_(
connection.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, connection):
connection.execute(
self.tables.test_table.insert(), {"x": "X1", "y": "Y1"}
)
row = connection.execute(select(self.tables.test_table)).first()
eq_(row._mapping[self.tables.test_table.c.y], "Y1")
def test_targeting_by_string(self, connection):
connection.execute(
self.tables.test_table.insert(), {"x": "X1", "y": "Y1"}
)
row = connection.execute(select(self.tables.test_table)).first()
eq_(row._mapping["y"], "Y1")
def test_targeting_apply_labels(self, connection):
connection.execute(
self.tables.test_table.insert(), {"x": "X1", "y": "Y1"}
)
row = connection.execute(
select(self.tables.test_table).set_label_style(
LABEL_STYLE_TABLENAME_PLUS_COL
)
).first()
eq_(row._mapping[self.tables.test_table.c.y], "Y1")
def test_targeting_individual_labels(self, connection):
connection.execute(
self.tables.test_table.insert(), {"x": "X1", "y": "Y1"}
)
row = connection.execute(
select(
self.tables.test_table.c.x.label("xbar"),
self.tables.test_table.c.y.label("ybar"),
)
).first()
eq_(row._mapping[self.tables.test_table.c.y], "Y1")
class StringRoundTripTest(fixtures.TablesTest, RoundTripTestBase):
__requires__ = ("string_type_isnt_subtype",)
@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 UserDefinedTypeRoundTripTest(fixtures.TablesTest, RoundTripTestBase):
@classmethod
def define_tables(cls, metadata):
class MyString(UserDefinedType):
cache_ok = True
def get_col_spec(self, **kw):
return "VARCHAR(50)"
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()),
)
class TypeDecRoundTripTest(fixtures.TablesTest, RoundTripTestBase):
@classmethod
def define_tables(cls, metadata):
class MyString(TypeDecorator):
impl = String
cache_ok = True
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__ = ("insert_returning",)
@classmethod
def define_tables(cls, metadata):
class MyString(TypeDecorator):
impl = String
cache_ok = True
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, connection):
table = self.tables.test_table
result = connection.execute(
table.insert().returning(table.c.y), {"x": "xvalue"}
)
eq_(result.first(), ("yvalue",))
|