github.com/msales/pkg/v3@v3.24.0/clix/context_test.go (about)

     1  package clix_test
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/msales/pkg/v3/clix"
    10  	"github.com/msales/pkg/v3/log"
    11  	"github.com/msales/pkg/v3/stats"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/mock"
    14  	"gopkg.in/urfave/cli.v1"
    15  )
    16  
    17  func TestWithLogger(t *testing.T) {
    18  	c, _ := newTestContext()
    19  	ctx, err := clix.NewContext(c, clix.WithLogger(log.Null))
    20  	assert.NoError(t, err)
    21  
    22  	l, ok := log.FromContext(ctx)
    23  	assert.True(t, ok)
    24  	assert.Equal(t, l, log.Null)
    25  }
    26  
    27  func TestWithStats(t *testing.T) {
    28  	c, _ := newTestContext()
    29  	ctx, err := clix.NewContext(c, clix.WithStats(stats.Null))
    30  	assert.NoError(t, err)
    31  
    32  	s, ok := stats.FromContext(ctx)
    33  	assert.True(t, ok)
    34  	assert.Equal(t, s, stats.Null)
    35  }
    36  
    37  func TestNewContext(t *testing.T) {
    38  	c, _ := newTestContext()
    39  
    40  	ctx, err := clix.NewContext(c)
    41  
    42  	assert.NoError(t, err)
    43  	assert.IsType(t, &clix.Context{}, ctx)
    44  }
    45  
    46  func TestNewContext_LoggerError(t *testing.T) {
    47  	c, flags := newTestContext()
    48  	flags.String(clix.FlagLogFormat, "test", "")
    49  
    50  	_, err := clix.NewContext(c)
    51  
    52  	assert.Error(t, err)
    53  }
    54  
    55  func TestNewContext_StatsError(t *testing.T) {
    56  	c, flags := newTestContext()
    57  	flags.String(clix.FlagStatsDSN, "test://", "")
    58  
    59  	_, err := clix.NewContext(c)
    60  
    61  	assert.Error(t, err)
    62  }
    63  
    64  func TestContext_Close(t *testing.T) {
    65  	c, _ := newTestContext()
    66  	ctx, _ := clix.NewContext(c, clix.WithLogger(log.Null), clix.WithStats(stats.Null))
    67  
    68  	err := ctx.Close()
    69  
    70  	assert.NoError(t, err)
    71  }
    72  
    73  func TestContext_CloseErrors(t *testing.T) {
    74  	tests := []struct {
    75  		name     string
    76  		logErr   error
    77  		statsErr error
    78  	}{
    79  		{
    80  			name:     "No Error",
    81  			logErr:   nil,
    82  			statsErr: nil,
    83  		},
    84  		{
    85  			name:     "Logger Error",
    86  			logErr:   errors.New("test"),
    87  			statsErr: nil,
    88  		},
    89  		{
    90  			name:     "Stats Error",
    91  			logErr:   nil,
    92  			statsErr: errors.New("test"),
    93  		},
    94  	}
    95  
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			l := new(MockLogger)
    99  			l.On("Close").Return(tt.logErr)
   100  
   101  			s := new(MockStats)
   102  			s.On("Close").Return(tt.statsErr)
   103  
   104  			c, _ := newTestContext()
   105  			ctx, err := clix.NewContext(c, clix.WithLogger(l), clix.WithStats(s))
   106  			assert.NoError(t, err)
   107  
   108  			err = ctx.Close()
   109  
   110  			if tt.logErr != nil || tt.statsErr != nil {
   111  				assert.Error(t, err)
   112  			} else {
   113  				assert.NoError(t, err)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func newTestContext() (*cli.Context, *flag.FlagSet) {
   120  	fs := flag.NewFlagSet("test", flag.ContinueOnError)
   121  	c := cli.NewContext(cli.NewApp(), fs, nil)
   122  
   123  	return c, fs
   124  }
   125  
   126  type MockLogger struct {
   127  	mock.Mock
   128  }
   129  
   130  func (m *MockLogger) Debug(msg string, ctx ...interface{}) {}
   131  
   132  func (m *MockLogger) Info(msg string, ctx ...interface{}) {}
   133  
   134  func (m *MockLogger) Warn(msg string, ctx ...interface{}) {}
   135  
   136  func (m *MockLogger) Error(msg string, ctx ...interface{}) {}
   137  
   138  func (m *MockLogger) Crit(msg string, ctx ...interface{}) {}
   139  
   140  func (m *MockLogger) Close() error {
   141  	args := m.Called()
   142  	return args.Error(0)
   143  }
   144  
   145  type MockStats struct {
   146  	mock.Mock
   147  }
   148  
   149  func (m *MockStats) Inc(name string, value int64, rate float32, tags ...interface{}) error {
   150  	return nil
   151  }
   152  
   153  func (m *MockStats) Dec(name string, value int64, rate float32, tags ...interface{}) error {
   154  	return nil
   155  }
   156  
   157  func (m *MockStats) Gauge(name string, value float64, rate float32, tags ...interface{}) error {
   158  	return nil
   159  }
   160  
   161  func (m *MockStats) Timing(name string, value time.Duration, rate float32, tags ...interface{}) error {
   162  	return nil
   163  }
   164  
   165  func (m *MockStats) Close() error {
   166  	args := m.Called()
   167  	return args.Error(0)
   168  }