Test: new changes in 3.4.0

This commit is contained in:
HFO4
2021-11-20 16:59:29 +08:00
parent 532bff820a
commit 9056ef9171
6 changed files with 112 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
package serializer
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewError(t *testing.T) {
a := assert.New(t)
err := NewError(400, "Bad Request", errors.New("error"))
a.Error(err)
a.EqualValues(400, err.Code)
err.WithError(errors.New("error2"))
a.Equal("error2", err.RawError.Error())
a.Equal("Bad Request", err.Error())
resp := &Response{
Code: 400,
Msg: "Bad Request",
Error: "error",
}
err = NewErrorFromResponse(resp)
a.Error(err)
}
func TestDBErr(t *testing.T) {
a := assert.New(t)
resp := DBErr("", nil)
a.NotEmpty(resp.Msg)
resp = ParamErr("", nil)
a.NotEmpty(resp.Msg)
}
func TestErr(t *testing.T) {
a := assert.New(t)
err := NewError(400, "Bad Request", errors.New("error"))
resp := Err(400, "", err)
a.Equal("Bad Request", resp.Msg)
}

View File

@@ -0,0 +1,33 @@
package serializer
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewResponseWithGobData(t *testing.T) {
a := assert.New(t)
type args struct {
data interface{}
}
res := NewResponseWithGobData(args{})
a.Equal(CodeInternalSetting, res.Code)
res = NewResponseWithGobData("TestNewResponseWithGobData")
a.Equal(0, res.Code)
a.NotEmpty(res.Data)
}
func TestResponse_GobDecode(t *testing.T) {
a := assert.New(t)
res := NewResponseWithGobData("TestResponse_GobDecode")
jsonContent, err := json.Marshal(res)
a.NoError(err)
resDecoded := &Response{}
a.NoError(json.Unmarshal(jsonContent, resDecoded))
var target string
resDecoded.GobDecode(&target)
a.Equal("TestResponse_GobDecode", target)
}

View File

@@ -0,0 +1,20 @@
package serializer
import (
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSlaveTransferReq_Hash(t *testing.T) {
a := assert.New(t)
s1 := &SlaveTransferReq{
Src: "1",
Policy: &model.Policy{},
}
s2 := &SlaveTransferReq{
Src: "2",
Policy: &model.Policy{},
}
a.NotEqual(s1.Hash("1"), s2.Hash("1"))
}