package app import ( "context" "testing" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gcfg" ) func TestAppDiagnosisTrafficDatabaseFor(t *testing.T) { tests := []struct { name string config appDiagnosisTrafficConfig stable string want string }{ { name: "daily usage uses daily database", config: appDiagnosisTrafficConfig{DbName: "iotlcm", DailyDbName: "iotlcm_daily", MonthlyDbName: "iotlcm_monthly"}, stable: appDiagnosisDailyStable, want: "iotlcm_daily", }, { name: "monthly usage uses monthly database", config: appDiagnosisTrafficConfig{DbName: "iotlcm", DailyDbName: "iotlcm_daily", MonthlyDbName: "iotlcm_monthly"}, stable: appDiagnosisMonthlyStable, want: "iotlcm_monthly", }, { name: "daily usage falls back to legacy database", config: appDiagnosisTrafficConfig{DbName: "iotlcm"}, stable: appDiagnosisDailyStable, want: "iotlcm", }, { name: "monthly usage falls back to legacy database", config: appDiagnosisTrafficConfig{DbName: "iotlcm"}, stable: appDiagnosisMonthlyStable, want: "iotlcm", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := test.config.databaseFor(test.stable); got != test.want { t.Fatalf("databaseFor(%q) = %q, want %q", test.stable, got, test.want) } }) } } func TestAppDiagnosisGetTrafficConfig(t *testing.T) { tests := []struct { name string content string wantDaily string wantMonthly string }{ { name: "explicit split databases", content: `{"tsdb":{"tdengine":{"dbName":"iotlcm","dailyDbName":"iotlcm_daily","monthlyDbName":"iotlcm_monthly"}}}`, wantDaily: "iotlcm_daily", wantMonthly: "iotlcm_monthly", }, { name: "custom database names are trimmed", content: `{"tsdb":{"tdengine":{"dbName":" legacy ","dailyDbName":" custom_daily ","monthlyDbName":" custom_monthly "}}}`, wantDaily: "custom_daily", wantMonthly: "custom_monthly", }, { name: "missing split settings use configured database", content: `{"tsdb":{"tdengine":{"dbName":"iotlcm"}}}`, wantDaily: "iotlcm", wantMonthly: "iotlcm", }, { name: "blank split settings use configured database", content: `{"tsdb":{"tdengine":{"dbName":"iotlcm","dailyDbName":" ","monthlyDbName":""}}}`, wantDaily: "iotlcm", wantMonthly: "iotlcm", }, { name: "missing settings match console default database", content: `{"tsdb":{"tdengine":{}}}`, wantDaily: "iotlcm_daily", wantMonthly: "iotlcm_daily", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { adapter, err := gcfg.NewAdapterContent(test.content) if err != nil { t.Fatalf("configure traffic database: %v", err) } previous := g.Cfg().GetAdapter() g.Cfg().SetAdapter(adapter) t.Cleanup(func() { g.Cfg().SetAdapter(previous) }) config := appDiagnosisGetTrafficConfig(context.Background()) if got := config.databaseFor(appDiagnosisDailyStable); got != test.wantDaily { t.Fatalf("daily database = %q, want %q", got, test.wantDaily) } if got := config.databaseFor(appDiagnosisMonthlyStable); got != test.wantMonthly { t.Fatalf("monthly database = %q, want %q", got, test.wantMonthly) } }) } }