167 lines
5.0 KiB
Go
167 lines
5.0 KiB
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.hujye.com/infrastructure/go-web-gin/web"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
// LoggerTestSuite is the test suite for Logger
|
|
type LoggerTestSuite struct {
|
|
suite.Suite
|
|
logger *Logger
|
|
}
|
|
|
|
// SetupSuite runs once before all tests
|
|
func (s *LoggerTestSuite) SetupSuite() {
|
|
s.logger = GetLogger()
|
|
}
|
|
|
|
// TearDownSuite runs once after all tests
|
|
func (s *LoggerTestSuite) TearDownSuite() {
|
|
s.logger.Sync()
|
|
}
|
|
|
|
// SetupTest runs before each test
|
|
func (s *LoggerTestSuite) SetupTest() {
|
|
// Reset logger instance if needed
|
|
}
|
|
|
|
// TearDownTest runs after each test
|
|
func (s *LoggerTestSuite) TearDownTest() {
|
|
// Cleanup after each test if needed
|
|
}
|
|
|
|
// TestGetLogger tests the GetLogger function
|
|
func (s *LoggerTestSuite) TestGetLogger() {
|
|
logger := GetLogger()
|
|
s.NotNil(logger, "GetLogger should return a non-nil logger")
|
|
s.NotNil(logger.logger, "logger field should be initialized")
|
|
}
|
|
|
|
// TestGetLoggerSingleton tests that GetLogger returns the same instance
|
|
func (s *LoggerTestSuite) TestGetLoggerSingleton() {
|
|
logger1 := GetLogger()
|
|
logger2 := GetLogger()
|
|
s.Equal(logger1, logger2, "GetLogger should return the same instance")
|
|
}
|
|
|
|
// TestInfoWithNilContext tests Info method with nil context
|
|
func (s *LoggerTestSuite) TestInfoWithNilContext() {
|
|
s.NotPanics(func() {
|
|
s.logger.Info(nil, "Test info message")
|
|
}, "Info should not panic with nil context")
|
|
}
|
|
|
|
// TestErrorWithNilContext tests Error method with nil context
|
|
func (s *LoggerTestSuite) TestErrorWithNilContext() {
|
|
s.NotPanics(func() {
|
|
s.logger.Error(nil, "Test error message")
|
|
}, "Error should not panic with nil context")
|
|
}
|
|
|
|
// TestDebugWithNilContext tests Debug method with nil context
|
|
func (s *LoggerTestSuite) TestDebugWithNilContext() {
|
|
s.NotPanics(func() {
|
|
s.logger.Debug(nil, "Test debug message")
|
|
}, "Debug should not panic with nil context")
|
|
}
|
|
|
|
// TestWarnWithNilContext tests Warn method with nil context
|
|
func (s *LoggerTestSuite) TestWarnWithNilContext() {
|
|
s.NotPanics(func() {
|
|
s.logger.Warn(nil, "Test warn message")
|
|
}, "Warn should not panic with nil context")
|
|
}
|
|
|
|
// TestInfoWithContext tests Info method with context containing user info
|
|
func (s *LoggerTestSuite) TestInfoWithContext() {
|
|
ctx := context.Background()
|
|
ctx = web.SetUserID(ctx, "test-user-123")
|
|
ctx = web.SetUserName(ctx, "testuser")
|
|
ctx = web.SetTrace(ctx, "trace-test-456")
|
|
ctx = web.SetFromIP(ctx, "192.168.1.1")
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Info(ctx, "Test info with context")
|
|
}, "Info should not panic with valid context")
|
|
}
|
|
|
|
// TestErrorWithContext tests Error method with context containing user info
|
|
func (s *LoggerTestSuite) TestErrorWithContext() {
|
|
ctx := context.Background()
|
|
ctx = web.SetUserID(ctx, "error-user-789")
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Error(ctx, "Test error with context")
|
|
}, "Error should not panic with valid context")
|
|
}
|
|
|
|
// TestInfoWithFormattedMessage tests Info method with formatted message
|
|
func (s *LoggerTestSuite) TestInfoWithFormattedMessage() {
|
|
ctx := context.Background()
|
|
ctx = web.SetUserID(ctx, "format-user-001")
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Info(ctx, "User %s logged in from %s", "admin", "10.0.0.1")
|
|
}, "Info should handle formatted messages correctly")
|
|
}
|
|
|
|
// TestErrorWithFormattedMessage tests Error method with formatted message
|
|
func (s *LoggerTestSuite) TestErrorWithFormattedMessage() {
|
|
s.NotPanics(func() {
|
|
s.logger.Error(nil, "Error code: %d, message: %s", 500, "Internal Server Error")
|
|
}, "Error should handle formatted messages correctly")
|
|
}
|
|
|
|
// TestAllLogLevels tests all log level methods
|
|
func (s *LoggerTestSuite) TestAllLogLevels() {
|
|
ctx := context.Background()
|
|
ctx = web.SetUserID(ctx, "level-test-user")
|
|
ctx = web.SetUserName(ctx, "leveltest")
|
|
ctx = web.SetTrace(ctx, "trace-level-test")
|
|
ctx = web.SetFromIP(ctx, "10.20.30.40")
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Debug(ctx, "Debug message")
|
|
s.logger.Info(ctx, "Info message")
|
|
s.logger.Warn(ctx, "Warn message")
|
|
s.logger.Error(ctx, "Error message")
|
|
// Skip Fatal as it will exit the program
|
|
}, "All log levels should work correctly")
|
|
}
|
|
|
|
// TestEmptyContext tests with empty context (no user info)
|
|
func (s *LoggerTestSuite) TestEmptyContext() {
|
|
ctx := context.Background()
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Info(ctx, "Message with empty context")
|
|
}, "Should handle empty context without user info")
|
|
}
|
|
|
|
// TestPartialUserInfo tests with partial user information
|
|
func (s *LoggerTestSuite) TestPartialUserInfo() {
|
|
ctx := context.Background()
|
|
// Only set user ID, not other fields
|
|
ctx = web.SetUserID(ctx, "partial-user")
|
|
|
|
s.NotPanics(func() {
|
|
s.logger.Info(ctx, "Message with partial user info")
|
|
}, "Should handle partial user information")
|
|
}
|
|
|
|
// TestLoggerSync tests the Sync method
|
|
func (s *LoggerTestSuite) TestLoggerSync() {
|
|
err := s.logger.Sync()
|
|
// Sync may return an error for stderr in test environment, which is acceptable
|
|
s.T().Logf("Sync returned error (may be expected for stderr): %v", err)
|
|
}
|
|
|
|
// TestLoggerRunSuite runs the test suite
|
|
func TestLoggerRunSuite(t *testing.T) {
|
|
suite.Run(t, new(LoggerTestSuite))
|
|
}
|