Commit d2b3bcf3 by Emil Hessman Committed by GitHub

Chore: Rewrite models tags test to standard library (#30041)

parent a99edd2f
......@@ -3,93 +3,94 @@ package models
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParsingTags(t *testing.T) {
Convey("Testing parsing a tag pairs into tags", t, func() {
Convey("Can parse one empty tag", func() {
tags := ParseTagPairs([]string{""})
So(len(tags), ShouldEqual, 0)
})
func TestParseTagPairs(t *testing.T) {
t.Run("Can parse one empty tag", func(t *testing.T) {
tags := ParseTagPairs([]string{""})
require.Empty(t, tags)
})
Convey("Can parse valid tags", func() {
tags := ParseTagPairs([]string{"outage", "type:outage", "error"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
t.Run("Can parse valid tags", func(t *testing.T) {
tags := ParseTagPairs([]string{"outage", "type:outage", "error"})
require.Len(t, tags, 3)
assert.Equal(t, "outage", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "type", tags[1].Key)
assert.Equal(t, "outage", tags[1].Value)
assert.Equal(t, "error", tags[2].Key)
assert.Empty(t, tags[2].Value)
})
Convey("Can parse tags with spaces", func() {
tags := ParseTagPairs([]string{" outage ", " type : outage ", "error "})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
t.Run("Can parse tags with spaces", func(t *testing.T) {
tags := ParseTagPairs([]string{" outage ", " type : outage ", "error "})
require.Len(t, tags, 3)
assert.Equal(t, "outage", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "type", tags[1].Key)
assert.Equal(t, "outage", tags[1].Value)
assert.Equal(t, "error", tags[2].Key)
assert.Empty(t, tags[2].Value)
})
Convey("Can parse empty tags", func() {
tags := ParseTagPairs([]string{" outage ", "", "", ":", "type : outage ", "error ", "", ""})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
t.Run("Can parse empty tags", func(t *testing.T) {
tags := ParseTagPairs([]string{" outage ", "", "", ":", "type : outage ", "error ", "", ""})
require.Len(t, tags, 3)
assert.Equal(t, "outage", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "type", tags[1].Key)
assert.Equal(t, "outage", tags[1].Value)
assert.Equal(t, "error", tags[2].Key)
assert.Empty(t, tags[2].Value)
})
Convey("Can parse tags with extra colons", func() {
tags := ParseTagPairs([]string{" outage", "type : outage:outage2 :outage3 ", "error :"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
t.Run("Can parse tags with extra colons", func(t *testing.T) {
tags := ParseTagPairs([]string{" outage", "type : outage:outage2 :outage3 ", "error :"})
require.Len(t, tags, 3)
assert.Equal(t, "outage", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "type", tags[1].Key)
assert.Equal(t, "outage", tags[1].Value)
assert.Equal(t, "error", tags[2].Key)
assert.Empty(t, tags[2].Value)
})
Convey("Can parse tags that contains key and values with spaces", func() {
tags := ParseTagPairs([]string{" outage 1", "type 1: outage 1 ", "has error "})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage 1")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type 1")
So(tags[1].Value, ShouldEqual, "outage 1")
So(tags[2].Key, ShouldEqual, "has error")
So(tags[2].Value, ShouldEqual, "")
})
t.Run("Can parse tags that contains key and values with spaces", func(t *testing.T) {
tags := ParseTagPairs([]string{" outage 1", "type 1: outage 1 ", "has error "})
require.Len(t, tags, 3)
assert.Equal(t, "outage 1", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "type 1", tags[1].Key)
assert.Equal(t, "outage 1", tags[1].Value)
assert.Equal(t, "has error", tags[2].Key)
assert.Empty(t, tags[2].Value)
})
Convey("Can filter out duplicate tags", func() {
tags := ParseTagPairs([]string{"test", "test", "key:val1", "key:val2"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "test")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "key")
So(tags[1].Value, ShouldEqual, "val1")
So(tags[2].Key, ShouldEqual, "key")
So(tags[2].Value, ShouldEqual, "val2")
})
t.Run("Can filter out duplicate tags", func(t *testing.T) {
tags := ParseTagPairs([]string{"test", "test", "key:val1", "key:val2"})
require.Len(t, tags, 3)
assert.Equal(t, "test", tags[0].Key)
assert.Empty(t, tags[0].Value)
assert.Equal(t, "key", tags[1].Key)
assert.Equal(t, "val1", tags[1].Value)
assert.Equal(t, "key", tags[2].Key)
assert.Equal(t, "val2", tags[2].Value)
})
}
Convey("Can join tag pairs", func() {
tagPairs := []*Tag{
{Key: "key1", Value: "val1"},
{Key: "key2", Value: ""},
{Key: "key3"},
}
tags := JoinTagPairs(tagPairs)
So(len(tags), ShouldEqual, 3)
So(tags[0], ShouldEqual, "key1:val1")
So(tags[1], ShouldEqual, "key2")
So(tags[2], ShouldEqual, "key3")
})
func TestJoinTagPairs(t *testing.T) {
t.Run("Can join tag pairs", func(t *testing.T) {
tagPairs := []*Tag{
{Key: "key1", Value: "val1"},
{Key: "key2", Value: ""},
{Key: "key3"},
}
tags := JoinTagPairs(tagPairs)
require.Len(t, tags, 3)
assert.Equal(t, "key1:val1", tags[0])
assert.Equal(t, "key2", tags[1])
assert.Equal(t, "key3", tags[2])
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment