650 lines
18 KiB
Go
650 lines
18 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"daili/internal/domain"
|
|
)
|
|
|
|
func newTestDB(t *testing.T) *DB {
|
|
t.Helper()
|
|
db, err := Open(filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatalf("打开数据库失败: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
if err := db.Migrate(); err != nil {
|
|
t.Fatalf("迁移失败: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func mustPackage(t *testing.T, db *DB, name, unit string, amount int) *Package {
|
|
t.Helper()
|
|
id, err := db.CreatePackage(&Package{Name: name, Unit: unit, DefaultAmount: amount, Color: "#2F5D50"})
|
|
if err != nil {
|
|
t.Fatalf("创建套餐失败: %v", err)
|
|
}
|
|
p, err := db.Package(id)
|
|
if err != nil {
|
|
t.Fatalf("读取套餐失败: %v", err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func mustCustomer(t *testing.T, db *DB, tail, name string) int64 {
|
|
t.Helper()
|
|
id, err := db.CreateCustomer(&Customer{Tail: tail, Name: name})
|
|
if err != nil {
|
|
t.Fatalf("创建客户失败: %v", err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestCreateSubAndDuplicate(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
|
|
sub, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("创建订阅失败: %v", err)
|
|
}
|
|
if sub.EndDate != domain.Date(domain.AddDays(today, 29)) {
|
|
t.Fatalf("结束日=%s", sub.EndDate)
|
|
}
|
|
if sub.Unit != domain.UnitDay || sub.Status != domain.StatusActive {
|
|
t.Fatalf("unit=%s status=%s", sub.Unit, sub.Status)
|
|
}
|
|
|
|
if _, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
}); err == nil {
|
|
t.Fatal("同一客户同一套餐应禁止重复订阅")
|
|
}
|
|
|
|
// 初始记录
|
|
adj, err := db.AdjustmentsBySub(sub.ID)
|
|
if err != nil {
|
|
t.Fatalf("读取追加记录失败: %v", err)
|
|
}
|
|
if len(adj) != 1 || adj[0].Kind != "initial" || adj[0].DaysAdded != 30 {
|
|
t.Fatalf("初始追加记录不正确: %+v", adj)
|
|
}
|
|
}
|
|
|
|
func TestExtendDaysFromTodayWhenExpired(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
|
|
sub, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.AddDays(today, -40)), Days: 30,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sub.EndDate != domain.Date(domain.AddDays(today, -11)) {
|
|
t.Fatalf("end=%s", sub.EndDate)
|
|
}
|
|
|
|
newEnd := domain.AddDays(today, 29)
|
|
plan, err := db.ExtendDays(sub.ID, domain.Date(newEnd), "续费", 0)
|
|
if err != nil {
|
|
t.Fatalf("追加失败: %v", err)
|
|
}
|
|
if domain.Date(plan.Base) != domain.Date(today) {
|
|
t.Fatalf("已到期订阅应从今天起算,实际 %s", domain.Date(plan.Base))
|
|
}
|
|
if plan.Days != 30 {
|
|
t.Fatalf("追加天数=%d want 30", plan.Days)
|
|
}
|
|
got, _ := db.Sub(sub.ID)
|
|
if got.EndDate != domain.Date(newEnd) {
|
|
t.Fatalf("新结束日=%s", got.EndDate)
|
|
}
|
|
if got.Status != domain.StatusActive {
|
|
t.Fatalf("状态=%s", got.Status)
|
|
}
|
|
|
|
adj, _ := db.AdjustmentsBySub(sub.ID)
|
|
if len(adj) != 2 || adj[0].Kind != "extend_days" || adj[0].DaysAdded != 30 {
|
|
t.Fatalf("追加记录不正确: %+v", adj[0])
|
|
}
|
|
if adj[0].BaseDate != domain.Date(today) {
|
|
t.Fatalf("记录起点=%s", adj[0].BaseDate)
|
|
}
|
|
}
|
|
|
|
func TestExtendDaysFromOldEndWhenActive(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
|
|
sub, _ := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
})
|
|
newEnd := domain.AddDays(today, 39)
|
|
plan, err := db.ExtendDays(sub.ID, domain.Date(newEnd), "", 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if domain.Date(plan.Base) != domain.Date(domain.AddDays(today, 30)) {
|
|
t.Fatalf("未到期应从原结束日次日起算,实际 %s", domain.Date(plan.Base))
|
|
}
|
|
if plan.Days != 10 {
|
|
t.Fatalf("追加天数=%d want 10", plan.Days)
|
|
}
|
|
}
|
|
|
|
func TestExtendDaysRejectsBackwards(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
|
|
sub, _ := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
})
|
|
if _, err := db.ExtendDays(sub.ID, domain.Date(domain.AddDays(today, 10)), "", 0); err == nil {
|
|
t.Fatal("早于计算起点的日期应被拒绝")
|
|
}
|
|
}
|
|
|
|
func TestAddCountAndCheckinConsumption(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "深渊十次", domain.UnitCount, 10)
|
|
cid := mustCustomer(t, db, "5678", "大鹏")
|
|
today := domain.Today()
|
|
|
|
sub, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), TotalCount: 10,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sub.TotalCount != 10 || sub.UsedCount != 0 {
|
|
t.Fatalf("total=%d used=%d", sub.TotalCount, sub.UsedCount)
|
|
}
|
|
|
|
// 打卡完成 → 扣 1 次
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(today), "done", "", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := db.Sub(sub.ID)
|
|
if got.UsedCount != 1 {
|
|
t.Fatalf("used=%d want 1", got.UsedCount)
|
|
}
|
|
|
|
// 同一天改成「跳过」→ 返还
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(today), "skipped", "", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = db.Sub(sub.ID)
|
|
if got.UsedCount != 0 {
|
|
t.Fatalf("used=%d want 0", got.UsedCount)
|
|
}
|
|
|
|
// 再改回完成 → 再扣
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(today), "done", "", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = db.Sub(sub.ID)
|
|
if got.UsedCount != 1 {
|
|
t.Fatalf("used=%d want 1", got.UsedCount)
|
|
}
|
|
|
|
// 撤销打卡 → 返还
|
|
ci, err := db.CheckinFor(sub.ID, domain.Date(today))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.DeleteCheckin(ci.ID, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = db.Sub(sub.ID)
|
|
if got.UsedCount != 0 {
|
|
t.Fatalf("used=%d want 0", got.UsedCount)
|
|
}
|
|
|
|
// 次数用尽后不能再打卡
|
|
_, _ = db.sql.Exec(`UPDATE subscriptions SET used_count=total_count WHERE id=?`, sub.ID)
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(domain.AddDays(today, -1)), "done", "", 0); err == nil {
|
|
t.Fatal("次数用尽时打卡应被拒绝")
|
|
}
|
|
|
|
// 追加次数
|
|
if err := db.AddCount(sub.ID, 5, "", "加购", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = db.Sub(sub.ID)
|
|
if got.TotalCount != 15 {
|
|
t.Fatalf("total=%d want 15", got.TotalCount)
|
|
}
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(domain.AddDays(today, -1)), "done", "", 0); err != nil {
|
|
t.Fatalf("追加后应可打卡: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPauseResumeShiftsEndDate(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
|
|
sub, _ := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
})
|
|
if err := db.PauseSub(sub.ID, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := db.Sub(sub.ID)
|
|
if got.Status != domain.StatusPaused {
|
|
t.Fatalf("status=%s", got.Status)
|
|
}
|
|
|
|
// 把暂停开始时间改成 5 天前,模拟暂停了 5 天
|
|
oldStart := domain.Date(domain.AddDays(today, -5)) + " 09:00:00"
|
|
if _, err := db.sql.Exec(`UPDATE sub_pauses SET start_at=? WHERE sub_id=? AND end_at IS NULL`, oldStart, sub.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
shift, err := db.ResumeSub(sub.ID, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if shift != 5 {
|
|
t.Fatalf("顺延天数=%d want 5", shift)
|
|
}
|
|
got, _ = db.Sub(sub.ID)
|
|
if got.EndDate != domain.Date(domain.AddDays(today, 34)) {
|
|
t.Fatalf("end=%s want %s", got.EndDate, domain.Date(domain.AddDays(today, 34)))
|
|
}
|
|
if got.Status != domain.StatusActive {
|
|
t.Fatalf("status=%s", got.Status)
|
|
}
|
|
}
|
|
|
|
func TestCalendarSubsExcludesOpenEndedCount(t *testing.T) {
|
|
db := newTestDB(t)
|
|
dayPkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cntPkg := mustPackage(t, db, "深渊十次", domain.UnitCount, 10)
|
|
today := domain.Today()
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
|
|
daySub, _ := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: dayPkg.ID, StartDate: domain.Date(today), Days: 30,
|
|
})
|
|
cid2 := mustCustomer(t, db, "5678", "大鹏")
|
|
if _, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid2, PackageID: cntPkg.ID, StartDate: domain.Date(today), TotalCount: 10,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
subs, err := db.CalendarSubs(domain.Date(today), domain.Date(today))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(subs) != 1 || subs[0].ID != daySub.ID {
|
|
t.Fatalf("无有效期的按次订阅不应出现在日历: %+v", subs)
|
|
}
|
|
}
|
|
|
|
func TestBackup(t *testing.T) {
|
|
db := newTestDB(t)
|
|
mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
path, err := db.BackupNow(14)
|
|
if err != nil {
|
|
t.Fatalf("备份失败: %v", err)
|
|
}
|
|
list, err := db.ListBackups()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 1 {
|
|
t.Fatalf("备份数量=%d want 1 (%s)", len(list), path)
|
|
}
|
|
// 同日再备份:文件名按秒生成,允许重复
|
|
if _, err := db.BackupNow(1); err != nil {
|
|
// 同一秒内会重名,算作已存在,不视为失败
|
|
t.Logf("第二次备份返回: %v", err)
|
|
}
|
|
list, _ = db.ListBackups()
|
|
if len(list) > 1 {
|
|
t.Fatalf("保留份数未生效: %d", len(list))
|
|
}
|
|
}
|
|
|
|
func TestSettingsAndBadges(t *testing.T) {
|
|
db := newTestDB(t)
|
|
_ = db.SetSetting("warn_days", "5")
|
|
if got := db.Setting("warn_days", "3"); got != "5" {
|
|
t.Fatalf("setting=%s", got)
|
|
}
|
|
if got := db.Setting("missing", "def"); got != "def" {
|
|
t.Fatalf("默认值=%s", got)
|
|
}
|
|
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
today := domain.Today()
|
|
_, _ = db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.AddDays(today, -30)), Days: 31,
|
|
})
|
|
due, due3, err := db.Badges(today)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if due != 1 || due3 != 1 {
|
|
t.Fatalf("due=%d due3=%d want 1/1", due, due3)
|
|
}
|
|
}
|
|
|
|
func TestSearchByTail(t *testing.T) {
|
|
db := newTestDB(t)
|
|
mustCustomer(t, db, "8899", "阿飞")
|
|
mustCustomer(t, db, "8890", "阿飞二号")
|
|
today := domain.Today()
|
|
list, total, err := db.ListCustomers("8899", 20, 0, domain.Date(today), 3)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 1 || len(list) != 1 || list[0].Name != "阿飞" {
|
|
t.Fatalf("搜索命中 %d 条", total)
|
|
}
|
|
list, total, _ = db.ListCustomers("889", 20, 0, domain.Date(today), 3)
|
|
if total != 2 {
|
|
t.Fatalf("前缀搜索应命中 2 条,实际 %d", total)
|
|
}
|
|
}
|
|
|
|
func TestUniqueCustomer(t *testing.T) {
|
|
db := newTestDB(t)
|
|
mustCustomer(t, db, "1234", "老张")
|
|
_, err := db.CreateCustomer(&Customer{Tail: "1234", Name: "老张"})
|
|
if err == nil {
|
|
t.Fatal("重复的尾号+姓名应被拒绝")
|
|
}
|
|
if _, err := db.CreateCustomer(&Customer{Tail: "1234", Name: "另一位"}); err != nil {
|
|
t.Fatalf("同尾号不同姓名应允许: %v", err)
|
|
}
|
|
}
|
|
|
|
var _ = time.Now
|
|
|
|
func TestScreenTopLimitedToTen(t *testing.T) {
|
|
db := newTestDB(t)
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
today := domain.Today()
|
|
for i := 0; i < 12; i++ {
|
|
cid, err := db.CreateCustomer(&Customer{Tail: "T" + strconv.Itoa(i), Name: "客户" + strconv.Itoa(i)})
|
|
if err != nil {
|
|
t.Fatalf("建客户失败: %v", err)
|
|
}
|
|
if _, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30 - i,
|
|
}); err != nil {
|
|
t.Fatalf("建订阅失败: %v", err)
|
|
}
|
|
}
|
|
st, err := db.ScreenStats(today, 3)
|
|
if err != nil {
|
|
t.Fatalf("统计失败: %v", err)
|
|
}
|
|
if len(st.Top) != 10 {
|
|
t.Fatalf("TOP 榜应为 10 条,实际 %d 条", len(st.Top))
|
|
}
|
|
}
|
|
|
|
func TestBackupKeepPrune(t *testing.T) {
|
|
db := newTestDB(t)
|
|
mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
dir := db.BackupDir()
|
|
names := []string{
|
|
"daili-20260101-000001.db",
|
|
"daili-20260102-000001.db",
|
|
"daili-20260103-000001.db",
|
|
}
|
|
for _, n := range names {
|
|
if _, err := db.BackupTo(filepath.Join(dir, n), 0); err != nil {
|
|
t.Fatalf("备份失败: %v", err)
|
|
}
|
|
}
|
|
if err := db.pruneBackups(2); err != nil {
|
|
t.Fatalf("清理失败: %v", err)
|
|
}
|
|
list, err := db.ListBackups()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 2 {
|
|
t.Fatalf("保留 2 份,实际 %d 份: %v", len(list), list)
|
|
}
|
|
if list[0] != names[2] || list[1] != names[1] {
|
|
t.Fatalf("保留的不是最新两份: %v", list)
|
|
}
|
|
}
|
|
|
|
func TestBackupDirOverride(t *testing.T) {
|
|
db := newTestDB(t)
|
|
custom := filepath.Join(t.TempDir(), "自定义备份")
|
|
db.SetBackupDir(custom)
|
|
if db.BackupDir() != custom {
|
|
t.Fatalf("备份目录未生效: %s", db.BackupDir())
|
|
}
|
|
if _, err := db.BackupNow(5); err != nil {
|
|
t.Fatalf("备份失败: %v", err)
|
|
}
|
|
if _, err := os.Stat(custom); err != nil {
|
|
t.Fatalf("备份目录未创建: %v", err)
|
|
}
|
|
}
|
|
|
|
// copyFile 用于模拟“用户只拷贝数据库文件”。
|
|
func copyFile(t *testing.T, src, dst string) {
|
|
t.Helper()
|
|
data, err := os.ReadFile(src)
|
|
if err != nil {
|
|
t.Fatalf("读取 %s 失败: %v", src, err)
|
|
}
|
|
if err := os.WriteFile(dst, data, 0o644); err != nil {
|
|
t.Fatalf("写入 %s 失败: %v", dst, err)
|
|
}
|
|
}
|
|
|
|
func TestSingleFileDatabase(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "daili.db")
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Migrate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if mode, err := db.JournalMode(); err != nil || !strings.EqualFold(mode, "delete") {
|
|
t.Fatalf("journal_mode=%s err=%v", mode, err)
|
|
}
|
|
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
if _, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, suffix := range []string{"-wal", "-shm"} {
|
|
if _, err := os.Stat(path + suffix); err == nil {
|
|
t.Fatalf("运行中不应出现 %s", suffix)
|
|
}
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, suffix := range []string{"-wal", "-shm", "-journal"} {
|
|
if _, err := os.Stat(path + suffix); err == nil {
|
|
t.Fatalf("关闭后不应残留 %s", suffix)
|
|
}
|
|
}
|
|
if fi, err := os.Stat(path); err != nil || fi.Size() < 8192 {
|
|
t.Fatalf("主库大小异常: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCopyDatabaseIsLossless(t *testing.T) {
|
|
srcDir := t.TempDir()
|
|
srcPath := filepath.Join(srcDir, "daili.db")
|
|
db, err := Open(srcPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Migrate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
|
cid := mustCustomer(t, db, "1234", "老张")
|
|
sub, err := db.CreateSub(CreateSubInput{
|
|
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveCheckin(sub.ID, domain.Date(domain.Today()), "done", "迁移前打卡", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// 只拷贝 daili.db 一个文件
|
|
dstDir := t.TempDir()
|
|
dstPath := filepath.Join(dstDir, "daili.db")
|
|
copyFile(t, srcPath, dstPath)
|
|
|
|
cp, err := Open(dstPath)
|
|
if err != nil {
|
|
t.Fatalf("打开副本失败: %v", err)
|
|
}
|
|
defer cp.Close()
|
|
if n, err := cp.CustomerCount(); err != nil || n != 1 {
|
|
t.Fatalf("客户数=%d err=%v", n, err)
|
|
}
|
|
got, err := cp.Sub(sub.ID)
|
|
if err != nil || got.CustomerTail != "1234" || got.EndDate != sub.EndDate {
|
|
t.Fatalf("订阅丢失: %+v err=%v", got, err)
|
|
}
|
|
ci, err := cp.CheckinFor(sub.ID, domain.Date(domain.Today()))
|
|
if err != nil || ci.Note != "迁移前打卡" {
|
|
t.Fatalf("打卡记录丢失: %+v err=%v", ci, err)
|
|
}
|
|
for _, suffix := range []string{"-wal", "-shm"} {
|
|
if _, err := os.Stat(dstPath + suffix); err == nil {
|
|
t.Fatalf("副本目录出现了 %s", suffix)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeleteDatabaseResets(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "daili.db")
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Migrate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mustCustomer(t, db, "1234", "老张")
|
|
if err := db.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{path, path + "-wal", path + "-shm", path + "-journal"} {
|
|
_ = os.Remove(p)
|
|
}
|
|
|
|
fresh, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("删除后重建失败: %v", err)
|
|
}
|
|
defer fresh.Close()
|
|
if err := fresh.Migrate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := fresh.CustomerCount(); n != 0 {
|
|
t.Fatalf("删库后应无客户,实际 %d", n)
|
|
}
|
|
if n, _ := fresh.CountUsers(); n != 0 {
|
|
t.Fatalf("删库后应无管理员,实际 %d", n)
|
|
}
|
|
}
|
|
|
|
func TestStaleSidecarsCleanup(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "daili.db")
|
|
// 模拟“删了主库但留下旧版本 WAL”的情况
|
|
if err := os.WriteFile(path+"-wal", []byte("stale"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path+"-shm", []byte("stale"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("打开失败: %v", err)
|
|
}
|
|
defer db.Close()
|
|
for _, suffix := range []string{"-wal", "-shm"} {
|
|
if _, err := os.Stat(path + suffix); err == nil {
|
|
t.Fatalf("残留的 %s 应被清理", suffix)
|
|
}
|
|
}
|
|
if err := db.Migrate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestOpenLegacyWALDatabaseKeepsData(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "legacy.db")
|
|
|
|
// 模拟 v1.1 的 WAL 库
|
|
dsn := strings.ReplaceAll(path, "\\", "/") + "?_pragma=journal_mode(WAL)"
|
|
old, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := old.Exec(`CREATE TABLE legacy(x INTEGER)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := old.Exec(`INSERT INTO legacy VALUES(42)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := old.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("打开旧库失败: %v", err)
|
|
}
|
|
defer db.Close()
|
|
var x int
|
|
if err := db.sql.QueryRow(`SELECT x FROM legacy`).Scan(&x); err != nil || x != 42 {
|
|
t.Fatalf("旧库数据丢失: x=%d err=%v", x, err)
|
|
}
|
|
if mode, _ := db.JournalMode(); !strings.EqualFold(mode, "delete") {
|
|
t.Fatalf("旧库未切到单文件模式: %s", mode)
|
|
}
|
|
}
|