170 lines
5.3 KiB
Go
170 lines
5.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// Helper: create prerequisite Account for AgentCapacityPolicy tests.
|
|
func createAgentCapacityPolicyPrereqs(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
|
|
account := &model.Account{Name: "ACPTestOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
return account
|
|
}
|
|
|
|
// Helper: create a test AgentCapacityPolicy with all prerequisites.
|
|
func createTestAgentCapacityPolicy(t *testing.T, db *gorm.DB, accountID uint, name string) *model.AgentCapacityPolicy {
|
|
t.Helper()
|
|
|
|
policy := &model.AgentCapacityPolicy{
|
|
AccountID: accountID,
|
|
Name: name,
|
|
AssignmentLogic: "round_robin",
|
|
ExclusionRules: json.RawMessage(`{"exclude_offline":true}`),
|
|
}
|
|
require.NoError(t, db.Create(policy).Error)
|
|
return policy
|
|
}
|
|
|
|
// ========== 1. FindByID ==========
|
|
|
|
func TestAgentCapacityPolicyRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
policy := createTestAgentCapacityPolicy(t, db, account.ID, "TestCapacityPolicy")
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, policy.ID, found.ID)
|
|
assert.Equal(t, "TestCapacityPolicy", found.Name)
|
|
assert.Equal(t, "round_robin", found.AssignmentLogic)
|
|
}
|
|
|
|
func TestAgentCapacityPolicyRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// ========== 2. FindByAccount ==========
|
|
|
|
func TestAgentCapacityPolicyRepo_FindByAccount(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
createTestAgentCapacityPolicy(t, db, account.ID, "Policy1")
|
|
createTestAgentCapacityPolicy(t, db, account.ID, "Policy2")
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, policies, 2)
|
|
}
|
|
|
|
func TestAgentCapacityPolicyRepo_FindByAccount_Pagination(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
createTestAgentCapacityPolicy(t, db, account.ID, "Policy1")
|
|
createTestAgentCapacityPolicy(t, db, account.ID, "Policy2")
|
|
createTestAgentCapacityPolicy(t, db, account.ID, "Policy3")
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, policies, 2)
|
|
|
|
policies2, total2, err2 := repo.FindByAccount(context.Background(), account.ID, 2, 2)
|
|
assert.NoError(t, err2)
|
|
assert.Equal(t, int64(3), total2)
|
|
assert.Len(t, policies2, 1)
|
|
}
|
|
|
|
func TestAgentCapacityPolicyRepo_FindByAccount_NoMatches(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), 9999, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), total)
|
|
assert.Len(t, policies, 0)
|
|
}
|
|
|
|
// ========== 3. Create ==========
|
|
|
|
func TestAgentCapacityPolicyRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
policy := &model.AgentCapacityPolicy{
|
|
AccountID: account.ID,
|
|
Name: "NewCapacityPolicy",
|
|
AssignmentLogic: "least_busy",
|
|
ExclusionRules: json.RawMessage(`{"exclude_offline":true,"exclude_busy":true}`),
|
|
}
|
|
err := repo.Create(context.Background(), policy)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, policy.ID)
|
|
}
|
|
|
|
// ========== 4. Update ==========
|
|
|
|
func TestAgentCapacityPolicyRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
policy := createTestAgentCapacityPolicy(t, db, account.ID, "BeforeUpdate")
|
|
|
|
policy.Name = "AfterUpdate"
|
|
policy.AssignmentLogic = "least_busy"
|
|
err := repo.Update(context.Background(), policy)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "AfterUpdate", found.Name)
|
|
assert.Equal(t, "least_busy", found.AssignmentLogic)
|
|
}
|
|
|
|
// ========== 5. Delete ==========
|
|
|
|
func TestAgentCapacityPolicyRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
account := createAgentCapacityPolicyPrereqs(t, db)
|
|
policy := createTestAgentCapacityPolicy(t, db, account.ID, "DeletePolicy")
|
|
|
|
err := repo.Delete(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestAgentCapacityPolicyRepo_Delete_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentCapacityPolicy{})
|
|
repo := NewAgentCapacityPolicyRepo(db)
|
|
|
|
err := repo.Delete(context.Background(), 9999)
|
|
assert.NoError(t, err)
|
|
} |