-
Notifications
You must be signed in to change notification settings - Fork 1
/
initial.go
50 lines (41 loc) · 908 Bytes
/
initial.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
package tests
import (
"database/sql"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/sqlite3"
_ "github.com/mattn/go-sqlite3"
)
var testDBName = "test.db"
var testDBType = "sqlite3"
func init() {
// Recreate test.db file
if _, err := os.Stat(testDBName); err == nil {
if err = os.Remove(testDBName); err != nil {
log.Fatalln(err)
}
}
_, err := os.Create(testDBName)
if err != nil {
log.Fatalln(err)
}
// Up migrations
err = exec.Command("sql-migrate", "up", "-config=dbtest.yml").Run()
if err != nil {
log.Fatalln(err)
}
}
// Create sqlite3 connection
func New() *goqu.Database {
dialect := goqu.Dialect(strings.ToLower(testDBType))
config := fmt.Sprintf("file:%s?cache=shared&mode=rw", testDBName)
c, err := sql.Open(testDBType, config)
if err != nil {
log.Fatalln(err)
}
return dialect.DB(c)
}