36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
var testDBCounter int64
|
|
|
|
// OpenTestDB opens an in-memory SQLite DB with the v2 schema migrated.
|
|
// Shared by all package tests (pay has no prior DB test harness — this is it).
|
|
// Each call gets a uniquely named in-memory DB (cache=shared is still needed
|
|
// so GORM's connection pool sees the same migrated schema across connections),
|
|
// so concurrent/parallel tests in the same package don't share state.
|
|
func OpenTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
n := atomic.AddInt64(&testDBCounter, 1)
|
|
dsn := fmt.Sprintf("file:testdb_%d?mode=memory&cache=shared", n)
|
|
db, err := gorm.Open(sqlite.Open(dsn),
|
|
&gorm.Config{Logger: logger.Default.LogMode(logger.Silent), TranslateError: true})
|
|
if err != nil {
|
|
t.Fatalf("open test db: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&OrderV2{}, &Attempt{}, &Account{}, &Refund{}, &WebhookDelivery{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
sqlDB, _ := db.DB()
|
|
t.Cleanup(func() { _ = sqlDB.Close() })
|
|
return db
|
|
}
|