Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Jan 10, 2024
1 parent 4b032c1 commit 22e45ad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
6 changes: 3 additions & 3 deletions internal/utils/utils.go
Expand Up @@ -80,9 +80,9 @@ func Keys[M ~map[K]V, K comparable, V any](m M) []K {
return r
}

func DedupStringSlice[T string](strings []T) []T {
keys := make(map[T]bool)
var list []T
func DedupStringSlice(strings []string) []string {
keys := make(map[string]bool)
var list []string
for _, item := range strings {
if _, value := keys[item]; !value {
keys[item] = true
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/utils_test.go
Expand Up @@ -15,3 +15,13 @@ func TestObfuscate(t *testing.T) {
assert.Equal(t, "****-text", Obfuscate("test-text", 5))
assert.Equal(t, "****", Obfuscate("test", 6))
}

func TestDedupStringSlice(t *testing.T) {
assert.Equal(t, []string{"a", "b"}, DedupStringSlice([]string{"a", "b", "b", "a"}))
assert.Equal(t, []string{"a", "b"}, DedupStringSlice([]string{"a", "b"}))
}

func TestKeys(t *testing.T) {
assert.Contains(t, Keys(map[string]int{"a": 1, "b": 2}), "a")
assert.Contains(t, Keys(map[string]int{"a": 1, "b": 2}), "b")
}
6 changes: 4 additions & 2 deletions log/logger.go
Expand Up @@ -10,7 +10,7 @@ import (
type Level int

type Logger interface {
GetLevel() configcat.LogLevel // for the SDK
GetConfigCatLevel() configcat.LogLevel // for the SDK

Level() Level

Expand Down Expand Up @@ -82,7 +82,7 @@ func (l *logger) WithPrefix(prefix string) Logger {
}
}

func (l *logger) GetLevel() configcat.LogLevel {
func (l *logger) GetConfigCatLevel() configcat.LogLevel {
switch l.level {
case Debug:
return configcat.LogLevelDebug
Expand All @@ -92,6 +92,8 @@ func (l *logger) GetLevel() configcat.LogLevel {
return configcat.LogLevelWarn
case Error:
return configcat.LogLevelError
case None:
return configcat.LogLevelNone
default:
return configcat.LogLevelWarn
}
Expand Down
27 changes: 27 additions & 0 deletions log/logger_test.go
Expand Up @@ -2,6 +2,8 @@ package log

import (
"bytes"
"fmt"
configcat "github.com/configcat/go-sdk/v9"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -98,4 +100,29 @@ func TestLogger(t *testing.T) {
l.Errorf("error")
l.Reportf("rep")
})
t.Run("debug logger", func(t *testing.T) {
l := NewDebugLogger()
assert.Equal(t, Debug, l.Level())
})
t.Run("sdk loglevel", func(t *testing.T) {
tests := []struct {
level Level
configCatLevel configcat.LogLevel
}{
{Error, configcat.LogLevelError},
{Warn, configcat.LogLevelWarn},
{Info, configcat.LogLevelInfo},
{Debug, configcat.LogLevelDebug},
{None, configcat.LogLevelNone},
{500, configcat.LogLevelWarn},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%v == %v", test.level, test.configCatLevel), func(t *testing.T) {
var out, err bytes.Buffer
l := NewLogger(&err, &out, test.level)
assert.Equal(t, test.configCatLevel, l.GetConfigCatLevel())
})
}
})
}
2 changes: 1 addition & 1 deletion sdk/sdk.go
Expand Up @@ -108,7 +108,7 @@ func NewClient(sdkCtx *Context, log log.Logger) Client {
SDKKey: key,
DataGovernance: configcat.Global,
Logger: sdkLog,
LogLevel: sdkLog.GetLevel(),
LogLevel: sdkLog.GetConfigCatLevel(),
Transport: OverrideUserAgent(transport),
Hooks: &configcat.Hooks{},
}
Expand Down

0 comments on commit 22e45ad

Please sign in to comment.