package common import ( "fmt" "testing" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/request" "github.com/gogf/gf/v2/frame/g" ) func TestBatchCardsFilterStatus(t *testing.T) { ctx := setupBatchExecutionLogListDB(t) for _, statement := range []string{ "ALTER TABLE batch_main ADD target_dept_id INTEGER DEFAULT 0", "ALTER TABLE batch_main ADD dept_id INTEGER DEFAULT 0", "ALTER TABLE batch_main ADD updated_by INTEGER DEFAULT 0", "ALTER TABLE batch_main ADD batch_async_status INTEGER DEFAULT 3", } { if _, err := g.DB().Exec(ctx, statement); err != nil { t.Fatal(err) } } for _, batchType := range []int{consts.BatchTypeInbound, consts.BatchTypeOutbound, consts.BatchTypeAllocate} { batchNo := fmt.Sprintf("STATUS-%d", batchType) if _, err := g.DB().Exec(ctx, "INSERT INTO batch_main(batch_no,batch_type,batch_info) VALUES(?,?,'{}')", batchNo, batchType); err != nil { t.Fatal(err) } for _, status := range []int{0, 1, 2, 3, 4, 5} { if _, err := g.DB().Exec(ctx, "INSERT INTO batch_detail(batch_no,batch_type,detail_status,row_no,item_id,error_reason,snapshot) VALUES(?,?,?,?,?,?,'{}')", batchNo, batchType, status, status+1, fmt.Sprintf("card-%d", status), fmt.Sprintf("reason-%d", status)); err != nil { t.Fatal(err) } } for _, status := range []int{0, 1, 2, 3, 4, 5} { rows, total, err := NewCommonBatchMain().ListCards(ctx, &model.BatchCardListParam{BatchNo: batchNo, DetailStatus: &status, PageInfo: request.PageInfo{Page: 1, PageSize: 20}}) if err != nil || total != 1 || len(rows) != 1 { t.Fatalf("type=%d status=%d total=%d err=%v", batchType, status, total, err) } if rows[0].DetailStatus != status || rows[0].ErrorReason != fmt.Sprintf("reason-%d", status) { t.Fatalf("incorrect detail: %+v", rows[0]) } } for page, wantStatus := range []int{3, 0, 1, 2, 4, 5} { rows, total, err := NewCommonBatchMain().ListCards(ctx, &model.BatchCardListParam{BatchNo: batchNo, PageInfo: request.PageInfo{Page: page + 1, PageSize: 1}}) if err != nil || total != 6 || len(rows) != 1 || rows[0].DetailStatus != wantStatus { t.Fatalf("type=%d page=%d status=%d total=%d err=%v", batchType, page+1, wantStatus, total, err) } } if _, err := g.DB().Exec(ctx, "INSERT INTO batch_detail(batch_no,batch_type,detail_status,row_no,item_id,error_reason,snapshot) VALUES(?,?,?,?,?,?,'{}')", batchNo, batchType, consts.BatchDetailStatusFailed, 0, "cascade", "本批次存在失败明细,整批未执行"); err != nil { t.Fatal(err) } rows, total, err := NewCommonBatchMain().ListCards(ctx, &model.BatchCardListParam{BatchNo: batchNo, PageInfo: request.PageInfo{Page: 1, PageSize: 1}}) if err != nil || total != 7 || len(rows) != 1 || rows[0].Iccid != "card-3" { t.Fatalf("type=%d: real failure should precede legacy cascade: rows=%+v total=%d err=%v", batchType, rows, total, err) } } }