-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Int32 and Float32 type testing #258
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bfae711
Add Int32 and Float32 type testing
SBGoods ed03e39
Add overflow and underflow tests for Float32
SBGoods bbbb477
Add `MaxFloat32` precision tests for `float64_precision_resource` and…
SBGoods 14e4ff2
Fix tfversioncheck constraints
SBGoods 6154929
Fix tfversioncheck constraints
SBGoods b7f6ec1
Merge branch 'refs/heads/main' into SBGoods/int32-float32-testing
SBGoods 3976874
Update tfversioncheck constraints and comments to use major release v…
SBGoods 70aeb17
Add Int32 and Float32 type tests for schema_resource
SBGoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var _ function.Function = Float32Function{} | ||
|
||
func NewFloat32Function() function.Function { | ||
return &Float32Function{} | ||
} | ||
|
||
type Float32Function struct{} | ||
|
||
func (f Float32Function) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "float32" | ||
} | ||
|
||
func (f Float32Function) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Parameters: []function.Parameter{ | ||
function.Float32Parameter{ | ||
Name: "float32_param", | ||
}, | ||
}, | ||
Return: function.Float32Return{}, | ||
} | ||
} | ||
|
||
func (f Float32Function) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var arg float32 | ||
|
||
resp.Error = req.Arguments.Get(ctx, &arg) | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, arg)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/providerserver" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/plancheck" | ||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestFloat32Function_known(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(1.23) | ||
}`, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownOutputValue("test", knownvalue.Float32Exact(1.23)), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestFloat32Function_null(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(null) | ||
}`, | ||
ExpectError: regexp.MustCompile("Invalid function argument"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestFloat32Function_unknown(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
resource "terraform_data" "test" { | ||
input = provider::framework::float32(1.23) | ||
} | ||
|
||
output "test" { | ||
value = terraform_data.test.output | ||
}`, | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectUnknownOutputValue("test"), | ||
}, | ||
}, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownOutputValue("test", knownvalue.Float32Exact(1.23)), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestFloat32Function_overflow_underflow(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
// float32 overflow | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(3.40282346638528859811704183484516925440e+39) | ||
}`, | ||
ExpectError: regexp.MustCompile("Invalid function argument"), | ||
}, | ||
// float32 negative overflow | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(-3.40282346638528859811704183484516925440e+39) | ||
}`, | ||
ExpectError: regexp.MustCompile("Invalid function argument"), | ||
}, | ||
// float32 underflow | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(1.401298464324817070923729583289916131280e-46) | ||
}`, | ||
ExpectError: regexp.MustCompile("Invalid function argument"), | ||
}, | ||
// float32 negative underflow | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::framework::float32(-1.401298464324817070923729583289916131280e-46) | ||
}`, | ||
ExpectError: regexp.MustCompile("Invalid function argument"), | ||
}, | ||
}, | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ resource.Resource = Float32PrecisionResource{} | ||
|
||
func NewFloat32PrecisionResource() resource.Resource { | ||
return &Float32PrecisionResource{} | ||
} | ||
|
||
// Float32PrecisionResource is for testing Float32/cty.Number quirks | ||
// https://github.com/hashicorp/terraform-plugin-framework/issues/815 | ||
type Float32PrecisionResource struct{} | ||
|
||
func (r Float32PrecisionResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_float32_precision" | ||
} | ||
|
||
func (r Float32PrecisionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"float32_attribute": schema.Float32Attribute{ | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r Float32PrecisionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data Float32PrecisionResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Test semantic equality by losing the precision of the initial *big.Float | ||
data.Float32Attribute = types.Float32Value(data.Float32Attribute.ValueFloat32()) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r Float32PrecisionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data Float32PrecisionResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r Float32PrecisionResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data Float32PrecisionResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Test semantic equality by losing the precision of the initial *big.Float | ||
data.Float32Attribute = types.Float32Value(data.Float32Attribute.ValueFloat32()) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r Float32PrecisionResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
} | ||
|
||
type Float32PrecisionResourceModel struct { | ||
Float32Attribute types.Float32 `tfsdk:"float32_attribute"` | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!