add self referential tesT

This commit is contained in:
Shreyas Goenka 2024-08-22 17:38:24 +02:00
parent 08133126ae
commit 7c70179091
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 75 additions and 40 deletions

View File

@ -9,23 +9,9 @@ import (
"github.com/stretchr/testify/require"
)
// TODO: Add a test that checks the primitive overrides for reference regexs wor.
// TODO: Add a test that checks the primitive overrides for reference regexs work.
// Basically that the custom override for bundle regex works.
func TestErrorWithTrace(t *testing.T) {
tracker := newTracker()
dummyType := reflect.TypeOf(struct{}{})
err := tracker.errWithTrace("with empty trace", "root")
assert.ErrorContains(t, err, "with empty trace. traversal trace: root")
tracker.push(dummyType, "resources")
err = tracker.errWithTrace("with depth = 1", "root")
assert.ErrorContains(t, err, "with depth = 1. traversal trace: root -> resources")
tracker.push(dummyType, "pipelines")
tracker.push(dummyType, "datasets")
err = tracker.errWithTrace("with depth = 4", "root")
assert.ErrorContains(t, err, "with depth = 4. traversal trace: root -> resources -> pipelines -> datasets")
}
func TestGenericSchema(t *testing.T) {
type Person struct {
@ -599,20 +585,3 @@ func TestErrorIfStructRefersToItself(t *testing.T) {
_, err := New(reflect.TypeOf(elem), nil)
assert.ErrorContains(t, err, "cycle detected. traversal trace: root -> my_foo")
}
func TestErrorIfStructHasLoop(t *testing.T) {
type Apple struct {
MyVal int `json:"my_val"`
MyMango struct {
MyGuava struct {
MyPapaya struct {
MyApple *Apple `json:"my_apple"`
} `json:"my_papaya"`
} `json:"my_guava"`
} `json:"my_mango"`
}
elem := Apple{}
_, err := New(reflect.TypeOf(elem), nil)
assert.ErrorContains(t, err, "cycle detected. traversal trace: root -> my_mango -> my_guava -> my_papaya -> my_apple")
}

View File

@ -1,7 +1,6 @@
package jsonschema
import (
"encoding/json"
"reflect"
"testing"
@ -314,12 +313,69 @@ func TestFromTypeRecursive(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, s)
jsonSchema, err := json.MarshalIndent(s, " ", " ")
assert.NoError(t, err)
// jsonSchema, err := json.MarshalIndent(s, " ", " ")
// assert.NoError(t, err)
expectedJson, err := json.MarshalIndent(expected, " ", " ")
assert.NoError(t, err)
// expectedJson, err := json.MarshalIndent(expected, " ", " ")
// assert.NoError(t, err)
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", string(expectedJson))
// t.Log("[DEBUG] actual: ", string(jsonSchema))
// t.Log("[DEBUG] expected: ", string(expectedJson))
}
func TestFromTypeSelfReferential(t *testing.T) {
selfRef := "#/$defs/github.com/databricks/cli/libs/jsonschema/test_types.Self"
stringRef := "#/$defs/string"
expected := Schema{
Type: "object",
Definitions: map[string]any{
"github.com": map[string]any{
"databricks": map[string]any{
"cli": map[string]any{
"libs": map[string]any{
"jsonschema": map[string]any{
"test_types.Self": Schema{
Type: "object",
Properties: map[string]*Schema{
"self": {
Reference: &selfRef,
},
"s": {
Reference: &stringRef,
},
},
AdditionalProperties: false,
Required: []string{},
},
},
},
},
},
},
"string": Schema{
Type: "string",
},
},
Properties: map[string]*Schema{
"self": {
Reference: &selfRef,
},
},
AdditionalProperties: false,
Required: []string{},
}
s, err := FromType(reflect.TypeOf(test_types.OuterSelf{}), nil)
assert.NoError(t, err)
assert.Equal(t, expected, s)
// jsonSchema, err := json.MarshalIndent(s, " ", " ")
// assert.NoError(t, err)
// expectedJson, err := json.MarshalIndent(expected, " ", " ")
// assert.NoError(t, err)
// t.Log("[DEBUG] actual: ", string(jsonSchema))
// t.Log("[DEBUG] expected: ", string(expectedJson))
}

View File

@ -13,3 +13,13 @@ type Bar struct {
type Outer struct {
Foo Foo `json:"foo"`
}
type Self struct {
Self *Self `json:"self,omitempty"`
S string `json:"s,omitempty"`
}
type OuterSelf struct {
Self Self `json:"self,omitempty"`
}