Merge pull request #149 from hantmac/develop

Add unit test for routes/node
This commit is contained in:
Marvin Zhang
2019-08-19 19:06:35 +08:00
committed by GitHub
2 changed files with 195 additions and 1 deletions

View File

@@ -2,14 +2,29 @@ package database
import (
"crawlab/config"
"github.com/apex/log"
"github.com/globalsign/mgo"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"reflect"
"testing"
)
func init() {
if err := config.InitConfig("../conf/config.yml"); err != nil {
log.Fatal("Init config failed")
}
log.Infof("初始化配置成功")
err := InitMongo()
if err != nil {
log.Fatal("Init mongodb failed")
}
}
func TestGetDb(t *testing.T) {
Convey("Test GetDb", t, func() {
if err := config.InitConfig(""); err != nil {
if err := config.InitConfig("../conf/config.yml"); err != nil {
t.Fatal("Init config failed")
}
t.Log("初始化配置成功")
@@ -26,3 +41,30 @@ func TestGetDb(t *testing.T) {
})
})
}
func TestGetCol(t *testing.T) {
var c = "nodes"
var colActual *mgo.Collection
Convey("Test GetCol", t, func() {
s, col := GetCol(c)
Convey("s should resemble Session.Copy", func() {
So(s, ShouldResemble, Session.Copy())
So(reflect.TypeOf(col), ShouldResemble, reflect.TypeOf(colActual))
})
})
}
func TestGetGridFs(t *testing.T) {
var prefix = "files"
var gfActual *mgo.GridFS
Convey("Test GetGridFs", t, func() {
s, gf := GetGridFs(prefix)
Convey("s should be session.copy", func() {
So(s, ShouldResemble, Session.Copy())
})
Convey("gf should be *mgo.GridFS", func() {
So(reflect.TypeOf(gf), ShouldResemble, reflect.TypeOf(gfActual))
})
})
}

152
backend/routes/node_test.go Normal file
View File

@@ -0,0 +1,152 @@
package routes
import (
"crawlab/config"
"crawlab/database"
"encoding/json"
"github.com/apex/log"
"github.com/gin-gonic/gin"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"net/http"
"net/http/httptest"
"runtime/debug"
"testing"
)
var app *gin.Engine
// 本测试依赖MongoDB的服务所以在测试之前需要启动MongoDB及相关服务
func init() {
app = gin.Default()
// 初始化配置
if err := config.InitConfig("../conf/config.yml"); err != nil {
panic(err)
}
log.Info("初始化配置成功")
// 初始化日志设置
logLevel := viper.GetString("log.level")
if logLevel != "" {
log.SetLevelFromString(logLevel)
}
log.Info("初始化日志设置成功")
// 初始化Mongodb数据库
if err := database.InitMongo(); err != nil {
debug.PrintStack()
panic(err)
}
log.Info("初始化Mongodb数据库成功")
// 初始化Redis数据库
if err := database.InitRedis(); err != nil {
debug.PrintStack()
panic(err)
}
log.Info("初始化Redis数据库成功")
// 路由
// 节点相关的API
app.GET("/ping", Ping)
app.GET("/nodes", GetNodeList) // 节点列表
app.GET("/nodes/:id", GetNode) // 节点详情
app.POST("/nodes/:id", PostNode) // 修改节点
app.GET("/nodes/:id/tasks", GetNodeTaskList) // 节点任务列表
app.GET("/nodes/:id/system", GetSystemInfo) // 节点任务列表
app.DELETE("/nodes/:id", DeleteNode) // 删除节点
// 爬虫
}
//先测试GetNodeList得到一个节点的ID再继续测试下面的API
func TestGetNodeList(t *testing.T) {
var resp Response
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/nodes", nil)
app.ServeHTTP(w, req)
err := json.Unmarshal([]byte(w.Body.String()), &resp)
if err != nil {
t.Fatal("Unmarshal resp failed")
}
t.Log(resp.Data)
Convey("Test API GetNodeList", t, func() {
Convey("Test response status", func() {
So(resp.Status, ShouldEqual, "ok")
So(resp.Message, ShouldEqual, "success")
})
})
}
//依赖MongoDB中的数据,_id=5d429e6c19f7abede924fee2,实际测试时需替换
func TestGetNode(t *testing.T) {
var resp Response
var mongoId = "5d5a658319f7ab423585b0b0"
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/nodes/"+mongoId, nil)
app.ServeHTTP(w, req)
err := json.Unmarshal([]byte(w.Body.String()), &resp)
if err != nil {
t.Fatal("Unmarshal resp failed")
}
t.Log(resp.Data)
Convey("Test API GetNode", t, func() {
Convey("Test response status", func() {
So(resp.Status, ShouldEqual, "ok")
So(resp.Message, ShouldEqual, "success")
So(resp.Data.(map[string]interface{})["_id"], ShouldEqual, mongoId)
})
})
}
func TestPing(t *testing.T) {
var resp Response
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
app.ServeHTTP(w, req)
err := json.Unmarshal([]byte(w.Body.String()), &resp)
if err != nil {
t.Fatal("Unmarshal resp failed")
}
Convey("Test API ping", t, func() {
Convey("Test response status", func() {
So(resp.Status, ShouldEqual, "ok")
So(resp.Message, ShouldEqual, "success")
})
})
}
func TestGetNodeTaskList(t *testing.T) {
var resp Response
var mongoId = "5d5a658319f7ab423585b0b0"
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "nodes/"+mongoId+"/tasks", nil)
app.ServeHTTP(w, req)
err := json.Unmarshal([]byte(w.Body.String()), &resp)
if err != nil {
t.Fatal("Unmarshal resp failed")
}
Convey("Test API GetNodeTaskList", t, func() {
Convey("Test response status", func() {
So(resp.Status, ShouldEqual, "ok")
So(resp.Message, ShouldEqual, "success")
})
})
}
func TestDeleteNode(t *testing.T) {
var resp Response
var mongoId = "5d5a658319f7ab423585b0b0"
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "nodes/"+mongoId, nil)
app.ServeHTTP(w, req)
err := json.Unmarshal([]byte(w.Body.String()), &resp)
if err != nil {
t.Fatal("Unmarshal resp failed")
}
Convey("Test API DeleteNode", t, func() {
Convey("Test response status", func() {
So(resp.Status, ShouldEqual, "ok")
So(resp.Message, ShouldEqual, "success")
})
})
}