github.com/blend/go-sdk@v1.20220411.3/logger/scope_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"bytes"
    12  	"context"
    13  	"fmt"
    14  	"net/http"
    15  	"testing"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  )
    19  
    20  func TestNewScope(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	log := None()
    24  	sc := NewScope(
    25  		log,
    26  		OptScopePath("foo", "bar"),
    27  		OptScopeLabels(Labels{"moo": "loo"}),
    28  		OptScopeAnnotations(Annotations{"alpha": "bravo"}),
    29  	)
    30  	assert.NotNil(sc.Logger)
    31  	assert.Equal([]string{"foo", "bar"}, sc.Path)
    32  	assert.Equal("loo", sc.Labels["moo"])
    33  
    34  	sub := sc.WithPath("example-string").WithLabels(Labels{"what": "where"}).WithAnnotations(Annotations{"zoo": 47})
    35  	assert.Equal([]string{"foo", "bar", "example-string"}, sub.Path)
    36  	assert.Equal("where", sub.Labels["what"])
    37  	assert.Equal("loo", sub.Labels["moo"])
    38  	assert.Equal(47, sub.Annotations["zoo"])
    39  	assert.Equal("bravo", sub.Annotations["alpha"])
    40  }
    41  
    42  func TestWithPath(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	log := None()
    46  	sc := log.WithPath("foo", "bar")
    47  	assert.Equal([]string{"foo", "bar"}, sc.Path)
    48  }
    49  
    50  func TestWithLabels(t *testing.T) {
    51  	assert := assert.New(t)
    52  
    53  	log := None()
    54  	sc := log.WithLabels(Labels{"foo": "bar"})
    55  	assert.Equal("bar", sc.Labels["foo"])
    56  }
    57  
    58  func TestWithAnnotations(t *testing.T) {
    59  	assert := assert.New(t)
    60  
    61  	log := None()
    62  	sc := log.WithAnnotations(Annotations{"foo": "bar"})
    63  	assert.Equal("bar", sc.Annotations["foo"])
    64  }
    65  
    66  func TestScopeMethods(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	log := All()
    70  	log.Formatter = NewTextOutputFormatter(OptTextNoColor(), OptTextHideTimestamp())
    71  
    72  	buf := new(bytes.Buffer)
    73  	log.Output = buf
    74  	log.Info("format", " test")
    75  	assert.Equal("[info] format test\n", buf.String())
    76  
    77  	buf = new(bytes.Buffer)
    78  	log.Output = buf
    79  	log.Debug("format", " test")
    80  	assert.Equal("[debug] format test\n", buf.String())
    81  
    82  	buf = new(bytes.Buffer)
    83  	log.Output = buf
    84  	log.Warning(fmt.Errorf("only a test"), OptErrorEventState(&http.Request{Method: "foo"}))
    85  	assert.Equal("[warning] only a test\n", buf.String())
    86  
    87  	buf = new(bytes.Buffer)
    88  	log.Output = buf
    89  	log.Error(fmt.Errorf("only a test"), OptErrorEventState(&http.Request{Method: "foo"}))
    90  	assert.Equal("[error] only a test\n", buf.String())
    91  
    92  	buf = new(bytes.Buffer)
    93  	log.Output = buf
    94  	log.Fatal(fmt.Errorf("only a test"), OptErrorEventState(&http.Request{Method: "foo"}))
    95  	assert.Equal("[fatal] only a test\n", buf.String())
    96  
    97  	buf = new(bytes.Buffer)
    98  	log.Output = buf
    99  	log.Path = []string{"outer", "inner"}
   100  	log.Labels = Labels{"foo": "bar"}
   101  	log.Info("format test")
   102  	assert.Equal("[outer > inner] [info] format test\tfoo=bar\n", buf.String())
   103  }
   104  
   105  func TestScopeFromContext(t *testing.T) {
   106  	assert := assert.New(t)
   107  
   108  	sc := NewScope(None())
   109  	sc.Path = []string{"one", "two"}
   110  	sc.Labels = Labels{"foo": "bar"}
   111  
   112  	ctx := WithLabels(context.Background(), Labels{"moo": "loo"})
   113  	ctx = WithPath(ctx, "three", "four")
   114  
   115  	final := sc.FromContext(ctx)
   116  	assert.Equal([]string{"three", "four", "one", "two"}, final.Path)
   117  	assert.Equal("bar", final.Labels["foo"])
   118  	assert.Equal("loo", final.Labels["moo"])
   119  }
   120  
   121  func TestScopeApply(t *testing.T) {
   122  	assert := assert.New(t)
   123  
   124  	sc := NewScope(None())
   125  	sc.Path = []string{"one", "two"}
   126  	sc.Labels = Labels{"foo": "bar"}
   127  
   128  	ctx := WithLabels(context.Background(), Labels{"moo": "loo"})
   129  	ctx = WithPath(ctx, "three", "four")
   130  
   131  	final := sc.ApplyContext(ctx)
   132  	assert.Equal([]string{"three", "four", "one", "two"}, GetPath(final))
   133  	assert.Equal("bar", GetLabels(final)["foo"])
   134  	assert.Equal("loo", GetLabels(final)["moo"])
   135  }