enttest.go 1.8 KB

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