Skip to content

Commit

Permalink
Adding tests for list attribute imports, schema, and model field (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Oct 24, 2023
1 parent 9e4eea2 commit 08826d9
Showing 1 changed file with 173 additions and 0 deletions.
173 changes: 173 additions & 0 deletions internal/datasource_generate/list_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,110 @@ func TestGeneratorListAttribute_Imports(t *testing.T) {
},
},
},
"associated-external-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ListAttribute",
},
},
},
expected: []code.Import{
{
Path: "github.com/hashicorp/terraform-plugin-framework/types",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
},
},
"associated-external-type-with-import": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Import: &code.Import{
Path: "github.com/api",
},
Type: "*api.ListAttribute",
},
},
},
expected: []code.Import{
{
Path: "github.com/hashicorp/terraform-plugin-framework/types",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
{
Path: "github.com/api",
},
},
},
"associated-external-type-with-custom-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Import: &code.Import{
Path: "github.com/api",
},
Type: "*api.ListAttribute",
},
},
CustomType: &specschema.CustomType{
Import: &code.Import{
Path: "github.com/my_account/my_project/attribute",
},
},
},
expected: []code.Import{
{
Path: "github.com/my_account/my_project/attribute",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
{
Path: "github.com/api",
},
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -605,6 +709,44 @@ CustomType: my_custom_type,
},`,
},

"associated-external-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ListAttribute",
},
},
ElementType: specschema.ElementType{
String: &specschema.StringType{},
},
},
expected: `
"list_attribute": schema.ListAttribute{
CustomType: ListAttributeType{
types.ListType{
ElemType: types.StringType,
},
},
},`,
},

"custom-type-overriding-associated-external-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ListAttribute",
},
},
CustomType: &specschema.CustomType{
Type: "my_custom_type",
},
},
expected: `
"list_attribute": schema.ListAttribute{
CustomType: my_custom_type,
},`,
},

"required": {
input: GeneratorListAttribute{
ListAttribute: schema.ListAttribute{
Expand Down Expand Up @@ -961,6 +1103,37 @@ func TestGeneratorListAttribute_ModelField(t *testing.T) {
TfsdkName: "list_attribute",
},
},
"associated-external-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.BoolAttribute",
},
},
},
expected: model.Field{
Name: "ListAttribute",
ValueType: "ListAttributeValue",
TfsdkName: "list_attribute",
},
},
"custom-type-overriding-associated-external-type": {
input: GeneratorListAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.BoolAttribute",
},
},
CustomType: &specschema.CustomType{
ValueType: "my_custom_value_type",
},
},
expected: model.Field{
Name: "ListAttribute",
ValueType: "my_custom_value_type",
TfsdkName: "list_attribute",
},
},
}

for name, testCase := range testCases {
Expand Down

0 comments on commit 08826d9

Please sign in to comment.