influxdb/pkg/jsonparser/jsonparser_test.go

46 lines
1.0 KiB
Go

package jsonparser_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/pkg/jsonparser"
)
func TestGetID(t *testing.T) {
t.Run("decode valid id", func(t *testing.T) {
json := `{ "id": "000000000000000a" }`
got, err := jsonparser.GetID([]byte(json), "id")
if err != nil {
t.Error("unexpected error:", err)
}
if exp := influxdb.ID(10); got != exp {
t.Error("unexpected value: -got/+exp", cmp.Diff(got, exp))
}
})
t.Run("error invalid id", func(t *testing.T) {
json := `{ "id": "00000000000a" }`
_, err := jsonparser.GetID([]byte(json), "id")
if err == nil {
t.Error("expected error")
}
})
}
func TestGetOptionalID(t *testing.T) {
t.Run("missing id", func(t *testing.T) {
json := `{ "name": "foo" }`
_, got, err := jsonparser.GetOptionalID([]byte(json), "id")
if err != nil {
t.Error("unexpected error:", err)
}
if exp := false; got != exp {
t.Error("unexpected value: -got/+exp", cmp.Diff(got, exp))
}
})
}