Skip to content

Commit

Permalink
feat: added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 13, 2024
1 parent 92e282f commit 754481c
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 110 deletions.
46 changes: 33 additions & 13 deletions controllers/base_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"context"
"encoding/json"
"github.com/crawlab-team/crawlab-core/controllers"
"github.com/crawlab-team/crawlab-core/models/models"
"github.com/crawlab-team/crawlab-core/models/service"
"github.com/crawlab-team/crawlab-core/user"
"github.com/spf13/viper"
"net/http"
"net/http/httptest"
Expand All @@ -13,19 +16,39 @@ import (
"github.com/crawlab-team/crawlab-db/mongo"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

// TestModel is a simple struct to be used as a model in tests
type TestModel struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty" collection:"test_collection"`
Name string `bson:"name" json:"name"`
func init() {

}

// TestModel is a simple struct to be used as a model in tests
type TestModel models.TestModel

var TestToken string

// SetupTestDB sets up the test database
func SetupTestDB() {
viper.Set("mongo.db", "testdb")
modelSvc := service.NewModelServiceV2[models.UserV2]()
u := models.UserV2{
Username: "admin",
}
id, err := modelSvc.InsertOne(u)
if err != nil {
panic(err)
}
u.SetId(id)

userSvc, err := user.GetUserServiceV2()
if err != nil {
panic(err)
}
token, err := userSvc.MakeToken(&u)
if err != nil {
panic(err)
}
TestToken = token
}

// SetupRouter sets up the gin router for testing
Expand All @@ -44,8 +67,7 @@ func TestBaseControllerV2_GetById(t *testing.T) {
defer CleanupTestDB()

// Insert a test document
id := primitive.NewObjectID()
_, err := mongo.GetMongoCol("test_collection").Insert(bson.M{"_id": id, "name": "test"})
id, err := service.NewModelServiceV2[TestModel]().InsertOne(TestModel{Name: "test"})
assert.NoError(t, err)

// Initialize the controller
Expand Down Expand Up @@ -101,8 +123,7 @@ func TestBaseControllerV2_Post(t *testing.T) {
assert.Equal(t, "test", response.Data.Name)

// Check if the document was inserted into the database
var result TestModel
err = mongo.GetMongoCol("test_collection").Find(bson.M{"_id": response.Data.Id}, nil).One(&result)
result, err := service.NewModelServiceV2[TestModel]().GetById(response.Data.Id)
assert.NoError(t, err)
assert.Equal(t, "test", result.Name)
}
Expand All @@ -112,8 +133,7 @@ func TestBaseControllerV2_DeleteById(t *testing.T) {
defer CleanupTestDB()

// Insert a test document
id := primitive.NewObjectID()
_, err := mongo.GetMongoCol("test_collection").Insert(bson.M{"_id": id, "name": "test"})
id, err := service.NewModelServiceV2[TestModel]().InsertOne(TestModel{Name: "test"})
assert.NoError(t, err)

// Initialize the controller
Expand All @@ -134,6 +154,6 @@ func TestBaseControllerV2_DeleteById(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)

// Check if the document was deleted from the database
err = mongo.GetMongoCol("test_collection").Find(bson.M{"_id": id}, nil).One(&TestModel{})
_, err = service.NewModelServiceV2[TestModel]().GetById(id)
assert.Error(t, err)
}
8 changes: 1 addition & 7 deletions controllers/schedule_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ func PostSchedule(c *gin.Context) {
HandleErrorInternalServerError(c, err)
return
}

_s, err := modelSvc.GetById(id)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}
s = *_s
s.Id = id

if s.Enabled {
scheduleSvc, err := schedule.GetScheduleServiceV2()
Expand Down
17 changes: 7 additions & 10 deletions controllers/spider_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func PostSpider(c *gin.Context) {
}

// upsert data collection
if err := upsertSpiderDataCollection(c, &s); err != nil {
if err := upsertSpiderDataCollection(&s); err != nil {
HandleErrorInternalServerError(c, err)
return
}
Expand All @@ -218,26 +218,23 @@ func PostSpider(c *gin.Context) {
// add
s.SetCreated(u.Id)
s.SetUpdated(u.Id)
_, err := service.NewModelServiceV2[models.SpiderV2]().InsertOne(s)
id, err := service.NewModelServiceV2[models.SpiderV2]().InsertOne(s)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}
s.SetId(id)

// add stat
st := models.SpiderStatV2{
Id: s.Id,
}
st := models.SpiderStatV2{}
st.SetId(id)
st.SetCreated(u.Id)
st.SetUpdated(u.Id)
_, err = service.NewModelServiceV2[models.SpiderStatV2]().InsertOne(st)

_s, err := service.NewModelServiceV2[models.SpiderV2]().GetById(s.Id)
if err != nil {
HandleErrorInternalServerError(c, err)
return
}
s = *_s

HandleSuccessWithData(c, s)
}
Expand All @@ -257,7 +254,7 @@ func PutSpiderById(c *gin.Context) {
}

// upsert data collection
if err := upsertSpiderDataCollection(c, &s); err != nil {
if err := upsertSpiderDataCollection(&s); err != nil {
HandleErrorInternalServerError(c, err)
return
}
Expand Down Expand Up @@ -1138,7 +1135,7 @@ func spiderGitPull(gitClient *vcs.GitClient, remote, branch string) (err error)
return nil
}

func upsertSpiderDataCollection(c *gin.Context, s *models.SpiderV2) (err error) {
func upsertSpiderDataCollection(s *models.SpiderV2) (err error) {
modelSvc := service.NewModelServiceV2[models.DataCollectionV2]()
if s.ColId.IsZero() {
// validate
Expand Down
208 changes: 208 additions & 0 deletions controllers/spider_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package controllers_test

import (
"bytes"
"encoding/json"
"github.com/crawlab-team/crawlab-core/controllers"
"github.com/crawlab-team/crawlab-core/middlewares"
"github.com/crawlab-team/crawlab-core/models/models"
"github.com/crawlab-team/crawlab-core/models/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func TestCreateSpider(t *testing.T) {
SetupTestDB()
defer CleanupTestDB()

gin.SetMode(gin.TestMode)

router := gin.Default()
router.Use(middlewares.AuthorizationMiddlewareV2())
router.POST("/spiders", controllers.PostSpider)

payload := models.SpiderV2{
Name: "Test Spider",
ColName: "test_spiders",
}
jsonValue, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", "/spiders", bytes.NewBuffer(jsonValue))
req.Header.Set("Authorization", TestToken)
resp := httptest.NewRecorder()

router.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)

var response controllers.Response[models.SpiderV2]
err := json.Unmarshal(resp.Body.Bytes(), &response)
require.Nil(t, err)
assert.False(t, response.Data.Id.IsZero())
assert.Equal(t, payload.Name, response.Data.Name)
}

func TestGetSpiderById(t *testing.T) {
SetupTestDB()
defer CleanupTestDB()

gin.SetMode(gin.TestMode)

router := gin.Default()
router.Use(middlewares.AuthorizationMiddlewareV2())
router.GET("/spiders/:id", controllers.GetSpiderById)

id := primitive.NewObjectID()
model := models.SpiderV2{
Name: "Test Spider",
ColName: "test_spiders",
}
model.SetId(id)
jsonValue, _ := json.Marshal(model)
_, err := http.NewRequest("POST", "/spiders", bytes.NewBuffer(jsonValue))
require.Nil(t, err)
time.Sleep(100 * time.Millisecond)

req, _ := http.NewRequest("GET", "/spiders/"+id.Hex(), nil)
req.Header.Set("Authorization", TestToken)
resp := httptest.NewRecorder()

router.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)

var response controllers.Response[models.SpiderV2]
err = json.Unmarshal(resp.Body.Bytes(), &response)
require.Nil(t, err)
assert.Equal(t, model.Name, response.Data.Name)
}

func TestUpdateSpiderById(t *testing.T) {
SetupTestDB()
defer CleanupTestDB()

gin.SetMode(gin.TestMode)

router := gin.Default()
router.Use(middlewares.AuthorizationMiddlewareV2())
router.PUT("/spiders/:id", controllers.PutSpiderById)

id := primitive.NewObjectID()
model := models.SpiderV2{
Name: "Test Spider",
ColName: "test_spiders",
}
model.SetId(id)
jsonValue, _ := json.Marshal(model)
_, err := http.NewRequest("POST", "/spiders", bytes.NewBuffer(jsonValue))
require.Nil(t, err)

spiderId := id.Hex()
payload := models.SpiderV2{
Name: "Updated Spider",
}
jsonValue, _ = json.Marshal(payload)
req, _ := http.NewRequest("PUT", "/spiders/"+spiderId, bytes.NewBuffer(jsonValue))
req.Header.Set("Authorization", TestToken)
resp := httptest.NewRecorder()

router.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)

var response controllers.Response[models.SpiderV2]
err = json.Unmarshal(resp.Body.Bytes(), &response)
require.Nil(t, err)
assert.Equal(t, payload.Name, response.Data.Name)

svc := service.NewModelServiceV2[models.SpiderV2]()
resModel, err := svc.GetById(id)
require.Nil(t, err)
assert.Equal(t, payload.Name, resModel.Name)
}

func TestDeleteSpiderById(t *testing.T) {
SetupTestDB()
defer CleanupTestDB()

gin.SetMode(gin.TestMode)

router := gin.Default()
router.Use(middlewares.AuthorizationMiddlewareV2())
router.DELETE("/spiders/:id", controllers.DeleteSpiderById)

id := primitive.NewObjectID()
model := models.SpiderV2{
Name: "Test Spider",
ColName: "test_spiders",
}
model.SetId(id)
jsonValue, _ := json.Marshal(model)
_, err := http.NewRequest("POST", "/spiders", bytes.NewBuffer(jsonValue))
require.Nil(t, err)

req, _ := http.NewRequest("DELETE", "/spiders/"+id.Hex(), nil)
req.Header.Set("Authorization", TestToken)
resp := httptest.NewRecorder()

router.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)

svc := service.NewModelServiceV2[models.SpiderV2]()
_, err = svc.GetById(id)
require.NotNil(t, err)
}

func TestDeleteSpiderList(t *testing.T) {
SetupTestDB()
defer CleanupTestDB()

gin.SetMode(gin.TestMode)

router := gin.Default()
router.Use(middlewares.AuthorizationMiddlewareV2())
router.DELETE("/spiders", controllers.DeleteSpiderList)

svc := service.NewModelServiceV2[models.SpiderV2]()
modelList := []models.SpiderV2{
{
Name: "Test Name 1",
ColName: "test_spiders",
}, {
Name: "Test Name 2",
ColName: "test_spiders",
},
}
var ids []primitive.ObjectID
for _, model := range modelList {
id := primitive.NewObjectID()
model.SetId(id)
jsonValue, _ := json.Marshal(model)
_, err := http.NewRequest("POST", "/spiders", bytes.NewBuffer(jsonValue))
require.Nil(t, err)
ids = append(ids, id)
}

payload := struct {
Ids []string `json:"ids"`
}{}
jsonValue, _ := json.Marshal(payload)
req, _ := http.NewRequest("DELETE", "/spiders", bytes.NewBuffer(jsonValue))
req.Header.Set("Authorization", TestToken)
resp := httptest.NewRecorder()

router.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)

total, err := svc.Count(bson.M{"_id": bson.M{"$in": ids}})
require.Nil(t, err)
require.Equal(t, 0, total)
}
Loading

0 comments on commit 754481c

Please sign in to comment.