use subtest for table units (pkg/kubectl)

This commit is contained in:
Guoliang Wang
2018-05-12 17:24:28 +08:00
parent d057795f3b
commit bae074ef38
34 changed files with 1841 additions and 1301 deletions

View File

@@ -58,18 +58,20 @@ func TestSplitAndParseResourceRequest(t *testing.T) {
}
mapper := testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...)
for _, test := range tests {
gotInResource, gotFieldsPath, err := SplitAndParseResourceRequest(test.inresource, mapper)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotInResource, gotFieldsPath, err := SplitAndParseResourceRequest(tt.inresource, mapper)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expectedInResource, gotInResource) && !test.expectedErr {
t.Errorf("%s: expected inresource: %s, got: %s", test.name, test.expectedInResource, gotInResource)
}
if !reflect.DeepEqual(tt.expectedInResource, gotInResource) && !tt.expectedErr {
t.Errorf("%s: expected inresource: %s, got: %s", tt.name, tt.expectedInResource, gotInResource)
}
if !reflect.DeepEqual(test.expectedFieldsPath, gotFieldsPath) && !test.expectedErr {
t.Errorf("%s: expected fieldsPath: %s, got: %s", test.name, test.expectedFieldsPath, gotFieldsPath)
}
if !reflect.DeepEqual(tt.expectedFieldsPath, gotFieldsPath) && !tt.expectedErr {
t.Errorf("%s: expected fieldsPath: %s, got: %s", tt.name, tt.expectedFieldsPath, gotFieldsPath)
}
})
}
}

View File

@@ -33,49 +33,57 @@ func TestFindField(t *testing.T) {
}
tests := []struct {
name string
path []string
err string
expectedPath string
}{
{
name: "test1",
path: []string{},
expectedPath: "OneKind",
},
{
name: "test2",
path: []string{"field1"},
expectedPath: "OneKind.field1",
},
{
name: "test3",
path: []string{"field1", "array"},
expectedPath: "OtherKind.array",
},
{
name: "test4",
path: []string{"field1", "what?"},
err: `field "what?" does not exist`,
},
{
name: "test5",
path: []string{"field1", ""},
err: `field "" does not exist`,
},
}
for _, test := range tests {
path, err := LookupSchemaForField(schema, test.path)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, err := LookupSchemaForField(schema, tt.path)
gotErr := ""
if err != nil {
gotErr = err.Error()
}
gotErr := ""
if err != nil {
gotErr = err.Error()
}
gotPath := ""
if path != nil {
gotPath = path.GetPath().String()
}
gotPath := ""
if path != nil {
gotPath = path.GetPath().String()
}
if gotErr != test.err || gotPath != test.expectedPath {
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
test.path, gotPath, gotErr, test.expectedPath, test.err)
}
if gotErr != tt.err || gotPath != tt.expectedPath {
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
}
})
}
}

View File

@@ -36,45 +36,53 @@ func TestReferenceTypename(t *testing.T) {
}
tests := []struct {
name string
path []string
expected string
}{
{
// Kind is "Object"
name: "test1",
path: []string{},
expected: "Object",
},
{
// Reference is equal to pointed type "Object"
name: "test2",
path: []string{"field1"},
expected: "Object",
},
{
// Reference is equal to pointed type "string"
name: "test3",
path: []string{"field1", "primitive"},
expected: "string",
},
{
// Array of object of reference to string
name: "test4",
path: []string{"field2"},
expected: "[]map[string]string",
},
{
// Array of integer
name: "test5",
path: []string{"field1", "array"},
expected: "[]integer",
},
}
for _, test := range tests {
s, err := LookupSchemaForField(schema, test.path)
if err != nil {
t.Fatalf("Invalid test.path %v: %v", test.path, err)
}
got := GetTypeName(s)
if got != test.expected {
t.Errorf("Got %q, expected %q", got, test.expected)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := LookupSchemaForField(schema, tt.path)
if err != nil {
t.Fatalf("Invalid tt.path %v: %v", tt.path, err)
}
got := GetTypeName(s)
if got != tt.expected {
t.Errorf("Got %q, expected %q", got, tt.expected)
}
})
}
}