Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Nov 26, 2023
1 parent b834b2b commit 7c5ba2f
Showing 4 changed files with 77 additions and 4 deletions.
4 changes: 2 additions & 2 deletions append_handler_test.go
Original file line number Diff line number Diff line change
@@ -125,10 +125,10 @@ func TestAppendHandler_CaseInsensitiveKeepIfBuiltinConflict(t *testing.T) {
t.Parallel()

tester := &testHandler{}
h := NewAppendHandler(tester, &AppendHandlerOptions{
h := NewAppendMiddleware(&AppendHandlerOptions{
KeyCompare: CaseInsensitiveCmp,
ResolveBuiltinKeyConflict: KeepIfBuiltinKeyConflict,
})
})(tester)

log := slog.New(h)
log.Info("case insenstive, keep builtin conflict", "arg1", "val1", "ARG1", "val2", slog.MessageKey, "builtin-conflict")
35 changes: 35 additions & 0 deletions ignore_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slogdedup

import (
"log/slog"
"strings"
"testing"
)
@@ -51,3 +52,37 @@ func TestIgnoreHandler(t *testing.T) {

checkRecordForDuplicates(t, tester.Record)
}

func TestIgnoreHandler_ResolveBuiltinKeyConflict(t *testing.T) {
t.Parallel()

tester := &testHandler{}
h := NewIgnoreMiddleware(&IgnoreHandlerOptions{
ResolveBuiltinKeyConflict: func(k string) (string, bool) {
if k == "time" {
return "", false
} else {
return "arg-" + k, true
}
},
})(tester)

slog.New(h).Info("main message", "time", "hello", "foo", "bar")

jBytes, err := tester.MarshalJSON()
if err != nil {
t.Errorf("Unable to marshal json: %v", err)
}
jStr := strings.TrimSpace(string(jBytes))

expected := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","arg-foo":"bar"}`
if jStr != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr)
}

// Uncomment to see the results
// t.Error(jStr)
// t.Error(tester.String())

checkRecordForDuplicates(t, tester.Record)
}
38 changes: 38 additions & 0 deletions increment_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slogdedup

import (
"log/slog"
"strconv"
"strings"
"testing"
)
@@ -90,3 +92,39 @@ func TestIncrementHandler(t *testing.T) {

checkRecordForDuplicates(t, tester.Record)
}

func TestIncrementHandler_DoesKeyConflict_IncrementKeyName(t *testing.T) {
t.Parallel()

tester := &testHandler{}
h := NewIncrementMiddleware(&IncrementHandlerOptions{
DoesBuiltinKeyConflict: func(key string) bool {
if key == "foo" {
return true
}
return false
},
IncrementKeyName: func(key string, index int) string {
return key + "@" + strconv.Itoa(index)
},
})(tester)

slog.New(h).Info("main message", "foo", "bar")

jBytes, err := tester.MarshalJSON()
if err != nil {
t.Errorf("Unable to marshal json: %v", err)
}
jStr := strings.TrimSpace(string(jBytes))

expected := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","foo@1":"bar"}`
if jStr != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr)
}

// Uncomment to see the results
// t.Error(jStr)
// t.Error(tester.String())

checkRecordForDuplicates(t, tester.Record)
}
4 changes: 2 additions & 2 deletions overwrite_handler_test.go
Original file line number Diff line number Diff line change
@@ -88,10 +88,10 @@ func TestOverwriteHandler_CaseInsensitiveDropBuiltinConflicts(t *testing.T) {
t.Parallel()

tester := &testHandler{}
h := NewOverwriteHandler(tester, &OverwriteHandlerOptions{
h := NewOverwriteMiddleware(&OverwriteHandlerOptions{
KeyCompare: CaseInsensitiveCmp,
ResolveBuiltinKeyConflict: DropIfBuiltinKeyConflict,
})
})(tester)

log := slog.New(h)
log.Info("case insenstive, drop builtin conflict", "arg1", "val1", "ARG1", "val2", slog.MessageKey, "builtin-conflict")

0 comments on commit 7c5ba2f

Please sign in to comment.