From 9678eb2677a09de7f2062e612f23e14ec6076365 Mon Sep 17 00:00:00 2001 From: zzzgydi Date: Thu, 4 Apr 2024 10:20:19 +0800 Subject: [PATCH] chore: improve test coverage --- logger_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/logger_test.go b/logger_test.go index cb4e2c8..473a79a 100644 --- a/logger_test.go +++ b/logger_test.go @@ -48,6 +48,9 @@ func Test_logger_Enabled(t *testing.T) { l := New(WithHandler(slog.NewTextHandler(buffer, &slog.HandlerOptions{Level: leveler}))) leveler.Set(slog.LevelWarn) + l.Info(nil, "an info message") + assert.Equal(t, 0, buffer.Len()) + l.Info(context.Background(), "an info message") assert.Equal(t, 0, buffer.Len()) @@ -55,6 +58,32 @@ func Test_logger_Enabled(t *testing.T) { assert.Greater(t, buffer.Len(), 0) } +func Test_logger_Trace_Enabled(t *testing.T) { + buffer := bytes.NewBuffer(nil) + leveler := &slog.LevelVar{} + l := New( + WithHandler(slog.NewTextHandler(buffer, &slog.HandlerOptions{Level: leveler})), + WithSlowThreshold(10*time.Second), + WithTraceAll(), + ) + + fc := func() (string, int64) { + return "SELECT * FROM user", 1 + } + + leveler.Set(slog.LevelWarn) + l.Trace(context.Background(), time.Now().Add(-1*time.Second), fc, nil) + assert.Equal(t, 0, buffer.Len()) + + leveler.Set(slog.LevelError) + l.Trace(context.Background(), time.Now().Add(-1*time.Minute), fc, nil) + assert.Equal(t, 0, buffer.Len()) + + leveler.Set(slog.Level(42)) + l.Trace(context.Background(), time.Now().Add(-1*time.Minute), fc, fmt.Errorf("awesome error")) + assert.Equal(t, 0, buffer.Len()) +} + func Test_logger_LogMode(t *testing.T) { l := logger{gormLevel: gormlogger.Info} actual := l.LogMode(gormlogger.Info)