Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis committed Jan 19, 2021
1 parent e0cad90 commit f77ccef
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/dbnode/storage/index/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/m3db/m3/src/x/checked"
"github.com/m3db/m3/src/x/ident"
"github.com/m3db/m3/src/x/pool"
"github.com/m3db/m3/src/x/serialize"
"github.com/m3db/m3/src/x/test"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -171,6 +172,113 @@ func TestFromSeriesIDAndTagIterReuseBytesFromSeriesId(t *testing.T) {
}
}

func TestFromSeriesIDAndEncodedTagsInvalid(t *testing.T) {
var (
id = ident.StringID("foo")
tags = ident.NewTags(
ident.StringTag(string(convert.ReservedFieldNameID), "value"),
)
encodedTags = toEncodedTags(t, tags)
)
_, err := convert.FromSeriesIDAndEncodedTags(id, encodedTags)
assert.Error(t, err)
}

func TestFromSeriesIDAndEncodedTagsValid(t *testing.T) {
var (
id = ident.StringID("foo")
tags = ident.NewTags(
ident.StringTag("bar", "baz"),
)
encodedTags = toEncodedTags(t, tags)
)
d, err := convert.FromSeriesIDAndEncodedTags(id, encodedTags)
assert.NoError(t, err)
assertContentsMatch(t, id, tags.Values(), d)
}

func TestFromSeriesIDAndEncodedTagsReuseBytesFromSeriesId(t *testing.T) {
tests := []struct {
name string
id string
}{
{
name: "tags in ID",
id: "bar=baz,quip=quix",
},
{
name: "tags in ID with specific format",
id: `{bar="baz",quip="quix"}`,
},
{
name: "tags in ID with specific format reverse order",
id: `{quip="quix",bar="baz"}`,
},
{
name: "inexact tag occurrence in ID",
id: "quixquip_bazillion_barometers",
},
}
var (
tags = ident.NewTags(
ident.StringTag("bar", "baz"),
ident.StringTag("quip", "quix"),
)
encodedTags = toEncodedTags(t, tags)
)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
seriesID := ident.StringID(tt.id)
d, err := convert.FromSeriesIDAndEncodedTags(seriesID, encodedTags)
assert.NoError(t, err)
assertContentsMatch(t, seriesID, tags.Values(), d)
for i := range d.Fields {
assertBackedBySameData(t, d.ID, d.Fields[i].Name)
assertBackedBySameData(t, d.ID, d.Fields[i].Value)
}
})
}
}

func TestFromSeriesIDAndEncodedTagsInvalidEncodedTags(t *testing.T) {
validEncodedTags := []byte{117, 39, 1, 0, 3, 0, 98, 97, 114, 3, 0, 98, 97, 122}

tests := []struct {
name string
encodedTags []byte
}{
{
name: "incomplete header",
encodedTags: validEncodedTags[:3],
},
{
name: "incomplete tag name length",
encodedTags: validEncodedTags[:5],
},
{
name: "incomplete tag value length",
encodedTags: validEncodedTags[:10],
},
{
name: "invalid magic number",
encodedTags: []byte{42, 42, 0, 0},
},
{
name: "empty tag name",
encodedTags: []byte{117, 39, 1, 0, 0, 0, 3, 0, 98, 97, 122},
},
}
seriesID := ident.StringID("foo")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := convert.FromSeriesIDAndEncodedTags(seriesID, tt.encodedTags)
assert.Error(t, err)
})
}
}

func TestToSeriesValid(t *testing.T) {
d := doc.Metadata{
ID: []byte("foo"),
Expand Down Expand Up @@ -311,3 +419,15 @@ func assertBackedBySameData(t *testing.T, outer, inner []byte) {
assert.Fail(t, "inner byte sequence wasn't found")
}
}

func toEncodedTags(t *testing.T, tags ident.Tags) []byte {
pool := serialize.NewTagEncoderPool(serialize.NewTagEncoderOptions(), nil)
pool.Init()
encoder := pool.Get()
defer encoder.Finalize()

require.NoError(t, encoder.Encode(ident.NewTagsIterator(tags)))
data, ok := encoder.Data()
require.True(t, ok)
return append([]byte(nil), data.Bytes()...)
}

0 comments on commit f77ccef

Please sign in to comment.