This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight_test.go
138 lines (130 loc) · 5.39 KB
/
highlight_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package highlight
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
"strings"
"testing"
"github.com/pkg/errors"
)
// TestConsumeError tests every case for ConsumeError
func TestConsumeError(t *testing.T) {
requester = mockRequester{}
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeys.SessionSecureID, "0")
ctx = context.WithValue(ctx, ContextKeys.RequestID, "0")
tests := map[string]struct {
errorInput interface{}
contextInput context.Context
tags []string
expectedFlushSize int
expectedEvent string
expectedStackTrace string
expectedError error
}{
"test builtin error": {expectedFlushSize: 1, contextInput: ctx, errorInput: fmt.Errorf("error here"), expectedEvent: "error here", expectedStackTrace: "error here"},
"test builtin error with invalid context": {expectedFlushSize: 0, contextInput: context.Background(), errorInput: fmt.Errorf("error here"), expectedError: fmt.Errorf(consumeErrorSessionIDMissing)},
"test simple github.com/pkg/errors error": {expectedFlushSize: 1, contextInput: ctx, errorInput: errors.New("error here"), expectedEvent: "error here", expectedStackTrace: `["github.com/highlight-run/highlight-go.TestConsumeError /Users/cameronbrill/Projects/work/Highlight/highlight-go/highlight_test.go:27","testing.tRunner /usr/local/opt/go/libexec/src/testing/testing.go:1259","runtime.goexit /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1581"]`},
"test github.com/pkg/errors error with stack trace": {expectedFlushSize: 1, contextInput: ctx, errorInput: errors.Wrap(errors.New("error here"), "error there"), expectedEvent: "error there: error here", expectedStackTrace: `["github.com/highlight-run/highlight-go.TestConsumeError /Users/cameronbrill/Projects/work/Highlight/highlight-go/highlight_test.go:28","testing.tRunner /usr/local/opt/go/libexec/src/testing/testing.go:1259","runtime.goexit /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1581"]`},
}
for name, input := range tests {
t.Run(name, func(t *testing.T) {
Start()
ConsumeError(input.contextInput, input.errorInput, input.tags...)
a, _ := flush()
if len(a) != input.expectedFlushSize {
t.Errorf("flush returned the wrong number of errors [%v != %v]", len(a), input.expectedFlushSize)
return
}
if len(a) < 1 {
return
}
if string(a[0].Event) != input.expectedEvent {
t.Errorf("event not equal to expected event: %v != %v", a[0].Event, input.expectedEvent)
}
// strings.Contains() is here because the actual stack trace will differ from machine to machine, because file paths are different.
if string(a[0].StackTrace) != input.expectedStackTrace && !strings.Contains(string(a[0].StackTrace), "highlight_test.go") {
t.Errorf("stack trace not equal to expected stack trace: %v != %v", a[0].StackTrace, input.expectedStackTrace)
}
})
}
Stop()
}
// TestConsumeError tests every case for RecordMetric
func TestRecordMetric(t *testing.T) {
requester = mockRequester{}
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeys.SessionSecureID, "0")
ctx = context.WithValue(ctx, ContextKeys.RequestID, "0")
tests := map[string]struct {
metricInput struct {
name string
value float64
}
contextInput context.Context
expectedFlushSize int
}{
"test": {expectedFlushSize: 1, contextInput: ctx, metricInput: struct {
name string
value float64
}{name: "myMetric", value: 123.456}},
}
for name, input := range tests {
t.Run(name, func(t *testing.T) {
Start()
RecordMetric(input.contextInput, input.metricInput.name, input.metricInput.value)
_, a := flush()
if len(a) != input.expectedFlushSize {
t.Errorf("flush returned the wrong number of metrics [%v != %v]", len(a), input.expectedFlushSize)
return
}
if len(a) < 1 {
return
}
if string(a[0].Name) != input.metricInput.name {
t.Errorf("name not equal to expected name: %v != %v", a[0].Name, input.metricInput.name)
}
if float64(a[0].Value) != input.metricInput.value {
t.Errorf("name not equal to expected name: %v != %v", a[0].Value, input.metricInput.value)
}
})
}
Stop()
}
func TestTracer(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeys.SessionSecureID, "0")
ctx = context.WithValue(ctx, ContextKeys.RequestID, "0")
ctx = graphql.WithOperationContext(ctx, &graphql.OperationContext{
Operation: &ast.OperationDefinition{},
OperationName: "test-operation",
RawQuery: "test-query",
})
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Field: graphql.CollectedField{
Field: &ast.Field{Name: "test-field"},
},
})
tr := NewGraphqlTracer("test")
t.Run("test basic intercept", func(t *testing.T) {
Start()
if res := tr.InterceptResponse(ctx, func(ctx context.Context) *graphql.Response {
return graphql.ErrorResponse(ctx, "foo error")
}); res == nil {
t.Errorf("got invalid response from intercept response")
}
if field, err := tr.InterceptField(ctx, func(ctx context.Context) (res interface{}, err error) {
return &graphql.Response{}, nil
}); field == nil || err != nil {
t.Errorf("got invalid response from intercept field")
}
_, a := flush()
// size, duration, errorsCount, fields duration
if len(a) != 4 {
t.Errorf("flush returned the wrong number of metrics [%v != %v]", len(a), 4)
return
}
})
Stop()
}