From b242187eb59cc4b09b0e0f1c00cecd54176effc3 Mon Sep 17 00:00:00 2001 From: David Bloss Date: Fri, 9 Aug 2024 12:15:18 -0500 Subject: [PATCH] break up tests into smaller chunks --- src/cmd/team_test.go | 84 +++++++++++++++++++++++++++++++------------- src/cmd/user_test.go | 79 +++++++++++++++++++++++++++++------------ 2 files changed, 115 insertions(+), 48 deletions(-) diff --git a/src/cmd/team_test.go b/src/cmd/team_test.go index e267c929..b4e32541 100644 --- a/src/cmd/team_test.go +++ b/src/cmd/team_test.go @@ -1,6 +1,7 @@ package cmd_test import ( + "fmt" "testing" "github.com/opslevel/cli/cmd" @@ -13,12 +14,52 @@ const ( ) func Test_TeamCRUD(t *testing.T) { + // Create Team teamToCreate := opslevel.TeamCreateInput{ Name: teamName, Responsibilities: opslevel.RefOf("all the things"), } + teamId, err := createTeam(teamToCreate) + if err != nil { + t.Error(err) + t.FailNow() + } + + // Get Team + createdTeam, err := getTeam(teamId) + if err != nil { + t.Error(err) + t.FailNow() + } + if createdTeam.Name != teamToCreate.Name || + createdTeam.Responsibilities != *teamToCreate.Responsibilities { + t.Errorf("Create 'team' failed, expected team '%+v' but got '%+v'", teamToCreate, createdTeam) + } + + // Update Team + teamToUpdate := opslevel.TeamUpdateInput{ + Name: opslevel.RefOf(createdTeam.Name), + Responsibilities: opslevel.RefOf("new things"), + } + updatedTeamId, err := updateTeam(teamId, teamToUpdate) + if err != nil { + _ = deleteTeam(string(createdTeam.Id)) + t.Error(err) + t.FailNow() + } + if string(createdTeam.Id) != updatedTeamId { + t.Errorf("Update 'team' failed, expected returned ID '%s' but got '%s'", string(createdTeam.Id), updatedTeamId) + } + + // Delete Team + if err = deleteTeam(string(createdTeam.Id)); err != nil { + t.Errorf("Delete 'team' failed, got error '%s'", err) + } +} + +func createTeam(teamToCreate opslevel.TeamCreateInput) (string, error) { if err := writeToYaml(teamFileName, teamToCreate); err != nil { - t.Errorf("Error while writing '%v' to file '%s': %v", teamToCreate, teamFileName, err) + return "", fmt.Errorf("Error while writing '%v' to file '%s': %v", teamToCreate, teamFileName, err) } cliArgs := []string{teamToCreate.Name, "-f", teamFileName} @@ -27,46 +68,39 @@ func Test_TeamCRUD(t *testing.T) { // Create Team createOutput, err := execCmd(Create, "team", cliArgs...) if err != nil { - t.Errorf("Create 'team' failed, got error: %v", err) + return "", fmt.Errorf("Create 'team' failed, got error: %v", err) } - teamId := asString(createOutput) + return asString(createOutput), nil +} - // Get Team +func getTeam(teamId string) (*opslevel.Team, error) { getOutput, err := execCmd(Get, "team", teamId) if err != nil { - t.Errorf("Get 'team' failed, got error: %v", err) + return nil, fmt.Errorf("Get 'team' failed, got error: %v", err) } createdTeam, err := jsonToResource[opslevel.Team](getOutput) if err != nil { - t.Error("Failed to convert JSON from API to 'opslevel.Team' struct") - } - if createdTeam.Name != teamToCreate.Name || - createdTeam.Responsibilities != *teamToCreate.Responsibilities { - t.Errorf("Create 'team' failed, expected team '%+v' but got '%+v'", teamToCreate, createdTeam) + return nil, fmt.Errorf("Failed to convert JSON from API to 'opslevel.Team' struct") } + return createdTeam, err +} - teamToUpdate := opslevel.TeamCreateInput{ - Name: createdTeam.Name, - Responsibilities: opslevel.RefOf("new things"), - } +func updateTeam(teamId string, teamToUpdate opslevel.TeamUpdateInput) (string, error) { if err := writeToYaml(teamFileName, teamToUpdate); err != nil { - t.Errorf("Error while writing '%v' to file '%s': %v", teamToUpdate, teamFileName, err) + return "", fmt.Errorf("Error while writing '%v' to file '%s': %v", teamToUpdate, teamFileName, err) } // Store Update Team stuff to "file" - cliArgs = []string{teamId, "-f", teamFileName} + cliArgs := []string{teamId, "-f", teamFileName} updateOutput, err := execCmd(Update, "team", cliArgs...) if err != nil { - t.Errorf("Update 'team' failed, got error: %v", err) - } - updatedTeamId := asString(updateOutput) - if string(createdTeam.Id) != updatedTeamId { - t.Errorf("Update 'team' failed, expected returned ID '%s' but got '%s'", string(createdTeam.Id), updatedTeamId) + return "", fmt.Errorf("Update 'team' failed, got error: %v", err) } + return asString(updateOutput), nil +} - // Delete Team - if _, err = execCmd(Delete, "team", string(createdTeam.Id)); err != nil { - t.Error(err) - } +func deleteTeam(teamId string) error { + _, err := execCmd(Delete, "team", teamId) + return err } diff --git a/src/cmd/user_test.go b/src/cmd/user_test.go index 624c4845..9669b0b2 100644 --- a/src/cmd/user_test.go +++ b/src/cmd/user_test.go @@ -1,6 +1,7 @@ package cmd_test import ( + "fmt" "strings" "testing" @@ -19,25 +20,18 @@ func Test_UserCRUD(t *testing.T) { UserId: opslevel.UserId{Email: "testcli+pat@opslevel.com"}, Name: userName, } - cliArgs := []string{expectedUser.Email, expectedUser.Name} - cmd.RootCmd.SetArgs(cliArgs) - // Create User - createOutput, err := execCmd(Create, "user", cliArgs...) + userId, err := createUser(expectedUser) if err != nil { - t.Errorf("Create 'user' failed, got error: %v", err) + t.Error(err) + t.FailNow() } - userId := asString(createOutput) // Get User - getOutput, err := execCmd(Get, "user", userId) + createdUser, err := getUser(userId) if err != nil { - t.Errorf("Get 'user' failed, got error: %v", err) - } - - createdUser, err := jsonToResource[opslevel.User](getOutput) - if err != nil { - t.Error("Failed to convert JSON from API to 'opslevel.User' struct") + t.Error(err) + t.FailNow() } if createdUser.Name != expectedUser.Name || createdUser.Email != expectedUser.Email || @@ -46,28 +40,67 @@ func Test_UserCRUD(t *testing.T) { t.Errorf("Create 'user' failed, expected user '%+v' but got '%+v'", expectedUser, createdUser) } + // Update User expectedUpdatedUser := opslevel.User{ UserId: createdUser.UserId, Name: createdUser.Name, Role: opslevel.UserRoleTeamMember, } - if err := writeToYaml(userFileName, expectedUpdatedUser); err != nil { - t.Errorf("Error while writing '%v' to file '%s': %v", expectedUpdatedUser, userFileName, err) - } - - // Store Update User stuff to "file" - cliArgs = []string{expectedUser.Email, "-f", userFileName} - updateOutput, err := execCmd(Update, "user", cliArgs...) + updatedUserId, err := updateUser(string(createdUser.Id), expectedUpdatedUser) if err != nil { - t.Errorf("Update 'user' failed, got error: %v", err) + t.Error(err) + t.FailNow() } - updatedUserId := asString(updateOutput) if string(createdUser.Id) != updatedUserId { t.Errorf("Update 'user' failed, expected returned ID '%s' but got '%s'", string(createdUser.Id), updatedUserId) } // Delete User - if _, err = execCmd(Delete, "user", string(createdUser.Id)); err != nil { + if err = deleteUser(string(createdUser.Id)); err != nil { t.Error(err) } } + +func createUser(expectedUser opslevel.User) (string, error) { + cliArgs := []string{expectedUser.Email, expectedUser.Name} + cmd.RootCmd.SetArgs(cliArgs) + + // Create User + createOutput, err := execCmd(Create, "user", cliArgs...) + if err != nil { + return "", fmt.Errorf("Create 'user' failed, got error: %v", err) + } + return asString(createOutput), nil +} + +func getUser(userId string) (*opslevel.User, error) { + getOutput, err := execCmd(Get, "user", userId) + if err != nil { + return nil, fmt.Errorf("Get 'user' failed, got error: %v", err) + } + + createdUser, err := jsonToResource[opslevel.User](getOutput) + if err != nil { + return nil, fmt.Errorf("Failed to convert JSON from API to 'opslevel.User' struct") + } + return createdUser, err +} + +func updateUser(userId string, userToUpdate opslevel.User) (string, error) { + if err := writeToYaml(userFileName, userToUpdate); err != nil { + return "", fmt.Errorf("Error while writing '%v' to file '%s': %v", userToUpdate, userFileName, err) + } + + // Store Update User stuff to "file" + cliArgs := []string{userId, "-f", userFileName} + updateOutput, err := execCmd(Update, "user", cliArgs...) + if err != nil { + return "", fmt.Errorf("Update 'user' failed, got error: %v", err) + } + return asString(updateOutput), nil +} + +func deleteUser(userId string) error { + _, err := execCmd(Delete, "user", userId) + return err +}