github.com/xmidt-org/webpa-common@v1.11.9/logging/context_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/go-kit/kit/log"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestWithLogger(t *testing.T) {
    13  	var (
    14  		require = require.New(t)
    15  		assert  = assert.New(t)
    16  		ctx     = WithLogger(context.Background(), DefaultLogger())
    17  	)
    18  
    19  	require.NotNil(ctx)
    20  
    21  	logger, ok := ctx.Value(loggerKey).(log.Logger)
    22  	assert.NotNil(logger)
    23  	assert.True(ok)
    24  }
    25  
    26  func testGetLoggerMissing(t *testing.T) {
    27  	assert := assert.New(t)
    28  	assert.NotNil(GetLogger(context.Background()))
    29  }
    30  
    31  func testGetLoggerPresent(t *testing.T) {
    32  	var (
    33  		require = require.New(t)
    34  		assert  = assert.New(t)
    35  		ctx     = WithLogger(context.Background(), New(nil))
    36  	)
    37  
    38  	require.NotNil(ctx)
    39  	assert.NotNil(GetLogger(ctx))
    40  }
    41  
    42  func TestGetLogger(t *testing.T) {
    43  	t.Run("Missing", testGetLoggerMissing)
    44  	t.Run("Present", testGetLoggerPresent)
    45  }