Skip to content

Commit

Permalink
added tests for logger pkg (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder authored Jul 26, 2024
1 parent 9ad2514 commit 99eead1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetLogLevelByName(logName string) (int, error) {
return 0, err
}

// This doesn't seem to be used anywhere.
// GetLogLevel returns current log level.
func GetLogLevel() int {
return logLevel
Expand Down
70 changes: 70 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package logger_test

import (
"fmt"
"testing"

"github.com/ansible/receptor/pkg/logger"
)

func TestGetLogLevelByName(t *testing.T) {
receptorLogger := logger.NewReceptorLogger("")

testCases := []struct {
name string
error bool
}{
{name: "error"},
{name: "warning"},
{name: "info"},
{name: "debug"},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
_, err := receptorLogger.GetLogLevelByName(testCase.name)
if err != nil {
t.Error(err)
}
})
}
}

func TestGetLogLevelByNameWithError(t *testing.T) {
receptorLogger := logger.NewReceptorLogger("")
_, err := receptorLogger.GetLogLevelByName("does not exist")
if err == nil {
t.Error("should have error")
}
}

func TestLogLevelToName(t *testing.T) {
receptorLogger := logger.NewReceptorLogger("")

testCases := []struct {
level int
}{
{level: 1},
{level: 2},
{level: 3},
{level: 4},
}

for _, testCase := range testCases {
name := fmt.Sprintf("level: %d", testCase.level)
t.Run(name, func(t *testing.T) {
_, err := receptorLogger.LogLevelToName(testCase.level)
if err != nil {
t.Error(err)
}
})
}
}

func TestLogLevelToNameWithError(t *testing.T) {
receptorLogger := logger.NewReceptorLogger("")
_, err := receptorLogger.LogLevelToName(123)
if err == nil {
t.Error("should have error")
}
}

0 comments on commit 99eead1

Please sign in to comment.