Skip to content

Commit

Permalink
Improve unit tests for jq's toType and byteToType (#1506)
Browse files Browse the repository at this point in the history
- Refactor the toType test to handle more cases
- Add additional cases to toTypes test
- Add a specific test for byteToType
  • Loading branch information
lburgazzoli authored Jan 16, 2025
1 parent c35c6c1 commit e1457ab
Showing 1 changed file with 137 additions and 19 deletions.
156 changes: 137 additions & 19 deletions pkg/utils/test/matchers/jq/jq_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,168 @@ package jq
import (
"encoding/json"
"reflect"
"strings"
"testing"

"github.com/onsi/gomega/gbytes"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"

. "github.com/onsi/gomega"
)

func TestBytesToType(t *testing.T) {
t.Parallel()

g := NewGomegaWithT(t)

tests := []struct {
name string
input []byte
expected any
}{
{
name: "Empty Input",
input: []byte{},
expected: nil,
},
{
name: "Valid JSON Object",
input: []byte(`{"key": "value"}`),
expected: map[string]any{"key": "value"},
},
{
name: "Valid JSON Array",
input: []byte(`[1, "two", 3.0]`),
expected: []any{float64(1), "two", 3.0},
},
{
name: "Invalid JSON",
input: []byte(`{invalid}`),
expected: nil,
},
{
name: "Non-Object/Array JSON",
input: []byte(`"string"`),
expected: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

result, err := byteToType(tt.input)

if tt.expected == nil {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(result).To(Equal(tt.expected))
}
})
}
}

func TestToType(t *testing.T) {
t.Parallel()

typeTestData := []byte(`{ "foo": "bar" }`)

g := NewWithT(t)

items := map[string]func() any{
"gbytes": func() any {
b := gbytes.NewBuffer()
tests := []struct {
name string
fn func() any
expectedType reflect.Kind
}{
{
name: "gbytes",
fn: func() any {
b := gbytes.NewBuffer()

_, err := b.Write(typeTestData)
g.Expect(err).ShouldNot(HaveOccurred())
_, err := b.Write(typeTestData)
g.Expect(err).ShouldNot(HaveOccurred())

return b
return b
},
expectedType: reflect.Map,
},
{
name: "bytes",
fn: func() any {
return typeTestData
},
expectedType: reflect.Map,
},
"bytes": func() any {
return typeTestData
{
name: "string_map",
fn: func() any {
return string(typeTestData)
},
expectedType: reflect.Map,
},
"string": func() any {
return string(typeTestData)
{
name: "string_slice",
fn: func() any {
return `[ "foo", "bar" ]`
},
expectedType: reflect.Slice,
},
"raw-message": func() any {
return json.RawMessage(typeTestData)
{
name: "json.RawMessage",
fn: func() any {
return json.RawMessage(typeTestData)
},
expectedType: reflect.Map,
},
{
name: "io.Reader",
fn: func() any {
return strings.NewReader(string(typeTestData))
},
expectedType: reflect.Map,
},
{
name: "unstructured.Unstructured",
fn: func() any {
return *resources.GvkToUnstructured(gvk.ConfigMap)
},
expectedType: reflect.Map,
},
{
name: "*unstructured.Unstructured",
fn: func() any {
return resources.GvkToUnstructured(gvk.ConfigMap)
},
expectedType: reflect.Map,
},
{
name: "map",
fn: func() any {
return map[string]string{"foo": "bar"}
},
expectedType: reflect.Map,
},
{
name: "slice",
fn: func() any {
return []string{"foo", "bar"}
},
expectedType: reflect.Slice,
},
}

for name, fn := range items {
f := fn

t.Run(name, func(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tt, err := toType(f())
convertedType, err := toType(tt.fn())

g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(tt).Should(Satisfy(func(in any) bool {
return reflect.TypeOf(in).Kind() == reflect.Map
g.Expect(convertedType).Should(Satisfy(func(in any) bool {
return reflect.TypeOf(in).Kind() == tt.expectedType
}))
})
}
Expand Down

0 comments on commit e1457ab

Please sign in to comment.