-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
bootstrap_test.go
109 lines (96 loc) · 2.5 KB
/
bootstrap_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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package txdb_test
import (
"context"
"database/sql"
"strings"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mysql"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
mysql_sql = `CREATE TABLE users (
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX uniq_email (email)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB`
psql_sql = `CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
)`
inserts = `INSERT INTO users (username, email) VALUES ('gopher', '[email protected]'), ('john', '[email protected]'), ('jane', '[email protected]')`
testDB = "txdb_test"
)
// bootstrap bootstraps the database for tests.
func bootstrap(t *testing.T, driver, dsn string) {
db, err := sql.Open(driver, dsn)
if err != nil {
t.Fatal(err)
}
switch driver {
case "mysql":
if _, err := db.Exec(mysql_sql); err != nil {
t.Fatal(err)
}
case "postgres":
if _, err := db.Exec(psql_sql); err != nil {
t.Fatal(err)
}
default:
panic("unrecognized driver: " + driver)
}
if _, err := db.Exec(inserts); err != nil {
t.Fatal(err)
}
}
func createDB(t *testing.T, driver, dsn string) {
db, err := sql.Open(driver, dsn)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec("DROP DATABASE IF EXISTS " + testDB); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("CREATE DATABASE " + testDB); err != nil {
t.Fatal(err)
}
}
func startPostgres(t *testing.T) string {
ctx := context.Background()
postgresContainer, err := postgres.Run(ctx, "docker.io/postgres:15.2-alpine",
postgres.WithDatabase(testDB),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(5*time.Second)),
)
if err != nil {
t.Fatal(err)
}
dsn, err := postgresContainer.ConnectionString(ctx)
if err != nil {
t.Fatal(err)
}
return strings.TrimSuffix(dsn, testDB+"?")
}
func startMySQL(t *testing.T) string {
ctx := context.Background()
mysqlContainer, err := mysql.Run(ctx, "mysql:8",
mysql.WithUsername("root"),
mysql.WithPassword("password"),
mysql.WithDatabase(testDB),
)
if err != nil {
t.Fatal(err)
}
dsn, err := mysqlContainer.ConnectionString(ctx)
if err != nil {
t.Fatal(err)
}
return strings.TrimSuffix(dsn, testDB)
}