github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/db/ent/enttest/enttest.go (about)

     1  // Code generated by ent, DO NOT EDIT.
     2  
     3  package enttest
     4  
     5  import (
     6  	"context"
     7  
     8  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     9  	// required by schema hooks.
    10  	_ "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/runtime"
    11  
    12  	"entgo.io/ent/dialect/sql/schema"
    13  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent/migrate"
    14  )
    15  
    16  type (
    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(...interface{})
    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.
    34  func 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.
    41  func WithMigrateOptions(opts ...schema.MigrateOption) Option {
    42  	return func(o *options) {
    43  		o.migrateOpts = append(o.migrateOpts, opts...)
    44  	}
    45  }
    46  
    47  func 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.
    56  func 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.
    68  func 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  }
    74  func 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  }