Files
gochat/backend/internal/service/folder_service_test.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

167 lines
4.3 KiB
Go

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/gochat/gochat/internal/repository"
)
// ========== Test Setup ==========
func setupFolderService(t *testing.T) (*FolderService, func()) {
t.Helper()
db := setupServiceTestDB(t)
repo := repository.NewFolderRepo(db)
svc := NewFolderService(repo)
return svc, func() {}
}
// Helper: create a full chain of account -> portal for folder tests
func setupFolderTestChain(t *testing.T) (*FolderService, uint, uint) {
t.Helper()
db := setupServiceTestDB(t)
repo := repository.NewFolderRepo(db)
svc := NewFolderService(repo)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
return svc, portal.ID, account.ID
}
// ========== Create ==========
func TestFolderService_Create(t *testing.T) {
svc, portalID, _ := setupFolderTestChain(t)
req := &CreateFolderRequest{
Name: "Documentation",
}
folder, err := svc.Create(context.Background(), portalID, req)
require.NoError(t, err)
assert.NotZero(t, folder.ID)
assert.Equal(t, portalID, folder.PortalID)
assert.Equal(t, "Documentation", folder.Name)
}
// ========== GetByID ==========
func TestFolderService_GetByID(t *testing.T) {
svc, portalID, _ := setupFolderTestChain(t)
req := &CreateFolderRequest{
Name: "API Docs",
}
created, err := svc.Create(context.Background(), portalID, req)
require.NoError(t, err)
folder, err := svc.GetByID(context.Background(), created.ID)
require.NoError(t, err)
assert.Equal(t, created.ID, folder.ID)
assert.Equal(t, "API Docs", folder.Name)
assert.Equal(t, portalID, folder.PortalID)
}
func TestFolderService_GetByID_NotFound(t *testing.T) {
svc, _, _ := setupFolderTestChain(t)
folder, err := svc.GetByID(context.Background(), 99999)
assert.Nil(t, folder)
assert.Error(t, err)
}
// ========== Update ==========
func TestFolderService_Update(t *testing.T) {
svc, portalID, _ := setupFolderTestChain(t)
req := &CreateFolderRequest{
Name: "Old Name",
}
created, err := svc.Create(context.Background(), portalID, req)
require.NoError(t, err)
updateReq := &UpdateFolderRequest{
Name: "New Name",
}
updated, err := svc.Update(context.Background(), created.ID, updateReq)
require.NoError(t, err)
assert.Equal(t, "New Name", updated.Name)
assert.Equal(t, created.ID, updated.ID)
}
func TestFolderService_Update_NotFound(t *testing.T) {
svc, _, _ := setupFolderTestChain(t)
updateReq := &UpdateFolderRequest{
Name: "Does Not Matter",
}
updated, err := svc.Update(context.Background(), 99999, updateReq)
assert.Nil(t, updated)
assert.Error(t, err)
}
// ========== Delete ==========
func TestFolderService_Delete(t *testing.T) {
svc, portalID, _ := setupFolderTestChain(t)
req := &CreateFolderRequest{
Name: "To Be Deleted",
}
created, err := svc.Create(context.Background(), portalID, req)
require.NoError(t, err)
err = svc.Delete(context.Background(), created.ID)
require.NoError(t, err)
// Verify it's actually gone
folder, err := svc.GetByID(context.Background(), created.ID)
assert.Nil(t, folder)
assert.Error(t, err)
}
func TestFolderService_Delete_NotFound(t *testing.T) {
svc, _, _ := setupFolderTestChain(t)
// Delete checks existence first, so deleting a non-existent folder should error
err := svc.Delete(context.Background(), 99999)
assert.Error(t, err)
}
// ========== ListByPortalID ==========
func TestFolderService_ListByPortalID(t *testing.T) {
svc, portalID, _ := setupFolderTestChain(t)
// Create multiple folders
names := []string{"Folder A", "Folder B", "Folder C"}
for _, name := range names {
req := &CreateFolderRequest{Name: name}
_, err := svc.Create(context.Background(), portalID, req)
require.NoError(t, err)
}
// List all (offset=0, limit=10)
folders, count, err := svc.ListByPortalID(context.Background(), portalID, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(3), count)
assert.Len(t, folders, 3)
// List with pagination (offset=1, limit=2)
folders2, count2, err := svc.ListByPortalID(context.Background(), portalID, 1, 2)
require.NoError(t, err)
assert.Equal(t, int64(3), count2)
assert.Len(t, folders2, 2)
// List for a portal with no folders (use a different portal ID)
folders3, count3, err := svc.ListByPortalID(context.Background(), 88888, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(0), count3)
assert.Len(t, folders3, 0)
}