Files
gochat/internal/handler/api/v1/agent_capacity_handler_test.go
T
2026-06-04 15:44:48 +08:00

133 lines
4.1 KiB
Go

package v1
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type AgentCapacityHandlerTestSuite struct {
suite.Suite
db *gorm.DB
handler *AgentCapacityHandler
account *model.Account
}
func (s *AgentCapacityHandlerTestSuite) SetupSuite() {
gin.SetMode(gin.TestMode)
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
s.Require().NoError(err)
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.User{}, &model.AgentCapacityPolicy{}))
s.db = db
repo := repository.NewAgentCapacityPolicyRepo(db)
svc := service.NewAgentCapacityPolicyService(repo)
s.handler = NewAgentCapacityHandler(svc)
s.account = &model.Account{Name: "test-capacity-account"}
s.Require().NoError(db.Create(s.account).Error)
}
func (s *AgentCapacityHandlerTestSuite) TearDownSuite() {
if s.db != nil {
sqlDB, _ := s.db.DB()
sqlDB.Close()
}
}
func TestAgentCapacityHandlerSuite(t *testing.T) {
suite.Run(t, new(AgentCapacityHandlerTestSuite))
}
func (s *AgentCapacityHandlerTestSuite) TestList_Success() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/agent_capacity_policies", s.handler.List)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies", s.account.ID), nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestList_BadRequest_InvalidAccountID() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/agent_capacity_policies", s.handler.List)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/agent_capacity_policies", nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusUnauthorized, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
r := gin.New()
r.POST("/api/v1/accounts/:account_id/agent_capacity_policies", s.handler.Create)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies", s.account.ID), nil)
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestGet_BadRequest_InvalidID() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/agent_capacity_policies/:id", s.handler.Get)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/abc", s.account.ID), nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestGet_NotFound() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/agent_capacity_policies/:id", s.handler.Get)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/99999", s.account.ID), nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusNotFound, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestUpdate_BadRequest_InvalidID() {
r := gin.New()
r.PUT("/api/v1/accounts/:account_id/agent_capacity_policies/:id", s.handler.Update)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/abc", s.account.ID), nil)
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
func (s *AgentCapacityHandlerTestSuite) TestDelete_BadRequest_InvalidID() {
r := gin.New()
r.DELETE("/api/v1/accounts/:account_id/agent_capacity_policies/:id", s.handler.Delete)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/agent_capacity_policies/abc", s.account.ID), nil)
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}