mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/integration: add query modifier example
This commit is contained in:
committed by
Ariel Mashraki
parent
f5c284787f
commit
e5bceccf1d
@@ -10,9 +10,11 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/entc/integration/template/ent"
|
||||
"entgo.io/ent/entc/integration/template/ent/hook"
|
||||
"entgo.io/ent/entc/integration/template/ent/migrate"
|
||||
"entgo.io/ent/entc/integration/template/ent/pet"
|
||||
"entgo.io/ent/entc/integration/template/ent/user"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -65,4 +67,16 @@ func TestCustomTemplate(t *testing.T) {
|
||||
result := client.User.Query().Where(user.NameGlob("a8*")).
|
||||
AllX(ctx)
|
||||
require.Equal(t, 1, len(result))
|
||||
|
||||
var v []struct{ ID, Owner int }
|
||||
client.Pet.Query().
|
||||
Modify(func(s *sql.Selector) {
|
||||
t := sql.Table(user.Table)
|
||||
s.Join(t).On(s.C(pet.OwnerColumn), t.C(user.FieldID))
|
||||
s.Select(s.C(pet.FieldID), sql.As(t.C(user.FieldID), "owner"))
|
||||
}).
|
||||
Select(pet.FieldID).
|
||||
ScanX(ctx, &v)
|
||||
require.Equal(t, p.ID, v[0].ID)
|
||||
require.Equal(t, u.ID, v[0].Owner)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user