Files
gochat/backend/internal/service/category_service_test.go.bak
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

87 lines
2.6 KiB
Plaintext

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCategoryService_Create(t *testing.T) {
db, _, svc := setupCategoryService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
cat, err := svc.Create(context.Background(), portal.ID, account.ID, &CreateCategoryRequest{
Name: "My Category",
Slug: "my-category",
Description: "A test category",
})
require.NoError(t, err)
assert.Equal(t, "My Category", cat.Name)
assert.Equal(t, "my-category", cat.Slug)
assert.Equal(t, portal.ID, cat.PortalID)
}
func TestCategoryService_GetByID(t *testing.T) {
db, _, svc := setupCategoryService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
cat := createTestCategory(t, db, portal.ID)
found, err := svc.GetByID(context.Background(), cat.ID)
require.NoError(t, err)
assert.Equal(t, cat.Name, found.Name)
}
func TestCategoryService_GetByID_NotFound(t *testing.T) {
_, _, svc := setupCategoryService(t)
_, err := svc.GetByID(context.Background(), 99999)
assert.Error(t, err)
}
func TestCategoryService_Update(t *testing.T) {
db, _, svc := setupCategoryService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
cat := createTestCategory(t, db, portal.ID)
updated, err := svc.Update(context.Background(), cat.ID, &UpdateCategoryRequest{
Name: "Updated Category",
Description: "Updated description",
})
require.NoError(t, err)
assert.Equal(t, "Updated Category", updated.Name)
}
func TestCategoryService_Delete(t *testing.T) {
db, _, svc := setupCategoryService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
cat := createTestCategory(t, db, portal.ID)
err := svc.Delete(context.Background(), cat.ID)
require.NoError(t, err)
_, err = svc.GetByID(context.Background(), cat.ID)
assert.Error(t, err)
}
func TestCategoryService_ListByPortalID(t *testing.T) {
db, _, svc := setupCategoryService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
_, err := svc.Create(context.Background(), portal.ID, account.ID, &CreateCategoryRequest{Name: "C1", Slug: "c1"})
require.NoError(t, err)
_, err = svc.Create(context.Background(), portal.ID, account.ID, &CreateCategoryRequest{Name: "C2", Slug: "c2"})
require.NoError(t, err)
cats, count, err := svc.ListByPortalID(context.Background(), portal.ID, "", 1, 10)
require.NoError(t, err)
assert.Equal(t, int64(2), count)
assert.Len(t, cats, 2)
}