Files
iotlcm/server/internal/logic/app/app_diagnosis_legacy_card_test.go
T

116 lines
4.1 KiB
Go

package app
import (
"context"
"fmt"
"os"
"strings"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcfg"
)
func TestDiagnosisPrefersPlatformChannelWithoutLegacyDB(t *testing.T) {
if os.Getenv("IOTLCM_TEST_LOCAL_READONLY") != "1" {
t.Skip("set IOTLCM_TEST_LOCAL_READONLY=1 for the local read-only DB integration test")
}
ctx := context.Background()
link := g.Cfg().MustGet(ctx, "database.default.link").String()
adapter, err := gcfg.NewAdapterContent(fmt.Sprintf(`{"database":{"default":{"link":%q},"legacyCard":{"link":""}}}`, link))
if err != nil {
t.Fatal(err)
}
previous := g.Cfg().GetAdapter()
g.Cfg().SetAdapter(adapter)
t.Cleanup(func() { g.Cfg().SetAdapter(previous) })
row := &appCardQueryRow{TSimCardInfo: entity.TSimCardInfo{Id: 1, TenantId: "000000", Iccid: "898608771525C0625498", ChanKey: "129"}}
if err = NewAppDiagnosis().resolveDiagnosisCardChannel(ctx, NewAppCard(), row, row.Iccid, "查询诊断卡片"); err != nil {
t.Fatalf("platform channel should not need the legacy DB: %v", err)
}
if row.ChanKey != "129" || row.TempKey == "" {
t.Fatalf("unexpected platform channel: key=%q template=%q", row.ChanKey, row.TempKey)
}
}
func TestLegacyDiagnosisCardRejectsMissingReadOnlyDatabase(t *testing.T) {
adapter, err := gcfg.NewAdapterContent(`{"database":{"legacyCard":{"link":""}}}`)
if err != nil {
t.Fatal(err)
}
previous := g.Cfg().GetAdapter()
g.Cfg().SetAdapter(adapter)
t.Cleanup(func() { g.Cfg().SetAdapter(previous) })
card, err := queryLegacyDiagnosisCard(context.Background(), "89860401788250435125")
if card != nil || err == nil || !strings.Contains(err.Error(), "旧平台只读数据库未配置") {
t.Fatalf("missing read-only config: card=%+v err=%v", card, err)
}
}
func TestLifecycleChannelAvailability(t *testing.T) {
tests := []struct {
name string
info *appCardChannelInfo
want bool
}{
{name: "enabled channel", info: &appCardChannelInfo{ChanKey: "129", ChanStatus: 0}, want: true},
{name: "disabled channel", info: &appCardChannelInfo{ChanKey: "129", ChanStatus: 1}},
{name: "missing channel key", info: &appCardChannelInfo{ChanStatus: 0}},
{name: "missing channel"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := appCardChannelAvailable(test.info); got != test.want {
t.Fatalf("appCardChannelAvailable() = %t, want %t", got, test.want)
}
})
}
}
func TestDiagnosisRangeAssociationWithoutLegacyDatabase(t *testing.T) {
if os.Getenv("IOTLCM_TEST_LOCAL_READONLY") != "1" {
t.Skip("set IOTLCM_TEST_LOCAL_READONLY=1 for the local read-only DB integration test")
}
ctx := context.Background()
const iccid = "898608141623D0229706"
cardLogic := NewAppCard()
row, err := NewAppDiagnosis().resolveAssocOnlyDiagnosisCard(ctx, cardLogic, "000000", iccid, "查询诊断卡片")
if err != nil || row == nil {
t.Fatalf("range association should work without legacy database: row=%+v err=%v", row, err)
}
if row.ChanKey != "28" || row.TempKey != "sim_yd_v5" || row.Id != 0 {
t.Fatalf("unexpected range association: %+v", row)
}
if err = NewAppDiagnosis().resolveDiagnosisCardChannel(ctx, cardLogic, row, iccid, "查询诊断卡片"); err != nil {
t.Fatal(err)
}
}
func TestLegacyDiagnosisCardReadOnlyIntegration(t *testing.T) {
if os.Getenv("IOTLCM_TEST_LEGACY_READONLY") != "1" {
t.Skip("set IOTLCM_TEST_LEGACY_READONLY=1 for the read-only legacy DB integration test")
}
for _, test := range []struct {
iccid string
chanKey string
}{
{iccid: "898608771525C0625498", chanKey: "129"},
{iccid: "898604F4152391115474", chanKey: "112"},
{iccid: "8986112223600594371", chanKey: "71"},
} {
t.Run(test.iccid, func(t *testing.T) {
card, err := queryLegacyDiagnosisCard(context.Background(), test.iccid)
if err != nil {
t.Fatalf("read-only legacy card lookup: %v", err)
}
if card == nil || card.ICCID != test.iccid || card.ChannelKey != test.chanKey {
t.Fatalf("unexpected legacy channel mapping: card=%+v, want channel %q", card, test.chanKey)
}
})
}
}