-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
model_test.go
54 lines (48 loc) · 1.35 KB
/
model_test.go
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
package gormlock
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mysql"
gormMysql "gorm.io/driver/mysql"
"gorm.io/gorm"
"testing"
)
func TestAutoMigration_MySQL(t *testing.T) {
t.Parallel()
tests := map[string]struct {
img string
args []string
}{
"8.0.36": {
img: "mysql:8.0.36",
args: []string{"charset=utf8mb4", "parseTime=True", "loc=Local"},
},
}
for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
mysqlContainer, err := mysql.Run(ctx,
tc.img,
mysql.WithDatabase("test"),
mysql.WithUsername("root"),
mysql.WithPassword("password"),
)
require.NoError(t, err, "failed to start container")
defer func() {
if err := testcontainers.TerminateContainer(mysqlContainer); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
}()
connStr, errConn := mysqlContainer.ConnectionString(ctx, tc.args...)
require.NoError(t, errConn, "failed to get mysql connection string")
db, errOpeningConn := gorm.Open(gormMysql.Open(connStr), &gorm.Config{})
require.NoError(t, errOpeningConn)
err = db.AutoMigrate(&CronJobLock{})
assert.NoError(t, err)
})
}
}