236 lines
7.4 KiB
Go
236 lines
7.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// ========== Team Repo ==========
|
|
|
|
func TestTeamRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamCreateOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "Support Team", Description: "Main support", AllowAutoAssignment: true}
|
|
err := repo.Create(context.Background(), team)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, team.ID)
|
|
}
|
|
|
|
func TestTeamRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamFindOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "Sales Team"}
|
|
require.NoError(t, repo.Create(context.Background(), team))
|
|
|
|
found, err := repo.FindByID(context.Background(), team.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "Sales Team", found.Name)
|
|
assert.Equal(t, account.ID, found.AccountID)
|
|
}
|
|
|
|
func TestTeamRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestTeamRepo_FindByIDAndAccount(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamScopedOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "Scoped Team"}
|
|
require.NoError(t, repo.Create(context.Background(), team))
|
|
|
|
found, err := repo.FindByIDAndAccount(context.Background(), team.ID, account.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, team.ID, found.ID)
|
|
|
|
// Different account should not find it
|
|
found2, err2 := repo.FindByIDAndAccount(context.Background(), team.ID, 9999)
|
|
assert.Error(t, err2)
|
|
assert.Nil(t, found2)
|
|
}
|
|
|
|
func TestTeamRepo_ListByAccount(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamListOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
names := []string{"Alpha", "Beta", "Gamma"}
|
|
for _, n := range names {
|
|
require.NoError(t, repo.Create(context.Background(), &model.Team{
|
|
AccountID: account.ID, Name: n,
|
|
}))
|
|
}
|
|
|
|
teams, total, err := repo.ListByAccount(context.Background(), account.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, teams, 3)
|
|
}
|
|
|
|
func TestTeamRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamUpdateOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "BeforeUpdate"}
|
|
require.NoError(t, repo.Create(context.Background(), team))
|
|
|
|
team.Name = "AfterUpdate"
|
|
team.Description = "Updated desc"
|
|
require.NoError(t, repo.Update(context.Background(), team))
|
|
|
|
found, err := repo.FindByID(context.Background(), team.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "AfterUpdate", found.Name)
|
|
assert.Equal(t, "Updated desc", found.Description)
|
|
}
|
|
|
|
func TestTeamRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewTeamRepo(db)
|
|
|
|
account := &model.Account{Name: "TeamDeleteOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "DeleteMe"}
|
|
require.NoError(t, repo.Create(context.Background(), team))
|
|
|
|
err := repo.Delete(context.Background(), team.ID, account.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), team.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// ========== TeamMember Repo ==========
|
|
|
|
func TestTeamMemberRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
tmRepo := NewTeamMemberRepo(db)
|
|
|
|
account := &model.Account{Name: "TmCreateOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "CreateTmTeam"}
|
|
require.NoError(t, db.Create(team).Error)
|
|
|
|
user := &model.User{Name: "CreateAgent", Email: "create@example.com", Provider: "email"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
|
|
tm := &model.TeamMember{TeamID: team.ID, UserID: user.ID, AvailabilityStatus: "offline"}
|
|
err := tmRepo.Create(context.Background(), tm)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, tm.ID)
|
|
}
|
|
|
|
func TestTeamMemberRepo_FindByTeamAndUser(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
tmRepo := NewTeamMemberRepo(db)
|
|
|
|
account := &model.Account{Name: "TmFindByOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "FindTeam"}
|
|
require.NoError(t, db.Create(team).Error)
|
|
|
|
user := &model.User{Name: "FindAgent", Email: "find@example.com", Provider: "email"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
|
|
require.NoError(t, tmRepo.Create(context.Background(), &model.TeamMember{TeamID: team.ID, UserID: user.ID, AvailabilityStatus: "online"}))
|
|
|
|
found, err := tmRepo.FindByTeamAndUser(context.Background(), team.ID, user.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "online", found.AvailabilityStatus)
|
|
}
|
|
|
|
func TestTeamMemberRepo_FindByTeam(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
tmRepo := NewTeamMemberRepo(db)
|
|
|
|
account := &model.Account{Name: "TmFindByTeamOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "FindTeamMembers"}
|
|
require.NoError(t, db.Create(team).Error)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
user := &model.User{Name: "TmAgent" + string(rune('A'+i)), Email: "tm" + string(rune('A'+i)) + "@example.com", Provider: "email"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
require.NoError(t, tmRepo.Create(context.Background(), &model.TeamMember{TeamID: team.ID, UserID: user.ID}))
|
|
}
|
|
|
|
members, err := tmRepo.FindByTeam(context.Background(), team.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, members, 2)
|
|
}
|
|
|
|
func TestTeamMemberRepo_FindByUser(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
tmRepo := NewTeamMemberRepo(db)
|
|
|
|
account := &model.Account{Name: "TmFindByUserOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
user := &model.User{Name: "FindUser", Email: "finduser@example.com", Provider: "email"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
team := &model.Team{AccountID: account.ID, Name: "UserTeam" + string(rune('A'+i))}
|
|
require.NoError(t, db.Create(team).Error)
|
|
require.NoError(t, tmRepo.Create(context.Background(), &model.TeamMember{TeamID: team.ID, UserID: user.ID}))
|
|
}
|
|
|
|
members, err := tmRepo.FindByUser(context.Background(), user.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, members, 2)
|
|
}
|
|
|
|
func TestTeamMemberRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
tmRepo := NewTeamMemberRepo(db)
|
|
|
|
account := &model.Account{Name: "TmDeleteOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
team := &model.Team{AccountID: account.ID, Name: "DeleteTmTeam"}
|
|
require.NoError(t, db.Create(team).Error)
|
|
|
|
user := &model.User{Name: "DeleteAgent", Email: "delete@example.com", Provider: "email"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
|
|
tm := &model.TeamMember{TeamID: team.ID, UserID: user.ID}
|
|
require.NoError(t, tmRepo.Create(context.Background(), tm))
|
|
|
|
err := tmRepo.Delete(context.Background(), team.ID, user.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := tmRepo.FindByTeamAndUser(context.Background(), team.ID, user.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
} |