102 lines
3.2 KiB
Go
102 lines
3.2 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"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/model/entity"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
func TestRepositorySaveOutcomeUpdatesOnlySuccessfulCard(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)
|
|
}
|
|
defer g.DB().Close(ctx)
|
|
|
|
statements := []string{
|
|
`CREATE TABLE t_sim_card_info (
|
|
id INTEGER PRIMARY KEY,
|
|
iccid TEXT NOT NULL,
|
|
boss_card_status INTEGER NOT NULL,
|
|
status_sync_time DATETIME,
|
|
tenant_id TEXT NOT NULL,
|
|
updated_by INTEGER,
|
|
updated_at DATETIME
|
|
)`,
|
|
`CREATE TABLE batch_detail (
|
|
id INTEGER PRIMARY KEY,
|
|
batch_no TEXT NOT NULL,
|
|
detail_status INTEGER NOT NULL,
|
|
error_reason TEXT,
|
|
snapshot TEXT,
|
|
updated_by INTEGER,
|
|
updated_at DATETIME
|
|
)`,
|
|
`INSERT INTO t_sim_card_info(id, iccid, boss_card_status, tenant_id) VALUES
|
|
(1, 'card-success', 2, 'tenant-1'),
|
|
(2, 'card-failed', 2, 'tenant-1')`,
|
|
`INSERT INTO batch_detail(id, batch_no, detail_status) VALUES
|
|
(1, 'STP-test', 1),
|
|
(2, 'STP-test', 1)`,
|
|
}
|
|
for _, statement := range statements {
|
|
if _, err := g.DB().Exec(ctx, statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
config, err := OperationSuspend.Config()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := &Task{
|
|
Batch: &entity.BatchMain{BatchNo: "STP-test", TenantId: "tenant-1"},
|
|
Config: config,
|
|
Operator: batchimport.OperatorContext{TenantId: "tenant-1", UserId: 10},
|
|
}
|
|
repository := NewRepository()
|
|
successRow := &Row{DetailID: 1, Iccid: "card-success", StatusBefore: consts.V3ApiSimBossCardStatusActivated}
|
|
failedRow := &Row{DetailID: 2, Iccid: "card-failed", StatusBefore: consts.V3ApiSimBossCardStatusActivated}
|
|
if err = repository.SaveOutcome(ctx, task, successRow, &entity.TSimCardInfo{Id: 1}, Outcome{Success: true, OrderNum: "order-1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = repository.SaveOutcome(ctx, task, failedRow, &entity.TSimCardInfo{Id: 2}, Outcome{Code: 500, Message: "rejected"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var cards []struct {
|
|
Iccid string `orm:"iccid"`
|
|
BossCardStatus int `orm:"boss_card_status"`
|
|
}
|
|
if err = g.DB().Model("t_sim_card_info").OrderAsc("id").Scan(&cards); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cards) != 2 || cards[0].BossCardStatus != consts.V3ApiSimBossCardStatusStopped || cards[1].BossCardStatus != consts.V3ApiSimBossCardStatusActivated {
|
|
t.Fatalf("unexpected local card statuses: %+v", cards)
|
|
}
|
|
|
|
var details []struct {
|
|
DetailStatus int `orm:"detail_status"`
|
|
ErrorReason string `orm:"error_reason"`
|
|
}
|
|
if err = g.DB().Model("batch_detail").OrderAsc("id").Scan(&details); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(details) != 2 || details[0].DetailStatus != consts.BatchDetailStatusSuccess || details[1].DetailStatus != consts.BatchDetailStatusFailed || details[1].ErrorReason != "rejected" {
|
|
t.Fatalf("unexpected detail results: %+v", details)
|
|
}
|
|
}
|