Files
crawlab/core/controllers/router_test.go
Marvin Zhang 95a6eb4e7b chore: add OpenTelemetry auto SDK dependency
- Introduced go.opentelemetry.io/auto/sdk v1.1.0 to go.mod and updated go.sum accordingly.
- Enhanced observability capabilities by including the OpenTelemetry SDK for automatic instrumentation.
- Ensured consistency in dependency management across the project.
2025-03-17 14:24:07 +08:00

93 lines
2.3 KiB
Go

package controllers_test
import (
"testing"
"github.com/crawlab-team/crawlab/core/controllers"
"github.com/crawlab-team/crawlab/core/models/models"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestRouterGroups(t *testing.T) {
router := gin.Default()
groups := controllers.NewRouterGroups(router)
assertions := []struct {
group *gin.RouterGroup
name string
}{
{groups.AuthGroup.GinRouterGroup(), "AuthGroup"},
{groups.AnonymousGroup.GinRouterGroup(), "AnonymousGroup"},
}
for _, a := range assertions {
assert.NotNil(t, a.group, a.name+" should not be nil")
}
}
func TestRegisterController_Routes(t *testing.T) {
router := gin.Default()
groups := controllers.NewRouterGroups(router)
ctr := controllers.NewController[models.TestModel]()
basePath := "/testmodels"
controllers.RegisterController(groups.AuthGroup, basePath, ctr)
// Check if all routes are registered
routes := router.Routes()
var methodPaths []string
for _, route := range routes {
methodPaths = append(methodPaths, route.Method+" - "+route.Path)
}
expectedRoutes := []gin.RouteInfo{
{Method: "GET", Path: basePath},
{Method: "GET", Path: basePath + "/:id"},
{Method: "POST", Path: basePath},
{Method: "PUT", Path: basePath + "/:id"},
{Method: "PATCH", Path: basePath},
{Method: "DELETE", Path: basePath + "/:id"},
{Method: "DELETE", Path: basePath},
}
assert.Equal(t, len(expectedRoutes), len(routes))
for _, route := range expectedRoutes {
assert.Contains(t, methodPaths, route.Method+" - "+route.Path)
}
}
func TestInitRoutes_ProjectsRoute(t *testing.T) {
router := gin.Default()
_ = controllers.InitRoutes(router)
// Check if the projects route is registered
routes := router.Routes()
var methodPaths []string
for _, route := range routes {
methodPaths = append(methodPaths, route.Method+" - "+route.Path)
}
expectedRoutes := []gin.RouteInfo{
{Method: "GET", Path: "/projects"},
{Method: "GET", Path: "/projects/:id"},
{Method: "POST", Path: "/projects"},
{Method: "PUT", Path: "/projects/:id"},
{Method: "PATCH", Path: "/projects"},
{Method: "DELETE", Path: "/projects/:id"},
{Method: "DELETE", Path: "/projects"},
}
for _, route := range expectedRoutes {
assert.Contains(t, methodPaths, route.Method+" - "+route.Path)
}
}
func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode)
m.Run()
}