package common import ( "testing" "github.com/gogf/gf/v2/frame/g" "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" ) func TestInboundBatchChannelFiltering(t *testing.T) { ctx := setupBatchExecutionLogListDB(t) for _, statement := range []string{ "ALTER TABLE batch_main ADD target_dept_id INTEGER DEFAULT 0", "CREATE TABLE t_sim_card_stock(batch_no TEXT,tenant_id TEXT)", "INSERT INTO t_sim_card_stock VALUES('A-NESTED',''),('A-NESTED','')", } { if _, err := g.DB().Exec(ctx, statement); err != nil { t.Fatal(err) } } fixtures := []struct { name string info string batchType int runResult int }{ {"A-NESTED", `{"inboundOptions":{"chanKey":"A"}}`, 1, consts.BatchRunResultSuccess}, {"A-LEGACY", `{"chanKey":"A"}`, 1, consts.BatchRunResultSuccess}, {"B-NESTED", `{"inboundOptions":{"chanKey":"B"},"chanKey":"A"}`, 1, consts.BatchRunResultSuccess}, {"A-EMPTY-NESTED", `{"inboundOptions":{"chanKey":""},"chanKey":"A"}`, 1, consts.BatchRunResultSuccess}, {"NO-CHANNEL", "{}", 1, consts.BatchRunResultSuccess}, {"INVALID-JSON", "broken", 1, consts.BatchRunResultSuccess}, {"OTHER-TYPE", `{"chanKey":"A"}`, 2, consts.BatchRunResultSuccess}, {"NOT-SUCCESS", `{"chanKey":"A"}`, 1, consts.BatchRunResultFailed}, } for _, fixture := range fixtures { if _, err := g.DB().Exec(ctx, "INSERT INTO batch_main(batch_no,batch_type,batch_info,total_count,run_result) VALUES(?,?,?,?,?)", fixture.name, fixture.batchType, fixture.info, 2, fixture.runResult); err != nil { t.Fatal(err) } } for _, test := range []struct { name string channel string page int size int wantTotal int wantRows int }{ {"渠道A兼容新旧格式", "A", 1, 20, 3, 3}, {"渠道B优先使用嵌套编码", "B", 1, 20, 1, 1}, {"清空恢复全部入库批次", "", 1, 20, 6, 6}, {"前后空白不影响筛选", " A ", 1, 20, 3, 3}, {"总数按渠道过滤后分页", "A", 2, 2, 3, 1}, {"不存在的渠道返回空", "missing", 1, 20, 0, 0}, {"特殊字符作为参数而不是SQL", "A' OR 1=1 --", 1, 20, 0, 0}, } { t.Run(test.name, func(t *testing.T) { rows, total, err := NewCommonBatchMain().SuccessList(ctx, &model.BatchMainSuccessListParam{BatchType: consts.BatchTypeInbound, ChanKey: test.channel, PageInfo: request.PageInfo{Page: test.page, PageSize: test.size}}) if err != nil || total != test.wantTotal || len(rows) != test.wantRows { t.Fatalf("total=%d rows=%d err=%v", total, len(rows), err) } for _, row := range rows { if row.BatchNo == "A-NESTED" && row.StockCount != 2 { t.Fatalf("stock count changed: %+v", row) } if test.channel == "B" && row.BatchNo != "B-NESTED" { t.Fatalf("nested channel priority violated: %+v", row) } } }) } }