Refactor the code to use more modular and testable approach.

This commit is contained in:
2025-02-25 19:11:19 +00:00
parent 5964da3cef
commit 942e648d56
18 changed files with 1719 additions and 595 deletions
+53
View File
@@ -0,0 +1,53 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInitLogger(t *testing.T) {
// Test with debug mode enabled
logger := InitLogger(true)
assert.NotNil(t, logger, "Logger should not be nil")
assert.NotNil(t, Logger, "Global logger should not be nil")
// Test with debug mode disabled
logger = InitLogger(false)
assert.NotNil(t, logger, "Logger should not be nil")
assert.NotNil(t, Logger, "Global logger should not be nil")
}
func TestLoggingFunctions(t *testing.T) {
// Initialize logger with debug mode
InitLogger(true)
// Just test that these don't panic
Debug("Debug message", map[string]interface{}{"key": "value"})
Info("Info message", map[string]interface{}{"key": "value"})
Error("Error message", map[string]interface{}{"key": "value"})
// Skip testing Critical as it might call os.Exit
// Critical("Critical message", map[string]interface{}{"key": "value"})
// Test passes if we get here without panicking
assert.True(t, true)
}
func TestLoggingWithNilLogger(t *testing.T) {
// Temporarily set logger to nil
oldLogger := Logger
Logger = nil
defer func() { Logger = oldLogger }()
// These should not panic
Debug("Debug message", map[string]interface{}{"key": "value"})
Info("Info message", map[string]interface{}{"key": "value"})
Error("Error message", map[string]interface{}{"key": "value"})
// Skip testing Critical as it might call os.Exit
// Critical("Critical message", map[string]interface{}{"key": "value"})
// Test passes if we get here without panicking
assert.True(t, true)
}