mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
unit test for stats.go
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"ucloudBilling/ucloud/log"
|
||||
)
|
||||
|
||||
var app *gin.Engine
|
||||
@@ -29,6 +28,7 @@ func init() {
|
||||
app.GET("/nodes/:id/system", GetSystemInfo) // 节点任务列表
|
||||
app.DELETE("/nodes/:id", DeleteNode) // 删除节点
|
||||
//// 爬虫
|
||||
app.GET("/stats/home",GetHomeStats) // 首页统计数据
|
||||
// 定时任务
|
||||
app.GET("/schedules", GetScheduleList) // 定时任务列表
|
||||
app.GET("/schedules/:id", GetSchedule) // 定时任务详情
|
||||
@@ -44,7 +44,6 @@ func TestGetNodeList(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/nodes", nil)
|
||||
app.ServeHTTP(w, req)
|
||||
err := json.Unmarshal([]byte(w.Body.String()), &resp)
|
||||
t.Log(resp.Data)
|
||||
if err != nil {
|
||||
t.Fatal("Unmarshal resp failed")
|
||||
}
|
||||
@@ -148,7 +147,6 @@ func TestPostNode(t *testing.T) {
|
||||
|
||||
var resp Response
|
||||
body, _ := json.Marshal(newItem)
|
||||
log.Info(strings.NewReader(string(body)))
|
||||
|
||||
var mongoId = "5d429e6c19f7abede924fee2"
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
64
backend/mock/stats.go
Normal file
64
backend/mock/stats.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"crawlab/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
|
||||
var taskDailyItems = []model.TaskDailyItem{
|
||||
{
|
||||
Date: "2019/08/19",
|
||||
TaskCount: 2,
|
||||
AvgRuntimeDuration: 1000,
|
||||
},
|
||||
{
|
||||
Date: "2019/08/20",
|
||||
TaskCount: 3,
|
||||
AvgRuntimeDuration: 10130,
|
||||
},
|
||||
}
|
||||
|
||||
func GetHomeStats(c *gin.Context) {
|
||||
type DataOverview struct {
|
||||
TaskCount int `json:"task_count"`
|
||||
SpiderCount int `json:"spider_count"`
|
||||
ActiveNodeCount int `json:"active_node_count"`
|
||||
ScheduleCount int `json:"schedule_count"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Overview DataOverview `json:"overview"`
|
||||
Daily []model.TaskDailyItem `json:"daily"`
|
||||
}
|
||||
|
||||
// 任务总数
|
||||
taskCount := 10
|
||||
|
||||
// 在线节点总数
|
||||
activeNodeCount := 4
|
||||
|
||||
// 爬虫总数
|
||||
spiderCount := 5
|
||||
// 定时任务数
|
||||
scheduleCount := 2
|
||||
|
||||
// 每日任务数
|
||||
items := taskDailyItems
|
||||
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Status: "ok",
|
||||
Message: "success",
|
||||
Data: Data{
|
||||
Overview: DataOverview{
|
||||
ActiveNodeCount: activeNodeCount,
|
||||
TaskCount: taskCount,
|
||||
SpiderCount: spiderCount,
|
||||
ScheduleCount: scheduleCount,
|
||||
},
|
||||
Daily: items,
|
||||
},
|
||||
})
|
||||
}
|
||||
29
backend/mock/stats_test.go
Normal file
29
backend/mock/stats_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetHomeStats(t *testing.T) {
|
||||
var resp Response
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/stats/home", nil)
|
||||
app.ServeHTTP(w, req)
|
||||
err := json.Unmarshal([]byte(w.Body.String()), &resp)
|
||||
fmt.Println(resp.Data)
|
||||
if err != nil {
|
||||
t.Fatal("Unmarshal resp failed")
|
||||
}
|
||||
|
||||
Convey("Test API GetHomeStats", t, func() {
|
||||
Convey("Test response status", func() {
|
||||
So(resp.Status, ShouldEqual, "ok")
|
||||
So(resp.Message, ShouldEqual, "success")
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user