|
7 | 7 | "bytes"
|
8 | 8 | "context"
|
9 | 9 | "encoding/json"
|
10 |
| - "strings" |
11 | 10 | "testing"
|
12 | 11 | "time"
|
13 | 12 |
|
@@ -96,88 +95,80 @@ func TestRunsList(t *testing.T) {
|
96 | 95 | }
|
97 | 96 |
|
98 | 97 | func TestRunsListQueryParams(t *testing.T) {
|
| 98 | + type testCase struct { |
| 99 | + options *RunListOptions |
| 100 | + description string |
| 101 | + assertion func(tc testCase, rl *RunList, err error) |
| 102 | + } |
| 103 | + |
99 | 104 | client := testClient(t)
|
100 | 105 | ctx := context.Background()
|
101 | 106 |
|
102 | 107 | orgTest, orgTestCleanup := createOrganization(t, client)
|
103 | 108 | defer orgTestCleanup()
|
104 | 109 |
|
105 |
| - wTest, _ := createWorkspace(t, client, orgTest) |
106 |
| - rTest1, _ := createPlannedRun(t, client, wTest) |
107 |
| - rTest2, _ := createRun(t, client, wTest) |
| 110 | + workspaceTest, _ := createWorkspace(t, client, orgTest) |
| 111 | + createPlannedRun(t, client, workspaceTest) |
| 112 | + pendingRun, _ := createRun(t, client, workspaceTest) |
108 | 113 |
|
109 | 114 | currentUser, _ := client.Users.ReadCurrent(ctx)
|
110 | 115 | userSearch := currentUser.Username
|
111 | 116 |
|
112 |
| - t.Run("with status query parameter", func(t *testing.T) { |
113 |
| - skipIfBeta(t) |
114 |
| - rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{ |
115 |
| - Include: []RunIncludeOpt{RunWorkspace}, |
116 |
| - Status: strings.Join([]string{string(RunPending)}, ","), |
117 |
| - }) |
118 |
| - assert.NoError(t, err) |
119 |
| - |
120 |
| - found := []string{} |
121 |
| - for _, r := range rl.Items { |
122 |
| - found = append(found, r.ID) |
123 |
| - } |
124 |
| - |
125 |
| - assert.NotContains(t, found, rTest1.ID) |
126 |
| - assert.Contains(t, found, rTest2.ID) |
127 |
| - assert.Equal(t, rl.Items[0].Status, RunPending) |
128 |
| - }) |
129 |
| - |
130 |
| - t.Run("with source query parameter", func(t *testing.T) { |
131 |
| - skipIfBeta(t) |
132 |
| - rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{ |
133 |
| - Include: []RunIncludeOpt{RunWorkspace}, |
134 |
| - Source: strings.Join([]string{string(RunSourceAPI)}, ","), |
135 |
| - }) |
136 |
| - |
137 |
| - assert.NoError(t, err) |
138 |
| - assert.Equal(t, 2, len(rl.Items)) |
139 |
| - assert.Equal(t, rl.Items[0].Source, RunSourceAPI) |
140 |
| - assert.NotNil(t, rl.Items[0].Source) |
141 |
| - }) |
142 |
| - |
143 |
| - t.Run("with operation of plan_only parameter", func(t *testing.T) { |
144 |
| - skipIfBeta(t) |
145 |
| - rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{ |
146 |
| - Operation: strings.Join([]string{string(RunOperationPlanOnly)}, ","), |
147 |
| - }) |
148 |
| - |
149 |
| - assert.NoError(t, err) |
150 |
| - assert.Equal(t, 0, len(rl.Items)) |
151 |
| - }) |
152 |
| - |
153 |
| - t.Run("with name parameter", func(t *testing.T) { |
154 |
| - skipIfBeta(t) |
155 |
| - rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{ |
156 |
| - Name: userSearch, |
157 |
| - }) |
158 |
| - |
159 |
| - assert.NoError(t, err) |
160 |
| - |
161 |
| - found := []string{} |
162 |
| - for _, r := range rl.Items { |
163 |
| - found = append(found, r.ID) |
164 |
| - } |
165 |
| - |
166 |
| - assert.Equal(t, 2, len(rl.Items)) |
167 |
| - assert.Contains(t, found, rTest2.ID) |
168 |
| - assert.NotNil(t, rTest2.CreatedBy.ID) |
169 |
| - }) |
| 117 | + testCases := []testCase{ |
| 118 | + { |
| 119 | + description: "with status query parameter", |
| 120 | + options: &RunListOptions{Status: string(RunPending), Include: []RunIncludeOpt{RunWorkspace}}, |
| 121 | + assertion: func(tc testCase, rl *RunList, err error) { |
| 122 | + assert.NoError(t, err) |
| 123 | + assert.Equal(t, 1, len(rl.Items)) |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + description: "with source query parameter", |
| 128 | + options: &RunListOptions{Source: string(RunSourceAPI), Include: []RunIncludeOpt{RunWorkspace}}, |
| 129 | + assertion: func(tc testCase, rl *RunList, err error) { |
| 130 | + assert.NoError(t, err) |
| 131 | + assert.Equal(t, 2, len(rl.Items)) |
| 132 | + assert.Equal(t, rl.Items[0].Source, RunSourceAPI) |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + description: "with operation of plan_only parameter", |
| 137 | + options: &RunListOptions{Operation: string(RunOperationPlanOnly), Include: []RunIncludeOpt{RunWorkspace}}, |
| 138 | + assertion: func(tc testCase, rl *RunList, err error) { |
| 139 | + assert.NoError(t, err) |
| 140 | + assert.Equal(t, 0, len(rl.Items)) |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + description: "with name parameter", |
| 145 | + options: &RunListOptions{Name: userSearch}, |
| 146 | + assertion: func(tc testCase, rl *RunList, err error) { |
| 147 | + found := []string{} |
| 148 | + for _, r := range rl.Items { |
| 149 | + found = append(found, r.ID) |
| 150 | + } |
| 151 | + assert.Equal(t, 2, len(rl.Items)) |
| 152 | + assert.Contains(t, found, pendingRun.ID) |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + description: "with name & commit parameter", |
| 157 | + options: &RunListOptions{Name: randomString(t), Commit: randomString(t)}, |
| 158 | + assertion: func(tc testCase, rl *RunList, err error) { |
| 159 | + assert.NoError(t, err) |
| 160 | + assert.Equal(t, 0, len(rl.Items)) |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
170 | 164 |
|
171 |
| - t.Run("with name & commit parameter", func(t *testing.T) { |
172 |
| - skipIfBeta(t) |
173 |
| - rl, err := client.Runs.List(ctx, wTest.ID, &RunListOptions{ |
174 |
| - Name: randomString(t), |
175 |
| - Commit: randomString(t), |
| 165 | + for _, tc := range testCases { |
| 166 | + t.Run(tc.description, func(t *testing.T) { |
| 167 | + skipIfBeta(t) |
| 168 | + runListResult, err := client.Runs.List(ctx, workspaceTest.ID, tc.options) |
| 169 | + tc.assertion(tc, runListResult, err) |
176 | 170 | })
|
177 |
| - |
178 |
| - assert.NoError(t, err) |
179 |
| - assert.Equal(t, 0, len(rl.Items)) |
180 |
| - }) |
| 171 | + } |
181 | 172 | }
|
182 | 173 |
|
183 | 174 | func TestRunsCreate(t *testing.T) {
|
|
0 commit comments