Files
gochat/backend/internal/service/portal_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

100 lines
2.8 KiB
Plaintext

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPortalService_Create(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal, err := svc.Create(context.Background(), account.ID, &CreatePortalRequest{
Name: "My Portal",
Slug: "my-portal",
Description: "A test portal",
Locale: "en",
})
require.NoError(t, err)
assert.Equal(t, "My Portal", portal.Name)
assert.Equal(t, "my-portal", portal.Slug)
assert.Equal(t, account.ID, portal.AccountID)
assert.False(t, portal.Archived)
}
func TestPortalService_GetByID(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
found, err := svc.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, portal.Name, found.Name)
assert.Equal(t, portal.Slug, found.Slug)
}
func TestPortalService_GetByID_NotFound(t *testing.T) {
_, _, svc := setupPortalService(t)
_, err := svc.GetByID(context.Background(), 99999)
assert.Error(t, err)
}
func TestPortalService_Update(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
updated, err := svc.Update(context.Background(), portal.ID, &UpdatePortalRequest{
Name: "Updated Portal",
Description: "Updated description",
})
require.NoError(t, err)
assert.Equal(t, "Updated Portal", updated.Name)
assert.Equal(t, "Updated description", updated.Description)
}
func TestPortalService_Update_Archive(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
archived := true
updated, err := svc.Update(context.Background(), portal.ID, &UpdatePortalRequest{
Archived: &archived,
})
require.NoError(t, err)
assert.True(t, updated.Archived)
}
func TestPortalService_Delete(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
err := svc.Delete(context.Background(), portal.ID)
require.NoError(t, err)
_, err = svc.GetByID(context.Background(), portal.ID)
assert.Error(t, err)
}
func TestPortalService_ListByAccountID(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
// Create 2 portals
_, err := svc.Create(context.Background(), account.ID, &CreatePortalRequest{Name: "P1", Slug: "p1", Locale: "en"})
require.NoError(t, err)
_, err = svc.Create(context.Background(), account.ID, &CreatePortalRequest{Name: "P2", Slug: "p2", Locale: "en"})
require.NoError(t, err)
portals, count, err := svc.ListByAccountID(context.Background(), account.ID, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(2), count)
assert.Len(t, portals, 2)
}