Fixed current unittest and added a new one.

This commit is contained in:
Eric 2016-07-21 09:54:09 -04:00
parent cf191c3c03
commit 6f63fbcf8e
2 changed files with 44 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package opentsdb
import ( import (
"fmt" "fmt"
"net" "net"
"sort"
"strconv" "strconv"
"strings" "strings"
@ -49,12 +50,14 @@ var sampleConfig = `
type TagSet map[string]string type TagSet map[string]string
func (t TagSet) ToLineFormat() string { func (t TagSet) ToLineFormat() string {
var line string tags := make([]string, len(t))
index := 0
for k, v := range t { for k, v := range t {
line += fmt.Sprintf(" %s=%s", k, v) tags[index] = fmt.Sprintf("%s=%s", k, v)
index++
} }
sort.Strings(tags)
return strings.TrimLeft(line, " ") return strings.Join(tags, " ")
} }
func (o *OpenTSDB) Connect() error { func (o *OpenTSDB) Connect() error {

View File

@ -7,34 +7,60 @@ import (
// "github.com/stretchr/testify/require" // "github.com/stretchr/testify/require"
) )
func TestBuildTagsTelnet(t *testing.T) { func TestCleanTags(t *testing.T) {
var tagtests = []struct { var tagtests = []struct {
ptIn map[string]string ptIn map[string]string
outTags []string outTags TagSet
}{ }{
{ {
map[string]string{"one": "two", "three": "four"}, map[string]string{"one": "two", "three": "four"},
[]string{"one=two", "three=four"}, TagSet{"one": "two", "three": "four"},
}, },
{ {
map[string]string{"aaa": "bbb"}, map[string]string{"aaa": "bbb"},
[]string{"aaa=bbb"}, TagSet{"aaa": "bbb"},
},
{
map[string]string{"one": "two", "aaa": "bbb"},
[]string{"aaa=bbb", "one=two"},
}, },
{ {
map[string]string{"Sp%ci@l Chars": "g$t repl#ced"}, 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{}, map[string]string{},
[]string{}, TagSet{},
}, },
} }
for _, tt := range tagtests { 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) { if !reflect.DeepEqual(tags, tt.outTags) {
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags) t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
} }