test(H-271): remove race-suite lifecycle blockers (#45)
Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -26,12 +27,32 @@ func init() {
|
||||
|
||||
// ============ DB-Backed Test Helpers (Cov16) ============
|
||||
|
||||
// ponytail: coverage tests are sequential; use per-test DBs if they adopt t.Parallel.
|
||||
var (
|
||||
coverageTestDBMu sync.Mutex
|
||||
coverageTestDBs = make(map[string]*gorm.DB)
|
||||
)
|
||||
|
||||
func sharedCoverageTestDB(t *testing.T, name string, models ...interface{}) *gorm.DB {
|
||||
t.Helper()
|
||||
coverageTestDBMu.Lock()
|
||||
defer coverageTestDBMu.Unlock()
|
||||
|
||||
db := coverageTestDBs[name]
|
||||
if db == nil {
|
||||
var err error
|
||||
db, err = gorm.Open(sqlite.Open("file:"+name+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
require.NoError(t, err, "failed to open test database")
|
||||
require.NoError(t, db.AutoMigrate(models...), "failed to auto-migrate models")
|
||||
coverageTestDBs[name] = db
|
||||
}
|
||||
t.Cleanup(func() { testutil.TruncateAll(t, db) })
|
||||
return db
|
||||
}
|
||||
|
||||
// newTestDB_Cov16 creates an in-memory SQLite DB with a broad set of models auto-migrated.
|
||||
func newTestDB_Cov16(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err, "failed to open test database")
|
||||
|
||||
models := []interface{}{
|
||||
&model.Account{},
|
||||
&model.User{},
|
||||
@@ -100,10 +121,7 @@ func newTestDB_Cov16(t *testing.T) *gorm.DB {
|
||||
&model.Audit{},
|
||||
}
|
||||
|
||||
err = db.AutoMigrate(models...)
|
||||
require.NoError(t, err, "failed to auto-migrate models")
|
||||
|
||||
return db
|
||||
return sharedCoverageTestDB(t, "coverage16", models...)
|
||||
}
|
||||
|
||||
// seedAccount_Cov16 creates and persists a test account.
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
@@ -18,8 +17,6 @@ import (
|
||||
|
||||
func newTestDB_Cov18(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
models := []interface{}{
|
||||
&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{},
|
||||
&model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{},
|
||||
@@ -36,10 +33,7 @@ func newTestDB_Cov18(t *testing.T) *gorm.DB {
|
||||
&model.WorkingHour{}, &model.CaptainAssistant{}, &model.CaptainDocument{},
|
||||
&model.CaptainPreference{},
|
||||
}
|
||||
for _, m := range models {
|
||||
require.NoError(t, db.AutoMigrate(m))
|
||||
}
|
||||
return db
|
||||
return sharedCoverageTestDB(t, "coverage18", models...)
|
||||
}
|
||||
|
||||
func seedAccount_Cov18(t *testing.T, db *gorm.DB) uint {
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
@@ -22,9 +21,6 @@ import (
|
||||
|
||||
func newTestDB_Cov19(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err, "failed to open test database")
|
||||
|
||||
models := []interface{}{
|
||||
&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{},
|
||||
&model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{},
|
||||
@@ -42,8 +38,7 @@ func newTestDB_Cov19(t *testing.T) *gorm.DB {
|
||||
&model.ContactNote{}, &model.CaptainAssistant{}, &model.CaptainDocument{},
|
||||
&model.CaptainPreference{},
|
||||
}
|
||||
require.NoError(t, db.AutoMigrate(models...), "failed to auto-migrate models")
|
||||
return db
|
||||
return sharedCoverageTestDB(t, "coverage19", models...)
|
||||
}
|
||||
|
||||
func seedAccount_Cov19(t *testing.T, db *gorm.DB) *model.Account {
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
@@ -24,9 +23,6 @@ import (
|
||||
|
||||
func newTestDB_Cov20(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err, "failed to open test database")
|
||||
|
||||
models := []interface{}{
|
||||
&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{},
|
||||
&model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{},
|
||||
@@ -44,8 +40,7 @@ func newTestDB_Cov20(t *testing.T) *gorm.DB {
|
||||
&model.WorkingHour{}, &model.ContactNote{}, &model.CaptainAssistant{},
|
||||
&model.CaptainDocument{}, &model.CaptainPreference{}, &model.WidgetTest{},
|
||||
}
|
||||
require.NoError(t, db.AutoMigrate(models...), "failed to auto-migrate models")
|
||||
return db
|
||||
return sharedCoverageTestDB(t, "coverage20", models...)
|
||||
}
|
||||
|
||||
func seedAccount_Cov20(t *testing.T, db *gorm.DB) *model.Account {
|
||||
|
||||
Reference in New Issue
Block a user