package query import ( "context" "testing" _ "github.com/gogf/gf/contrib/drivers/sqlite/v2" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/contexts" "git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model" ) func TestBatchStatusScopeAndAllocationChildren(t *testing.T) { ctx := context.Background() if err := gdb.SetConfig(gdb.Config{"default": gdb.ConfigGroup{{Type: "sqlite", Link: "sqlite::@file(:memory:)", MaxOpenConnCount: 1}}}); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = g.DB().Close(ctx) }) if err := g.DB().GetCore().ClearTableFieldsAll(ctx); err != nil { t.Fatal(err) } for _, statement := range []string{ "CREATE TABLE batch_main (id INTEGER PRIMARY KEY, batch_no TEXT, batch_type INTEGER, source_batch_no TEXT, tenant_id TEXT, dept_id INTEGER, batch_async_status INTEGER, source_channel TEXT, execution_mode INTEGER, source_type INTEGER, total_count INTEGER, valid_count INTEGER, success_count INTEGER, fail_count INTEGER, run_result INTEGER, fail_reason TEXT)", "INSERT INTO batch_main VALUES (1,'mine',101,'mine','A',7,3,'api',1,2,1,1,1,0,2,''),(2,'other-tenant',101,'','B',7,3,'api',1,2,1,1,1,0,2,''),(3,'other-dept',101,'','A',8,3,'api',1,2,1,1,1,0,2,''),(4,'parent',3,'','A',7,2,'api',2,2,2,2,1,0,1,''),(5,'child',3,'parent','A',7,2,'api',2,2,1,1,0,0,1,''),(6,'self',3,'self','A',7,3,'api',2,2,1,1,1,0,2,'')", } { if _, err := g.DB().Exec(ctx, statement); err != nil { t.Fatal(err) } } ctx = contexts.Set(ctx, &model.Context{ User: &model.Identity{BaseClaims: model.BaseClaims{TenantId: "A"}}, AgentUser: &model.BaseClaimsApi{DeptId: 7, TenantId: "A"}, }) for _, batchNo := range []string{"mine", "parent", "self"} { result, err := NewQuerySim().QueryBatchStatus(ctx, &model.QueryBatchStatusParam{AppId: "app", BatchNo: batchNo}) if err != nil || result == nil || result.BatchNo != batchNo || result.SourceChannel != "api" { t.Fatalf("batch %s: result=%+v err=%v", batchNo, result, err) } } for _, batchNo := range []string{"other-tenant", "other-dept", "child", "missing"} { if _, err := NewQuerySim().QueryBatchStatus(ctx, &model.QueryBatchStatusParam{AppId: "app", BatchNo: batchNo}); err == nil { t.Fatalf("batch exposed: %s", batchNo) } } }