github.com/blend/go-sdk@v1.20220411.3/logger/error_event_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 "encoding/json" 14 "fmt" 15 "net/http" 16 "strings" 17 "testing" 18 19 "github.com/blend/go-sdk/assert" 20 "github.com/blend/go-sdk/ex" 21 ) 22 23 func TestNewErrorEvent(t *testing.T) { 24 assert := assert.New(t) 25 26 /// stuff 27 ee := NewErrorEvent( 28 Fatal, 29 fmt.Errorf("not a test"), 30 OptErrorEventState(&http.Request{Method: "POST"}), 31 ) 32 assert.Equal(Fatal, ee.GetFlag()) 33 assert.Equal("not a test", ee.Err.Error()) 34 assert.NotNil(ee.State) 35 assert.Equal("POST", ee.State.(*http.Request).Method) 36 37 buf := new(bytes.Buffer) 38 tf := TextOutputFormatter{ 39 NoColor: true, 40 } 41 42 ee.WriteText(tf, buf) 43 assert.Equal("not a test", buf.String()) 44 45 contents, err := json.Marshal(ee.Decompose()) 46 assert.Nil(err) 47 assert.Contains(string(contents), "not a test") 48 49 ee = NewErrorEvent(Fatal, ex.New("this is only a test")) 50 contents, err = json.Marshal(ee.Decompose()) 51 assert.Nil(err) 52 assert.Contains(string(contents), "this is only a test") 53 } 54 55 func TestErrorEventListener(t *testing.T) { 56 assert := assert.New(t) 57 58 ee := NewErrorEvent(Fatal, fmt.Errorf("only a test")) 59 60 var didCall bool 61 ml := NewErrorEventListener(func(ctx context.Context, e ErrorEvent) { 62 didCall = true 63 }) 64 65 ml(context.Background(), ee) 66 assert.True(didCall) 67 } 68 69 func TestScopedErrorEventListener(t *testing.T) { 70 testCases := []struct { 71 scopes *Scopes 72 enabledContexts []context.Context 73 disabledContexts []context.Context 74 }{ 75 { 76 scopes: NewScopes("*"), 77 enabledContexts: []context.Context{ 78 WithPath(context.Background(), "test0", "test1"), 79 WithPath(context.Background(), "test0"), 80 WithPath(context.Background(), "test1"), 81 }, 82 }, 83 { 84 scopes: NewScopes("-*"), 85 disabledContexts: []context.Context{ 86 WithPath(context.Background(), "test0", "test1"), 87 WithPath(context.Background(), "test0"), 88 WithPath(context.Background(), "test1"), 89 }, 90 }, 91 { 92 scopes: NewScopes("test0/test1"), 93 enabledContexts: []context.Context{ 94 WithPath(context.Background(), "test0", "test1"), 95 }, 96 disabledContexts: []context.Context{ 97 WithPath(context.Background(), "test0"), 98 WithPath(context.Background(), "test0", "test2"), 99 WithPath(context.Background(), "test0", "test1", "test2"), 100 WithPath(context.Background(), "test1"), 101 }, 102 }, 103 } 104 105 for _, testCase := range testCases { 106 name := fmt.Sprintf("Scope '%s'", testCase.scopes.String()) 107 t.Run(name, func(t *testing.T) { 108 assert := assert.New(t) 109 110 for _, ctx := range testCase.enabledContexts { 111 ee := NewErrorEvent(Fatal, fmt.Errorf("only a test")) 112 113 var didCall bool 114 ml := NewScopedErrorEventListener(func(ctx context.Context, e ErrorEvent) { 115 didCall = true 116 }, testCase.scopes) 117 118 ml(ctx, ee) 119 assert.True(didCall, strings.Join(GetPath(ctx), "/")) 120 } 121 122 for _, ctx := range testCase.disabledContexts { 123 ee := NewErrorEvent(Fatal, fmt.Errorf("only a test")) 124 125 var didCall bool 126 ml := NewScopedErrorEventListener(func(ctx context.Context, e ErrorEvent) { 127 didCall = true 128 }, testCase.scopes) 129 130 ml(ctx, ee) 131 assert.False(didCall, strings.Join(GetPath(ctx), "/")) 132 } 133 }) 134 } 135 }