-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
74 lines (67 loc) · 1.34 KB
/
main_test.go
File metadata and controls
74 lines (67 loc) · 1.34 KB
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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
)
func TestMain(m *testing.M) {
Db = *setUpTestDb()
log_file := SetUpTestLog()
defer log_file.Close()
destroyTestTables()
createTestTables()
exitVal := m.Run()
os.Exit(exitVal)
}
func setUpTestDb() (db *sqlx.DB) {
DbUrl = fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
os.Getenv("TL_DB_USER"),
os.Getenv("TL_DB_PASS"),
os.Getenv("TL_DB_HOST"),
"25060",
"testwebapp",
)
db, _ = sqlx.Connect("postgres", DbUrl)
return
}
func createTestTables() {
query, err := ioutil.ReadFile("db/sql/test/create_tables.sql")
if err != nil {
panic(err)
}
if _, err := Db.Exec(string(query)); err != nil {
panic(err)
}
}
func destroyTestTables() {
query, err := ioutil.ReadFile("db/sql/test/destroy_tables.sql")
if err != nil {
panic(err)
}
if _, err := Db.Exec(string(query)); err != nil {
panic(err)
}
}
func SetUpTestLog() (file *os.File) {
file, err := os.OpenFile(
"logs_test.log",
os.O_APPEND|os.O_CREATE|os.O_RDWR,
0o666,
)
if err != nil {
log.WithFields(log.Fields{
"custom_msg": "Failed setting up log file",
}).Error(err)
return
}
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.JSONFormatter{})
// log.SetOutput(io.MultiWriter(file, os.Stdout))
log.SetOutput(io.MultiWriter(file))
return
}