[feature] migrate to monorepo
This commit is contained in:
commit
05ddc1f783
267 changed files with 75165 additions and 0 deletions
57
backend/internal/testutil/db.go
Normal file
57
backend/internal/testutil/db.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"tss-rocks-be/ent"
|
||||
)
|
||||
|
||||
// SetupTestDB creates a new test database and returns a client
|
||||
func SetupTestDB(t *testing.T) *ent.Client {
|
||||
// Create a temporary SQLite database for testing
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "test.db")
|
||||
|
||||
client, err := ent.Open(dialect.SQLite, "file:"+dbPath+"?mode=memory&cache=shared&_fk=1")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Run the auto migration tool
|
||||
err = client.Schema.Create(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Clean up the database after the test
|
||||
t.Cleanup(func() {
|
||||
client.Close()
|
||||
os.Remove(dbPath)
|
||||
})
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// CleanupTestDB removes all data from the test database
|
||||
func CleanupTestDB(t *testing.T, client *ent.Client) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Delete all data in reverse order of dependencies
|
||||
_, err := client.Permission.Delete().Exec(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.Role.Delete().Exec(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = client.User.Delete().Exec(ctx)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// IsSQLiteConstraintError checks if the error is a SQLite constraint error
|
||||
func IsSQLiteConstraintError(err error) bool {
|
||||
sqliteErr, ok := err.(sqlite3.Error)
|
||||
return ok && sqliteErr.Code == sqlite3.ErrConstraint
|
||||
}
|
32
backend/internal/testutil/mock.go
Normal file
32
backend/internal/testutil/mock.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// MockReadCloser is a mock implementation of io.ReadCloser
|
||||
type MockReadCloser struct {
|
||||
io.Reader
|
||||
CloseFunc func() error
|
||||
}
|
||||
|
||||
func (m MockReadCloser) Close() error {
|
||||
if m.CloseFunc != nil {
|
||||
return m.CloseFunc()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewMockReadCloser creates a new MockReadCloser with the given content
|
||||
func NewMockReadCloser(content string) io.ReadCloser {
|
||||
return MockReadCloser{Reader: strings.NewReader(content)}
|
||||
}
|
||||
|
||||
// RequireMockEquals asserts that two mocks are equal
|
||||
func RequireMockEquals(t *testing.T, expected, actual interface{}) {
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
70
backend/internal/testutil/testutil.go
Normal file
70
backend/internal/testutil/testutil.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"tss-rocks-be/ent"
|
||||
"tss-rocks-be/ent/enttest"
|
||||
)
|
||||
|
||||
// SetupTestRouter returns a new Gin engine for testing
|
||||
func SetupTestRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
return gin.New()
|
||||
}
|
||||
|
||||
// NewTestClient creates a new ent client for testing
|
||||
func NewTestClient() *ent.Client {
|
||||
client := enttest.Open(testing.TB(nil), "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
return client
|
||||
}
|
||||
|
||||
// MakeTestRequest performs a test HTTP request and returns the response
|
||||
func MakeTestRequest(t *testing.T, router *gin.Engine, method, path string, body interface{}) *httptest.ResponseRecorder {
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
jsonBytes, err := json.Marshal(body)
|
||||
require.NoError(t, err)
|
||||
reqBody = bytes.NewBuffer(jsonBytes)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(method, path, reqBody)
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
// AssertResponse asserts the HTTP response status code and body
|
||||
func AssertResponse(t *testing.T, w *httptest.ResponseRecorder, expectedStatus int, expectedBody interface{}) {
|
||||
assert.Equal(t, expectedStatus, w.Code)
|
||||
|
||||
if expectedBody != nil {
|
||||
var actualBody interface{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), &actualBody)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expectedBody, actualBody)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertErrorResponse asserts an error response with a specific message
|
||||
func AssertErrorResponse(t *testing.T, w *httptest.ResponseRecorder, expectedStatus int, expectedMessage string) {
|
||||
assert.Equal(t, expectedStatus, w.Code)
|
||||
|
||||
var response struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expectedMessage, response.Error)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue