Skip to content

Commit

Permalink
refactor to table driven tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjyocca committed Jun 13, 2022
1 parent 4ac9e8f commit 8e033bf
Showing 1 changed file with 62 additions and 72 deletions.
134 changes: 62 additions & 72 deletions run_integration_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -96,88 +95,79 @@ func TestRunsList(t *testing.T) {
}

func TestRunsListQueryParams(t *testing.T) {
type testCase struct {
options *RunListOptions
description string
assertion func(tc testCase, rl *RunList, err error)
}

client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

wTest, _ := createWorkspace(t, client, orgTest)
rTest1, _ := createPlannedRun(t, client, wTest)
rTest2, _ := createRun(t, client, wTest)
workspaceTest, _ := createWorkspace(t, client, orgTest)
createPlannedRun(t, client, workspaceTest)
pendingRun, _ := createRun(t, client, workspaceTest)

currentUser, _ := client.Users.ReadCurrent(ctx)
userSearch := currentUser.Username

t.Run("with status query parameter", func(t *testing.T) {
skipIfBeta(t)
rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{
Include: []RunIncludeOpt{RunWorkspace},
Status: strings.Join([]string{string(RunPending)}, ","),
})
assert.NoError(t, err)

found := []string{}
for _, r := range rl.Items {
found = append(found, r.ID)
}

assert.NotContains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Equal(t, rl.Items[0].Status, RunPending)
})

t.Run("with source query parameter", func(t *testing.T) {
skipIfBeta(t)
rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{
Include: []RunIncludeOpt{RunWorkspace},
Source: strings.Join([]string{string(RunSourceAPI)}, ","),
})

assert.NoError(t, err)
assert.Equal(t, 2, len(rl.Items))
assert.Equal(t, rl.Items[0].Source, RunSourceAPI)
assert.NotNil(t, rl.Items[0].Source)
})

t.Run("with operation of plan_only parameter", func(t *testing.T) {
skipIfBeta(t)
rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{
Operation: strings.Join([]string{string(RunOperationPlanOnly)}, ","),
})

assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
})

t.Run("with name parameter", func(t *testing.T) {
skipIfBeta(t)
rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{
Name: userSearch,
})

assert.NoError(t, err)

found := []string{}
for _, r := range rl.Items {
found = append(found, r.ID)
}

assert.Equal(t, 2, len(rl.Items))
assert.Contains(t, found, rTest2.ID)
assert.NotNil(t, rTest2.CreatedBy.ID)
})
testCases := []testCase{
{
description: "with status query parameter",
options: &RunListOptions{Status: string(RunPending), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 1, len(rl.Items))
},
},
{
description: "with source query parameter",
options: &RunListOptions{Source: string(RunSourceAPI), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 2, len(rl.Items))
assert.Equal(t, rl.Items[0].Source, RunSourceAPI)
},
},
{
description: "with operation of plan_only parameter",
options: &RunListOptions{Operation: string(RunOperationPlanOnly), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
},
},
{
description: "with name parameter",
options: &RunListOptions{Name: currentUser.Username, Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
found := []string{}
for _, r := range rl.Items {
found = append(found, r.ID)
}
assert.Equal(t, 2, len(rl.Items))
assert.Contains(t, found, pendingRun.ID)
},
},
{
description: "with name & commit parameter",
options: &RunListOptions{Name: randomString(t), Commit: randomString(t), Include: []RunIncludeOpt{RunWorkspace}},
assertion: func(tc testCase, rl *RunList, err error) {
assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
},
},
}

t.Run("with name & commit parameter", func(t *testing.T) {
skipIfBeta(t)
rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{
Name: randomString(t),
Commit: randomString(t),
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
skipIfBeta(t)
runs, err := client.Runs.List(ctx, workspaceTest.ID, testCase.options)
testCase.assertion(testCase, runs, err)
})

assert.NoError(t, err)
assert.Equal(t, 0, len(rl.Items))
})
}
}

func TestRunsCreate(t *testing.T) {
Expand Down

0 comments on commit 8e033bf

Please sign in to comment.