-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
context_inferrer_test.go
89 lines (80 loc) · 2.26 KB
/
context_inferrer_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_NewPriorityContextInferrer_Infer(t *testing.T) {
tests := []struct {
name string
priority []string
statements []string
expected string
}{
{
name: "with priority and contexts",
priority: []string{"spanevent", "span", "resource"},
statements: []string{"set(span.foo, resource.value) where spanevent.bar == true"},
expected: "spanevent",
},
{
name: "with multiple statements",
priority: []string{"spanevent", "span", "resource"},
statements: []string{
"set(resource.foo, resource.value) where span.bar == true",
"set(resource.foo, resource.value) where spanevent.bar == true",
},
expected: "spanevent",
},
{
name: "with no context",
priority: []string{"log", "resource"},
statements: []string{"set(foo, true) where bar == true"},
expected: "",
},
{
name: "with empty priority",
statements: []string{"set(foo.name, true) where bar.name == true"},
expected: "foo",
},
{
name: "with unknown context",
priority: []string{"foo", "bar"},
statements: []string{"set(span.foo, true) where span.bar == true"},
expected: "span",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inferrer := newPriorityContextInferrer(tt.priority)
inferredContext, err := inferrer.infer(tt.statements)
require.NoError(t, err)
assert.Equal(t, tt.expected, inferredContext)
})
}
}
func Test_NewPriorityContextInferrer_InvalidStatement(t *testing.T) {
inferrer := newPriorityContextInferrer([]string{"foo"})
statements := []string{"set(foo.field,"}
_, err := inferrer.infer(statements)
require.ErrorContains(t, err, "unexpected token")
}
func Test_DefaultPriorityContextInferrer(t *testing.T) {
expectedPriority := []string{
"log",
"metric",
"datapoint",
"spanevent",
"span",
"resource",
"scope",
"instrumentation_scope",
}
inferrer := defaultPriorityContextInferrer().(*priorityContextInferrer)
require.NotNil(t, inferrer)
for pri, ctx := range expectedPriority {
require.Equal(t, pri, inferrer.contextPriority[ctx])
}
}