-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go.disabled
More file actions
86 lines (70 loc) · 2.27 KB
/
main_test.go.disabled
File metadata and controls
86 lines (70 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"log"
"os"
"testing"
"time"
"github.com/joho/godotenv"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestMain(m *testing.M) {
// Load the .env.test file
err := godotenv.Load(".env.test")
if err != nil {
log.Fatalf("Error loading .env.test file: %v", err)
}
// Create PostgreSQL container
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "postgres:15", // PostgreSQL version you want to use
ExposedPorts: []string{"5432/tcp"},
Env: map[string]string{
"POSTGRES_USER": os.Getenv("DB_USER"),
"POSTGRES_PASSWORD": os.Getenv("DB_PASSWORD"),
"POSTGRES_DB": os.Getenv("DB_NAME"),
},
WaitingFor: wait.ForListeningPort("5432/tcp").WithStartupTimeout(60 * time.Second),
}
postgresContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
log.Fatalf("Could not start container: %v", err)
}
defer postgresContainer.Terminate(ctx)
host, _ := postgresContainer.Host(ctx)
port, _ := postgresContainer.MappedPort(ctx, "5432/tcp")
os.Setenv("DB_HOST", host)
os.Setenv("DB_PORT", port.Port())
// Now call main() to run migrations and initialize everything
go main()
// Give the server some time to start
time.Sleep(5 * time.Second)
// Set up the connection string for GORM
// dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
// host, port.Port(), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_NAME"))
// var db *gorm.DB
// db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
// Logger: logger.Default.LogMode(logger.Info),
// })
// if err != nil {
// log.Fatalf("Could not connect to the test database: %v", err)
// }
// // Run database migrations
// runMigrations()
// router := gin.Default()
// router.Use(func(c *gin.Context) {
// c.Set("db", db)
// c.Next()
// })
// // router.Use(middlewares.InjectDBMiddleware(db))
// router.Use(middlewares.CORSMiddleware())
// routes.ConfigureRouteEndpoints(router)
// This code will run all the tests of this repository regardless of package
code := m.Run()
// Exit with the correct exit code
os.Exit(code)
}