Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suniastar committed Oct 21, 2024
1 parent 2a0d9be commit 335a4b4
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package otelzap
import (
"context"
"fmt"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"testing"
"time"

Expand Down Expand Up @@ -164,6 +165,82 @@ func TestCoreEnabled(t *testing.T) {
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
}

func TestCoreWithCaller(t *testing.T) {
rec := logtest.NewRecorder()
zc := NewCore(loggerName, WithLoggerProvider(rec))

t.Run("WithAddCaller", func(t *testing.T) {
logger := zap.New(zc, zap.AddCaller())
logger.Info(testMessage)
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
assert.Equal(t, 3, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
switch kv.Key {
case string(semconv.CodeFilepathKey):
assert.Contains(t, kv.Value.AsString(), "core_test.go")
case string(semconv.CodeLineNumberKey):
assert.Greater(t, kv.Value.AsInt64(), int64(0))
case string(semconv.CodeFunctionKey):
assert.Contains(t, kv.Value.AsString(), "TestCoreWithCaller")
default:
assert.Fail(t, "unexpected attribute key", kv.Key)
}
return true
})
})

rec.Reset()

t.Run("Default", func(t *testing.T) {
logger := zap.New(zc)
logger.Info(testMessage)
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
assert.Equal(t, 0, got.AttributesLen())
})
}

func TestCoreWithStacktrace(t *testing.T) {
rec := logtest.NewRecorder()
zc := NewCore(loggerName, WithLoggerProvider(rec))
logger := zap.New(zc, zap.AddStacktrace(zapcore.ErrorLevel))

t.Run("Error", func(t *testing.T) {
logger.Error(testMessage)
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityError, got.Severity())
assert.Equal(t, zap.ErrorLevel.String(), got.SeverityText())
assert.Equal(t, 1, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, string(semconv.CodeStacktraceKey), kv.Key)
assert.NotEmpty(t, kv.Value.AsString())
return true
})
})

rec.Reset()

t.Run("Warn", func(t *testing.T) {
logger.Warn(testMessage)
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityWarn, got.Severity())
assert.Equal(t, zap.WarnLevel.String(), got.SeverityText())
assert.Equal(t, 0, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
println(kv.Key)
println(kv.Value.AsString())
return true
})
})
}

func TestNewCoreConfiguration(t *testing.T) {
t.Run("Default", func(t *testing.T) {
r := logtest.NewRecorder()
Expand Down

0 comments on commit 335a4b4

Please sign in to comment.