Fix compareLabels, add test

This commit is contained in:
Fabian Reinartz 2016-12-14 15:47:05 +01:00
parent fc992fafc2
commit 725385ea05
2 changed files with 47 additions and 1 deletions

View file

@ -182,7 +182,7 @@ func compareLabels(a, b Labels) int {
}
}
// If all labels so far were in common, the set with fewer labels comes first.
return len(b) - len(a)
return len(a) - len(b)
}
func (s *shardSeriesSet) Series() Series {

46
querier_test.go Normal file
View file

@ -0,0 +1,46 @@
package tsdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCompareLabels(t *testing.T) {
cases := []struct {
a, b []Label
res int
}{
{
a: []Label{},
b: []Label{},
res: 0,
},
{
a: []Label{{"a", ""}},
b: []Label{{"a", ""}, {"b", ""}},
res: -1,
},
{
a: []Label{{"a", ""}},
b: []Label{{"a", ""}},
res: 0,
},
{
a: []Label{{"aa", ""}, {"aa", ""}},
b: []Label{{"aa", ""}, {"ab", ""}},
res: -1,
},
{
a: []Label{{"aa", ""}, {"abb", ""}},
b: []Label{{"aa", ""}, {"ab", ""}},
res: 1,
},
}
for _, c := range cases {
// Use constructor to ensure sortedness.
a, b := NewLabels(c.a...), NewLabels(c.b...)
require.Equal(t, c.res, compareLabels(a, b))
}
}