Skip to content

Commit

Permalink
Add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
komer3 committed Jan 13, 2025
1 parent d3152f0 commit 6092d19
Showing 1 changed file with 195 additions and 0 deletions.
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 6092d19

Please sign in to comment.