forked from trussworks/go-sample-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
63 lines (48 loc) · 1.24 KB
/
context_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
package appcontext
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
)
type ContextTestSuite struct {
suite.Suite
logger *zap.Logger
}
func TestContextTestSuite(t *testing.T) {
contextTestSuite := &ContextTestSuite{
Suite: suite.Suite{},
logger: zap.NewNop(),
}
suite.Run(t, contextTestSuite)
}
func (s ContextTestSuite) TestWithLogger() {
ctx := context.Background()
expectedLogger := zap.NewNop()
ctx = WithLogger(ctx, expectedLogger)
logger := ctx.Value(loggerKey).(*zap.Logger)
s.Equal(expectedLogger, logger)
}
func (s ContextTestSuite) TestLogger() {
ctx := context.Background()
expectedLogger := zap.NewNop()
ctx = context.WithValue(ctx, loggerKey, expectedLogger)
logger, ok := Logger(ctx)
s.True(ok)
s.Equal(expectedLogger, logger)
}
func (s ContextTestSuite) TestWithTrace() {
ctx, tID := WithTrace(context.Background())
traceID := ctx.Value(traceKey).(uuid.UUID)
s.NotEqual(uuid.UUID{}, traceID)
s.Equal(tID.String(), traceID.String())
}
func (s ContextTestSuite) TestTrace() {
ctx := context.Background()
expectedID := uuid.New()
ctx = context.WithValue(ctx, traceKey, expectedID)
traceID, ok := Trace(ctx)
s.True(ok)
s.Equal(expectedID, traceID)
}