forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyvalue_test.go
181 lines (167 loc) · 6.59 KB
/
keyvalue_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package model_test
import (
"testing"
"github.com/stretchr/testify/assert"
"encoding/json"
"github.com/uber/jaeger/model"
)
func TestValueTypeToFromString(t *testing.T) {
badValueType := model.ValueType(-1)
testCases := []struct {
v model.ValueType
s string
}{
{model.StringType, "string"},
{model.BoolType, "bool"},
{model.Int64Type, "int64"},
{model.Float64Type, "float64"},
{model.BinaryType, "binary"},
{badValueType, "<invalid>"},
}
for _, testCase := range testCases {
assert.Equal(t, testCase.s, testCase.v.String(), testCase.s)
v2, err := model.ValueTypeFromString(testCase.s)
if testCase.v == badValueType {
assert.Error(t, err)
} else {
assert.NoError(t, err, testCase.s)
assert.Equal(t, testCase.v, v2, testCase.s)
}
}
}
func TestValueTypeToFromJSON(t *testing.T) {
kv := model.Int64("x", 123)
out, err := json.Marshal(kv)
assert.NoError(t, err)
assert.Equal(t, `{"key":"x","vType":"int64","vNum":123}`, string(out))
var kv2, kv3 model.KeyValue
if assert.NoError(t, json.Unmarshal(out, &kv2)) {
assert.True(t, kv.Equal(&kv2))
assert.Equal(t, kv, kv2)
}
err = json.Unmarshal([]byte(`{"key":"x","vType":"BAD","vNum":123}`), &kv3)
assert.EqualError(t, err, "not a valid ValueType string BAD")
}
func TestKeyValueString(t *testing.T) {
kv := model.String("x", "y")
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.StringType, kv.VType)
assert.Equal(t, "y", kv.VStr)
assert.False(t, kv.Bool())
assert.Equal(t, int64(0), kv.Int64())
assert.Equal(t, float64(0), kv.Float64())
assert.Nil(t, kv.Binary())
}
func TestKeyValueBool(t *testing.T) {
kv := model.Bool("x", true)
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.BoolType, kv.VType)
assert.True(t, kv.Bool())
}
func TestKeyValueInt64(t *testing.T) {
kv := model.Int64("x", 123)
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.Int64Type, kv.VType)
assert.Equal(t, int64(123), kv.Int64())
}
func TestKeyValueFloat64(t *testing.T) {
kv := model.Float64("x", 123.345)
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.Float64Type, kv.VType)
assert.Equal(t, 123.345, kv.Float64())
}
func TestKeyValueBinary(t *testing.T) {
kv := model.Binary("x", []byte{123, 45})
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.BinaryType, kv.VType)
assert.Equal(t, []byte{123, 45}, kv.Binary())
}
func TestKeyValueIsLessAndEqual(t *testing.T) {
testCases := []struct {
name string
kv1 model.KeyValue
kv2 model.KeyValue
equal bool
}{
{name: "different keys", kv1: model.String("x", "123"), kv2: model.String("y", "123")},
{name: "same key different types", kv1: model.String("x", "123"), kv2: model.Int64("x", 123)},
{name: "different string values", kv1: model.String("x", "123"), kv2: model.String("x", "567")},
{name: "different bool values", kv1: model.Bool("x", false), kv2: model.Bool("x", true)},
{name: "different int64 values", kv1: model.Int64("x", 123), kv2: model.Int64("x", 567)},
{name: "different float64 values", kv1: model.Float64("x", 123), kv2: model.Float64("x", 567)},
{name: "different blob length", kv1: model.Binary("x", []byte{1, 2}), kv2: model.Binary("x", []byte{1, 2, 3})},
{name: "different blob values", kv1: model.Binary("x", []byte{1, 2, 3}), kv2: model.Binary("x", []byte{1, 2, 4})},
{name: "empty blob", kv1: model.Binary("x", nil), kv2: model.Binary("x", nil), equal: true},
{name: "identical blob", kv1: model.Binary("x", []byte{1, 2, 3}), kv2: model.Binary("x", []byte{1, 2, 3}), equal: true},
}
for _, tt := range testCases {
testCase := tt // capture loop var
t.Run(testCase.name, func(t *testing.T) {
if testCase.equal {
assert.False(t, testCase.kv1.IsLess(&testCase.kv2))
assert.True(t, testCase.kv1.Equal(&testCase.kv2))
assert.True(t, testCase.kv2.Equal(&testCase.kv1))
} else {
assert.True(t, testCase.kv1.IsLess(&testCase.kv2))
assert.False(t, testCase.kv1.Equal(&testCase.kv2))
assert.False(t, testCase.kv2.Equal(&testCase.kv1))
}
assert.False(t, testCase.kv2.IsLess(&testCase.kv1))
})
}
t.Run("invalid type", func(t *testing.T) {
v1 := model.KeyValue{Key: "x", VType: model.ValueType(-1)}
v2 := model.KeyValue{Key: "x", VType: model.ValueType(-1)}
assert.False(t, v1.IsLess(&v2))
assert.False(t, v2.IsLess(&v1))
assert.False(t, v1.Equal(&v2))
assert.False(t, v2.Equal(&v1))
})
}
func TestKeyValueAsStringAndValue(t *testing.T) {
testCases := []struct {
kv model.KeyValue
str string
val interface{}
}{
{kv: model.String("x", "Bender is great!"), str: "Bender is great!", val: "Bender is great!"},
{kv: model.Bool("x", false), str: "false", val: false},
{kv: model.Bool("x", true), str: "true", val: true},
{kv: model.Int64("x", 3000), str: "3000", val: int64(3000)},
{kv: model.Int64("x", -1947), str: "-1947", val: int64(-1947)},
{kv: model.Float64("x", 3.14159265359), str: "3.141592654", val: float64(3.14159265359)},
{kv: model.Binary("x", []byte("Bender")), str: "42656e646572", val: []byte("Bender")},
{kv: model.Binary("x", []byte("Bender Bending Rodrigues")), str: "42656e6465722042656e64696e672052...", val: []byte("Bender Bending Rodrigues")},
}
for _, tt := range testCases {
testCase := tt // capture loop var
t.Run(testCase.str, func(t *testing.T) {
assert.Equal(t, testCase.str, testCase.kv.AsString())
assert.Equal(t, testCase.val, testCase.kv.Value())
})
}
t.Run("invalid type", func(t *testing.T) {
kv := model.KeyValue{Key: "x", VType: model.ValueType(-1)}
assert.Equal(t, "unknown type -1", kv.AsString())
assert.EqualError(t, kv.Value().(error), "unknown type -1")
})
}