MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_cloned_alias

Method test_cloned_alias

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

Source from the content-addressed store, hash-verified

848 )
849
850 def test_cloned_alias(self):
851 entity = table(
852 "entity", column("id"), column("employer_id"), column("name")
853 )
854 tag = table("tag", column("tag"), column("entity_id"))
855
856 tags = (
857 select(tag.c.entity_id, func.array_agg(tag.c.tag).label("tags"))
858 .group_by(tag.c.entity_id)
859 .cte("unaliased_tags")
860 )
861
862 entity_tags = tags.alias(name="entity_tags")
863 employer_tags = tags.alias(name="employer_tags")
864
865 q = (
866 select(entity.c.name)
867 .select_from(
868 entity.outerjoin(
869 entity_tags, tags.c.entity_id == entity.c.id
870 ).outerjoin(
871 employer_tags, tags.c.entity_id == entity.c.employer_id
872 )
873 )
874 .where(entity_tags.c.tags.op("@>")(bindparam("tags")))
875 .where(employer_tags.c.tags.op("@>")(bindparam("tags")))
876 )
877
878 self.assert_compile(
879 q,
880 "WITH unaliased_tags AS "
881 "(SELECT tag.entity_id AS entity_id, array_agg(tag.tag) AS tags "
882 "FROM tag GROUP BY tag.entity_id)"
883 " SELECT entity.name "
884 "FROM entity "
885 "LEFT OUTER JOIN unaliased_tags AS entity_tags ON "
886 "unaliased_tags.entity_id = entity.id "
887 "LEFT OUTER JOIN unaliased_tags AS employer_tags ON "
888 "unaliased_tags.entity_id = entity.employer_id "
889 "WHERE (entity_tags.tags @> :tags) AND "
890 "(employer_tags.tags @> :tags)",
891 )
892
893 cloned = q.params(tags=["tag1", "tag2"])
894 self.assert_compile(
895 cloned,
896 "WITH unaliased_tags AS "
897 "(SELECT tag.entity_id AS entity_id, array_agg(tag.tag) AS tags "
898 "FROM tag GROUP BY tag.entity_id)"
899 " SELECT entity.name "
900 "FROM entity "
901 "LEFT OUTER JOIN unaliased_tags AS entity_tags ON "
902 "unaliased_tags.entity_id = entity.id "
903 "LEFT OUTER JOIN unaliased_tags AS employer_tags ON "
904 "unaliased_tags.entity_id = entity.employer_id "
905 "WHERE (entity_tags.tags @> :tags) AND "
906 "(employer_tags.tags @> :tags)",
907 )

Callers

nothing calls this directly

Calls 15

tableFunction · 0.90
columnFunction · 0.90
selectFunction · 0.90
bindparamFunction · 0.90
array_aggMethod · 0.80
assert_compileMethod · 0.80
cteMethod · 0.45
group_byMethod · 0.45
labelMethod · 0.45
aliasMethod · 0.45
whereMethod · 0.45
select_fromMethod · 0.45

Tested by

no test coverage detected