-
Notifications
You must be signed in to change notification settings - Fork 23
/
main_test.go
66 lines (57 loc) · 1.17 KB
/
main_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
package gormgen
import (
"io/ioutil"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/suite"
)
//go:generate gormgen -structs BasicModel,ComplexModel -output structs_test.go
var modelRegistry = []interface{}{
BasicModel{},
ComplexModel{},
}
type MainTestSuite struct {
suite.Suite
DB *gorm.DB
testDBFileName string
}
func (m *MainTestSuite) migrateModels() {
db := m.getDBConn()
for _, m := range modelRegistry {
db.AutoMigrate(m)
}
}
func (m *MainTestSuite) SetupTest() {
f, err := ioutil.TempFile("", "test.db")
if err != nil {
log.Fatalf("Couldn't create temp sqllite database: %v", err.Error())
}
m.testDBFileName = f.Name()
m.migrateModels()
}
func (m *MainTestSuite) TearDownTest() {
m.DB.Close()
m.DB = nil
err := os.Remove(m.testDBFileName)
if err != nil {
log.Fatalf("Couldn't delete temp sqllite database: %v", err.Error())
}
}
func (m *MainTestSuite) getDBConn() *gorm.DB {
if m.DB == nil {
db, err := gorm.Open("sqlite3", m.testDBFileName)
if err != nil {
panic(err)
}
m.DB = db
}
return m.DB
}
func TestMain(m *testing.M) {
rand.Seed(time.Now().UnixNano())
os.Exit(m.Run())
}