Skip to content

Commit

Permalink
[test] Add unit test cases for Sentry (#294)
Browse files Browse the repository at this point in the history
* Add unit test cases

* Update the Makefile to include ./sentry dir
  • Loading branch information
komer3 authored Jan 14, 2025
1 parent 0b6ab0d commit ec8a70a
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fmt:
.PHONY: test
# we say code is not worth testing unless it's formatted
test: fmt codegen
go test -v -cover -coverprofile ./coverage.out ./cloud/... $(TEST_ARGS)
go test -v -cover -coverprofile ./coverage.out ./cloud/... ./sentry/... $(TEST_ARGS)

.PHONY: build-linux
build-linux: codegen
Expand Down
195 changes: 195 additions & 0 deletions sentry/sentry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package sentry

import (
"context"
"testing"

"github.com/getsentry/sentry-go"
"github.com/stretchr/testify/assert"
)

func TestInitialize(t *testing.T) {
// Reset the initialized flag before each test
initialized = false

tests := []struct {
name string
dsn string
environment string
release string
wantErr bool
}{
{
name: "successful initialization",
dsn: "https://test@sentry.io/123",
environment: "test",
release: "1.0.0",
wantErr: false,
},
{
name: "empty DSN",
dsn: "",
environment: "test",
release: "1.0.0",
wantErr: true,
},
{
name: "double initialization",
dsn: "https://test@sentry.io/123",
environment: "test",
release: "1.0.0",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Initialize(tt.dsn, tt.environment, tt.release)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.True(t, initialized)
}
})
}
}

func TestSetHubOnContext(t *testing.T) {
// Reset the initialized flag
initialized = false
_ = Initialize("https://test@sentry.io/123", "test", "1.0.0")

ctx := context.Background()
newCtx := SetHubOnContext(ctx)

assert.True(t, sentry.HasHubOnContext(newCtx))
assert.NotNil(t, sentry.GetHubFromContext(newCtx))
}

func TestGetHubFromContext(t *testing.T) {
tests := []struct {
name string
setupFunc func() context.Context
initialized bool
wantNil bool
}{
{
name: "valid hub in context",
setupFunc: func() context.Context {
ctx := context.Background()
return SetHubOnContext(ctx)
},
initialized: true,
wantNil: false,
},
{
name: "no hub in context",
setupFunc: func() context.Context {
return context.Background()
},
initialized: true,
wantNil: true,
},
{
name: "sentry not initialized",
setupFunc: func() context.Context {
return context.Background()
},
initialized: false,
wantNil: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset the initialized flag
initialized = false
if tt.initialized {
_ = Initialize("https://test@sentry.io/123", "test", "1.0.0")
}

ctx := tt.setupFunc()
hub := getHubFromContext(ctx)

if tt.wantNil {
assert.Nil(t, hub)
} else {
assert.NotNil(t, hub)
}
})
}
}

func TestSetTag(t *testing.T) {
// Reset the initialized flag
initialized = false
_ = Initialize("https://test@sentry.io/123", "test", "1.0.0")

tests := []struct {
name string
setupFunc func() context.Context
key string
value string
}{
{
name: "set tag with valid hub",
setupFunc: func() context.Context {
return SetHubOnContext(context.Background())
},
key: "test-key",
value: "test-value",
},
{
name: "set tag with no hub",
setupFunc: func() context.Context {
return context.Background()
},
key: "test-key",
value: "test-value",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.setupFunc()
// This should not panic
SetTag(ctx, tt.key, tt.value)
})
}
}

func TestCaptureError(t *testing.T) {
// Reset the initialized flag
initialized = false
_ = Initialize("https://test@sentry.io/123", "test", "1.0.0")

tests := []struct {
name string
setupFunc func() context.Context
err error
}{
{
name: "capture error with valid hub",
setupFunc: func() context.Context {
return SetHubOnContext(context.Background())
},
err: assert.AnError,
},
{
name: "capture error with no hub",
setupFunc: func() context.Context {
return context.Background()
},
err: assert.AnError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.setupFunc()
// This should not panic
CaptureError(ctx, tt.err)
})
}
}

0 comments on commit ec8a70a

Please sign in to comment.