@@ -18,11 +18,14 @@ import (
18
18
"database/sql/driver"
19
19
"encoding/json"
20
20
"fmt"
21
+ "time"
21
22
22
23
"github.com/pkg/errors"
23
24
24
25
"github.com/erda-project/erda/apistructs"
26
+ "github.com/erda-project/erda/modules/dop/services/apierrors"
25
27
"github.com/erda-project/erda/pkg/database/dbengine"
28
+ "github.com/erda-project/erda/pkg/strutil"
26
29
)
27
30
28
31
// TestCase 测试用例
@@ -188,3 +191,192 @@ func (client *DBClient) CleanTestCasesByTestSetID(projectID, testSetID uint64) e
188
191
func (client * DBClient ) BatchDeleteTestCases (ids []uint64 ) error {
189
192
return client .Where ("`id` IN (?)" , ids ).Delete (TestCase {}).Error
190
193
}
194
+
195
+ // order
196
+ const (
197
+ tcFieldPriority = "priority"
198
+ tcFieldID = "id"
199
+ tcFieldTestSetID = "test_set_id"
200
+ tcFieldUpdaterID = "updater_id"
201
+ tcFieldUpdatedAt = "updated_at"
202
+ )
203
+
204
+ func (client * DBClient ) PagingTestCases (req apistructs.TestCasePagingRequest ) ([]TestCase , uint64 , error ) {
205
+ // validate request
206
+ if err := validateTestCasePagingRequest (req ); err != nil {
207
+ return nil , 0 , err
208
+ }
209
+ // set default for request
210
+ setDefaultForTestCasePagingRequest (& req )
211
+
212
+ sql := client .DB .Table (TestCase {}.TableName () + " AS `tc`" ).Select ("*" )
213
+
214
+ if len (req .NotInTestPlanIDs ) > 0 {
215
+ // left join test_sets
216
+ sql = sql .Joins ("LEFT JOIN " + TestSet {}.TableName () + " AS `ts` ON `tc`.`test_set_id` = `ts`.`id`" )
217
+ } else {
218
+ // left join test_plan_case_relations
219
+ sql = sql .Joins ("LEFT JOIN " + TestPlanCaseRel {}.TableName () + " AS `rel` ON `tc`.`id` = `rel`.`test_case_id`" )
220
+ sql = sql .Where ("`rel`.`test_plan_id` IS NULL OR `rel`.`test_plan_id` NOT IN (?)" , req .NotInTestPlanIDs )
221
+ }
222
+
223
+ // where clauses
224
+ // project id
225
+ sql = sql .Where ("`tc`.`project_id` = ?" , req .ProjectID )
226
+ // recycled
227
+ sql = sql .Where ("`tc`.`recycled` = ?" , req .Recycled )
228
+ // name
229
+ if req .Query != "" {
230
+ sql = sql .Where ("`tc`.`name` LIKE ?" , strutil .Concat ("%" , req .Query , "%" ))
231
+ }
232
+ // priority
233
+ if len (req .Priorities ) > 0 {
234
+ sql = sql .Where ("`tc`.`priority` IN (?)" , req .Priorities )
235
+ }
236
+ // updater
237
+ if len (req .UpdaterIDs ) > 0 {
238
+ sql = sql .Where ("`tc`.`updater_id` IN (?)" , req .UpdaterIDs )
239
+ }
240
+ // updatedAtBegin (Left closed Section)
241
+ if req .TimestampSecUpdatedAtBegin != nil {
242
+ t := time .Unix (int64 (* req .TimestampSecUpdatedAtBegin ), 0 )
243
+ req .UpdatedAtBeginInclude = & t
244
+ }
245
+ if req .UpdatedAtBeginInclude != nil {
246
+ sql = sql .Where ("`tc`.`updated_at` >= ?" , req .UpdatedAtBeginInclude )
247
+ }
248
+ // updatedAtEnd (Right closed Section)
249
+ if req .TimestampSecUpdatedAtEnd != nil {
250
+ t := time .Unix (int64 (* req .TimestampSecUpdatedAtEnd ), 0 )
251
+ req .UpdatedAtEndInclude = & t
252
+ }
253
+ if req .UpdatedAtEndInclude != nil {
254
+ sql = sql .Where ("`tc`.`updated_at` <= ?" , req .UpdatedAtEndInclude )
255
+ }
256
+ // testCaseIDs
257
+ if len (req .TestCaseIDs ) > 0 {
258
+ sql = sql .Where ("`tc`.`id` IN (?)" , req .TestCaseIDs )
259
+ }
260
+ if len (req .NotInTestCaseIDs ) > 0 {
261
+ sql = sql .Where ("`tc`.`id` NOT IN (?)" , req .NotInTestCaseIDs )
262
+ }
263
+
264
+ // order by fields
265
+ for _ , orderField := range req .OrderFields {
266
+ switch orderField {
267
+ case tcFieldID :
268
+ if req .OrderByIDAsc != nil {
269
+ sql = sql .Order ("`tc`.`id` ASC" )
270
+ }
271
+ if req .OrderByIDDesc != nil {
272
+ sql = sql .Order ("`tc`.`id` DESC" )
273
+ }
274
+ case tcFieldTestSetID :
275
+ if req .OrderByTestSetIDAsc != nil {
276
+ sql = sql .Order ("`tc`.`test_set_id` ASC" )
277
+ }
278
+ if req .OrderByTestSetIDDesc != nil {
279
+ sql = sql .Order ("`tc`.`test_set_id` DESC" )
280
+ }
281
+ case tcFieldPriority :
282
+ if req .OrderByPriorityAsc != nil {
283
+ sql = sql .Order ("`tc`.`priority` ASC" )
284
+ }
285
+ if req .OrderByPriorityDesc != nil {
286
+ sql = sql .Order ("`tc`.`priority` DESC" )
287
+ }
288
+ case tcFieldUpdaterID :
289
+ if req .OrderByUpdaterIDAsc != nil {
290
+ sql = sql .Order ("`tc`.`updater_id` ASC" )
291
+ }
292
+ if req .OrderByUpdaterIDDesc != nil {
293
+ sql = sql .Order ("`tc`.`updater_id` DESC" )
294
+ }
295
+ case tcFieldUpdatedAt :
296
+ if req .OrderByUpdaterIDAsc != nil {
297
+ sql = sql .Order ("`tc`.`updated_at` ASC" )
298
+ }
299
+ if req .OrderByUpdaterIDDesc != nil {
300
+ sql = sql .Order ("`tc`.`updated_at` DESC" )
301
+ }
302
+ }
303
+ }
304
+
305
+ // result
306
+ var (
307
+ testCases []TestCase
308
+ total uint64
309
+ )
310
+
311
+ // offset, limit
312
+ offset := (req .PageNo - 1 ) * req .PageSize
313
+ limit := req .PageSize
314
+ sql = sql .Offset (offset ).Limit (limit ).Find (& testCases )
315
+ // reset offset & limit before count
316
+ sql = sql .Offset (0 ).Limit (- 1 ).Count (& total )
317
+
318
+ // execute sql
319
+ if err := sql .Error ; err != nil {
320
+ return nil , 0 , apierrors .ErrPagingTestCases .InternalError (err )
321
+ }
322
+
323
+ return testCases , total , nil
324
+ }
325
+
326
+ func validateTestCasePagingRequest (req apistructs.TestCasePagingRequest ) error {
327
+ if req .ProjectID == 0 {
328
+ return apierrors .ErrPagingTestCases .MissingParameter ("projectID" )
329
+ }
330
+ for _ , priority := range req .Priorities {
331
+ if ! priority .IsValid () {
332
+ return apierrors .ErrPagingTestCases .InvalidParameter (fmt .Sprintf ("priority: %s" , priority ))
333
+ }
334
+ }
335
+ if req .OrderByPriorityAsc != nil && req .OrderByPriorityDesc != nil {
336
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by priority ASC or DESC?" )
337
+ }
338
+ if req .OrderByUpdaterIDAsc != nil && req .OrderByUpdaterIDDesc != nil {
339
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by updaterID ASC or DESC?" )
340
+ }
341
+ if req .OrderByUpdatedAtAsc != nil && req .OrderByUpdatedAtDesc != nil {
342
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by updatedAt ASC or DESC?" )
343
+ }
344
+ if req .OrderByIDAsc != nil && req .OrderByIDDesc != nil {
345
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by id ASC or DESC?" )
346
+ }
347
+ if req .OrderByTestSetIDAsc != nil && req .OrderByTestSetIDDesc != nil {
348
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by testSetID ASC or DESC?" )
349
+ }
350
+ if req .OrderByTestSetNameAsc != nil && req .OrderByTestSetNameDesc != nil {
351
+ return apierrors .ErrPagingTestCases .InvalidParameter ("order by testSetName ASC or DESC?" )
352
+ }
353
+
354
+ return nil
355
+ }
356
+
357
+ func setDefaultForTestCasePagingRequest (req * apistructs.TestCasePagingRequest ) {
358
+ // must order by testSet
359
+ if req .OrderByTestSetIDAsc == nil && req .OrderByTestSetIDDesc == nil &&
360
+ req .OrderByTestSetNameAsc == nil && req .OrderByTestSetNameDesc == nil {
361
+ // default order by `test_set_id` ASC
362
+ req .OrderByTestSetIDAsc = & []bool {true }[0 ]
363
+ req .OrderFields = append (req .OrderFields , tcFieldTestSetID )
364
+ }
365
+
366
+ // set default order inside a testSet
367
+ if req .OrderByPriorityAsc == nil && req .OrderByPriorityDesc == nil &&
368
+ req .OrderByUpdaterIDAsc == nil && req .OrderByUpdaterIDDesc == nil &&
369
+ req .OrderByUpdatedAtAsc == nil && req .OrderByUpdatedAtDesc == nil &&
370
+ req .OrderByIDAsc == nil && req .OrderByIDDesc == nil {
371
+ // default order by `id` ASC
372
+ req .OrderByIDAsc = & []bool {true }[0 ]
373
+ req .OrderFields = append (req .OrderFields , tcFieldID )
374
+ }
375
+
376
+ if req .PageNo == 0 {
377
+ req .PageNo = 1
378
+ }
379
+ if req .PageSize == 0 {
380
+ req .PageSize = 20
381
+ }
382
+ }
0 commit comments