OpenSlides/docs/modelsvalidator/check/check_test.go

127 lines
2.5 KiB
Go
Raw Normal View History

2021-01-06 11:32:16 +01:00
package check_test
2020-11-11 12:02:23 +01:00
import (
"errors"
"strings"
"testing"
2021-01-06 11:32:16 +01:00
"github.com/OpenSlides/Openslides/modelsvalidator/check"
models "github.com/OpenSlides/openslides-models-to-go"
2020-11-11 12:02:23 +01:00
)
const yamlUnknownFieldType = `---
some_model:
field: unknown
`
const yamlNonExistingModel = `---
some_model:
no_other_model:
type: relation
to: not_existing/field
no_other_field:
type: relation
to: other_model/bar
other_model:
foo: string
`
const yamlNonExistingField = `---
some_model:
no_other_field:
type: relation
to: other_model/bar
other_model:
foo: string
`
const yamlDuplicateTemplatePrefix = `---
some_model:
field_$_1: number
field_$_2: number
`
const yamlWrongReverseRelaitonType = `---
some_model:
other_model:
type: relation
to: other_model/field
other_model:
field: HTMLStrict
`
func TestCheck(t *testing.T) {
for _, tt := range []struct {
name string
yaml string
err string
}{
{
"unknown type",
yamlUnknownFieldType,
"Unknown type `unknown` in some_model/field",
},
{
"non-existing model",
yamlNonExistingModel,
"some_model/no_other_model directs to nonexisting model `not_existing`",
},
{
"non-existing Field",
yamlNonExistingField,
"some_model/no_other_field directs to nonexisting collectionfield `other_model/bar`",
},
{
"duplicate template prefix",
yamlDuplicateTemplatePrefix,
"Duplicate template prefix field_ in some_model",
},
{
"wrong reverse relation type",
yamlWrongReverseRelaitonType,
"some_model/other_model directs to `other_model/field`, but it is not a relation, but HTMLStrict",
},
} {
t.Run(tt.name, func(t *testing.T) {
data, err := models.Unmarshal(strings.NewReader(tt.yaml))
if err != nil {
t.Fatalf("Can not unmarshal yaml: %v", err)
}
2021-01-06 11:32:16 +01:00
gotErr := check.Check(data)
2020-11-11 12:02:23 +01:00
if tt.err == "" {
if gotErr != nil {
t.Errorf("Models.Check() returned an unexepcted error: %v", err)
}
return
}
if gotErr == nil {
t.Fatalf("Models.Check() did not return an error, expected: %v", tt.err)
}
2021-01-06 11:32:16 +01:00
var errList *check.ErrorList
2020-11-11 12:02:23 +01:00
if !errors.As(gotErr, &errList) {
t.Fatalf("Models.Check() did not return a ListError, got: %v", gotErr)
}
var found bool
for _, err := range errList.Errs {
2021-01-06 11:32:16 +01:00
var errList *check.ErrorList
2020-11-11 12:02:23 +01:00
if !errors.As(err, &errList) {
continue
}
for _, err := range errList.Errs {
if err.Error() == tt.err {
found = true
}
}
}
if !found {
t.Errorf("Models.Check() returned %v, expected %v", gotErr, tt.err)
}
})
}
}