forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_test.go
55 lines (44 loc) · 1.14 KB
/
mysql_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
55
package mysql
import (
"context"
"database/sql"
"fmt"
"testing"
// Import mysql into the scope of this package (required)
_ "github.com/go-sql-driver/mysql"
)
func TestMysql(t *testing.T) {
ctx := context.Background()
container, err := setupMysql(ctx)
if err != nil {
t.Fatal(err)
}
// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// perform assertions
host, _ := container.Host(ctx)
p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
"root", "password", host, port, "database")
db, err := sql.Open("mysql", connectionString)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if err = db.Ping(); err != nil {
t.Errorf("error pinging db: %+v\n", err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS a_table ( \n" +
" `col_1` VARCHAR(128) NOT NULL, \n" +
" `col_2` VARCHAR(128) NOT NULL, \n" +
" PRIMARY KEY (`col_1`, `col_2`) \n" +
")")
if err != nil {
t.Errorf("error creating table: %+v\n", err)
}
}