2026-05-03 23:01:31 +08:00
|
|
|
package db_test
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-04 00:50:56 +08:00
|
|
|
"os"
|
2026-05-03 23:01:31 +08:00
|
|
|
"testing"
|
2026-05-09 14:54:55 +08:00
|
|
|
"time"
|
2026-05-04 00:50:56 +08:00
|
|
|
|
2026-05-03 23:01:31 +08:00
|
|
|
"apigo.cc/go/db"
|
2026-05-09 14:54:55 +08:00
|
|
|
"apigo.cc/go/log"
|
2026-05-03 23:01:31 +08:00
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestVersionControl(t *testing.T) {
|
2026-05-09 14:54:55 +08:00
|
|
|
db.ResetAllForTest()
|
|
|
|
|
dbPath := "./test_version.db"
|
|
|
|
|
os.Remove(dbPath)
|
|
|
|
|
db.SetConfigForTest("test_version", &db.Config{Type: "sqlite", Host: dbPath})
|
|
|
|
|
dbInst := db.GetDB("test_version", log.DefaultLogger)
|
|
|
|
|
if dbInst == nil {
|
|
|
|
|
t.Fatal("db is nil")
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
dbInst.Destroy()
|
|
|
|
|
os.Remove(dbPath)
|
|
|
|
|
}()
|
2026-05-04 00:50:56 +08:00
|
|
|
|
|
|
|
|
|
2026-05-09 14:54:55 +08:00
|
|
|
dbInst.Exec("CREATE TABLE versioned_docs (id INTEGER PRIMARY KEY, content TEXT, autoVersion BIGINT)")
|
2026-05-04 00:50:56 +08:00
|
|
|
|
2026-05-09 14:54:55 +08:00
|
|
|
// Initial insert
|
|
|
|
|
res := dbInst.Insert("versioned_docs", map[string]string{"content": "v1"})
|
2026-05-04 00:50:56 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
|
t.Fatalf("Insert failed: %v", res.Error)
|
|
|
|
|
}
|
2026-05-09 14:54:55 +08:00
|
|
|
if res.Id() != 1 {
|
|
|
|
|
t.Fatalf("Expected ID 1, got %d", res.Id())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check initial version
|
|
|
|
|
v1 := dbInst.Query("SELECT autoVersion FROM versioned_docs WHERE id=1").IntOnR1C1()
|
|
|
|
|
if v1 <= 0 {
|
|
|
|
|
t.Errorf("Expected initial version > 0, got %d", v1)
|
|
|
|
|
}
|
2026-05-04 00:50:56 +08:00
|
|
|
|
2026-05-09 14:54:55 +08:00
|
|
|
// Update should increment version
|
|
|
|
|
time.Sleep(1 * time.Millisecond) // Ensure NextVersion has a different timestamp if needed by underlying implementation
|
|
|
|
|
updateRes := dbInst.Update("versioned_docs", map[string]string{"content": "v2"}, "id=?", 1)
|
|
|
|
|
if updateRes.Error != nil {
|
|
|
|
|
t.Fatalf("Update failed: %v", updateRes.Error)
|
2026-05-04 00:50:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-09 14:54:55 +08:00
|
|
|
v2 := dbInst.Query("SELECT autoVersion FROM versioned_docs WHERE id=1").IntOnR1C1()
|
|
|
|
|
if v2 <= v1 {
|
|
|
|
|
t.Errorf("Expected version to increment, got v2=%d, v1=%d", v2, v1)
|
2026-05-04 00:50:56 +08:00
|
|
|
}
|
|
|
|
|
}
|