feat: 保存并展示 PDD 店铺与价格采样信息 (#31)

This commit is contained in:
chengma
2026-08-07 17:13:25 +08:00
parent a2fc1501c4
commit 93beef8b36
17 changed files with 492 additions and 73 deletions
+43
View File
@@ -452,6 +452,37 @@ func TestMigrate_已经是最新版本再次调用不报错(t *testing.T) {
}
}
func TestMigrate_v4增加可空店铺名且老数据保持NULL(t *testing.T) {
db := newV2NewStructureDB(t)
if _, err := db.Exec(`
INSERT INTO pdd_products (goods_id, url, collect_status, created_at, updated_at)
VALUES ('737116531267', 'https://example.invalid', 'pending', '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z')`); err != nil {
t.Fatalf("插入 v2 老数据失败: %v", err)
}
if err := Migrate(db); err != nil {
t.Fatalf("迁移到 v4 失败: %v", err)
}
columns, err := tableColumnSet(db, "pdd_products")
if err != nil {
t.Fatalf("读取 pdd_products 列失败: %v", err)
}
if !columns["shop_name"] {
t.Fatal("v4 必须新增 shop_name 列")
}
var shop sql.NullString
if err := db.QueryRow(
`SELECT shop_name FROM pdd_products WHERE goods_id = '737116531267'`,
).Scan(&shop); err != nil {
t.Fatalf("读取老数据店铺名失败: %v", err)
}
if shop.Valid {
t.Errorf("老数据的 shop_name 应为 NULL,实际 %q", shop.String)
}
}
// v2 新结构库(#16 改写后的 v1)第一次 Migrate 时,migrateV3 检测到结构
// 已经是最终形态,只更新版本号、不改任何表——第二次调用(对应用户重启
// 服务)应该是彻底的空操作:user_version 已经是 3,Migrate 最外层的
@@ -848,6 +879,18 @@ func TestCheckSchema_v2老库迁移后通过(t *testing.T) {
}
}
func TestCheckSchema_缺少v4关键列时拒绝(t *testing.T) {
db := newV2NewStructureDB(t)
if err := migrateV3(db); err != nil {
t.Fatalf("准备 v3 数据库失败: %v", err)
}
err := CheckSchema(db)
if err == nil || !strings.Contains(err.Error(), "shop_name") {
t.Fatalf("缺少 shop_name 时应拒绝启动并指出列名,实际 %v", err)
}
}
func TestCheckSchema_缺表时拒绝(t *testing.T) {
db := newFreshDB(t)
if _, err := db.Exec(`DROP TABLE pdd_products`); err != nil {