Skip to content

Commit 2649046

Browse files
committed
Address review comments
Signed-off-by: Timo Sand <[email protected]>
1 parent c45768c commit 2649046

1 file changed

Lines changed: 26 additions & 41 deletions

File tree

github/resource_github_emu_group_mapping_migration_test.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,17 @@ import (
44
"fmt"
55
"net/http"
66
"net/url"
7-
"strconv"
87
"testing"
98

109
"github.com/google/go-cmp/cmp"
1110
"github.com/google/go-github/v82/github"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13-
)
14-
15-
type (
16-
currentStateFunc func() map[string]any
17-
expectedStateFunc func(t *testing.T) map[string]any
1811
)
1912

2013
var (
2114
testTeamID = 432574718
2215
testGroupID = 1234567890
2316
)
2417

25-
func testResourceGithubEMUGroupMappingInstanceStateDataV0() map[string]any {
26-
return map[string]any{
27-
"id": "teams/test-team/external-groups",
28-
"team_slug": "test-team",
29-
"group_id": testGroupID,
30-
}
31-
}
32-
33-
func testResourceGithubEMUGroupMappingInstanceStateDataV1(t *testing.T) map[string]any {
34-
v0 := testResourceGithubEMUGroupMappingInstanceStateDataV0()
35-
v0["team_id"] = int64(testTeamID)
36-
resourceID, err := buildID(strconv.Itoa(testTeamID), v0["team_slug"].(string), strconv.Itoa(v0["group_id"].(int)))
37-
if err != nil {
38-
t.Fatalf("error building resource ID: %s", err)
39-
}
40-
v0["id"] = resourceID
41-
return v0
42-
}
43-
4418
func buildMockResponsesForMigrationV0toV1() []*mockResponse {
4519
return []*mockResponse{
4620
{
@@ -75,7 +49,7 @@ func buildMockResponsesForMigrationV0toV1() []*mockResponse {
7549
}
7650
}
7751

78-
func TestGithub_MigrateEMUGroupMappingsState(t *testing.T) {
52+
func Test_resourceGithubEMUGroupMappingStateUpgradeV0(t *testing.T) {
7953
t.Parallel()
8054

8155
meta := &Owner{
@@ -84,17 +58,24 @@ func TestGithub_MigrateEMUGroupMappingsState(t *testing.T) {
8458

8559
for _, d := range []struct {
8660
testName string
87-
migrationFunc schema.StateUpgradeFunc
88-
rawState currentStateFunc
89-
want expectedStateFunc
61+
rawState map[string]any
62+
want map[string]any
9063
buildMockResponses func() []*mockResponse
9164
shouldError bool
9265
}{
9366
{
94-
testName: "migrates v0 to v1",
95-
migrationFunc: resourceGithubEMUGroupMappingStateUpgradeV0,
96-
rawState: testResourceGithubEMUGroupMappingInstanceStateDataV0,
97-
want: testResourceGithubEMUGroupMappingInstanceStateDataV1,
67+
testName: "migrates v0 to v1",
68+
rawState: map[string]any{
69+
"id": "teams/test-team/external-groups",
70+
"team_slug": "test-team",
71+
"group_id": testGroupID,
72+
},
73+
want: map[string]any{
74+
"id": "432574718:test-team:1234567890",
75+
"team_slug": "test-team",
76+
"team_id": int64(testTeamID),
77+
"group_id": testGroupID,
78+
},
9879
buildMockResponses: buildMockResponsesForMigrationV0toV1,
9980
shouldError: false,
10081
},
@@ -113,14 +94,18 @@ func TestGithub_MigrateEMUGroupMappingsState(t *testing.T) {
11394
client.BaseURL = u
11495
meta.v3client = client
11596

116-
currentState := d.rawState()
117-
got, err := d.migrationFunc(t.Context(), currentState, meta)
118-
expectedState := d.want(t)
119-
if (err != nil) != d.shouldError {
120-
t.Fatalf("unexpected error state: %s", err.Error())
97+
currentState := d.rawState
98+
got, err := resourceGithubEMUGroupMappingStateUpgradeV0(t.Context(), currentState, meta)
99+
expectedState := d.want
100+
didError := err != nil
101+
if d.shouldError && !didError {
102+
t.Fatalf("state upgrade should have returned an error. Instead got: %#v", got)
103+
}
104+
if !d.shouldError && didError {
105+
t.Fatalf("state upgrade should not have returned an error. Instead got: %s", err.Error())
121106
}
122-
if diff := cmp.Diff(expectedState, got); !d.shouldError && diff != "" {
123-
t.Fatal(diff)
107+
if diff := cmp.Diff(expectedState, got); diff != "" {
108+
t.Fatalf("state upgrade returned unexpected state. Diff: %s", diff)
124109
}
125110
})
126111
}

0 commit comments

Comments
 (0)