MCPcopy
hub / github.com/ent/ent / TestWindowFunction

Function TestWindowFunction

dialect/sql/builder_test.go:2079–2099  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2077}
2078
2079func TestWindowFunction(t *testing.T) {
2080 posts := Table("posts")
2081 base := Select(posts.Columns("id", "content", "author_id")...).
2082 From(posts).
2083 Where(EQ("active", true))
2084 with := With("active_posts").
2085 As(base).
2086 With("selected_posts").
2087 As(
2088 Select().
2089 AppendSelect("*").
2090 AppendSelectExprAs(
2091 RowNumber().PartitionBy("author_id").OrderBy("id").OrderExpr(Expr("f(`s`)")),
2092 "row_number",
2093 ).
2094 From(Table("active_posts")),
2095 )
2096 query, args := Select("*").From(Table("selected_posts")).Where(LTE("row_number", 2)).Prefix(with).Query()
2097 require.Equal(t, "WITH `active_posts` AS (SELECT `posts`.`id`, `posts`.`content`, `posts`.`author_id` FROM `posts` WHERE `active`), `selected_posts` AS (SELECT *, (ROW_NUMBER() OVER (PARTITION BY `author_id` ORDER BY `id`, f(`s`))) AS `row_number` FROM `active_posts`) SELECT * FROM `selected_posts` WHERE `row_number` <= ?", query)
2098 require.Equal(t, []any{2}, args)
2099}
2100
2101func TestWindowFunction_Select(t *testing.T) {
2102 posts := Table("posts")

Callers

nothing calls this directly

Calls 15

WithFunction · 0.85
RowNumberFunction · 0.85
AppendSelectExprAsMethod · 0.80
AppendSelectMethod · 0.80
PartitionByMethod · 0.80
TableFunction · 0.70
SelectFunction · 0.70
EQFunction · 0.70
ExprFunction · 0.70
LTEFunction · 0.70
WhereMethod · 0.65
ColumnsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…