75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"cmsp/internal/config"
|
|
"cmsp/internal/store"
|
|
)
|
|
|
|
// 显式设置配置路径才执行真实查询;只写临时 SQLite,不执行店铺写操作。
|
|
func TestERPGoLiveReadOnlySync(t *testing.T) {
|
|
path := os.Getenv("CMSP_ERPGo_LIVE_CONFIG")
|
|
if path == "" {
|
|
t.Skip("set CMSP_ERPGo_LIVE_CONFIG to run authorized read-only integration")
|
|
}
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatal("cannot load local integration configuration")
|
|
}
|
|
a := NewApp()
|
|
a.ctx = context.Background()
|
|
a.cfg = cfg
|
|
dbPath := filepath.Join(t.TempDir(), "live-query.db")
|
|
db, err := store.Open(dbPath)
|
|
if err != nil {
|
|
t.Fatal("cannot open temporary database")
|
|
}
|
|
a.db = db
|
|
t.Cleanup(func() { _ = a.db.Close() })
|
|
shops, err := a.RefreshShops()
|
|
if err != nil {
|
|
t.Fatalf("live shop query failed: %v", err)
|
|
}
|
|
t.Logf("live shop query and local cache succeeded: %d shops", len(shops))
|
|
if len(shops) == 0 {
|
|
t.Skip("no shops available for product integration")
|
|
}
|
|
if _, err := a.DownloadProductData(shops[0].PlatformShopID); err != nil {
|
|
t.Fatalf("live product pagination or persistence failed: %v", err)
|
|
}
|
|
page, err := a.db.ListProducts(store.ProductQuery{PlatformShopID: shops[0].PlatformShopID, Page: 1, PageSize: 1})
|
|
if err != nil {
|
|
t.Fatal("cannot read synchronized products")
|
|
}
|
|
t.Logf("live sequential product pagination and local persistence succeeded: %d products", page.Total)
|
|
if len(page.Items) > 0 {
|
|
p := page.Items[0]
|
|
diagnoses, err := a.db.ListDiagnoses(p.ID)
|
|
if err != nil {
|
|
t.Fatal("cannot read persisted diagnoses")
|
|
}
|
|
if err := a.db.UpdateProductStatus(p.ID, store.VideoFound, store.DownloadDone, store.UploadDone, "fictional-local-error"); err != nil {
|
|
t.Fatal("cannot seed local states")
|
|
}
|
|
if err := a.db.SyncProducts([]store.Product{p}, map[string][]store.Diagnosis{p.ID: diagnoses}, "demo-resync-time"); err != nil {
|
|
t.Fatal("cannot persist repeated local sync")
|
|
}
|
|
if err := a.db.Close(); err != nil {
|
|
t.Fatal("cannot close local cache")
|
|
}
|
|
a.db, err = store.Open(dbPath)
|
|
if err != nil {
|
|
t.Fatal("cannot reopen local cache")
|
|
}
|
|
got, found, err := a.db.GetProduct(p.ID)
|
|
if err != nil || !found || got.VideoStatus != store.VideoFound || got.DownloadStatus != store.DownloadDone || got.UploadStatus != store.UploadDone || got.LastError != "fictional-local-error" {
|
|
t.Fatal("local states did not survive persistence and reopen")
|
|
}
|
|
t.Log("download/upload/local video states survived repeated local persistence and database reopen")
|
|
}
|
|
}
|