4
4
package shared
5
5
6
6
import (
7
- "net/url"
8
7
"testing"
9
8
"time"
10
9
11
10
actions_model "code.gitea.io/gitea/models/actions"
12
11
"code.gitea.io/gitea/models/unittest"
13
- "code.gitea.io/gitea/services/context"
14
12
"code.gitea.io/gitea/services/contexttest"
15
13
16
14
"github.com/stretchr/testify/assert"
@@ -20,15 +18,6 @@ func TestMain(m *testing.M) {
20
18
unittest .MainTest (m )
21
19
}
22
20
23
- // setFormValue is a helper function to set form values in test context
24
- func setFormValue (ctx * context.APIContext , key , value string ) {
25
- // Initialize the form if it's nil
26
- if ctx .Req .Form == nil {
27
- ctx .Req .Form = make (url.Values )
28
- }
29
- ctx .Req .Form .Set (key , value )
30
- }
31
-
32
21
// TestListRunsWorkflowFiltering tests that ListRuns properly handles
33
22
// the workflow_id path parameter for filtering runs by workflow.
34
23
func TestListRunsWorkflowFiltering (t * testing.T ) {
@@ -77,108 +66,65 @@ func TestListRunsExcludePullRequestsParam(t *testing.T) {
77
66
unittest .PrepareTestEnv (t )
78
67
79
68
// Test case 1: With exclude_pull_requests=true
80
- ctx , _ := contexttest .MockAPIContext (t , "user2/repo1" )
69
+ ctx , _ := contexttest .MockAPIContext (t , "user2/repo1?exclude_pull_requests=true " )
81
70
contexttest .LoadRepo (t , ctx , 1 )
82
71
contexttest .LoadUser (t , ctx , 2 )
83
72
84
- // Set up form value
85
- setFormValue (ctx , "exclude_pull_requests" , "true" )
86
-
87
73
// Call the actual parsing logic from ListRuns
88
74
opts := actions_model.FindRunOptions {
89
75
RepoID : ctx .Repo .Repository .ID ,
90
76
}
91
77
92
- if exclude := ctx .FormString ("exclude_pull_requests" ); exclude != "" {
93
- if exclude == "true" || exclude == "1" {
94
- opts .ExcludePullRequests = true
95
- }
78
+ if ctx .FormBool ("exclude_pull_requests" ) {
79
+ opts .ExcludePullRequests = true
96
80
}
97
81
98
82
// Verify the ExcludePullRequests is correctly set based on the form value
99
83
assert .True (t , opts .ExcludePullRequests )
100
84
101
85
// Test case 2: With exclude_pull_requests=1
102
- ctx2 , _ := contexttest .MockAPIContext (t , "user2/repo1" )
86
+ ctx2 , _ := contexttest .MockAPIContext (t , "user2/repo1?exclude_pull_requests=1 " )
103
87
contexttest .LoadRepo (t , ctx2 , 1 )
104
88
contexttest .LoadUser (t , ctx2 , 2 )
105
89
106
- setFormValue (ctx2 , "exclude_pull_requests" , "1" )
107
-
108
90
opts2 := actions_model.FindRunOptions {
109
91
RepoID : ctx2 .Repo .Repository .ID ,
110
92
}
111
93
112
- if exclude := ctx2 .FormString ("exclude_pull_requests" ); exclude != "" {
113
- if exclude == "true" || exclude == "1" {
114
- opts2 .ExcludePullRequests = true
115
- }
94
+ if ctx2 .FormBool ("exclude_pull_requests" ) {
95
+ opts2 .ExcludePullRequests = true
116
96
}
117
97
118
98
// Verify the ExcludePullRequests is correctly set for "1" value
119
99
assert .True (t , opts2 .ExcludePullRequests )
120
100
121
101
// Test case 3: With exclude_pull_requests=false (should not set the flag)
122
- ctx3 , _ := contexttest .MockAPIContext (t , "user2/repo1" )
102
+ ctx3 , _ := contexttest .MockAPIContext (t , "user2/repo1?exclude_pull_requests=false " )
123
103
contexttest .LoadRepo (t , ctx3 , 1 )
124
104
contexttest .LoadUser (t , ctx3 , 2 )
125
105
126
- setFormValue (ctx3 , "exclude_pull_requests" , "false" )
127
-
128
106
opts3 := actions_model.FindRunOptions {
129
107
RepoID : ctx3 .Repo .Repository .ID ,
130
108
}
131
109
132
- if exclude := ctx3 .FormString ("exclude_pull_requests" ); exclude != "" {
133
- if exclude == "true" || exclude == "1" {
134
- opts3 .ExcludePullRequests = true
135
- }
110
+ if ctx3 .FormBool ("exclude_pull_requests" ) {
111
+ opts3 .ExcludePullRequests = true
136
112
}
137
113
138
114
// Verify the ExcludePullRequests is NOT set for "false" value
139
115
assert .False (t , opts3 .ExcludePullRequests )
140
116
}
141
117
142
- // TestListRunsCheckSuiteIDParam tests that ListRuns properly handles
143
- // the check_suite_id parameter.
144
- func TestListRunsCheckSuiteIDParam (t * testing.T ) {
145
- unittest .PrepareTestEnv (t )
146
-
147
- const testSuiteID int64 = 12345
148
-
149
- // Test case: With check_suite_id parameter
150
- ctx , _ := contexttest .MockAPIContext (t , "user2/repo1" )
151
- contexttest .LoadRepo (t , ctx , 1 )
152
- contexttest .LoadUser (t , ctx , 2 )
153
-
154
- setFormValue (ctx , "check_suite_id" , "12345" )
155
-
156
- // Call the actual parsing logic from ListRuns
157
- opts := actions_model.FindRunOptions {
158
- RepoID : ctx .Repo .Repository .ID ,
159
- }
160
-
161
- // This simulates the logic in ListRuns
162
- if checkSuiteID := ctx .FormInt64 ("check_suite_id" ); checkSuiteID > 0 {
163
- opts .CheckSuiteID = checkSuiteID
164
- }
165
-
166
- // Verify the CheckSuiteID is correctly set based on the form value
167
- assert .Equal (t , testSuiteID , opts .CheckSuiteID )
168
- }
169
-
170
118
// TestListRunsCreatedParam tests that ListRuns properly handles
171
119
// the created parameter for date filtering.
172
120
func TestListRunsCreatedParam (t * testing.T ) {
173
121
unittest .PrepareTestEnv (t )
174
122
175
123
// Test case 1: With created in date range format
176
- ctx , _ := contexttest .MockAPIContext (t , "user2/repo1" )
124
+ ctx , _ := contexttest .MockAPIContext (t , "user2/repo1?created=2023-01-01..2023-12-31 " )
177
125
contexttest .LoadRepo (t , ctx , 1 )
178
126
contexttest .LoadUser (t , ctx , 2 )
179
127
180
- setFormValue (ctx , "created" , "2023-01-01..2023-12-31" )
181
-
182
128
opts := actions_model.FindRunOptions {
183
129
RepoID : ctx .Repo .Repository .ID ,
184
130
}
@@ -204,12 +150,10 @@ func TestListRunsCreatedParam(t *testing.T) {
204
150
assert .Equal (t , expectedEnd , opts .CreatedBefore )
205
151
206
152
// Test case 2: With created in ">=" format
207
- ctx2 , _ := contexttest .MockAPIContext (t , "user2/repo1" )
153
+ ctx2 , _ := contexttest .MockAPIContext (t , "user2/repo1?created=>=2023-01-01 " )
208
154
contexttest .LoadRepo (t , ctx2 , 1 )
209
155
contexttest .LoadUser (t , ctx2 , 2 )
210
156
211
- setFormValue (ctx2 , "created" , ">=2023-01-01" )
212
-
213
157
opts2 := actions_model.FindRunOptions {
214
158
RepoID : ctx2 .Repo .Repository .ID ,
215
159
}
@@ -229,12 +173,10 @@ func TestListRunsCreatedParam(t *testing.T) {
229
173
assert .True (t , opts2 .CreatedBefore .IsZero ())
230
174
231
175
// Test case 3: With created in exact date format
232
- ctx3 , _ := contexttest .MockAPIContext (t , "user2/repo1" )
176
+ ctx3 , _ := contexttest .MockAPIContext (t , "user2/repo1?created=2023-06-15 " )
233
177
contexttest .LoadRepo (t , ctx3 , 1 )
234
178
contexttest .LoadUser (t , ctx3 , 2 )
235
179
236
- setFormValue (ctx3 , "created" , "2023-06-15" )
237
-
238
180
opts3 := actions_model.FindRunOptions {
239
181
RepoID : ctx3 .Repo .Repository .ID ,
240
182
}
0 commit comments