135 lines
5.4 KiB
Go
135 lines
5.4 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
|
dataService "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/data"
|
|
)
|
|
|
|
func TestPreferredDataServiceChanKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
appBatch bool
|
|
snapshot string
|
|
card string
|
|
want string
|
|
}{
|
|
{name: "app resolved snapshot wins", appBatch: true, snapshot: " legacy-71 ", card: "stale-1", want: "legacy-71"},
|
|
{name: "console keeps card channel", snapshot: "legacy-71", card: " card-112 ", want: "card-112"},
|
|
{name: "card fallback", card: " card-112 ", want: "card-112"},
|
|
{name: "empty", want: ""},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := preferredDataServiceChanKey(test.appBatch, test.snapshot, test.card); got != test.want {
|
|
t.Fatalf("preferredDataServiceChanKey() = %q, want %q", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDataAppBatchUsesSnapshotChannel(t *testing.T) {
|
|
if !dataAppBatchUsesSnapshotChannel(gjson.New(map[string]any{"source": "APP"})) {
|
|
t.Fatal("app batch should use the resolved snapshot channel")
|
|
}
|
|
if dataAppBatchUsesSnapshotChannel(gjson.New(map[string]any{"source": "console"})) {
|
|
t.Fatal("console batch must continue using the current card channel")
|
|
}
|
|
}
|
|
|
|
func TestSaveResultDoesNotClearTrafficCutoffFields(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := gdb.New(gdb.ConfigNode{Type: "sqlite", Link: "sqlite::@file(:memory:)"})
|
|
if err != nil {
|
|
t.Fatalf("create sqlite database: %v", err)
|
|
}
|
|
statements := []string{
|
|
"CREATE TABLE t_sim_card_info (id INTEGER PRIMARY KEY, tenant_id TEXT NOT NULL, data_service_status INTEGER NOT NULL DEFAULT 1, status_sync_time DATETIME, traffic_cutoff_status INTEGER NOT NULL DEFAULT 0, traffic_cutoff_threshold REAL NOT NULL DEFAULT 0, traffic_cutoff_cross_month_hold INTEGER NOT NULL DEFAULT 0, traffic_cutoff_triggered_at DATETIME, updated_by INTEGER NOT NULL DEFAULT 0, updated_at DATETIME)",
|
|
"CREATE TABLE batch_detail (id INTEGER PRIMARY KEY, batch_no TEXT NOT NULL, detail_status INTEGER NOT NULL, error_code TEXT, error_reason TEXT, snapshot TEXT, updated_by INTEGER NOT NULL DEFAULT 0, updated_at DATETIME)",
|
|
"INSERT INTO t_sim_card_info (id, tenant_id, data_service_status, traffic_cutoff_status, traffic_cutoff_threshold, traffic_cutoff_cross_month_hold, updated_by) VALUES (1, '000000', 1, 1, 512, 1, 7)",
|
|
"INSERT INTO batch_detail (id, batch_no, detail_status) VALUES (9, 'DST-TEST', 1)",
|
|
}
|
|
for _, statement := range statements {
|
|
if _, err = db.Exec(ctx, statement); err != nil {
|
|
t.Fatalf("execute setup SQL: %v", err)
|
|
}
|
|
}
|
|
|
|
repo := &Repository{db: db}
|
|
task := &dataService.Task{
|
|
Batch: dataService.Batch{
|
|
BatchNo: "DST-TEST",
|
|
BatchType: consts.BatchTypeDataStop,
|
|
TenantID: "000000",
|
|
CreatedBy: 13,
|
|
},
|
|
Operation: dataService.Operation{
|
|
BatchType: consts.BatchTypeDataStop,
|
|
TargetStatus: consts.SimDataServiceStatusDisabled,
|
|
},
|
|
}
|
|
detail := &dataService.Detail{
|
|
ID: 9,
|
|
Card: &dataService.Card{ID: 1, DataServiceStatus: consts.SimDataServiceStatusEnabled},
|
|
}
|
|
if _, err = repo.SaveResult(ctx, task, detail, dataService.Outcome{Success: true}); err != nil {
|
|
t.Fatalf("SaveResult() error = %v", err)
|
|
}
|
|
|
|
row, err := db.GetOne(ctx, "SELECT data_service_status, traffic_cutoff_status, traffic_cutoff_threshold, traffic_cutoff_cross_month_hold FROM t_sim_card_info WHERE id = 1")
|
|
if err != nil {
|
|
t.Fatalf("query card result: %v", err)
|
|
}
|
|
if got := row["data_service_status"].Int(); got != consts.SimDataServiceStatusDisabled {
|
|
t.Fatalf("data_service_status = %d", got)
|
|
}
|
|
if got := row["traffic_cutoff_status"].Int(); got != 1 {
|
|
t.Fatalf("traffic_cutoff_status = %d, want 1", got)
|
|
}
|
|
if got := row["traffic_cutoff_threshold"].Float64(); got != 512 {
|
|
t.Fatalf("traffic_cutoff_threshold = %v, want 512", got)
|
|
}
|
|
if got := row["traffic_cutoff_cross_month_hold"].Int(); got != 1 {
|
|
t.Fatalf("traffic_cutoff_cross_month_hold = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestDataServiceUpdateDataTracksOnlyCrossMonthCutoff(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
batchType int
|
|
hold int
|
|
wantNil bool
|
|
}{
|
|
{name: "cutoff with hold", batchType: consts.BatchTypeDataCut, hold: 1, wantNil: false},
|
|
{name: "cutoff without hold", batchType: consts.BatchTypeDataCut, hold: 0, wantNil: true},
|
|
{name: "manual stop", batchType: consts.BatchTypeDataStop, hold: 1, wantNil: true},
|
|
{name: "data start clears trigger", batchType: consts.BatchTypeDataStart, hold: 1, wantNil: false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
task := &dataService.Task{
|
|
Batch: dataService.Batch{BatchType: tt.batchType, CreatedBy: 7},
|
|
Operation: dataService.Operation{TargetStatus: consts.SimDataServiceStatusDisabled},
|
|
}
|
|
detail := &dataService.Detail{TrafficCutoffCrossMonthHold: tt.hold}
|
|
update := dataServiceUpdateData(task, detail)
|
|
if gotNil := update.TrafficCutoffTriggeredAt == nil; gotNil != tt.wantNil {
|
|
t.Fatalf("TrafficCutoffTriggeredAt nil = %v, want %v", gotNil, tt.wantNil)
|
|
}
|
|
if update.DataServiceStatus != task.Operation.TargetStatus {
|
|
t.Fatalf("DataServiceStatus = %v, want %d", update.DataServiceStatus, task.Operation.TargetStatus)
|
|
}
|
|
if update.StatusSyncTime == nil {
|
|
t.Fatal("StatusSyncTime must be set")
|
|
}
|
|
})
|
|
}
|
|
}
|