logger/logger_test.go
2024-08-09 14:25:34 -06:00

78 lines
2.6 KiB
Go

package logger
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"os"
"testing"
)
func TestAppConfigSuite(t *testing.T) {
suite.Run(t, new(LoggingTestSuite))
}
type LoggingTestSuite struct{ suite.Suite }
func (suite *LoggingTestSuite) SetupSuite() {
SetLogger(Testing)
}
func (suite *LoggingTestSuite) TearDownSuite() {
AtExit.Cleanup()
}
func (suite *LoggingTestSuite) Test_DebugOnError() {
var err error
DebugOnError(fmt.Errorf("0001: Simple DEBUG test log message"))
assert.FileExists(suite.T(), "./logs/debug_test", "Logger did not create output file")
b, err := os.ReadFile("./logs/debug_test")
assert.NoError(suite.T(), err, "Error Reading Log File")
assert.Contains(suite.T(), string(b), "0001: Simple DEBUG test log message", "Log does not contain expected message")
}
func (suite *LoggingTestSuite) Test_InfoOnError() {
var err error
InfoOnError(fmt.Errorf("0002: Simple INFO test log message"))
assert.FileExists(suite.T(), "./logs/debug_test", "Logger did not create output file")
b, err := os.ReadFile("./logs/debug_test")
assert.NoError(suite.T(), err, "Error Reading Log File")
assert.Contains(suite.T(), string(b), "0002: Simple INFO test log message", "Log does not contain expected message")
}
func (suite *LoggingTestSuite) Test_WarnOnError() {
var err error
WarnOnError(fmt.Errorf("0003: Simple WARN test log message"))
assert.FileExists(suite.T(), "./logs/debug_test", "Logger did not create output file")
b, err := os.ReadFile("./logs/debug_test")
assert.NoError(suite.T(), err, "Error Reading Log File")
assert.Contains(suite.T(), string(b), "0003: Simple WARN test log message", "Log does not contain expected message")
}
func (suite *LoggingTestSuite) Test_ErrorOnError() {
var err error
ErrorOnError(fmt.Errorf("0001: Simple ERROR test log message"))
assert.FileExists(suite.T(), "./logs/debug_test", "Logger did not create output file")
b, err := os.ReadFile("./logs/debug_test")
assert.NoError(suite.T(), err, "Error Reading Log File")
assert.Contains(suite.T(), string(b), "0001: Simple ERROR test log message", "Log does not contain expected message")
}
func (suite *LoggingTestSuite) Test_DebugWithVariables() {
Logger.Debug("0010 DEBUG test with data", zap.String("Field1", "Sample Text"), zap.Int("Field2", 1500))
assert.FileExists(suite.T(), "./logs/debug_test", "Logger did not create output file")
b, err := os.ReadFile("./logs/debug_test")
assert.NoError(suite.T(), err, "Error Reading Log File")
assert.Contains(suite.T(), string(b), "0010 DEBUG test with data\t{\"Field1\": \"Sample Text\", \"Field2\": 1500}", "Log does not contain expected message")
}