MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_union_correspondence

Method test_union_correspondence

test/sql/test_selectable.py:977–1027  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

975 )
976
977 def test_union_correspondence(self):
978 # tests that we can correspond a column in a Select statement
979 # with a certain Table, against a column in a Union where one of
980 # its underlying Selects matches to that same Table
981
982 u = select(
983 table1.c.col1,
984 table1.c.col2,
985 table1.c.col3,
986 table1.c.colx,
987 null().label("coly"),
988 ).union(
989 select(
990 table2.c.col1,
991 table2.c.col2,
992 table2.c.col3,
993 null().label("colx"),
994 table2.c.coly,
995 )
996 )
997 s1 = table1.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
998 s2 = table2.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
999
1000 assert (
1001 u.corresponding_column(s1.selected_columns.table1_col2)
1002 is u.selected_columns.col2
1003 )
1004
1005 # right now, the "selected_columns" of a union are those of the
1006 # first selectable. so without using a subquery that represents
1007 # all the SELECTs in the union, we can't do corresponding column
1008 # like this. perhaps compoundselect shouldn't even implement
1009 # .corresponding_column directly
1010 assert (
1011 u.corresponding_column(s2.selected_columns.table2_col2) is None
1012 ) # really? u.selected_columns.col2
1013
1014 usub = u.subquery()
1015 assert (
1016 usub.corresponding_column(s1.selected_columns.table1_col2)
1017 is usub.c.col2
1018 )
1019 assert (
1020 usub.corresponding_column(s2.selected_columns.table2_col2)
1021 is usub.c.col2
1022 )
1023
1024 s1sub = s1.subquery()
1025 s2sub = s2.subquery()
1026 assert usub.corresponding_column(s1sub.c.table1_col2) is usub.c.col2
1027 assert usub.corresponding_column(s2sub.c.table2_col2) is usub.c.col2
1028
1029 def test_union_precedence(self):
1030 # conflicting column correspondence should be resolved based on

Callers

nothing calls this directly

Calls 8

selectFunction · 0.90
nullFunction · 0.90
unionMethod · 0.45
labelMethod · 0.45
set_label_styleMethod · 0.45
selectMethod · 0.45
corresponding_columnMethod · 0.45
subqueryMethod · 0.45

Tested by

no test coverage detected