Fixed current unittest and added a new one.
This commit is contained in:
parent
cf191c3c03
commit
6f63fbcf8e
|
@ -3,6 +3,7 @@ package opentsdb
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -49,12 +50,14 @@ var sampleConfig = `
|
|||
type TagSet map[string]string
|
||||
|
||||
func (t TagSet) ToLineFormat() string {
|
||||
var line string
|
||||
tags := make([]string, len(t))
|
||||
index := 0
|
||||
for k, v := range t {
|
||||
line += fmt.Sprintf(" %s=%s", k, v)
|
||||
tags[index] = fmt.Sprintf("%s=%s", k, v)
|
||||
index++
|
||||
}
|
||||
|
||||
return strings.TrimLeft(line, " ")
|
||||
sort.Strings(tags)
|
||||
return strings.Join(tags, " ")
|
||||
}
|
||||
|
||||
func (o *OpenTSDB) Connect() error {
|
||||
|
|
|
@ -7,34 +7,60 @@ import (
|
|||
// "github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBuildTagsTelnet(t *testing.T) {
|
||||
func TestCleanTags(t *testing.T) {
|
||||
var tagtests = []struct {
|
||||
ptIn map[string]string
|
||||
outTags []string
|
||||
outTags TagSet
|
||||
}{
|
||||
{
|
||||
map[string]string{"one": "two", "three": "four"},
|
||||
[]string{"one=two", "three=four"},
|
||||
TagSet{"one": "two", "three": "four"},
|
||||
},
|
||||
{
|
||||
map[string]string{"aaa": "bbb"},
|
||||
[]string{"aaa=bbb"},
|
||||
},
|
||||
{
|
||||
map[string]string{"one": "two", "aaa": "bbb"},
|
||||
[]string{"aaa=bbb", "one=two"},
|
||||
TagSet{"aaa": "bbb"},
|
||||
},
|
||||
{
|
||||
map[string]string{"Sp%ci@l Chars": "g$t repl#ced"},
|
||||
[]string{"Sp-ci-l_Chars=g-t_repl-ced"},
|
||||
TagSet{"Sp-ci-l_Chars": "g-t_repl-ced"},
|
||||
},
|
||||
{
|
||||
map[string]string{},
|
||||
[]string{},
|
||||
TagSet{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tagtests {
|
||||
tags := buildTags(tt.ptIn)
|
||||
tags := cleanTags(tt.ptIn)
|
||||
if !reflect.DeepEqual(tags, tt.outTags) {
|
||||
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTagsTelnet(t *testing.T) {
|
||||
var tagtests = []struct {
|
||||
ptIn TagSet
|
||||
outTags string
|
||||
}{
|
||||
{
|
||||
TagSet{"one": "two", "three": "four"},
|
||||
"one=two three=four",
|
||||
},
|
||||
{
|
||||
TagSet{"aaa": "bbb"},
|
||||
"aaa=bbb",
|
||||
},
|
||||
{
|
||||
TagSet{"one": "two", "aaa": "bbb"},
|
||||
"aaa=bbb one=two",
|
||||
},
|
||||
{
|
||||
TagSet{},
|
||||
"",
|
||||
},
|
||||
}
|
||||
for _, tt := range tagtests {
|
||||
tags := tt.ptIn.ToLineFormat()
|
||||
if !reflect.DeepEqual(tags, tt.outTags) {
|
||||
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue