-
Notifications
You must be signed in to change notification settings - Fork 0
/
testenv_test.go
52 lines (43 loc) · 969 Bytes
/
testenv_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
package testenv_test
import (
"testing"
"github.com/theplant/testenv"
"gorm.io/gorm"
)
type TestModel struct {
gorm.Model
Description string
}
var db *gorm.DB
func TestMain(m *testing.M) {
env, err := testenv.New().DBEnable(true).SetUp()
if err != nil {
panic(err)
}
defer env.TearDown()
// some initialization
db = env.DB
if err = db.AutoMigrate(&TestModel{}); err != nil {
panic(err)
}
m.Run()
}
func TestSelectVersion(t *testing.T) {
var version string
if err := db.Raw("SELECT version()").Scan(&version).Error; err != nil {
t.Fatal(err)
}
t.Logf("current database version: %q", version)
}
func TestSetupTestEnv(t *testing.T) {
// If you don't want to initialize in TestMain
env, err := testenv.New().DBEnable(true).SetUpWithT(t)
if err != nil {
t.Fatal(err)
}
var version string
if err := env.DB.Raw("SELECT version()").Scan(&version).Error; err != nil {
t.Fatal(err)
}
t.Logf("current database version: %q", version)
}