1
0
api/ent/enttest/enttest.go

85 lines
1.7 KiB
Go
Raw Normal View History

2022-08-31 07:49:08 +00:00
// Code generated by ent, DO NOT EDIT.
2022-08-31 06:18:14 +00:00
package enttest
import (
2023-04-05 06:05:14 +00:00
"api/ent"
2022-08-31 06:18:14 +00:00
"context"
// required by schema hooks.
2023-04-05 06:05:14 +00:00
_ "api/ent/runtime"
2022-08-31 06:18:14 +00:00
2023-04-05 06:05:14 +00:00
"api/ent/migrate"
2022-08-31 07:49:08 +00:00
2022-08-31 06:18:14 +00:00
"entgo.io/ent/dialect/sql/schema"
)
type (
// TestingT is the interface that is shared between
// testing.T and testing.B and used by enttest.
TestingT interface {
FailNow()
2022-08-31 07:49:08 +00:00
Error(...any)
2022-08-31 06:18:14 +00:00
}
// Option configures client creation.
Option func(*options)
options struct {
opts []ent.Option
migrateOpts []schema.MigrateOption
}
)
// WithOptions forwards options to client creation.
func WithOptions(opts ...ent.Option) Option {
return func(o *options) {
o.opts = append(o.opts, opts...)
}
}
// WithMigrateOptions forwards options to auto migration.
func WithMigrateOptions(opts ...schema.MigrateOption) Option {
return func(o *options) {
o.migrateOpts = append(o.migrateOpts, opts...)
}
}
func newOptions(opts []Option) *options {
o := &options{}
for _, opt := range opts {
opt(o)
}
return o
}
// Open calls ent.Open and auto-run migration.
func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Client {
o := newOptions(opts)
c, err := ent.Open(driverName, dataSourceName, o.opts...)
if err != nil {
t.Error(err)
t.FailNow()
}
2022-08-31 07:49:08 +00:00
migrateSchema(t, c, o)
2022-08-31 06:18:14 +00:00
return c
}
// NewClient calls ent.NewClient and auto-run migration.
func NewClient(t TestingT, opts ...Option) *ent.Client {
o := newOptions(opts)
c := ent.NewClient(o.opts...)
2022-08-31 07:49:08 +00:00
migrateSchema(t, c, o)
return c
}
func migrateSchema(t TestingT, c *ent.Client, o *options) {
tables, err := schema.CopyTables(migrate.Tables)
if err != nil {
t.Error(err)
t.FailNow()
}
if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil {
2022-08-31 06:18:14 +00:00
t.Error(err)
t.FailNow()
}
}