Skip to content

Commit 45cda78

Browse files
committed
🧪 Test: add test to logger package
1 parent 1446e80 commit 45cda78

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

pkg/log/logger_test.go

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,60 @@ import (
99
)
1010

1111
func TestSetLogLevel(t *testing.T) {
12-
err := SetLogLevel("info")
13-
assert.NoError(t, err)
14-
assert.Equal(t, logrus.InfoLevel, logger.Level)
15-
16-
err = SetLogLevel("debug")
17-
assert.NoError(t, err)
18-
assert.Equal(t, logrus.DebugLevel, logger.Level)
19-
20-
err = SetLogLevel("invalid")
21-
assert.Error(t, err)
12+
tests := []struct {
13+
level string
14+
expected logrus.Level
15+
expectError bool
16+
}{
17+
{"info", logrus.InfoLevel, false},
18+
{"debug", logrus.DebugLevel, false},
19+
{"invalid", logrus.InfoLevel, true}, // Mantiene el nivel anterior si es inválido
20+
}
21+
22+
for _, tt := range tests {
23+
t.Run(tt.level, func(t *testing.T) {
24+
err := SetLogLevel(tt.level)
25+
if tt.expectError {
26+
assert.Error(t, err)
27+
} else {
28+
assert.NoError(t, err)
29+
assert.Equal(t, tt.expected, logger.Level)
30+
}
31+
})
32+
}
2233
}
2334

2435
func TestInfoLog(t *testing.T) {
25-
var buf bytes.Buffer
26-
logger.SetOutput(&buf)
27-
28-
SetLogLevel("info")
29-
30-
Info("Info message")
31-
assert.Contains(t, buf.String(), "Info message")
32-
33-
buf.Reset()
34-
35-
Info("Info with fields", Fields{"myKey": "myValue"})
36-
assert.Contains(t, buf.String(), "Info with fields")
37-
assert.Contains(t, buf.String(), "myKey=myValue")
36+
t.Run("Without Fields", func(t *testing.T) {
37+
var buf bytes.Buffer
38+
originalOutput := logger.Out
39+
defer func() { logger.SetOutput(originalOutput) }()
40+
logger.SetOutput(&buf)
41+
42+
SetLogLevel("info")
43+
44+
Info("Info message")
45+
assert.Contains(t, buf.String(), "Info message")
46+
})
47+
48+
t.Run("With Fields", func(t *testing.T) {
49+
var buf bytes.Buffer
50+
originalOutput := logger.Out
51+
defer func() { logger.SetOutput(originalOutput) }()
52+
logger.SetOutput(&buf)
53+
54+
SetLogLevel("info")
55+
56+
Info("Info with fields", Fields{"myKey": "myValue"})
57+
assert.Contains(t, buf.String(), "Info with fields")
58+
assert.Contains(t, buf.String(), "myKey=myValue")
59+
})
3860
}
3961

4062
func TestDebugLog(t *testing.T) {
4163
var buf bytes.Buffer
64+
originalOutput := logger.Out
65+
defer func() { logger.SetOutput(originalOutput) }()
4266
logger.SetOutput(&buf)
4367

4468
SetLogLevel("debug")
@@ -49,6 +73,8 @@ func TestDebugLog(t *testing.T) {
4973

5074
func TestLogWithFields(t *testing.T) {
5175
var buf bytes.Buffer
76+
originalOutput := logger.Out
77+
defer func() { logger.SetOutput(originalOutput) }()
5278
logger.SetOutput(&buf)
5379

5480
SetLogLevel("info")
@@ -60,12 +86,13 @@ func TestLogWithFields(t *testing.T) {
6086

6187
func TestLogLevelFiltering(t *testing.T) {
6288
var buf bytes.Buffer
89+
originalOutput := logger.Out
90+
defer func() { logger.SetOutput(originalOutput) }()
6391
logger.SetOutput(&buf)
6492

6593
SetLogLevel("warning")
6694

6795
Info("This info message should not appear")
68-
6996
Warn("This warning message should appear")
7097

7198
assert.NotContains(t, buf.String(), "This info message should not appear")

0 commit comments

Comments
 (0)