Skip to content

Commit

Permalink
moved FloatAtLeast to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Nov 14, 2018
1 parent 359c273 commit 900b6a2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 104 deletions.
21 changes: 0 additions & 21 deletions azurerm/helpers/azure/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package azure
import (
"fmt"
"regexp"

"github.com/hashicorp/terraform/helper/schema"
)

func ValidateResourceID(i interface{}, k string) (_ []string, errors []error) {
Expand Down Expand Up @@ -60,22 +58,3 @@ func ValidateMsSqlServiceName(i interface{}, k string) (_ []string, errors []err

return nil, errors
}

// FloatAtLeast returns a SchemaValidateFunc which tests if the provided value
// is of type float64 and is at least min (inclusive)
func FloatAtLeast(min float64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(float64)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be float64", k))
return nil, errors
}

if v < min {
errors = append(errors, fmt.Errorf("expected %s to be at least (%f), got %f", k, min, v))
return nil, errors
}

return nil, errors
}
}
81 changes: 0 additions & 81 deletions azurerm/helpers/azure/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,84 +148,3 @@ func TestAzureValidateMsSqlServiceName(t *testing.T) {
})
}
}

func TestAzureFloatAtLeast(t *testing.T) {
cases := []struct {
Name string
MinValue float64
ActualValue float64
Errors int
}{
{
Name: "Min_Full_Stop_Zero_Greater",
MinValue: 0.0,
ActualValue: 1.0,
Errors: 0,
},
{
Name: "Min_One_Full_Stop_Zero_Lesser",
MinValue: 1.0,
ActualValue: 0.0,
Errors: 1,
},
{
Name: "Min_Full_Stop_Two_Five_Greater",
MinValue: 0.25,
ActualValue: 0.26,
Errors: 0,
},
{
Name: "Min_Full_Stop_Two_Five_Equal",
MinValue: 0.25,
ActualValue: 0.25,
Errors: 0,
},
{
Name: "Min_Full_Stop_Two_Five_Lesser",
MinValue: 0.25,
ActualValue: 0.24,
Errors: 1,
},
{
Name: "Min_Full_Stop_Long_Zero_Lesser",
MinValue: 0.0000000000000000000000000000000000000001,
ActualValue: 0,
Errors: 1,
},
{
Name: "Min_Full_Stop_Long_Greater",
MinValue: 0.0000000000000000000000000000000000000001,
ActualValue: -0,
Errors: 1,
},
{
Name: "Min_Negative_Full_Stop_Two_Five_Greater",
MinValue: -0.25,
ActualValue: 1,
Errors: 0,
},
{
Name: "Min_Zero_No_Full_Stop_Equal",
MinValue: 0,
ActualValue: -0,
Errors: 0,
},
{
Name: "Min_Negative_Full_Stop_Two_Five_Lesser",
MinValue: -0.25,
ActualValue: -0.26,
Errors: 1,
},
}

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
validateFunc := FloatAtLeast(tc.MinValue)
_, errors := validateFunc(tc.ActualValue, "floatValue")

if len(errors) < tc.Errors {
t.Fatalf("Expected FloatAtLeast to have %d not %d errors for %q", tc.Errors, len(errors), tc.Name)
}
})
}
}
25 changes: 25 additions & 0 deletions azurerm/helpers/validate/float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package validate

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)

// FloatAtLeast returns a SchemaValidateFunc which tests if the provided value
// is of type float64 and is at least min (inclusive)
func FloatAtLeast(min float64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(float64)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be float64", k))
return nil, errors
}

if v < min {
errors = append(errors, fmt.Errorf("expected %s to be at least (%f), got %f", k, min, v))
return nil, errors
}

return nil, errors
}
}
83 changes: 83 additions & 0 deletions azurerm/helpers/validate/float_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package validate

import "testing"

func TestAzureFloatAtLeast(t *testing.T) {
cases := []struct {
Name string
MinValue float64
ActualValue float64
Errors int
}{
{
Name: "Min_Full_Stop_Zero_Greater",
MinValue: 0.0,
ActualValue: 1.0,
Errors: 0,
},
{
Name: "Min_One_Full_Stop_Zero_Lesser",
MinValue: 1.0,
ActualValue: 0.0,
Errors: 1,
},
{
Name: "Min_Full_Stop_Two_Five_Greater",
MinValue: 0.25,
ActualValue: 0.26,
Errors: 0,
},
{
Name: "Min_Full_Stop_Two_Five_Equal",
MinValue: 0.25,
ActualValue: 0.25,
Errors: 0,
},
{
Name: "Min_Full_Stop_Two_Five_Lesser",
MinValue: 0.25,
ActualValue: 0.24,
Errors: 1,
},
{
Name: "Min_Full_Stop_Long_Zero_Lesser",
MinValue: 0.0000000000000000000000000000000000000001,
ActualValue: 0,
Errors: 1,
},
{
Name: "Min_Full_Stop_Long_Greater",
MinValue: 0.0000000000000000000000000000000000000001,
ActualValue: -0,
Errors: 1,
},
{
Name: "Min_Negative_Full_Stop_Two_Five_Greater",
MinValue: -0.25,
ActualValue: 1,
Errors: 0,
},
{
Name: "Min_Zero_No_Full_Stop_Equal",
MinValue: 0,
ActualValue: -0,
Errors: 0,
},
{
Name: "Min_Negative_Full_Stop_Two_Five_Lesser",
MinValue: -0.25,
ActualValue: -0.26,
Errors: 1,
},
}

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
_, errors := FloatAtLeast(tc.MinValue)(tc.ActualValue, "floatValue")

if len(errors) < tc.Errors {
t.Fatalf("Expected FloatAtLeast to have %d not %d errors for %q", tc.Errors, len(errors), tc.Name)
}
})
}
}
5 changes: 3 additions & 2 deletions azurerm/resource_arm_mssql_elasticpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -105,13 +106,13 @@ func resourceArmMsSqlElasticPool() *schema.Resource {
"min_capacity": {
Type: schema.TypeFloat,
Required: true,
ValidateFunc: azure.FloatAtLeast(0.0),
ValidateFunc: validate.FloatAtLeast(0.0),
},

"max_capacity": {
Type: schema.TypeFloat,
Required: true,
ValidateFunc: azure.FloatAtLeast(0.0),
ValidateFunc: validate.FloatAtLeast(0.0),
},
},
},
Expand Down

0 comments on commit 900b6a2

Please sign in to comment.