test: fix failed ut
This commit is contained in:
parent
89ee147961
commit
b1344616b8
8 changed files with 21 additions and 40 deletions
8
pkg/cache/redis_test.go
vendored
8
pkg/cache/redis_test.go
vendored
|
@ -16,13 +16,13 @@ func TestNewRedisStore(t *testing.T) {
|
|||
store := NewRedisStore(10, "tcp", "", "", "0")
|
||||
asserts.NotNil(store)
|
||||
|
||||
conn, err := store.pool.Dial()
|
||||
asserts.Nil(conn)
|
||||
asserts.Error(err)
|
||||
asserts.Panics(func() {
|
||||
store.pool.Dial()
|
||||
})
|
||||
|
||||
testConn := redigomock.NewConn()
|
||||
cmd := testConn.Command("PING").Expect("PONG")
|
||||
err = store.pool.TestOnBorrow(testConn, time.Now())
|
||||
err := store.pool.TestOnBorrow(testConn, time.Now())
|
||||
if testConn.Stats(cmd) != 1 {
|
||||
fmt.Println("Command was not used")
|
||||
return
|
||||
|
|
|
@ -328,7 +328,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
|
|||
// return none 200
|
||||
{
|
||||
mockRequest := &requestMock{}
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
Response: &http.Response{StatusCode: http.StatusConflict},
|
||||
})
|
||||
c := &slaveController{
|
||||
|
@ -345,7 +345,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
|
|||
// master return error
|
||||
{
|
||||
mockRequest := &requestMock{}
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader("{\"code\":1}")),
|
||||
|
@ -365,7 +365,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
|
|||
// success
|
||||
{
|
||||
mockRequest := &requestMock{}
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader("{\"data\":\"expected\"}")),
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -190,13 +189,10 @@ func TestHandler_Source(t *testing.T) {
|
|||
Name: "test.jpg",
|
||||
}
|
||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
|
||||
sourceURL, err := handler.Source(ctx, "", 0, false, 0)
|
||||
asserts.NoError(err)
|
||||
asserts.NotEmpty(sourceURL)
|
||||
asserts.Contains(sourceURL, "sign=")
|
||||
asserts.Contains(sourceURL, "https://cloudreve.org")
|
||||
}
|
||||
|
||||
// 下载
|
||||
|
@ -208,21 +204,16 @@ func TestHandler_Source(t *testing.T) {
|
|||
Name: "test.jpg",
|
||||
}
|
||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, true, 0)
|
||||
sourceURL, err := handler.Source(ctx, "", 0, true, 0)
|
||||
asserts.NoError(err)
|
||||
asserts.NotEmpty(sourceURL)
|
||||
asserts.Contains(sourceURL, "sign=")
|
||||
asserts.Contains(sourceURL, "download")
|
||||
asserts.Contains(sourceURL, "https://cloudreve.org")
|
||||
}
|
||||
|
||||
// 无法获取上下文
|
||||
{
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
|
||||
sourceURL, err := handler.Source(ctx, "", 0, false, 0)
|
||||
asserts.Error(err)
|
||||
asserts.Empty(sourceURL)
|
||||
}
|
||||
|
@ -237,9 +228,7 @@ func TestHandler_Source(t *testing.T) {
|
|||
Name: "test.jpg",
|
||||
}
|
||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
|
||||
sourceURL, err := handler.Source(ctx, "", 0, false, 0)
|
||||
asserts.NoError(err)
|
||||
asserts.NotEmpty(sourceURL)
|
||||
asserts.Contains(sourceURL, "sign=")
|
||||
|
@ -256,9 +245,7 @@ func TestHandler_Source(t *testing.T) {
|
|||
Name: "test.jpg",
|
||||
}
|
||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
|
||||
sourceURL, err := handler.Source(ctx, "", 0, false, 0)
|
||||
asserts.Error(err)
|
||||
asserts.Empty(sourceURL)
|
||||
}
|
||||
|
@ -279,19 +266,14 @@ func TestHandler_GetDownloadURL(t *testing.T) {
|
|||
Name: "test.jpg",
|
||||
}
|
||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0)
|
||||
downloadURL, err := handler.Source(ctx, "", 10, true, 0)
|
||||
asserts.NoError(err)
|
||||
asserts.Contains(downloadURL, "sign=")
|
||||
asserts.Contains(downloadURL, "https://cloudreve.org")
|
||||
}
|
||||
|
||||
// 无法获取上下文
|
||||
{
|
||||
baseURL, err := url.Parse("https://cloudreve.org")
|
||||
asserts.NoError(err)
|
||||
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0)
|
||||
downloadURL, err := handler.Source(ctx, "", 10, true, 0)
|
||||
asserts.Error(err)
|
||||
asserts.Empty(downloadURL)
|
||||
}
|
||||
|
|
|
@ -433,6 +433,7 @@ func (handler *Driver) Token(ctx context.Context, ttl int64, uploadSession *seri
|
|||
options := []oss.Option{
|
||||
oss.Expires(time.Now().Add(time.Duration(ttl) * time.Second)),
|
||||
oss.ForbidOverWrite(true),
|
||||
oss.ContentType(fileInfo.DetectMimeType()),
|
||||
}
|
||||
imur, err := handler.bucket.InitiateMultipartUpload(fileInfo.SavePath, options...)
|
||||
if err != nil {
|
||||
|
|
|
@ -352,8 +352,6 @@ func TestFileSystem_GetSource(t *testing.T) {
|
|||
sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}).
|
||||
AddRow(35, "local", true),
|
||||
)
|
||||
// 查找站点URL
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://cloudreve.org"))
|
||||
|
||||
sourceURL, err := fs.GetSource(ctx, 2)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/cos"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/googledrive"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/onedrive"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/oss"
|
||||
|
@ -177,7 +178,7 @@ func (fs *FileSystem) DispatchHandler() error {
|
|||
fs.Handler = handler
|
||||
return err
|
||||
case "googledrive":
|
||||
handler, err := googledrive.NewDriver(policy)
|
||||
handler, err := googledrive.NewDriver(currentPolicy)
|
||||
fs.Handler = handler
|
||||
return err
|
||||
default:
|
||||
|
|
|
@ -682,7 +682,7 @@ func TestFileSystem_Rename(t *testing.T) {
|
|||
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
|
||||
WithArgs("new.txt", 10).
|
||||
WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")
|
||||
|
@ -708,7 +708,7 @@ func TestFileSystem_Rename(t *testing.T) {
|
|||
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
|
||||
WithArgs("new.txt", 10).
|
||||
WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10).
|
||||
WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")
|
||||
|
|
|
@ -16,7 +16,6 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -60,8 +59,8 @@ func (m FileHeaderMock) Thumb(ctx context.Context, files *model.File) (*response
|
|||
return args.Get(0).(*response.ContentResponse), args.Error(1)
|
||||
}
|
||||
|
||||
func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, expires int64, isDownload bool, speed int) (string, error) {
|
||||
args := m.Called(ctx, path, url, expires, isDownload, speed)
|
||||
func (m FileHeaderMock) Source(ctx context.Context, path string, expires int64, isDownload bool, speed int) (string, error) {
|
||||
args := m.Called(ctx, path, expires, isDownload, speed)
|
||||
return args.Get(0).(string), args.Error(1)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue