MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_reduce_aliased_union_2

Method test_reduce_aliased_union_2

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

Source from the content-addressed store, hash-verified

2800 )
2801
2802 def test_reduce_aliased_union_2(self):
2803 metadata = MetaData()
2804 page_table = Table(
2805 "page", metadata, Column("id", Integer, primary_key=True)
2806 )
2807 magazine_page_table = Table(
2808 "magazine_page",
2809 metadata,
2810 Column(
2811 "page_id", Integer, ForeignKey("page.id"), primary_key=True
2812 ),
2813 )
2814 classified_page_table = Table(
2815 "classified_page",
2816 metadata,
2817 Column(
2818 "magazine_page_id",
2819 Integer,
2820 ForeignKey("magazine_page.page_id"),
2821 primary_key=True,
2822 ),
2823 )
2824
2825 # this is essentially the union formed by the ORM's
2826 # polymorphic_union function. we define two versions with
2827 # different ordering of selects.
2828 #
2829 # the first selectable has the "real" column
2830 # classified_page.magazine_page_id
2831
2832 pjoin = union(
2833 select(
2834 page_table.c.id,
2835 magazine_page_table.c.page_id,
2836 classified_page_table.c.magazine_page_id,
2837 ).select_from(
2838 page_table.join(magazine_page_table).join(
2839 classified_page_table
2840 )
2841 ),
2842 select(
2843 page_table.c.id,
2844 magazine_page_table.c.page_id,
2845 cast(null(), Integer).label("magazine_page_id"),
2846 ).select_from(page_table.join(magazine_page_table)),
2847 ).alias("pjoin")
2848 eq_(
2849 util.column_set(
2850 sql_util.reduce_columns(
2851 [pjoin.c.id, pjoin.c.page_id, pjoin.c.magazine_page_id]
2852 )
2853 ),
2854 util.column_set([pjoin.c.id]),
2855 )
2856
2857 # the first selectable has a CAST, which is a placeholder for
2858 # classified_page.magazine_page_id in the second selectable.
2859 # reduce_columns needs to take into account all foreign keys

Callers

nothing calls this directly

Calls 14

MetaDataClass · 0.90
TableClass · 0.90
ColumnClass · 0.90
ForeignKeyClass · 0.90
unionFunction · 0.90
selectFunction · 0.90
castFunction · 0.90
nullFunction · 0.90
eq_Function · 0.90
reduce_columnsMethod · 0.80
aliasMethod · 0.45
select_fromMethod · 0.45

Tested by

no test coverage detected