Skip to content

Commit

Permalink
Removing redundant tests; Cleaning and reorganizing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-GV committed Apr 12, 2022
1 parent b919a4b commit 231b64f
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 718 deletions.
17 changes: 0 additions & 17 deletions internal/sink/fake_encoder_test.go

This file was deleted.

96 changes: 96 additions & 0 deletions internal/sink/line_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package sink_test

import (
"fmt"
"testing"

"github.com/ViaQ/logerr/v2/kverrors"
"github.com/ViaQ/logerr/v2/internal/sink"
"github.com/stretchr/testify/require"
)

func TestLine_ProductionLogs(t *testing.T) {
// Logs with a level of 0 or 1 are considered to be production level logs
msg := "hello, world"
s, b := sinkWithBuffer("", 1)

s.Info(0, msg)
logMsg := string(b.Bytes())

require.NotEmpty(t, logMsg)
require.Contains(t, logMsg, fmt.Sprintf(`%q:%q`, sink.MessageKey, msg))
require.NotContains(t, logMsg, fmt.Sprintf(`%q`, sink.FileLineKey))
}

func TestLine_DeveloperLogs(t *testing.T) {
// Logs with a higher level than 1 are considered to be developer level logs
msg := "hello, world"
s, b := sinkWithBuffer("", 2)

s.Info(1, msg)
logMsg := string(b.Bytes())

require.NotEmpty(t, logMsg)
require.Contains(t, logMsg, fmt.Sprintf(`%q:%q`, sink.MessageKey, msg))
require.Contains(t, logMsg, fmt.Sprintf(`%q`, sink.FileLineKey))
}

func TestLine_WithNoContext(t *testing.T) {
msg := "hello, world"
l := sink.Line{Message: msg}

b, err := l.MarshalJSON()

require.NoError(t, err)
require.Contains(t, string(b), fmt.Sprintf(`%q:%q`, sink.MessageKey, msg))
}

func TestLine_LogLevel(t *testing.T) {
s, b := sinkWithBuffer("", 0)

for level := 0; level < 5; level++ {
b.Reset()
s.SetVerbosity(level)

s.Info(0, "hello, world")

require.Contains(t, string(b.Bytes()), fmt.Sprintf(`%q:"%d"`, sink.LevelKey, level))
}
}

func TestLine_WithKVError(t *testing.T) {
err := kverrors.New("an error", "key", "value")
msg := "Error bypasses the enabled check."

kverrMsg, _ := err.(*kverrors.KVError).MarshalJSON()

msgValue := fmt.Sprintf(`%q:%q`, sink.MessageKey, msg)
errorValue := fmt.Sprintf(`%q:%v`, sink.ErrorKey, string(kverrMsg))

s, b := sinkWithBuffer("", 0)

s.Error(err, msg)

logMsg := string(b.Bytes())
require.Contains(t, logMsg, msgValue)
require.Contains(t, logMsg, errorValue)
}

func TestLine_WithNestedKVError(t *testing.T) {
err := kverrors.New("an error", "key", "value")
wrappedErr := kverrors.Wrap(err, "main error", "key", "value")
msg := "Error bypasses the enabled check."

kverrMsg, _ := wrappedErr.(*kverrors.KVError).MarshalJSON()

msgValue := fmt.Sprintf(`%q:%q`, sink.MessageKey, msg)
errorValue := fmt.Sprintf(`%q:%v`, sink.ErrorKey, string(kverrMsg))

s, b := sinkWithBuffer("", 0)

s.Error(wrappedErr, msg)

logMsg := string(b.Bytes())
require.Contains(t, logMsg, msgValue)
require.Contains(t, logMsg, errorValue)
}
164 changes: 0 additions & 164 deletions internal/sink/observerable_test.go

This file was deleted.

5 changes: 5 additions & 0 deletions internal/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (s *Sink) SetVerbosity(v int) {
s.verbosity = Verbosity(v)
}

// GetLevel returns the log level
func (s *Sink) GetVerbosity() int {
return int(s.verbosity)
}

// log will log the message. It DOES NOT check Enabled() first so that should
// be checked by it's callers
func (s *Sink) log(msg string, context map[string]interface{}) {
Expand Down

0 comments on commit 231b64f

Please sign in to comment.