@ -20,6 +20,7 @@ import (
"bytes"
"bytes"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"strings"
"testing"
"testing"
"text/template"
"text/template"
@ -168,6 +169,33 @@ func TestReadSchemaFile(t *testing.T) {
matchSchema ( t , data )
matchSchema ( t , data )
}
}
func TestReadSchematizedValues ( t * testing . T ) {
_ , err := ReadValuesFile ( "./testdata/test-values.yaml" )
if err != nil {
t . Errorf ( "Got the following unexpected error while reading schematized values:\n%v" , err )
}
}
func TestReadSchematizedValuesNegative ( t * testing . T ) {
_ , err := ReadValuesFile ( "./testdata/test-values-negative.yaml" )
if err == nil {
t . Errorf ( "Expected an error, but got none" )
}
errString := err . Error ( )
if ! strings . Contains ( errString , "The values.yaml is not valid. see errors :" ) {
t . Errorf ( "Error string does not contain expected string: \"The values.yaml is not valid. see errors :\"" )
}
if ! strings . Contains ( errString , "- (root): employmentInfo is required" ) {
t . Errorf ( "Error string does not contain expected string: \"- (root): employmentInfo is required\"" )
}
if ! strings . Contains ( errString , "- age: Must be greater than or equal to 0/1" ) {
t . Errorf ( "Error string does not contain expected string: \"- age: Must be greater than or equal to 0/1\"" )
}
}
func ExampleValues ( ) {
func ExampleValues ( ) {
doc := `
doc := `
title : "Moby Dick"
title : "Moby Dick"
@ -454,17 +482,17 @@ properties:
type : string
type : string
lastname :
lastname :
type : string
type : string
likes - c offee:
likes C offee:
type : boolean
type : boolean
age :
age :
description : Age
description : Age
type : integer
type : integer
minimum : 0
minimum : 0
employment - i nfo:
employment I nfo:
type : object
type : object
properties :
properties :
salary :
salary :
type : float
type : number
minimum : 0
minimum : 0
title :
title :
type : string
type : string
@ -472,21 +500,25 @@ properties:
- salary
- salary
addresses :
addresses :
description : List of addresses
description : List of addresses
type : list [ object ]
type : array
properties :
items :
city :
type : object
type : string
properties :
street :
city :
type : string
type : string
number :
street :
type : number
type : string
phone - numbers :
number :
type : list [ string ]
type : number
phoneNumbers :
type : array
items :
type : string
required :
required :
- firstname
- firstname
- lastname
- lastname
- addresses
- addresses
- employment - i nfo
- employment I nfo
`
`
data , err := ReadSchema ( [ ] byte ( schemaTest ) )
data , err := ReadSchema ( [ ] byte ( schemaTest ) )
if err != nil {
if err != nil {
@ -496,154 +528,109 @@ required:
}
}
func matchSchema ( t * testing . T , data Schema ) {
func matchSchema ( t * testing . T , data Schema ) {
if data .Title != "Values" {
if data ["title" ] != "Values" {
t . Errorf ( "Expected .title to be 'Values', got '%s'" , data .Title )
t . Errorf ( "Expected .title to be 'Values', got '%s'" , data ["title" ] )
}
}
if data . Type != "object" {
if data [ "type" ] != "object" {
t . Errorf ( "Expected .type to be 'object', got '%s'" , data . Type )
t . Errorf ( "Expected .type to be 'object', got '%s'" , data [ "type" ] )
}
expectedRequired := [ ] string {
"firstname" ,
"lastname" ,
"addresses" ,
"employment-info" ,
}
if len ( data . Required ) != 4 {
t . Errorf ( "Expected length of .required to be 4, got %d" , len ( data . Required ) )
}
}
if ! assertEqualSlices ( data . Required , expectedRequired ) {
if o , err := ttpl ( "{{len .required}}" , data ) ; err != nil {
t . Errorf ( "Expected .required to be %v, got %v" , expectedRequired , data . Required )
t . Errorf ( "len required: %s" , err )
} else if o != "4" {
t . Errorf ( "Expected length of .required to be 4, got %s" , o )
}
}
var ok bool
property := ".required"
var firstname * Schema
expected := "[firstname lastname addresses employmentInfo]"
if firstname , ok = data . Properties [ "firstname" ] ; ! ok {
assertEqualProperty ( t , property , expected , data )
t . Errorf ( "Expected property '.properties.firstname' is missing" )
}
if firstname . Description != "First name" {
t . Errorf ( "Expected .properties.firstname.description to be 'First name', got '%s'" , firstname . Description )
}
if firstname . Type != "string" {
t . Errorf ( "Expected .properties.firstname.type to be 'string', got '%s'" , firstname . Type )
}
var lastname * Schema
property = ".properties.firstname.description"
if lastname , ok = data . Properties [ "lastname" ] ; ! ok {
expected = "First name"
t . Errorf ( "Expected property '.properties.lastname' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if lastname . Type != "string" {
t . Errorf ( "Expected .properties.lastname.type to be 'string', got '%s'" , lastname . Type )
}
var likesCoffee * Schema
property = ".properties.firstname.type"
if likesCoffee , ok = data . Properties [ "likes-coffee" ] ; ! ok {
expected = "string"
t . Errorf ( "Expected property '.properties.likes-coffee' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if likesCoffee . Type != "boolean" {
t . Errorf ( "Expected .properties.likes-coffee.type to be 'boolean', got '%s'" , likesCoffee . Type )
}
var age * Schema
property = ".properties.lastname.type"
if age , ok = data . Properties [ "age" ] ; ! ok {
expected = "string"
t . Errorf ( "Expected property '.properties.age' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if age . Description != "Age" {
t . Errorf ( "Expected .properties.age.description to be 'Age', got '%s'" , age . Description )
}
if age . Type != "integer" {
t . Errorf ( "Expected .properties.age.type to be 'string', got '%s'" , age . Type )
}
if age . Minimum != 0 {
t . Errorf ( "Expected .properties.age.minimum to be 0, got %d" , age . Minimum )
}
var employmentInfo * Schema
property = ".properties.likesCoffee.type"
if employmentInfo , ok = data . Properties [ "employment-info" ] ; ! ok {
expected = "boolean"
t . Errorf ( "Expected property '.properties.employment-info' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if employmentInfo . Type != "object" {
t . Errorf ( "Expected .properties.employment-info.type to be 'object', got '%s'" , employmentInfo . Type )
}
if len ( employmentInfo . Required ) != 1 {
t . Errorf ( "Expected length of .properties.employment-info.required to be 1, got %d" , len ( employmentInfo . Required ) )
}
if ! assertEqualSlices ( employmentInfo . Required , [ ] string { "salary" } ) {
t . Errorf ( "Expected .properties.employment-info.required to be %v, got %v" , [ ] string { "salary" } , data . Required )
}
var salary * Schema
property = ".properties.age.description"
if salary , ok = employmentInfo . Properties [ "salary" ] ; ! ok {
expected = "Age"
t . Errorf ( "Expected property '.properties.employment-info.properties.salary' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if salary . Type != "float" {
t . Errorf ( "Expected .properties.employment-info.properties.salary.type to be 'float', got '%s'" , salary . Type )
}
if salary . Minimum != 0 {
t . Errorf ( "Expected .properties.employment-info.properties.salary.minimum to be 0, got %d" , salary . Minimum )
}
var title * Schema
property = ".properties.age.type"
if title , ok = employmentInfo . Properties [ "title" ] ; ! ok {
expected = "integer"
t . Errorf ( "Expected property '.properties.employment-info.properties.title' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if title . Type != "string" {
t . Errorf ( "Expected .properties.employment-info.properties.title.type to be 'string', got '%s'" , title . Type )
}
var addresses * Schema
property = ".properties.age.minimum"
if addresses , ok = data . Properties [ "addresses" ] ; ! ok {
expected = "0"
t . Errorf ( "Expected property '.properties.addresses' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if addresses . Type != "list[object]" {
t . Errorf ( "Expected .properties.addresses.type to be 'list[object]', got '%s'" , addresses . Type )
}
if addresses . Description != "List of addresses" {
t . Errorf ( "Expected .properties.addresses.description to be 'List of addresses', got '%s'" , addresses . Description )
}
var city * Schema
property = ".properties.employmentInfo.type"
if city , ok = addresses . Properties [ "city" ] ; ! ok {
expected = "object"
t . Errorf ( "Expected property '.properties.addresses.properties.city' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if city . Type != "string" {
t . Errorf ( "Expected .properties.addresses.properties.city.type to be 'string', got '%s'" , city . Type )
}
var street * Schema
property = ".properties.employmentInfo.required"
if street , ok = addresses . Properties [ "street" ] ; ! ok {
expected = "[salary]"
t . Errorf ( "Expected property '.properties.addresses.properties.street' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if street . Type != "string" {
t . Errorf ( "Expected .properties.addresses.properties.street.type to be 'string', got '%s'" , street . Type )
}
var number * Schema
property = ".properties.employmentInfo.properties.salary.type"
if number , ok = addresses . Properties [ "number" ] ; ! ok {
expected = "number"
t . Errorf ( "Expected property '.properties.addresses.properties.number' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if number . Type != "number" {
t . Errorf ( "Expected .properties.addresses.properties.number.type to be 'number', got '%s'" , number . Type )
}
var phoneNumbers * Schema
property = ".properties.employmentInfo.properties.salary.minimum"
if phoneNumbers , ok = data . Properties [ "phone-numbers" ] ; ! ok {
expected = "0"
t . Errorf ( "Expected property '.properties.phone-numbers' is missing" )
assertEqualProperty ( t , property , expected , data )
}
if phoneNumbers . Type != "list[string]" {
property = ".properties.employmentInfo.properties.title.type"
t . Errorf ( "Expected .properties.phone-numbers.type to be 'list[object]', got '%s'" , addresses . Type )
expected = "string"
}
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.description"
expected = "List of addresses"
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.type"
expected = "array"
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.items.type"
expected = "object"
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.items.properties.city.type"
expected = "string"
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.items.properties.street.type"
expected = "string"
assertEqualProperty ( t , property , expected , data )
property = ".properties.addresses.items.properties.number.type"
expected = "number"
assertEqualProperty ( t , property , expected , data )
property = ".properties.phoneNumbers.type"
expected = "array"
assertEqualProperty ( t , property , expected , data )
property = ".properties.phoneNumbers.items.type"
expected = "string"
assertEqualProperty ( t , property , expected , data )
}
}
func assertEqualSlices ( a , b [ ] string ) bool {
func assertEqualProperty ( t * testing . T , property , expected string , data map [ string ] interface { } ) {
if len ( a ) != len ( b ) {
if o , err := ttpl ( "{{" + property + "}}" , data ) ; err != nil {
return false
t . Errorf ( "%s: %s" , property , err )
}
} else if o != expected {
for i := 0 ; i < len ( a ) ; i ++ {
t . Errorf ( "Expected %s to be %s, got %s" , property , expected , o )
if a [ i ] != b [ i ] {
return false
}
}
}
return true
}
}