Skip to content

Commit

Permalink
break up tests into smaller chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Aug 9, 2024
1 parent ac56d19 commit b242187
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 48 deletions.
84 changes: 59 additions & 25 deletions src/cmd/team_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"fmt"
"testing"

"github.com/opslevel/cli/cmd"
Expand All @@ -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}
Expand All @@ -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
}
79 changes: 56 additions & 23 deletions src/cmd/user_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"fmt"
"strings"
"testing"

Expand All @@ -19,25 +20,18 @@ func Test_UserCRUD(t *testing.T) {
UserId: opslevel.UserId{Email: "[email protected]"},
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 ||
Expand All @@ -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
}

0 comments on commit b242187

Please sign in to comment.