-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorm_test.go
85 lines (79 loc) · 2.17 KB
/
gorm_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package dbx
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/random"
otelTrace "go.opentelemetry.io/otel/trace"
"os"
"testing"
"github.com/uptrace/opentelemetry-go-extra/otelgorm"
"github.com/uptrace/uptrace-go/uptrace"
"go.opentelemetry.io/otel"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type simpleTable struct {
Key string `json:"key"`
Value string `json:"value"`
}
func TestOpel(t *testing.T) {
ctx := context.Background()
opelDSN := os.Getenv("TRACE_DSN")
if opelDSN == "" {
t.Error("openDSN not set")
t.FailNow()
}
uptrace.ConfigureOpentelemetry(
uptrace.WithDSN(opelDSN),
uptrace.WithServiceName("GormHelper"),
uptrace.WithServiceVersion("test"),
uptrace.WithDeploymentEnvironment("test"),
)
defer uptrace.Shutdown(ctx)
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
t.Errorf("failed to init db: %s", err.Error())
t.FailNow()
}
if err := db.Use(otelgorm.NewPlugin()); err != nil {
t.Errorf("failed to init otel plguin: %s", err.Error())
t.FailNow()
}
tracer := otel.Tracer("GormHelper-TestOpel@test")
mainCtx, mainTracer := tracer.Start(ctx, "TestOpel")
defer mainTracer.End()
var ch otelTrace.Span
var chCtx context.Context
chCtx, ch = tracer.Start(mainCtx, "db init")
if err := db.WithContext(chCtx).AutoMigrate(&simpleTable{}); err != nil {
errMsg := fmt.Errorf("failed to migrate db: %s", err.Error())
t.Errorf(errMsg.Error())
ch.RecordError(err)
ch.End()
t.FailNow()
}
ch.End()
chCtx, ch = tracer.Start(mainCtx, "data write")
if err := db.WithContext(chCtx).Create(&simpleTable{
Key: "testingKey",
Value: random.RandString(32),
}).Error; err != nil {
errMsg := fmt.Errorf("failed to write data: %s", err.Error())
t.Errorf(errMsg.Error())
ch.RecordError(err)
ch.End()
t.FailNow()
}
ch.End()
chCtx, ch = tracer.Start(mainCtx, "data read")
var readData simpleTable
if err := db.WithContext(chCtx).Where("key = ?", "testingKey").Take(&readData).Error; err != nil {
errMsg := fmt.Errorf("failed to read data: %s", err.Error())
t.Errorf(errMsg.Error())
ch.RecordError(err)
ch.End()
t.FailNow()
}
t.Logf("testingKey value: %s", readData.Value)
ch.End()
}