chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchmetadata"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
func TestBatchMainEditRejectsMissingCreationMetadata(t *testing.T) {
|
||||
for _, mode := range []int{batchmetadata.Unknown, -1, 3} {
|
||||
if err := NewCommonBatchMain().Edit(context.Background(), &model.BatchMainEditParam{ExecutionMode: &mode}); err == nil {
|
||||
t.Fatalf("accepted execution mode %d", mode)
|
||||
}
|
||||
}
|
||||
for _, input := range []*model.BatchMainEditParam{nil, {}} {
|
||||
if err := NewCommonBatchMain().Edit(context.Background(), input); err == nil {
|
||||
t.Fatal("accepted missing creation metadata")
|
||||
}
|
||||
}
|
||||
mode := batchmetadata.Single
|
||||
quantity := 2
|
||||
if err := NewCommonBatchMain().Edit(context.Background(), &model.BatchMainEditParam{ExecutionMode: &mode, TotalCount: &quantity}); err == nil {
|
||||
t.Fatal("accepted multiple cards as a single-card batch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchMainEditWritesTrustedCreationMetadata(t *testing.T) {
|
||||
ctx := setupBatchExecutionLogListDB(t)
|
||||
for _, column := range []string{"updated_by INTEGER", "updated_at TEXT"} {
|
||||
if _, err := g.DB().Exec(ctx, "ALTER TABLE batch_main ADD COLUMN "+column); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := g.DB().GetCore().ClearTableFieldsAll(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fields, err := g.Model("batch_main").Ctx(ctx).TableFields("batch_main")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fields["id"].Key = "PRI"
|
||||
if _, err := g.DB().GetCore().GetTablesWithCache(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx = contexts.Set(ctx, &model.Context{User: &model.Identity{BaseClaims: model.BaseClaims{ID: 7, TenantId: "A"}}})
|
||||
batchType := consts.BatchTypeInbound
|
||||
quantity := 1
|
||||
claimedUser := int64(999)
|
||||
claimedTenant := "B"
|
||||
for _, mode := range []int{batchmetadata.Single, batchmetadata.Bulk} {
|
||||
batchNo := "META-EDIT-SINGLE"
|
||||
if mode == batchmetadata.Bulk {
|
||||
batchNo = "META-EDIT-BULK"
|
||||
}
|
||||
input := &model.BatchMainEditParam{BatchNo: &batchNo, BatchType: &batchType, ExecutionMode: &mode, TotalCount: &quantity, CreatedBy: &claimedUser, TenantId: &claimedTenant}
|
||||
if err := NewCommonBatchMain().Edit(ctx, input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var batch *entity.BatchMain
|
||||
if err := g.Model("batch_main").Ctx(ctx).Where("batch_no", batchNo).Scan(&batch); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if batch == nil || batch.SourceChannel != batchmetadata.Console || batch.ExecutionMode != uint(mode) || batch.CreatedBy != 7 || batch.TenantId != "A" {
|
||||
t.Fatalf("unexpected batch: %+v", batch)
|
||||
}
|
||||
otherMode := 3 - mode
|
||||
if err := NewCommonBatchMain().Edit(ctx, &model.BatchMainEditParam{Id: batch.Id, BatchNo: &batchNo, ExecutionMode: &otherMode}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var updated *entity.BatchMain
|
||||
if err := g.Model("batch_main").Ctx(ctx).Where("id", batch.Id).Scan(&updated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if updated.SourceChannel != batchmetadata.Console || updated.ExecutionMode != uint(mode) {
|
||||
t.Fatalf("editing changed attribution: %+v", updated)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user