Skip to content

Commit

Permalink
test: fix failed ut
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed May 27, 2023
1 parent 89ee147 commit b134461
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 40 deletions.
8 changes: 4 additions & 4 deletions pkg/cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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}")),
Expand All @@ -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\"}")),
Expand Down
32 changes: 7 additions & 25 deletions pkg/filesystem/driver/local/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"io"
"net/url"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -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")
}

// 下载
Expand All @@ -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)
}
Expand All @@ -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=")
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/filesystem/driver/oss/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions pkg/filesystem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/filesystem/manage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
5 changes: 2 additions & 3 deletions pkg/filesystem/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit b134461

Please sign in to comment.