Skip to content

Commit ec8a70a

Browse files
authored
[test] Add unit test cases for Sentry (#294)
* Add unit test cases * Update the Makefile to include ./sentry dir
1 parent 0b6ab0d commit ec8a70a

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fmt:
7474
.PHONY: test
7575
# we say code is not worth testing unless it's formatted
7676
test: fmt codegen
77-
go test -v -cover -coverprofile ./coverage.out ./cloud/... $(TEST_ARGS)
77+
go test -v -cover -coverprofile ./coverage.out ./cloud/... ./sentry/... $(TEST_ARGS)
7878

7979
.PHONY: build-linux
8080
build-linux: codegen

sentry/sentry_test.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package sentry
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/getsentry/sentry-go"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestInitialize(t *testing.T) {
12+
// Reset the initialized flag before each test
13+
initialized = false
14+
15+
tests := []struct {
16+
name string
17+
dsn string
18+
environment string
19+
release string
20+
wantErr bool
21+
}{
22+
{
23+
name: "successful initialization",
24+
dsn: "https://[email protected]/123",
25+
environment: "test",
26+
release: "1.0.0",
27+
wantErr: false,
28+
},
29+
{
30+
name: "empty DSN",
31+
dsn: "",
32+
environment: "test",
33+
release: "1.0.0",
34+
wantErr: true,
35+
},
36+
{
37+
name: "double initialization",
38+
dsn: "https://[email protected]/123",
39+
environment: "test",
40+
release: "1.0.0",
41+
wantErr: true,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
err := Initialize(tt.dsn, tt.environment, tt.release)
48+
if tt.wantErr {
49+
assert.Error(t, err)
50+
} else {
51+
assert.NoError(t, err)
52+
assert.True(t, initialized)
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestSetHubOnContext(t *testing.T) {
59+
// Reset the initialized flag
60+
initialized = false
61+
_ = Initialize("https://[email protected]/123", "test", "1.0.0")
62+
63+
ctx := context.Background()
64+
newCtx := SetHubOnContext(ctx)
65+
66+
assert.True(t, sentry.HasHubOnContext(newCtx))
67+
assert.NotNil(t, sentry.GetHubFromContext(newCtx))
68+
}
69+
70+
func TestGetHubFromContext(t *testing.T) {
71+
tests := []struct {
72+
name string
73+
setupFunc func() context.Context
74+
initialized bool
75+
wantNil bool
76+
}{
77+
{
78+
name: "valid hub in context",
79+
setupFunc: func() context.Context {
80+
ctx := context.Background()
81+
return SetHubOnContext(ctx)
82+
},
83+
initialized: true,
84+
wantNil: false,
85+
},
86+
{
87+
name: "no hub in context",
88+
setupFunc: func() context.Context {
89+
return context.Background()
90+
},
91+
initialized: true,
92+
wantNil: true,
93+
},
94+
{
95+
name: "sentry not initialized",
96+
setupFunc: func() context.Context {
97+
return context.Background()
98+
},
99+
initialized: false,
100+
wantNil: true,
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
// Reset the initialized flag
107+
initialized = false
108+
if tt.initialized {
109+
_ = Initialize("https://[email protected]/123", "test", "1.0.0")
110+
}
111+
112+
ctx := tt.setupFunc()
113+
hub := getHubFromContext(ctx)
114+
115+
if tt.wantNil {
116+
assert.Nil(t, hub)
117+
} else {
118+
assert.NotNil(t, hub)
119+
}
120+
})
121+
}
122+
}
123+
124+
func TestSetTag(t *testing.T) {
125+
// Reset the initialized flag
126+
initialized = false
127+
_ = Initialize("https://[email protected]/123", "test", "1.0.0")
128+
129+
tests := []struct {
130+
name string
131+
setupFunc func() context.Context
132+
key string
133+
value string
134+
}{
135+
{
136+
name: "set tag with valid hub",
137+
setupFunc: func() context.Context {
138+
return SetHubOnContext(context.Background())
139+
},
140+
key: "test-key",
141+
value: "test-value",
142+
},
143+
{
144+
name: "set tag with no hub",
145+
setupFunc: func() context.Context {
146+
return context.Background()
147+
},
148+
key: "test-key",
149+
value: "test-value",
150+
},
151+
}
152+
153+
for _, tt := range tests {
154+
t.Run(tt.name, func(t *testing.T) {
155+
ctx := tt.setupFunc()
156+
// This should not panic
157+
SetTag(ctx, tt.key, tt.value)
158+
})
159+
}
160+
}
161+
162+
func TestCaptureError(t *testing.T) {
163+
// Reset the initialized flag
164+
initialized = false
165+
_ = Initialize("https://[email protected]/123", "test", "1.0.0")
166+
167+
tests := []struct {
168+
name string
169+
setupFunc func() context.Context
170+
err error
171+
}{
172+
{
173+
name: "capture error with valid hub",
174+
setupFunc: func() context.Context {
175+
return SetHubOnContext(context.Background())
176+
},
177+
err: assert.AnError,
178+
},
179+
{
180+
name: "capture error with no hub",
181+
setupFunc: func() context.Context {
182+
return context.Background()
183+
},
184+
err: assert.AnError,
185+
},
186+
}
187+
188+
for _, tt := range tests {
189+
t.Run(tt.name, func(t *testing.T) {
190+
ctx := tt.setupFunc()
191+
// This should not panic
192+
CaptureError(ctx, tt.err)
193+
})
194+
}
195+
}

0 commit comments

Comments
 (0)