|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + "slices" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/nix-united/golang-echo-boilerplate/internal/config" |
| 13 | + "github.com/nix-united/golang-echo-boilerplate/internal/db" |
| 14 | + "github.com/nix-united/golang-echo-boilerplate/tests/setup" |
| 15 | + |
| 16 | + "gorm.io/gorm" |
| 17 | +) |
| 18 | + |
| 19 | +var gormDB *gorm.DB |
| 20 | + |
| 21 | +func TestMain(m *testing.M) { |
| 22 | + ctx := context.Background() |
| 23 | + |
| 24 | + shutdown, err := setupMain(ctx) |
| 25 | + if err != nil { |
| 26 | + slog.ErrorContext(ctx, "Failed to setup integration tests", "err", err.Error()) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + code := m.Run() |
| 31 | + |
| 32 | + if err := shutdown(ctx); err != nil { |
| 33 | + slog.ErrorContext(ctx, "Failed to shutdown integration tests", "err", err.Error()) |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + os.Exit(code) |
| 38 | +} |
| 39 | + |
| 40 | +func setupMain(ctx context.Context) (_ func(context.Context) error, err error) { |
| 41 | + shutdownCallbacks := make([]func(context.Context) error, 0) |
| 42 | + |
| 43 | + shutdown := func(ctx context.Context) error { |
| 44 | + var err error |
| 45 | + for _, callback := range slices.Backward(shutdownCallbacks) { |
| 46 | + err = errors.Join(err, callback(ctx)) |
| 47 | + } |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("shutdown callbacks: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + defer func() { |
| 57 | + if err == nil { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if errShutdown := shutdown(context.Background()); errShutdown != nil { |
| 62 | + err = errors.Join(err, errShutdown) |
| 63 | + } |
| 64 | + }() |
| 65 | + |
| 66 | + mysqlConfig, mysqlShutdown, err := setup.SetupMySQL(ctx) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("setup mysql: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + shutdownCallbacks = append(shutdownCallbacks, mysqlShutdown) |
| 72 | + |
| 73 | + gormDB, err = db.NewGormDB(config.DBConfig{ |
| 74 | + User: mysqlConfig.User, |
| 75 | + Password: mysqlConfig.Password, |
| 76 | + Name: mysqlConfig.Name, |
| 77 | + Host: mysqlConfig.Host, |
| 78 | + Port: mysqlConfig.ExposedPort, |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("new gorm db connection: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + dbConnection, err := gormDB.DB() |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("get db connection: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + if err := setup.MigrateDB(dbConnection); err != nil { |
| 90 | + return nil, fmt.Errorf("run db migrations: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + shutdownCallbacks = append(shutdownCallbacks, func(ctx context.Context) error { |
| 94 | + if err := dbConnection.Close(); err != nil { |
| 95 | + return fmt.Errorf("close db connection: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + }) |
| 100 | + |
| 101 | + return shutdown, nil |
| 102 | +} |
0 commit comments