2014-08-14 18:54:20 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-08-14 18:54:20 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-22 21:40:47 +00:00
|
|
|
package fielderrors
|
2014-08-14 18:54:20 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-22 00:24:53 +00:00
|
|
|
"strings"
|
2014-08-14 18:54:20 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakeFuncs(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2014-11-20 22:11:23 +00:00
|
|
|
fn func() *ValidationError
|
2014-08-20 02:57:48 +00:00
|
|
|
expected ValidationErrorType
|
2014-08-14 18:54:20 +00:00
|
|
|
}{
|
|
|
|
{
|
2014-11-20 22:24:10 +00:00
|
|
|
func() *ValidationError { return NewFieldInvalid("f", "v", "d") },
|
2014-08-20 02:57:48 +00:00
|
|
|
ValidationErrorTypeInvalid,
|
2014-08-14 18:54:20 +00:00
|
|
|
},
|
|
|
|
{
|
2015-06-24 22:09:43 +00:00
|
|
|
func() *ValidationError { return NewFieldValueNotSupported("f", "v", nil) },
|
2014-08-20 02:57:48 +00:00
|
|
|
ValidationErrorTypeNotSupported,
|
2014-08-14 18:54:20 +00:00
|
|
|
},
|
|
|
|
{
|
2014-11-20 22:11:23 +00:00
|
|
|
func() *ValidationError { return NewFieldDuplicate("f", "v") },
|
2014-08-20 02:57:48 +00:00
|
|
|
ValidationErrorTypeDuplicate,
|
2014-08-14 18:54:20 +00:00
|
|
|
},
|
|
|
|
{
|
2014-11-20 22:11:23 +00:00
|
|
|
func() *ValidationError { return NewFieldNotFound("f", "v") },
|
2014-08-20 02:57:48 +00:00
|
|
|
ValidationErrorTypeNotFound,
|
|
|
|
},
|
|
|
|
{
|
2015-03-11 14:57:19 +00:00
|
|
|
func() *ValidationError { return NewFieldRequired("f") },
|
2014-08-20 02:57:48 +00:00
|
|
|
ValidationErrorTypeRequired,
|
2014-08-14 18:54:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
err := testCase.fn()
|
|
|
|
if err.Type != testCase.expected {
|
|
|
|
t.Errorf("expected Type %q, got %q", testCase.expected, err.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 21:06:16 +00:00
|
|
|
func TestValidationErrorUsefulMessage(t *testing.T) {
|
2014-11-20 22:24:10 +00:00
|
|
|
s := NewFieldInvalid("foo", "bar", "deet").Error()
|
2015-01-09 21:06:16 +00:00
|
|
|
t.Logf("message: %v", s)
|
|
|
|
for _, part := range []string{"foo", "bar", "deet", ValidationErrorTypeInvalid.String()} {
|
|
|
|
if !strings.Contains(s, part) {
|
|
|
|
t.Errorf("error message did not contain expected part '%v'", part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type complicated struct {
|
|
|
|
Baz int
|
|
|
|
Qux string
|
|
|
|
Inner interface{}
|
|
|
|
KV map[string]int
|
|
|
|
}
|
2015-01-27 23:55:54 +00:00
|
|
|
s = NewFieldInvalid(
|
2015-01-09 21:06:16 +00:00
|
|
|
"foo",
|
|
|
|
&complicated{
|
|
|
|
Baz: 1,
|
|
|
|
Qux: "aoeu",
|
|
|
|
Inner: &complicated{Qux: "asdf"},
|
|
|
|
KV: map[string]int{"Billy": 2},
|
|
|
|
},
|
2015-01-27 23:55:54 +00:00
|
|
|
"detail",
|
2015-01-09 21:06:16 +00:00
|
|
|
).Error()
|
|
|
|
t.Logf("message: %v", s)
|
|
|
|
for _, part := range []string{
|
2015-01-27 23:55:54 +00:00
|
|
|
"foo", ValidationErrorTypeInvalid.String(),
|
|
|
|
"Baz", "Qux", "Inner", "KV", "detail",
|
2015-01-09 21:06:16 +00:00
|
|
|
"1", "aoeu", "asdf", "Billy", "2",
|
|
|
|
} {
|
|
|
|
if !strings.Contains(s, part) {
|
|
|
|
t.Errorf("error message did not contain expected part '%v'", part)
|
|
|
|
}
|
2014-08-22 00:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 22:35:25 +00:00
|
|
|
func TestErrListFilter(t *testing.T) {
|
|
|
|
list := ValidationErrorList{
|
|
|
|
NewFieldInvalid("test.field", "", ""),
|
|
|
|
NewFieldInvalid("field.test", "", ""),
|
|
|
|
NewFieldDuplicate("test", "value"),
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorTypeMatcher(ValidationErrorTypeDuplicate))) != 2 {
|
|
|
|
t.Errorf("should not filter")
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorTypeMatcher(ValidationErrorTypeInvalid))) != 1 {
|
|
|
|
t.Errorf("should filter")
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("test"))) != 1 {
|
|
|
|
t.Errorf("should filter")
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("test."))) != 2 {
|
|
|
|
t.Errorf("should filter")
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher(""))) != 0 {
|
|
|
|
t.Errorf("should filter")
|
|
|
|
}
|
|
|
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("field."), NewValidationErrorTypeMatcher(ValidationErrorTypeDuplicate))) != 1 {
|
|
|
|
t.Errorf("should filter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 00:24:53 +00:00
|
|
|
func TestErrListPrefix(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2014-11-20 22:11:23 +00:00
|
|
|
Err *ValidationError
|
2014-08-22 00:24:53 +00:00
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
2014-09-03 21:16:00 +00:00
|
|
|
NewFieldNotFound("[0].bar", "value"),
|
2014-08-22 00:24:53 +00:00
|
|
|
"foo[0].bar",
|
|
|
|
},
|
|
|
|
{
|
2014-11-20 22:24:10 +00:00
|
|
|
NewFieldInvalid("field", "value", ""),
|
2014-08-22 00:24:53 +00:00
|
|
|
"foo.field",
|
|
|
|
},
|
|
|
|
{
|
2014-09-03 21:16:00 +00:00
|
|
|
NewFieldDuplicate("", "value"),
|
2014-08-22 00:24:53 +00:00
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2014-10-24 16:43:14 +00:00
|
|
|
errList := ValidationErrorList{testCase.Err}
|
2014-08-22 00:24:53 +00:00
|
|
|
prefix := errList.Prefix("foo")
|
|
|
|
if prefix == nil || len(prefix) != len(errList) {
|
|
|
|
t.Errorf("Prefix should return self")
|
|
|
|
}
|
2014-11-20 22:11:23 +00:00
|
|
|
if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a {
|
2014-08-22 00:24:53 +00:00
|
|
|
t.Errorf("expected %s, got %s", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrListPrefixIndex(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2014-11-20 22:11:23 +00:00
|
|
|
Err *ValidationError
|
2014-08-22 00:24:53 +00:00
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
2014-09-03 21:16:00 +00:00
|
|
|
NewFieldNotFound("[0].bar", "value"),
|
2014-08-22 00:24:53 +00:00
|
|
|
"[1][0].bar",
|
|
|
|
},
|
|
|
|
{
|
2014-11-20 22:24:10 +00:00
|
|
|
NewFieldInvalid("field", "value", ""),
|
2014-08-22 00:24:53 +00:00
|
|
|
"[1].field",
|
|
|
|
},
|
|
|
|
{
|
2014-09-03 21:16:00 +00:00
|
|
|
NewFieldDuplicate("", "value"),
|
2014-08-22 00:24:53 +00:00
|
|
|
"[1]",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2014-10-24 16:43:14 +00:00
|
|
|
errList := ValidationErrorList{testCase.Err}
|
2014-08-22 00:24:53 +00:00
|
|
|
prefix := errList.PrefixIndex(1)
|
|
|
|
if prefix == nil || len(prefix) != len(errList) {
|
|
|
|
t.Errorf("PrefixIndex should return self")
|
|
|
|
}
|
2014-11-20 22:11:23 +00:00
|
|
|
if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a {
|
2014-08-22 00:24:53 +00:00
|
|
|
t.Errorf("expected %s, got %s", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|