Skip to content

Commit

Permalink
Remove shouldError bool from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jun 3, 2022
1 parent 12384c4 commit f474cf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
35 changes: 15 additions & 20 deletions internal/provider/resource_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -395,20 +396,17 @@ func TestResourcePasswordStateUpgradeV0(t *testing.T) {
cases := []struct {
name string
stateV0 map[string]interface{}
shouldError bool
errMsg string
err error
expectedStateV1 map[string]interface{}
}{
{
name: "result is not string",
stateV0: map[string]interface{}{"result": 0},
shouldError: true,
errMsg: "resource password state upgrade failed, result is not a string: int",
name: "result is not string",
stateV0: map[string]interface{}{"result": 0},
err: errors.New("resource password state upgrade failed, result is not a string: int"),
},
{
name: "success",
stateV0: map[string]interface{}{"result": "abc123"},
shouldError: false,
expectedStateV1: map[string]interface{}{"result": "abc123", "bcrypt_hash": "123"},
},
}
Expand All @@ -417,10 +415,10 @@ func TestResourcePasswordStateUpgradeV0(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
actualStateV1, err := resourcePasswordStateUpgradeV0(context.Background(), c.stateV0, nil)

if c.shouldError {
if c.err != nil {
// Check error msg
if !cmp.Equal(c.errMsg, err.Error()) {
t.Errorf("expected: %q, got: %q", c.errMsg, err)
if !cmp.Equal(c.err.Error(), err.Error()) {
t.Errorf("expected: %q, got: %q", c.err.Error(), err)
}
// Check actualStateV1 is nil
if !cmp.Equal(c.expectedStateV1, actualStateV1) {
Expand Down Expand Up @@ -453,20 +451,17 @@ func TestResourcePasswordStateUpgradeV1(t *testing.T) {
cases := []struct {
name string
stateV1 map[string]interface{}
shouldError bool
errMsg string
err error
expectedStateV2 map[string]interface{}
}{
{
name: "number is not bool",
stateV1: map[string]interface{}{"number": 0},
shouldError: true,
errMsg: "resource password state upgrade failed, number is not a boolean: int",
name: "number is not bool",
stateV1: map[string]interface{}{"number": 0},
err: errors.New("resource password state upgrade failed, number is not a boolean: int"),
},
{
name: "success",
stateV1: map[string]interface{}{"number": true},
shouldError: false,
expectedStateV2: map[string]interface{}{"number": true, "numeric": true},
},
}
Expand All @@ -475,9 +470,9 @@ func TestResourcePasswordStateUpgradeV1(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
actualStateV2, err := resourcePasswordStateUpgradeV1(context.Background(), c.stateV1, nil)

if c.shouldError {
if !cmp.Equal(c.errMsg, err.Error()) {
t.Errorf("expected: %q, got: %q", c.errMsg, err)
if c.err != nil {
if !cmp.Equal(c.err.Error(), err.Error()) {
t.Errorf("expected: %q, got: %q", c.err.Error(), err)
}
if !cmp.Equal(c.expectedStateV2, actualStateV2) {
t.Errorf("expected: %+v, got: %+v", c.expectedStateV2, err)
Expand Down
18 changes: 8 additions & 10 deletions internal/provider/resource_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -347,20 +348,17 @@ func TestResourceStringStateUpgradeV1(t *testing.T) {
cases := []struct {
name string
stateV1 map[string]interface{}
shouldError bool
errMsg string
err error
expectedStateV2 map[string]interface{}
}{
{
name: "number is not bool",
stateV1: map[string]interface{}{"number": 0},
shouldError: true,
errMsg: "resource string state upgrade failed, number is not a boolean: int",
name: "number is not bool",
stateV1: map[string]interface{}{"number": 0},
err: errors.New("resource string state upgrade failed, number is not a boolean: int"),
},
{
name: "success",
stateV1: map[string]interface{}{"number": true},
shouldError: false,
expectedStateV2: map[string]interface{}{"number": true, "numeric": true},
},
}
Expand All @@ -369,9 +367,9 @@ func TestResourceStringStateUpgradeV1(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
actualStateV2, err := resourceStringStateUpgradeV1(context.Background(), c.stateV1, nil)

if c.shouldError {
if !cmp.Equal(c.errMsg, err.Error()) {
t.Errorf("expected: %q, got: %q", c.errMsg, err)
if c.err != nil {
if !cmp.Equal(c.err.Error(), err.Error()) {
t.Errorf("expected: %q, got: %q", c.err.Error(), err)
}
if !cmp.Equal(c.expectedStateV2, actualStateV2) {
t.Errorf("expected: %+v, got: %+v", c.expectedStateV2, err)
Expand Down

0 comments on commit f474cf8

Please sign in to comment.