enttest.go 1.7 KB

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