MCPcopy
hub / github.com/ent/ent / TestWritePath

Function TestWritePath

dialect/sql/sqljson/sqljson_test.go:17–410  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestWritePath(t *testing.T) {
18 tests := []struct {
19 input sql.Querier
20 wantQuery string
21 wantArgs []any
22 }{
23 {
24 input: sql.Dialect(dialect.Postgres).
25 Select("*").
26 From(sql.Table("users")).
27 Where(sqljson.ValueEQ("a", 1, sqljson.Path("b", "c", "[1]", "d"), sqljson.Cast("int"))),
28 wantQuery: `SELECT * FROM "users" WHERE ("a"->'b'->'c'->1->>'d')::int = $1`,
29 wantArgs: []any{1},
30 },
31 {
32 input: sql.Dialect(dialect.MySQL).
33 Select("*").
34 From(sql.Table("users")).
35 Where(sqljson.ValueEQ("a", "a", sqljson.DotPath("b.c[1].d"))),
36 wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, '$.b.c[1].d') = ?",
37 wantArgs: []any{"a"},
38 },
39 {
40 input: sql.Dialect(dialect.MySQL).
41 Select("*").
42 From(sql.Table("users")).
43 Where(sqljson.ValueEQ("a", true, sqljson.DotPath("b.c[1].d"))),
44 wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, '$.b.c[1].d') = true",
45 },
46 {
47 input: sql.Dialect(dialect.MySQL).
48 Select("*").
49 From(sql.Table("users")).
50 Where(sqljson.ValueEQ("a", "a", sqljson.DotPath("b.\"c[1]\".d[1][2].e"))),
51 wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, '$.b.\"c[1]\".d[1][2].e') = ?",
52 wantArgs: []any{"a"},
53 },
54 {
55 input: sql.Select("*").
56 From(sql.Table("test")).
57 Where(sqljson.ValueEQ("j", sqljson.ValuePath("j", sqljson.DotPath("a.*.b")), sqljson.DotPath("a.*.c"))),
58 wantQuery: "SELECT * FROM `test` WHERE JSON_EXTRACT(`j`, '$.a.*.c') = JSON_EXTRACT(`j`, '$.a.*.b')",
59 },
60 {
61 input: sql.Dialect(dialect.Postgres).
62 Select("*").
63 From(sql.Table("test")).
64 Where(sqljson.ValueEQ("j", sqljson.ValuePath("j", sqljson.DotPath("a.*.b")), sqljson.DotPath("a.*.c"))),
65 wantQuery: `SELECT * FROM "test" WHERE "j"->'a'->'*'->>'c' = "j"->'a'->'*'->'b'`,
66 },
67 {
68 input: sql.Select("*").
69 From(sql.Table("test")).
70 Where(sqljson.HasKey("j", sqljson.DotPath("a.*.c"))),
71 wantQuery: "SELECT * FROM `test` WHERE JSON_EXTRACT(`j`, '$.a.*.c') IS NOT NULL",
72 },
73 {
74 input: sql.Select("*").

Callers

nothing calls this directly

Calls 15

DialectFunction · 0.92
TableFunction · 0.92
ValueEQFunction · 0.92
PathFunction · 0.92
CastFunction · 0.92
DotPathFunction · 0.92
SelectFunction · 0.92
ValuePathFunction · 0.92
HasKeyFunction · 0.92
AndFunction · 0.92
GTFunction · 0.92
EQFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…