[feature] migrate to monorepo
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s

This commit is contained in:
CDN 2025-02-21 00:49:20 +08:00
commit 05ddc1f783
Signed by: CDN
GPG key ID: 0C656827F9F80080
267 changed files with 75165 additions and 0 deletions

View 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
}

View 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)
}

View 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)
}