94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package allocate
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchmetadata"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
|
|
|
"github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type allocateMetadataSQLiteDriver struct {
|
|
*sqlite.Driver
|
|
}
|
|
|
|
func (driver *allocateMetadataSQLiteDriver) New(core *gdb.Core, _ *gdb.ConfigNode) (gdb.DB, error) {
|
|
return &allocateMetadataSQLiteDriver{Driver: &sqlite.Driver{Core: core}}, nil
|
|
}
|
|
|
|
func (driver *allocateMetadataSQLiteDriver) DoSelect(ctx context.Context, link gdb.Link, query string, args ...any) (gdb.Result, error) {
|
|
return driver.Core.DoSelect(ctx, link, strings.TrimSuffix(query, " FOR UPDATE"), args...)
|
|
}
|
|
|
|
func TestAllocateSplitInheritsRequestMetadata(t *testing.T) {
|
|
if err := gdb.Register("allocate_metadata_sqlite", &allocateMetadataSQLiteDriver{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
if err := gdb.SetConfig(gdb.Config{"default": gdb.ConfigGroup{{Type: "allocate_metadata_sqlite", Name: ":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 table, record := range map[string]any{"batch_main": entity.BatchMain{}, "batch_detail": entity.BatchDetail{}} {
|
|
recordType := reflect.TypeOf(record)
|
|
columns := make([]string, 0, recordType.NumField())
|
|
for fieldIndex := 0; fieldIndex < recordType.NumField(); fieldIndex++ {
|
|
field := recordType.Field(fieldIndex)
|
|
column := field.Tag.Get("orm")
|
|
if column == "id" {
|
|
columns = append(columns, "id INTEGER PRIMARY KEY AUTOINCREMENT")
|
|
} else {
|
|
columns = append(columns, column+" TEXT")
|
|
}
|
|
}
|
|
if _, err := g.DB().Exec(ctx, "CREATE TABLE "+table+" ("+strings.Join(columns, ",")+")"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := g.Model(table).Ctx(ctx).TableFields(table); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := g.DB().GetCore().GetTablesWithCache(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, mode := range []int{batchmetadata.Single, batchmetadata.Bulk} {
|
|
parentNo := "META-SPLIT-SINGLE"
|
|
if mode == batchmetadata.Bulk {
|
|
parentNo = "META-SPLIT-BULK"
|
|
}
|
|
req := batchimport.SubmitRequest{SourceChannel: batchmetadata.Console, ExecutionMode: mode, SourceType: consts.BatchSourceTypeBatchInput, Operator: batchimport.OperatorContext{TenantId: "A", UserId: 7}}
|
|
processor := NewProcessor()
|
|
if err := createAllocateParent(ctx, req, processor, parentNo, nil, 100); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows := []batchimport.Row{&Row{RowNo: 1, Iccid: "card1"}}
|
|
result, err := createAllocateChunk(ctx, req, processor, parentNo, 1, rows, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, batchNo := range []string{parentNo, result.BatchNo} {
|
|
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 != req.SourceChannel || batch.ExecutionMode != uint(mode) || batch.SourceType != req.SourceType || batch.TotalCount != 1 {
|
|
t.Fatalf("metadata not inherited: %+v", batch)
|
|
}
|
|
if batchNo != parentNo && batch.SourceBatchNo != parentNo {
|
|
t.Fatalf("child lost parent association: %+v", batch)
|
|
}
|
|
}
|
|
}
|
|
}
|