Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing slog config tests #388

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 113 additions & 31 deletions pkg/config/slog_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,144 @@ package config
import (
"errors"
"testing"

"log/slog"
)

func TestGetSlogLogger(t *testing.T) {
t.Run("Wrong type of log level", func(t *testing.T) {
const LogLevel = "infox"
const LogFormat = defaultLogFormat
logger, err := getSlogLogger(LogLevel, LogFormat)
t.Run("wrong type of log level", func(t *testing.T) {
const logLevel = "infox"
const logFormat = defaultLogFormat
logger, err := getSlogLogger(logLevel, logFormat)
if logger != nil {
t.Errorf("expected logger to be nil, got %s\"", logger)
t.Errorf("expected logger to be nil, got %v", logger)
}
if err.Error() != "slog: level string \"infox\": unknown name" {
t.Errorf("expected error to be '%s', got %s\"", "slog: level string \"infox\": unknown name", err)
if err == nil || err.Error() != "slog: level string \"infox\": unknown name" {
t.Errorf("expected error to be '%s', got %v", "slog: level string \"infox\": unknown name", err)
}
})
t.Run("Wrong type of log level", func(t *testing.T) {
const LogLevel = "warn"
const LogFormat = defaultLogFormat
logger, err := getSlogLogger(LogLevel, LogFormat)

t.Run("correct log level with default format", func(t *testing.T) {
const logLevel = "warn"
const logFormat = defaultLogFormat
logger, err := getSlogLogger(logLevel, logFormat)
if err != nil {
t.Errorf("expected error to be nil, got %s\"", err)
t.Errorf("expected error to be nil, got %v", err)
}
if logger == nil {
t.Errorf("expected logger to be not nil, got %s\"", logger)
t.Errorf("expected logger to be not nil, got %v", logger)
}

verifyLoggerHandler(t, logger, logFormat)
})
t.Run("Wrong type of log format", func(t *testing.T) {
const LogLevel = defaultLogLevel
const LogFormat = "test"
logger, err := getSlogLogger(LogLevel, LogFormat)

t.Run("wrong type of log format", func(t *testing.T) {
const logLevel = defaultLogLevel
const logFormat = "test"
logger, err := getSlogLogger(logLevel, logFormat)
if logger != nil {
t.Errorf("expected logger to be nil, got %s\"", logger)
t.Errorf("expected logger to be nil, got %v", logger)
}
if !errors.Is(err, ErrUnknownLogFormat) {
t.Errorf("expected error to be '%s', got %s\"", ErrUnknownLogFormat, err)
t.Errorf("expected error to be '%v', got %v", ErrUnknownLogFormat, err)
}
})
t.Run("Correct type of log format", func(t *testing.T) {
const LogLevel = defaultLogLevel
const LogFormat = defaultLogFormat
logger, err := getSlogLogger(LogLevel, LogFormat)

t.Run("correct log level and format", func(t *testing.T) {
const logLevel = defaultLogLevel
const logFormat = defaultLogFormat
logger, err := getSlogLogger(logLevel, logFormat)
if err != nil {
t.Errorf("expected error to be nil, got %s\"", err)
t.Errorf("expected error to be nil, got %v", err)
}
if logger == nil {
t.Errorf("expected logger to be not nil, got %s\"", logger)
t.Errorf("expected logger to be not nil, got %v", logger)
}

verifyLoggerHandler(t, logger, logFormat)
})
t.Run("Json type of log format", func(t *testing.T) {
const LogLevel = defaultLogLevel
const LogFormat = "json"
logger, err := getSlogLogger(LogLevel, LogFormat)

t.Run("json log format", func(t *testing.T) {
const logLevel = defaultLogLevel
const logFormat = "json"
logger, err := getSlogLogger(logLevel, logFormat)
if err != nil {
t.Errorf("expected error to be nil, got %s\"", err)
t.Errorf("expected error to be nil, got %v", err)
}
if logger == nil {
t.Errorf("expected logger to be not nil, got %s\"", logger)
t.Errorf("expected logger to be not nil, got %v", logger)
}

verifyLoggerHandler(t, logger, logFormat)
})
}

func verifyLoggerHandler(t *testing.T, logger *slog.Logger, expectedFormat string) {
handler := logger.Handler()
if handler == nil {
t.Errorf("expected handler to be not nil, got nil")
}

switch h := handler.(type) {
case *slog.TextHandler:
if expectedFormat != LogFormatText {
t.Errorf("expected TextHandler for format %s, got %T", expectedFormat, h)
}
case *slog.JSONHandler:
if expectedFormat != LogFormatJSON {
t.Errorf("expected JSONHandler for format %s, got %T", expectedFormat, h)
}
default:
t.Errorf("unexpected handler type: %T", h)
}
}

func TestSetupSlog(t *testing.T) {
t.Run("valid config", func(t *testing.T) {
cfg := &Config{
Logging: LoggingConfig{
Level: "info",
Format: "text",
},
}

err := SetupSlog(cfg)
if err != nil {
t.Errorf("expected error to be nil, got %v", err)
}

// Verify that the default logger is set
defaultLogger := slog.Default()
if defaultLogger == nil {
t.Errorf("expected default logger to be not nil, got nil")
}
})

t.Run("invalid log level", func(t *testing.T) {
cfg := &Config{
Logging: LoggingConfig{
Level: "invalid-level",
Format: "text",
},
}

err := SetupSlog(cfg)
if err == nil {
t.Errorf("expected error, got nil")
}
})

t.Run("invalid log format", func(t *testing.T) {
cfg := &Config{
Logging: LoggingConfig{
Level: "info",
Format: "invalid-format",
},
}

err := SetupSlog(cfg)
if err == nil || !errors.Is(err, ErrUnknownLogFormat) {
t.Errorf("expected error '%v', got %v", ErrUnknownLogFormat, err)
}
})
}
Loading