Test: download file in filesystem
This commit is contained in:
parent
9bb432c220
commit
16067c3ac8
6 changed files with 138 additions and 5 deletions
|
@ -5,8 +5,10 @@ import (
|
|||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -50,3 +52,90 @@ func TestFileSystem_AddFile(t *testing.T) {
|
|||
asserts.NoError(err)
|
||||
asserts.Equal("/Uploads/1_sad.txt", f.SourceName)
|
||||
}
|
||||
|
||||
func TestFileSystem_GetContent(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.Background()
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// 文件不存在
|
||||
rs, err := fs.GetContent(ctx, "not exist file")
|
||||
asserts.Equal(ErrObjectNotExist, err)
|
||||
asserts.Nil(rs)
|
||||
|
||||
// 未知上传策略
|
||||
file, err := os.Create("TestFileSystem_GetContent.txt")
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "TestFileSystem_GetContent.txt", 1))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "unknown"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
asserts.Error(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 打开文件失败
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "TestFileSystem_GetContent.txt", 1))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type", "source_name"}).AddRow(1, "local", "not exist"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
asserts.Equal(serializer.CodeIOFailed, err.(serializer.AppError).Code)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 打开成功
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetContent.txt", 1, "TestFileSystem_GetContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFileSystem_GetDownloadContent(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.Background()
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
Policy: model.Policy{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
file, err := os.Create("TestFileSystem_GetDownloadContent.txt")
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetDownloadContent.txt", 1, "TestFileSystem_GetDownloadContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
// 无限速
|
||||
_, err = fs.GetDownloadContent(ctx, "TestFileSystem_GetDownloadContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 有限速
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetDownloadContent.txt", 1, "TestFileSystem_GetDownloadContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
fs.User.Group.SpeedLimit = 1
|
||||
_, err = fs.GetDownloadContent(ctx, "TestFileSystem_GetDownloadContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
|
|
@ -72,7 +72,6 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
|||
}
|
||||
|
||||
// dispatchHandler 根据存储策略分配文件适配器
|
||||
// TODO: 测试
|
||||
func (fs *FileSystem) dispatchHandler() error {
|
||||
var policyType string
|
||||
if fs.Policy == nil {
|
||||
|
|
|
@ -2,6 +2,7 @@ package filesystem
|
|||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http/httptest"
|
||||
|
@ -43,3 +44,22 @@ func TestNewFileSystemFromContext(t *testing.T) {
|
|||
asserts.Nil(fs)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
func TestDispatchHandler(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := &FileSystem{
|
||||
User: &model.User{Policy: model.Policy{
|
||||
Type: "local",
|
||||
}},
|
||||
}
|
||||
|
||||
// 未指定,使用用户默认
|
||||
err := fs.dispatchHandler()
|
||||
asserts.NoError(err)
|
||||
asserts.IsType(local.Handler{}, fs.Handler)
|
||||
|
||||
// 已指定,发生错误
|
||||
fs.Policy = &model.Policy{Type: "unknown"}
|
||||
err = fs.dispatchHandler()
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package local
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -13,7 +12,6 @@ import (
|
|||
type Handler struct{}
|
||||
|
||||
// Get 获取文件内容
|
||||
// TODO:测试
|
||||
func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, error) {
|
||||
// 打开文件
|
||||
file, err := os.Open(path)
|
||||
|
@ -32,8 +30,8 @@ func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, err
|
|||
func closeReader(ctx context.Context, closer io.Closer) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
err := closer.Close()
|
||||
fmt.Println("关闭reader", err)
|
||||
_ = closer.Close()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,3 +67,25 @@ func TestHandler_Delete(t *testing.T) {
|
|||
asserts.Equal([]string{}, list)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
func TestHandler_Get(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Handler{}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// 成功
|
||||
file, err := os.Create("TestHandler_Get.txt")
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
rs, err := handler.Get(ctx, "TestHandler_Get.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(rs)
|
||||
|
||||
// 文件不存在
|
||||
|
||||
rs, err = handler.Get(ctx, "TestHandler_Get_notExist.txt")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(rs)
|
||||
}
|
||||
|
|
|
@ -19,6 +19,11 @@ type FileHeaderMock struct {
|
|||
testMock.Mock
|
||||
}
|
||||
|
||||
func (m FileHeaderMock) Get(ctx context.Context, path string) (io.ReadSeeker, error) {
|
||||
args := m.Called(ctx, path)
|
||||
return args.Get(0).(io.ReadSeeker), args.Error(1)
|
||||
}
|
||||
|
||||
func (m FileHeaderMock) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
|
||||
args := m.Called(ctx, file, dst)
|
||||
return args.Error(0)
|
||||
|
|
Loading…
Reference in a new issue