at main 1.9 kB view raw
1// Code generated by ent, DO NOT EDIT. 2 3package enttest 4 5import ( 6 "context" 7 8 "tangled.sh/seiso.moe/aletheia.directory/ent" 9 // required by schema hooks. 10 _ "tangled.sh/seiso.moe/aletheia.directory/ent/runtime" 11 12 "entgo.io/ent/dialect/sql/schema" 13 "tangled.sh/seiso.moe/aletheia.directory/ent/migrate" 14) 15 16type ( 17 // TestingT is the interface that is shared between 18 // testing.T and testing.B and used by enttest. 19 TestingT interface { 20 FailNow() 21 Error(...any) 22 } 23 24 // Option configures client creation. 25 Option func(*options) 26 27 options struct { 28 opts []ent.Option 29 migrateOpts []schema.MigrateOption 30 } 31) 32 33// WithOptions forwards options to client creation. 34func WithOptions(opts ...ent.Option) Option { 35 return func(o *options) { 36 o.opts = append(o.opts, opts...) 37 } 38} 39 40// WithMigrateOptions forwards options to auto migration. 41func WithMigrateOptions(opts ...schema.MigrateOption) Option { 42 return func(o *options) { 43 o.migrateOpts = append(o.migrateOpts, opts...) 44 } 45} 46 47func newOptions(opts []Option) *options { 48 o := &options{} 49 for _, opt := range opts { 50 opt(o) 51 } 52 return o 53} 54 55// Open calls ent.Open and auto-run migration. 56func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Client { 57 o := newOptions(opts) 58 c, err := ent.Open(driverName, dataSourceName, o.opts...) 59 if err != nil { 60 t.Error(err) 61 t.FailNow() 62 } 63 migrateSchema(t, c, o) 64 return c 65} 66 67// NewClient calls ent.NewClient and auto-run migration. 68func NewClient(t TestingT, opts ...Option) *ent.Client { 69 o := newOptions(opts) 70 c := ent.NewClient(o.opts...) 71 migrateSchema(t, c, o) 72 return c 73} 74func migrateSchema(t TestingT, c *ent.Client, o *options) { 75 tables, err := schema.CopyTables(migrate.Tables) 76 if err != nil { 77 t.Error(err) 78 t.FailNow() 79 } 80 if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { 81 t.Error(err) 82 t.FailNow() 83 } 84}