MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_recursive_union_alias_four

Method test_recursive_union_alias_four

test/sql/test_cte.py:443–493  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

441 )
442
443 def test_recursive_union_alias_four(self):
444 # like test one and three, but let's refer
445 # previous version of "cte". here we test
446 # how the compiler resolves multiple instances
447 # of "cte".
448
449 s1 = select(literal(0).label("x"))
450 cte = s1.cte(name="cte", recursive=True)
451
452 bar = select(cte).cte("bar").alias("cs1")
453
454 cte = cte.union_all(select(cte.c.x + 1).where(cte.c.x < 10)).alias(
455 "cs2"
456 )
457
458 # outer cte rendered first, then bar, which
459 # includes "inner" cte
460 s2 = select(cte, bar)
461 self.assert_compile(
462 s2,
463 "WITH RECURSIVE cte(x) AS "
464 "(SELECT :param_1 AS x UNION ALL "
465 "SELECT cte.x + :x_2 AS anon_1 "
466 "FROM cte WHERE cte.x < :x_3), "
467 "bar AS (SELECT cte.x AS x FROM cte) "
468 "SELECT cs2.x, cs1.x AS x_1 FROM cte AS cs2, bar AS cs1",
469 )
470
471 # bar rendered, only includes "inner" cte,
472 # "outer" cte isn't present
473 s2 = select(bar)
474 self.assert_compile(
475 s2,
476 "WITH RECURSIVE cte(x) AS "
477 "(SELECT :param_1 AS x), "
478 "bar AS (SELECT cte.x AS x FROM cte) "
479 "SELECT cs1.x FROM bar AS cs1",
480 )
481
482 # bar rendered, but then the "outer"
483 # cte is rendered.
484 s2 = select(bar, cte)
485 self.assert_compile(
486 s2,
487 "WITH RECURSIVE bar AS (SELECT cte.x AS x FROM cte), "
488 "cte(x) AS "
489 "(SELECT :param_1 AS x UNION ALL "
490 "SELECT cte.x + :x_2 AS anon_1 "
491 "FROM cte WHERE cte.x < :x_3) "
492 "SELECT cs1.x, cs2.x AS x_1 FROM bar AS cs1, cte AS cs2",
493 )
494
495 @testing.combinations(True, False, argnames="identical")
496 @testing.variation("clone_type", ["none", "clone", "annotated"])

Callers

nothing calls this directly

Calls 8

selectFunction · 0.90
literalFunction · 0.90
assert_compileMethod · 0.80
labelMethod · 0.45
cteMethod · 0.45
aliasMethod · 0.45
union_allMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected