mirror of https://github.com/databricks/cli.git
added tests for slice of [primitive, slice, map, struct]
This commit is contained in:
parent
ef6737d7de
commit
f52d090166
|
@ -84,6 +84,7 @@ func NewSchema(golangType reflect.Type) (*Schema, error) {
|
||||||
Type: rootProp.Type,
|
Type: rootProp.Type,
|
||||||
Properties: rootProp.Properties,
|
Properties: rootProp.Properties,
|
||||||
AdditionalProperties: rootProp.AdditionalProperties,
|
AdditionalProperties: rootProp.AdditionalProperties,
|
||||||
|
Items: rootProp.Items,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,8 +235,10 @@ func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debu
|
||||||
}
|
}
|
||||||
items = &Schema{
|
items = &Schema{
|
||||||
// TODO: Add a test for slice of object
|
// TODO: Add a test for slice of object
|
||||||
Type: elemJavascriptType,
|
Type: elemJavascriptType,
|
||||||
Properties: elemProps.Properties,
|
Properties: elemProps.Properties,
|
||||||
|
AdditionalProperties: elemProps.AdditionalProperties,
|
||||||
|
Items: elemProps.Items,
|
||||||
}
|
}
|
||||||
// TODO: what if there is an array of maps. Add additional properties to
|
// TODO: what if there is an array of maps. Add additional properties to
|
||||||
// TODO: what if there are maps of maps
|
// TODO: what if there are maps of maps
|
||||||
|
|
|
@ -403,6 +403,110 @@ func TestMapOfSliceSchema(t *testing.T) {
|
||||||
assert.Equal(t, expected, string(jsonSchema))
|
assert.Equal(t, expected, string(jsonSchema))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSliceOfPrimitivesSchema(t *testing.T) {
|
||||||
|
var elem []float32
|
||||||
|
|
||||||
|
schema, err := NewSchema(reflect.TypeOf(elem))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
expected :=
|
||||||
|
`{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "number"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expected)
|
||||||
|
assert.Equal(t, expected, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSliceOfSliceSchema(t *testing.T) {
|
||||||
|
var elem [][]string
|
||||||
|
|
||||||
|
schema, err := NewSchema(reflect.TypeOf(elem))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
expected :=
|
||||||
|
`{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expected)
|
||||||
|
assert.Equal(t, expected, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSliceOfMapSchema(t *testing.T) {
|
||||||
|
var elem []map[string]int
|
||||||
|
|
||||||
|
schema, err := NewSchema(reflect.TypeOf(elem))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
expected :=
|
||||||
|
`{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "number"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expected)
|
||||||
|
assert.Equal(t, expected, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSliceOfStructSchema(t *testing.T) {
|
||||||
|
type Foo struct {
|
||||||
|
MyInt int `json:"my_int"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var elem []Foo
|
||||||
|
|
||||||
|
schema, err := NewSchema(reflect.TypeOf(elem))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
expected :=
|
||||||
|
`{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"my_int": {
|
||||||
|
"type": "number"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expected)
|
||||||
|
assert.Equal(t, expected, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
func TestObjectSchema(t *testing.T) {
|
func TestObjectSchema(t *testing.T) {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
Loading…
Reference in New Issue