-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor test to use test driven table
- added new coverage for different scenarios
- Loading branch information
Showing
1 changed file
with
86 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,113 @@ | ||
package openapi | ||
|
||
import ( | ||
"errors" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestValidateInput(t *testing.T) { | ||
|
||
// TODO: expand this test and add more coverage, it's missing validations checks | ||
// created this set of tests to enable better understanding about the set up and further extension, the code will | ||
// have to be refactored at some point removing boiler plate etc | ||
|
||
Convey("Given a data source factory and a resourceLocalData populated with a correct filter", t, func() { | ||
// This is representing the corresponding schema for a valid swagger model definition | ||
dataSourceFactory := dataSourceFactory{ | ||
openAPIResource: &specStubResource{ | ||
schemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
&specSchemaDefinitionProperty{ | ||
Name: "id", | ||
ReadOnly: true, | ||
}, | ||
&specSchemaDefinitionProperty{ | ||
Name: "label", | ||
}, | ||
testCases := []struct { | ||
name string | ||
specSchemaDefinition *specSchemaDefinition | ||
filtersInput map[string]interface{} | ||
expectedError error | ||
}{ | ||
{ | ||
name: "data source populated with a correct filters", | ||
specSchemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
newStringSchemaDefinitionPropertyWithDefaults("owner", "", false, true, nil), | ||
newStringSchemaDefinitionPropertyWithDefaults("label", "", false, true, nil), | ||
}, | ||
}, | ||
filtersInput: map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "owner", | ||
"values": []string{"some_owner"}, | ||
}, | ||
{ | ||
"name": "label", | ||
"values": []string{"label_to_fetch"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
resourceSchema := dataSourceFactory.createTerraformDataSourceSchema() | ||
// This is the input we would expect from the user | ||
resourceDataMap := map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "label", | ||
"values": []string{"label_to_fetch"}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "data source populated with an incorrect filter containing a property that does not match any of the schema definition", | ||
specSchemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
newStringSchemaDefinitionPropertyWithDefaults("label", "", false, true, nil), | ||
}, | ||
}, | ||
} | ||
resourceLocalData := schema.TestResourceDataRaw(t, resourceSchema, resourceDataMap) | ||
Convey("When create is called with resource data", func() { | ||
err := dataSourceFactory.validateInput(resourceLocalData) | ||
Convey("Then the error returned should be nil", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
}) | ||
}) | ||
|
||
Convey("Given a data source factory and a resourceLocalData populated with an incorrect filter containing a property that does not match any of the schema definition", t, func() { | ||
// This is representing the corresponding schema for a valid swagger model definition | ||
dataSourceFactory := dataSourceFactory{ | ||
openAPIResource: &specStubResource{ | ||
schemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
&specSchemaDefinitionProperty{ | ||
Name: "id", | ||
ReadOnly: true, | ||
}, | ||
&specSchemaDefinitionProperty{ | ||
Name: "label", | ||
}, | ||
filtersInput: map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "non_matching_property_name", | ||
"values": []string{"label_to_fetch"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
resourceSchema := dataSourceFactory.createTerraformDataSourceSchema() | ||
// This is the input we would expect from the user | ||
resourceDataMap := map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "non_matching_property_name", | ||
"values": []string{"label_to_fetch"}, | ||
expectedError: errors.New("filter name does not match any of the schema properties: property with name 'non_matching_property_name' not existing in resource schema definition"), | ||
}, | ||
{ | ||
name: "data source populated with an incorrect filter containing a property that is not a primitive", | ||
specSchemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
newListSchemaDefinitionPropertyWithDefaults("not_primitive", "", false, true, false, nil, typeString, nil), | ||
newStringSchemaDefinitionPropertyWithDefaults("label", "", false, true, nil), | ||
}, | ||
}, | ||
} | ||
resourceLocalData := schema.TestResourceDataRaw(t, resourceSchema, resourceDataMap) | ||
Convey("When create is called with resource data", func() { | ||
err := dataSourceFactory.validateInput(resourceLocalData) | ||
Convey("Then the error returned should be nil", func() { | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
}) | ||
filtersInput: map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "label", | ||
"values": []string{"my_label"}, | ||
}, | ||
{ | ||
"name": "not_primitive", | ||
"values": []string{"filters for non primitive properties are not supported at the moment"}, | ||
}, | ||
}, | ||
}, | ||
expectedError: errors.New("property not supported as as filter: not_primitive"), | ||
}, | ||
{ | ||
name: "data source populated with an incorrect filter containing multiple values for a primitive property", | ||
specSchemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
newStringSchemaDefinitionPropertyWithDefaults("label", "", false, true, nil), | ||
}, | ||
}, | ||
filtersInput: map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "label", | ||
"values": []string{"value1", "value2"}, | ||
}, | ||
}, | ||
}, | ||
expectedError: errors.New("filters for primitive properties can not have more than one value in the values field"), | ||
}, | ||
} | ||
|
||
Convey("Given a data source factory and a resourceLocalData populated with an incorrect filter containing multiple values for a primitive property", t, func() { | ||
// This is representing the corresponding schema for a valid swagger model definition | ||
for _, tc := range testCases { | ||
dataSourceFactory := dataSourceFactory{ | ||
openAPIResource: &specStubResource{ | ||
schemaDefinition: &specSchemaDefinition{ | ||
Properties: specSchemaDefinitionProperties{ | ||
&specSchemaDefinitionProperty{ | ||
Name: "id", | ||
ReadOnly: true, | ||
}, | ||
&specSchemaDefinitionProperty{ | ||
Name: "label", | ||
}, | ||
}, | ||
}, | ||
schemaDefinition: tc.specSchemaDefinition, | ||
}, | ||
} | ||
resourceSchema := dataSourceFactory.createTerraformDataSourceSchema() | ||
// This is the input we would expect from the user | ||
resourceDataMap := map[string]interface{}{ | ||
"filter": []map[string]interface{}{ | ||
{ | ||
"name": "label", | ||
"values": []string{"value1", "value2"}, | ||
}, | ||
}, | ||
resourceLocalData := schema.TestResourceDataRaw(t, resourceSchema, tc.filtersInput) | ||
err := dataSourceFactory.validateInput(resourceLocalData) | ||
if tc.expectedError == nil { | ||
assert.Nil(t, err, tc.name) | ||
} else { | ||
assert.Equal(t, tc.expectedError.Error(), err.Error(), tc.name) | ||
} | ||
resourceLocalData := schema.TestResourceDataRaw(t, resourceSchema, resourceDataMap) | ||
Convey("When create is called with resource data", func() { | ||
err := dataSourceFactory.validateInput(resourceLocalData) | ||
Convey("Then the error returned should be nil", func() { | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |