middleware-manager/internal/testutil/testutil.go
hhftechnologies d48d5b8a28
Some checks are pending
Build and Push Docker Image / build-and-push (push) Waiting to run
Tests / Run Tests (push) Waiting to run
Tests / Lint (push) Waiting to run
Tests / Build (push) Blocked by required conditions
lint-error fixes.
2026-04-20 19:52:56 +05:30

59 lines
1.5 KiB
Go

package testutil
import (
"io"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/gin-gonic/gin"
"github.com/hhftechnology/middleware-manager/database"
"github.com/hhftechnology/middleware-manager/services"
)
// NewTempDB returns a SQLite database initialized with the project's migrations.
func NewTempDB(t *testing.T) *database.DB {
t.Helper()
dbPath := filepath.Join(t.TempDir(), "test.db")
db, err := database.InitDB(dbPath)
if err != nil {
t.Fatalf("failed to init temp db: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Errorf("failed to close temp db: %v", err)
}
})
return db
}
// MustExec is a helper that fails the test if the statement errors.
func MustExec(t *testing.T, db *database.DB, query string, args ...interface{}) {
t.Helper()
if _, err := db.Exec(query, args...); err != nil {
t.Fatalf("exec failed: %v", err)
}
}
// NewTestConfigManager creates a ConfigManager backed by a temp file.
func NewTestConfigManager(t *testing.T) *services.ConfigManager {
t.Helper()
cfgPath := filepath.Join(t.TempDir(), "config.json")
cm, err := services.NewConfigManager(cfgPath)
if err != nil {
t.Fatalf("failed to create config manager: %v", err)
}
return cm
}
// NewContext returns a Gin test context and recorder.
func NewContext(t *testing.T, method, path string, body io.Reader) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = httptest.NewRequest(method, path, body)
return c, rec
}