mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-30 19:22:10 +03:00
12d51d7195
Most tests opened a throwaway panel DB with database.InitDB, which runs the full AutoMigrate + seed on an empty file every time: ~230ms, and ~850ms under -race because GORM's reflection-heavy migration is what the detector slows most. internal/web/service does this in ~550 of its 830 tests, so the CI race job spent ~10 of its ~14.6 minutes re-migrating empty databases. internal/database/dbtest.InitDB migrates once per test process, then hands each test its own copy of that file (~130ms under -race) and registers the CloseDB cleanup. The copy then goes through InitDB like a panel restart, so every test still starts from the state a fresh install has. Tests that reopen an existing file, migrate a hand-built legacy DB or target Postgres keep calling database.InitDB. Locally under -race: internal/web/service 626s (last CI run) -> 114s, internal/sub 246s -> 35s.
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
)
|
|
|
|
func TestSniffImportKind(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
header []byte
|
|
want int
|
|
}{
|
|
{"pg custom archive", []byte("PGDMP\x01\x10\x04"), importKindPgDump},
|
|
{"raw sqlite database", []byte("SQLite format 3\x00rest of header"), importKindSQLiteDB},
|
|
{"sqlite cli dump without pragma", []byte("BEGIN TRANSACTION;\nCREATE TABLE t(i);"), importKindSQLiteDump},
|
|
{"bom and whitespace before pragma", []byte("\xef\xbb\xbf\r\n PRAGMA foreign_keys=OFF;"), importKindSQLiteDump},
|
|
{"plain-format postgres dump", []byte("--\n-- PostgreSQL database dump\n--"), importKindUnknown},
|
|
{"empty file", nil, importKindUnknown},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := sniffImportKind(tc.header); got != tc.want {
|
|
t.Errorf("sniffImportKind(%q) = %d, want %d", tc.header, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("panel migration dump", func(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "x-ui.db")
|
|
dbtest.InitDB(t, dbPath)
|
|
dump, err := database.DumpSQLiteToBytes(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("DumpSQLiteToBytes: %v", err)
|
|
}
|
|
if got := sniffImportKind(dump[:64]); got != importKindSQLiteDump {
|
|
t.Errorf("sniffImportKind(real migration dump) = %d, want %d", got, importKindSQLiteDump)
|
|
}
|
|
})
|
|
}
|