Debian Codegraph @

attribute/kv_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute_test
5
6 import (
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10 "github.com/stretchr/testify/assert"
11
12 "go.opentelemetry.io/otel/attribute"
13 )
14
15 func TestKeyValueConstructors(t *testing.T) {
16 tt := []struct {
17 name string
18 actual attribute.KeyValue
19 expected attribute.KeyValue
20 }{
21 {
22 name: "Bool",
23 actual: attribute.Bool("k1", true),
24 expected: attribute.KeyValue{
25 Key: "k1",
26 Value: attribute.BoolValue(true),
27 },
28 },
29 {
30 name: "Int64",
31 actual: attribute.Int64("k1", 123),
32 expected: attribute.KeyValue{
33 Key: "k1",
34 Value: attribute.Int64Value(123),
35 },
36 },
37 {
38 name: "Float64",
39 actual: attribute.Float64("k1", 123.5),
40 expected: attribute.KeyValue{
41 Key: "k1",
42 Value: attribute.Float64Value(123.5),
43 },
44 },
45 {
46 name: "String",
47 actual: attribute.String("k1", "123.5"),
48 expected: attribute.KeyValue{
49 Key: "k1",
50 Value: attribute.StringValue("123.5"),
51 },
52 },
53 {
54 name: "Int",
55 actual: attribute.Int("k1", 123),
56 expected: attribute.KeyValue{
57 Key: "k1",
58 Value: attribute.IntValue(123),
59 },
60 },
61 }
62
63 for _, test := range tt {
64 t.Run(test.name, func(t *testing.T) {
65 if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
66 t.Fatal(diff)
67 }
68 })
69 }
70 }
71
72 func TestKeyValueValid(t *testing.T) {
73 tests := []struct {
74 desc string
75 valid bool
76 kv attribute.KeyValue
77 }{
78 {
79 desc: "uninitialized KeyValue should be invalid",
80 valid: false,
81 kv: attribute.KeyValue{},
82 },
83 {
84 desc: "empty key value should be invalid",
85 valid: false,
86 kv: attribute.Key("").Bool(true),
87 },
88 {
89 desc: "EMPTY value type should be valid",
90 valid: true,
91 kv: attribute.KeyValue{
92 Key: attribute.Key("valid key"),
93 // Default type is EMPTY.
94 Value: attribute.Value{},
95 },
96 },
97 {
98 desc: "non-empty key with BOOL type Value should be valid",
99 valid: true,
100 kv: attribute.Bool("bool", true),
101 },
102 {
103 desc: "non-empty key with INT64 type Value should be valid",
104 valid: true,
105 kv: attribute.Int64("int64", 0),
106 },
107 {
108 desc: "non-empty key with FLOAT64 type Value should be valid",
109 valid: true,
110 kv: attribute.Float64("float64", 0),
111 },
112 {
113 desc: "non-empty key with STRING type Value should be valid",
114 valid: true,
115 kv: attribute.String("string", ""),
116 },
117 }
118
119 for _, test := range tests {
120 if got, want := test.kv.Valid(), test.valid; got != want {
121 t.Error(test.desc)
122 }
123 }
124 }
125
126 func TestIncorrectCast(t *testing.T) {
127 testCases := []struct {
128 name string
129 val attribute.Value
130 }{
131 {
132 name: "Float64",
133 val: attribute.Float64Value(1.0),
134 },
135 {
136 name: "Int64",
137 val: attribute.Int64Value(2),
138 },
139 {
140 name: "String",
141 val: attribute.BoolValue(true),
142 },
143 {
144 name: "Float64Slice",
145 val: attribute.Float64SliceValue([]float64{1.0}),
146 },
147 {
148 name: "Int64Slice",
149 val: attribute.Int64SliceValue([]int64{2}),
150 },
151 {
152 name: "StringSlice",
153 val: attribute.BoolSliceValue([]bool{true}),
154 },
155 {
156 name: "Empty",
157 val: attribute.Value{},
158 },
159 }
160 for _, tt := range testCases {
161 t.Run(tt.name, func(t *testing.T) {
162 assert.NotPanics(t, func() {
163 tt.val.AsBool()
164 tt.val.AsBoolSlice()
165 tt.val.AsFloat64()
166 tt.val.AsFloat64Slice()
167 tt.val.AsInt64()
168 tt.val.AsInt64Slice()
169 tt.val.AsInterface()
170 tt.val.AsString()
171 tt.val.AsStringSlice()
172 })
173 })
174 }
175 }