mirror of https://github.com/zhufuyi/sponge
420 lines
11 KiB
Plaintext
420 lines
11 KiB
Plaintext
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/go-dev-frame/sponge/pkg/copier"
|
|
"github.com/go-dev-frame/sponge/pkg/sgorm/query"
|
|
"github.com/go-dev-frame/sponge/pkg/httpcli"
|
|
"github.com/go-dev-frame/sponge/pkg/gotest"
|
|
"github.com/go-dev-frame/sponge/pkg/utils"
|
|
|
|
"github.com/go-dev-frame/sponge/internal/cache"
|
|
"github.com/go-dev-frame/sponge/internal/dao"
|
|
"github.com/go-dev-frame/sponge/internal/database"
|
|
"github.com/go-dev-frame/sponge/internal/model"
|
|
"github.com/go-dev-frame/sponge/internal/types"
|
|
)
|
|
|
|
func newUserExampleHandler() *gotest.Handler {
|
|
testData := &model.UserExample{}
|
|
testData.ID = 1
|
|
// you can set the other fields of testData here, such as:
|
|
//testData.CreatedAt = time.Now()
|
|
//testData.UpdatedAt = testData.CreatedAt
|
|
|
|
// init mock cache
|
|
c := gotest.NewCache(map[string]interface{}{utils.Uint64ToStr(testData.ID): testData})
|
|
c.ICache = cache.NewUserExampleCache(&database.CacheType{
|
|
CType: "redis",
|
|
Rdb: c.RedisClient,
|
|
})
|
|
|
|
// init mock dao
|
|
d := gotest.NewDao(c, testData)
|
|
d.IDao = dao.NewUserExampleDao(d.DB, c.ICache.(cache.UserExampleCache))
|
|
|
|
// init mock handler
|
|
h := gotest.NewHandler(d, testData)
|
|
h.IHandler = &userExampleHandler{iDao: d.IDao.(dao.UserExampleDao)}
|
|
iHandler := h.IHandler.(UserExampleHandler)
|
|
|
|
testFns := []gotest.RouterInfo{
|
|
{
|
|
FuncName: "Create",
|
|
Method: http.MethodPost,
|
|
Path: "/userExample",
|
|
HandlerFunc: iHandler.Create,
|
|
},
|
|
{
|
|
FuncName: "DeleteByID",
|
|
Method: http.MethodDelete,
|
|
Path: "/userExample/:id",
|
|
HandlerFunc: iHandler.DeleteByID,
|
|
},
|
|
{
|
|
FuncName: "UpdateByID",
|
|
Method: http.MethodPut,
|
|
Path: "/userExample/:id",
|
|
HandlerFunc: iHandler.UpdateByID,
|
|
},
|
|
{
|
|
FuncName: "GetByID",
|
|
Method: http.MethodGet,
|
|
Path: "/userExample/:id",
|
|
HandlerFunc: iHandler.GetByID,
|
|
},
|
|
{
|
|
FuncName: "List",
|
|
Method: http.MethodPost,
|
|
Path: "/userExample/list",
|
|
HandlerFunc: iHandler.List,
|
|
},
|
|
{
|
|
FuncName: "DeleteByIDs",
|
|
Method: http.MethodPost,
|
|
Path: "/userExample/delete/ids",
|
|
HandlerFunc: iHandler.DeleteByIDs,
|
|
},
|
|
{
|
|
FuncName: "GetByCondition",
|
|
Method: http.MethodPost,
|
|
Path: "/userExample/condition",
|
|
HandlerFunc: iHandler.GetByCondition,
|
|
},
|
|
{
|
|
FuncName: "ListByIDs",
|
|
Method: http.MethodPost,
|
|
Path: "/userExample/list/ids",
|
|
HandlerFunc: iHandler.ListByIDs,
|
|
},
|
|
{
|
|
FuncName: "ListByLastID",
|
|
Method: http.MethodGet,
|
|
Path: "/userExample/list",
|
|
HandlerFunc: iHandler.ListByLastID,
|
|
},
|
|
}
|
|
|
|
h.GoRunHTTPServer(testFns)
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
return h
|
|
}
|
|
|
|
func Test_userExampleHandler_Create(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := &types.CreateUserExampleRequest{}
|
|
_ = copier.Copy(testData, h.TestData.(*model.UserExample))
|
|
|
|
h.MockDao.SQLMock.ExpectBegin()
|
|
args := h.MockDao.GetAnyArgs(h.TestData)
|
|
h.MockDao.SQLMock.ExpectExec("INSERT INTO .*").
|
|
WithArgs(args[:len(args)-1]...). // adjusted for the amount of test data
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
h.MockDao.SQLMock.ExpectCommit()
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Post(result, h.GetRequestURL("Create"), testData)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Logf("%+v", result)
|
|
// delete the templates code start
|
|
result = &httpcli.StdResult{}
|
|
testData = &types.CreateUserExampleRequest{
|
|
Name: "foo",
|
|
Password: "f447b20a7fcbf53a5d5be013ea0b15af",
|
|
Email: "foo@bar.com",
|
|
Phone: "+8616000000001",
|
|
Avatar: "http://foo/1.jpg",
|
|
Age: 10,
|
|
Gender: 1,
|
|
}
|
|
err = httpcli.Post(result, h.GetRequestURL("Create"), testData)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("%+v", result)
|
|
|
|
h.MockDao.SQLMock.ExpectBegin()
|
|
h.MockDao.SQLMock.ExpectCommit()
|
|
// create error test
|
|
result = &httpcli.StdResult{}
|
|
err = httpcli.Post(result, h.GetRequestURL("Create"), testData)
|
|
assert.Error(t, err)
|
|
// delete the templates code end
|
|
}
|
|
|
|
func Test_userExampleHandler_DeleteByID(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
expectedSQLForDeletion := "UPDATE .*"
|
|
expectedArgsForDeletionTime := h.MockDao.AnyTime
|
|
|
|
h.MockDao.SQLMock.ExpectBegin()
|
|
h.MockDao.SQLMock.ExpectExec(expectedSQLForDeletion).
|
|
WithArgs(expectedArgsForDeletionTime, testData.ID). // adjusted for the amount of test data
|
|
WillReturnResult(sqlmock.NewResult(int64(testData.ID), 1))
|
|
h.MockDao.SQLMock.ExpectCommit()
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Delete(result, h.GetRequestURL("DeleteByID", testData.ID))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero id error test
|
|
err = httpcli.Delete(result, h.GetRequestURL("DeleteByID", 0))
|
|
assert.NoError(t, err)
|
|
|
|
// delete error test
|
|
err = httpcli.Delete(result, h.GetRequestURL("DeleteByID", 111))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_UpdateByID(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := &types.UpdateUserExampleByIDRequest{}
|
|
_ = copier.Copy(testData, h.TestData.(*model.UserExample))
|
|
|
|
h.MockDao.SQLMock.ExpectBegin()
|
|
h.MockDao.SQLMock.ExpectExec("UPDATE .*").
|
|
WithArgs(h.MockDao.AnyTime, testData.ID). // adjusted for the amount of test data
|
|
WillReturnResult(sqlmock.NewResult(int64(testData.ID), 1))
|
|
h.MockDao.SQLMock.ExpectCommit()
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Put(result, h.GetRequestURL("UpdateByID", testData.ID), testData)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero id error test
|
|
err = httpcli.Put(result, h.GetRequestURL("UpdateByID", 0), testData)
|
|
assert.NoError(t, err)
|
|
|
|
// update error test
|
|
err = httpcli.Put(result, h.GetRequestURL("UpdateByID", 111), testData)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_GetByID(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
// column names and corresponding data
|
|
rows := sqlmock.NewRows([]string{"id"}).
|
|
AddRow(testData.ID)
|
|
|
|
h.MockDao.SQLMock.ExpectQuery("SELECT .*").
|
|
WithArgs(testData.ID, 1).
|
|
WillReturnRows(rows)
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Get(result, h.GetRequestURL("GetByID", testData.ID))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero id error test
|
|
err = httpcli.Get(result, h.GetRequestURL("GetByID", 0))
|
|
assert.NoError(t, err)
|
|
|
|
// get error test
|
|
err = httpcli.Get(result, h.GetRequestURL("GetByID", 111))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_List(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
// column names and corresponding data
|
|
rows := sqlmock.NewRows([]string{"id"}).
|
|
AddRow(testData.ID)
|
|
|
|
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Post(result, h.GetRequestURL("List"), &types.ListUserExamplesRequest{query.Params{
|
|
Page: 0,
|
|
Limit: 10,
|
|
Sort: "ignore count", // ignore test count
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// nil params error test
|
|
err = httpcli.Post(result, h.GetRequestURL("List"), nil)
|
|
assert.NoError(t, err)
|
|
|
|
// get error test
|
|
err = httpcli.Post(result, h.GetRequestURL("List"), &types.ListUserExamplesRequest{query.Params{
|
|
Page: 0,
|
|
Limit: 10,
|
|
Sort: "unknown-column",
|
|
}})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_DeleteByIDs(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
h.MockDao.SQLMock.ExpectBegin()
|
|
h.MockDao.SQLMock.ExpectExec("UPDATE .*").
|
|
WithArgs(h.MockDao.AnyTime, testData.ID). // adjusted for the amount of test data
|
|
WillReturnResult(sqlmock.NewResult(int64(testData.ID), 1))
|
|
h.MockDao.SQLMock.ExpectCommit()
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Post(result, h.GetRequestURL("DeleteByIDs"), &types.DeleteUserExamplesByIDsRequest{IDs: []uint64{testData.ID}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero id error test
|
|
err = httpcli.Post(result, h.GetRequestURL("DeleteByIDs"), nil)
|
|
assert.NoError(t, err)
|
|
|
|
// get error test
|
|
err = httpcli.Post(result, h.GetRequestURL("DeleteByIDs"), &types.DeleteUserExamplesByIDsRequest{IDs: []uint64{111}})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_GetByCondition(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
// column names and corresponding data
|
|
rows := sqlmock.NewRows([]string{"id"}).
|
|
AddRow(testData.ID)
|
|
|
|
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Post(result, h.GetRequestURL("GetByCondition"), &types.GetUserExampleByConditionRequest{
|
|
query.Conditions{
|
|
Columns: []query.Column{
|
|
{
|
|
Name: "id",
|
|
Value: testData.ID,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero error test
|
|
err = httpcli.Post(result, h.GetRequestURL("GetByCondition"), nil)
|
|
assert.NoError(t, err)
|
|
|
|
// get error test
|
|
err = httpcli.Post(result, h.GetRequestURL("GetByCondition"), &types.GetUserExampleByConditionRequest{
|
|
query.Conditions{
|
|
Columns: []query.Column{
|
|
{
|
|
Name: "id",
|
|
Value: 2,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_ListByIDs(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
// column names and corresponding data
|
|
rows := sqlmock.NewRows([]string{"id"}).
|
|
AddRow(testData.ID)
|
|
|
|
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Post(result, h.GetRequestURL("ListByIDs"), &types.ListUserExamplesByIDsRequest{IDs: []uint64{testData.ID}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// zero id error test
|
|
_ = httpcli.Post(result, h.GetRequestURL("ListByIDs"), nil)
|
|
|
|
// get error test
|
|
err = httpcli.Post(result, h.GetRequestURL("ListByIDs"), &types.ListUserExamplesByIDsRequest{IDs: []uint64{111}})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func Test_userExampleHandler_ListByLastID(t *testing.T) {
|
|
h := newUserExampleHandler()
|
|
defer h.Close()
|
|
testData := h.TestData.(*model.UserExample)
|
|
|
|
// column names and corresponding data
|
|
rows := sqlmock.NewRows([]string{"id"}).
|
|
AddRow(testData.ID)
|
|
|
|
h.MockDao.SQLMock.ExpectQuery("SELECT .*").WillReturnRows(rows)
|
|
|
|
result := &httpcli.StdResult{}
|
|
err := httpcli.Get(result, h.GetRequestURL("ListByLastID"), httpcli.WithParams(map[string]interface{}{"id": 10}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Code != 0 {
|
|
t.Fatalf("%+v", result)
|
|
}
|
|
|
|
// error test
|
|
err = httpcli.Get(result, h.GetRequestURL("ListByLastID"), httpcli.WithParams(map[string]interface{}{"lastID": 0, "limit": 10, "sort": "unknown-column"}))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestNewUserExampleHandler(t *testing.T) {
|
|
defer func() {
|
|
recover()
|
|
}()
|
|
_ = NewUserExampleHandler()
|
|
}
|