Skip to content

Commit

Permalink
Adding further tests to verify behaviour when attribute or summed att…
Browse files Browse the repository at this point in the history
…ribute(s) are null or unknown for int64 atLeastSumOf (#20)
  • Loading branch information
bendbennett committed May 30, 2022
1 parent a56e62f commit ebd2441
Showing 1 changed file with 66 additions and 11 deletions.
77 changes: 66 additions & 11 deletions int64validator/at_least_sum_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
type testCase struct {
val attr.Value
attributesToSumPaths []*tftypes.AttributePath
requestConfigRaw map[string]tftypes.Value
requestConfigRaw map[string]attr.Value
expectError bool
}
tests := map[string]testCase{
Expand All @@ -36,9 +36,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 15),
"two": tftypes.NewValue(tftypes.Number, 15),
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Value: 15},
"two": types.Int64{Value: 15},
},
expectError: true,
},
Expand All @@ -48,9 +48,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 5),
"two": tftypes.NewValue(tftypes.Number, 5),
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Value: 5},
"two": types.Int64{Value: 5},
},
},
"valid integer as Int64 greater than sum of attributes": {
Expand All @@ -59,21 +59,76 @@ func TestAtLeastSumOfValidator(t *testing.T) {
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 4),
"two": tftypes.NewValue(tftypes.Number, 4),
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Value: 4},
"two": types.Int64{Value: 4},
},
},
"valid integer as Int64 greater than sum of attributes, when one summed attribute is null": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Null: true},
"two": types.Int64{Value: 9},
},
},
"valid integer as Int64 does not return error when all attributes are null": {
val: types.Int64{Null: true},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Null: true},
"two": types.Int64{Null: true},
},
},
"valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Unknown: true},
"two": types.Int64{Value: 9},
},
},
"valid integer as Int64 does not return error when all attributes are unknown": {
val: types.Int64{Unknown: true},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
},
requestConfigRaw: map[string]attr.Value{
"one": types.Int64{Unknown: true},
"two": types.Int64{Unknown: true},
},
},
}

for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
reqConf := make(map[string]tftypes.Value, len(test.requestConfigRaw))

for k, v := range test.requestConfigRaw {
val, err := v.ToTerraformValue(context.Background())
if err != nil {
t.Fatalf("could not attr.Value at key:%s to tftypes.Value", k)
}

reqConf[k] = val
}

request := tfsdk.ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
AttributeConfig: test.val,
Config: tfsdk.Config{
Raw: tftypes.NewValue(tftypes.Object{}, test.requestConfigRaw),
Raw: tftypes.NewValue(tftypes.Object{}, reqConf),
Schema: tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {Type: types.Int64Type},
Expand Down

0 comments on commit ebd2441

Please sign in to comment.