forked from e421083458/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
executable file
·54 lines (46 loc) · 1.05 KB
/
logger_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
package gorm_test
import (
"crypto/md5"
"fmt"
"log"
"os"
"testing"
"time"
"github.com/brianwong1861/gorm"
)
func TestLoggerCtx(t *testing.T) {
DB.SetLogger(gorm.Logger{log.New(os.Stdout, "\r\n", 0)})
if debug := os.Getenv("DEBUG"); debug == "true" {
DB.LogMode(true)
} else if debug == "false" {
DB.LogMode(false)
}
if logCtx := os.Getenv("LOGCTX"); logCtx == "true" {
DB.LogCtx(true)
} else if logCtx == "false" {
DB.LogCtx(false)
}
i := 0
for i < 10 {
i++
//Generating context information
unixTime := fmt.Sprint(time.Now().Unix())
traceID := fmt.Sprintf("%x", md5.Sum([]byte(unixTime)))
ctxInfo := "\n[context] trace_id=" + traceID
builder := DB.SetCtx(ctxInfo)
if i > 5 {
builder = builder.Where("Age = ?", i)
} else {
builder = builder.Where("Name = ?", i)
}
if builder.Find(&User{}).Error == nil {
t.Errorf("Should got error with invalid SQL")
}
//Verify context information
ctxTmp, _ := builder.GetCtx()
ctxInfo2, _ := ctxTmp.(string)
if ctxInfo != ctxInfo2 {
t.Fatal("get context error")
}
}
}