MCPcopy
hub / github.com/ent/ent / TestReusePredicates

Function TestReusePredicates

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

Source from the content-addressed store, hash-verified

1992}
1993
1994func TestReusePredicates(t *testing.T) {
1995 tests := []struct {
1996 p *Predicate
1997 wantQuery string
1998 wantArgs []any
1999 }{
2000 {
2001 p: EQ("active", false),
2002 wantQuery: `SELECT * FROM "users" WHERE NOT "active"`,
2003 },
2004 {
2005 p: Or(
2006 EQ("a", "a"),
2007 EQ("b", "b"),
2008 ),
2009 wantQuery: `SELECT * FROM "users" WHERE "a" = $1 OR "b" = $2`,
2010 wantArgs: []any{"a", "b"},
2011 },
2012 {
2013 p: Or(
2014 EQ("a", "a"),
2015 In("b"),
2016 ),
2017 wantQuery: `SELECT * FROM "users" WHERE "a" = $1 OR FALSE`,
2018 wantArgs: []any{"a"},
2019 },
2020 {
2021 p: And(
2022 EQ("active", true),
2023 HasPrefix("name", "foo"),
2024 HasSuffix("name", "bar"),
2025 Or(
2026 In("id", Select("oid").From(Table("audit"))),
2027 In("id", Select("oid").From(Table("history"))),
2028 ),
2029 ),
2030 wantQuery: `SELECT * FROM "users" WHERE "active" AND "name" LIKE $1 AND "name" LIKE $2 AND ("id" IN (SELECT "oid" FROM "audit") OR "id" IN (SELECT "oid" FROM "history"))`,
2031 wantArgs: []any{"foo%", "%bar"},
2032 },
2033 {
2034 p: func() *Predicate {
2035 t1 := Table("groups")
2036 pivot := Table("user_groups")
2037 matches := Select(pivot.C("user_id")).
2038 From(pivot).
2039 Join(t1).
2040 On(pivot.C("group_id"), t1.C("id")).
2041 Where(EQ(t1.C("name"), "ent"))
2042 return And(
2043 GT("balance", 0),
2044 In("id", matches),
2045 GT("balance", 100),
2046 )
2047 }(),
2048 wantQuery: `SELECT * FROM "users" WHERE "balance" > $1 AND "id" IN (SELECT "user_groups"."user_id" FROM "user_groups" JOIN "groups" AS "t1" ON "user_groups"."group_id" = "t1"."id" WHERE "t1"."name" = $2) AND "balance" > $3`,
2049 wantArgs: []any{0, "ent", 100},
2050 },
2051 }

Callers

nothing calls this directly

Calls 15

HasPrefixFunction · 0.85
HasSuffixFunction · 0.85
DialectFunction · 0.85
OnMethod · 0.80
EQFunction · 0.70
OrFunction · 0.70
InFunction · 0.70
AndFunction · 0.70
SelectFunction · 0.70
TableFunction · 0.70
GTFunction · 0.70
WhereMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…