bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/enttest/enttest.go (about)

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