Test: util / filesystem

pull/247/head
HFO4 5 years ago
parent 06bcb3d6eb
commit 5df8f688d1

@ -168,6 +168,18 @@ func TestFileSystem_Use(t *testing.T) {
// 不存在 // 不存在
fs.Use("BeforeUpload2333", hook) fs.Use("BeforeUpload2333", hook)
asserts.NotPanics(func() {
for _, hookName := range []string{
"AfterUpload",
"AfterValidateFailed",
"AfterUploadCanceled",
"BeforeFileDownload",
} {
fs.Use(hookName, hook)
}
})
} }
func TestFileSystem_Trigger(t *testing.T) { func TestFileSystem_Trigger(t *testing.T) {
@ -195,3 +207,50 @@ func TestFileSystem_Trigger(t *testing.T) {
asserts.NoError(err) asserts.NoError(err)
asserts.Equal(uint64(4), fs.User.Storage) asserts.Equal(uint64(4), fs.User.Storage)
} }
func TestHookIsFileExist(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
}}
ctx := context.WithValue(context.Background(), PathCtx, "/test.txt")
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/", "test.txt").WillReturnRows(
sqlmock.NewRows([]string{"Name"}).AddRow("s"),
)
err := HookIsFileExist(ctx, fs)
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
}
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/", "test.txt").WillReturnRows(
sqlmock.NewRows([]string{"Name"}),
)
err := HookIsFileExist(ctx, fs)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
}
func TestHookValidateCapacity(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{ID: 1},
Storage: 0,
Group: model.Group{
MaxStorage: 11,
},
}}
ctx := context.WithValue(context.Background(), FileHeaderCtx, local.FileStream{Size: 10})
{
err := HookValidateCapacity(ctx, fs)
asserts.NoError(err)
}
{
err := HookValidateCapacity(ctx, fs)
asserts.Error(err)
}
}

@ -0,0 +1,48 @@
package local
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"strings"
"testing"
)
func TestFileStream_GetFileName(t *testing.T) {
asserts := assert.New(t)
file := FileStream{Name: "123"}
asserts.Equal("123", file.GetFileName())
}
func TestFileStream_GetMIMEType(t *testing.T) {
asserts := assert.New(t)
file := FileStream{MIMEType: "123"}
asserts.Equal("123", file.GetFileName())
}
func TestFileStream_GetSize(t *testing.T) {
asserts := assert.New(t)
file := FileStream{Size: 123}
asserts.Equal(uint64(123), file.GetSize())
}
func TestFileStream_Read(t *testing.T) {
asserts := assert.New(t)
file := FileStream{
File: ioutil.NopCloser(strings.NewReader("123")),
}
var p = make([]byte, 3)
{
n, err := file.Read(p)
asserts.Equal(3, n)
asserts.NoError(err)
}
}
func TestFileStream_Close(t *testing.T) {
asserts := assert.New(t)
file := FileStream{
File: ioutil.NopCloser(strings.NewReader("123")),
}
err := file.Close()
asserts.NoError(err)
}

@ -368,3 +368,78 @@ func TestFileSystem_Delete(t *testing.T) {
} }
} }
func TestFileSystem_Copy(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
Storage: 3,
Group: model.Group{MaxStorage: 3},
}}
ctx := context.Background()
// 目录不存在
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
sqlmock.NewRows([]string{"name"}),
)
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
sqlmock.NewRows([]string{"name"}),
)
err := fs.Copy(ctx, []string{}, []string{}, "/src", "/dst")
asserts.Equal(ErrPathNotExist, err)
asserts.NoError(mock.ExpectationsWereMet())
}
// 复制目录出错
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
sqlmock.NewRows([]string{"name"}).AddRow("123"),
)
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
sqlmock.NewRows([]string{"name"}).AddRow("123"),
)
err := fs.Copy(ctx, []string{"test"}, []string{}, "/src", "/dst")
asserts.Error(err)
}
}
func TestFileSystem_Move(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
Storage: 3,
Group: model.Group{MaxStorage: 3},
}}
ctx := context.Background()
// 目录不存在
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
sqlmock.NewRows([]string{"name"}),
)
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
sqlmock.NewRows([]string{"name"}),
)
err := fs.Move(ctx, []string{}, []string{}, "/src", "/dst")
asserts.Equal(ErrPathNotExist, err)
asserts.NoError(mock.ExpectationsWereMet())
}
// 移动目录出错
{
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
sqlmock.NewRows([]string{"name"}).AddRow("123"),
)
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
sqlmock.NewRows([]string{"name"}).AddRow("123"),
)
err := fs.Move(ctx, []string{"test"}, []string{}, "/src", "/dst")
asserts.Error(err)
}
}

@ -82,4 +82,18 @@ func TestFileSystem_Upload(t *testing.T) {
asserts.Error(err) asserts.Error(err)
testHandller2.AssertExpectations(t) testHandller2.AssertExpectations(t)
// AfterUpload失败
testHandller3 := new(FileHeaderMock)
testHandller3.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
fs.Handler = testHandller3
fs.Use("AfterUpload", func(ctx context.Context, fs *FileSystem) error {
return errors.New("error")
})
fs.Use("AfterValidateFailed", func(ctx context.Context, fs *FileSystem) error {
return errors.New("error")
})
err = fs.Upload(ctx, file)
asserts.Error(err)
testHandller2.AssertExpectations(t)
} }

@ -62,3 +62,9 @@ func TestBuildRegexp(t *testing.T) {
asserts.Equal("^/dir/", BuildRegexp([]string{"/dir"}, "^", "/", "|")) asserts.Equal("^/dir/", BuildRegexp([]string{"/dir"}, "^", "/", "|"))
asserts.Equal("^/dir/|^/dir/di\\*r/", BuildRegexp([]string{"/dir", "/dir/di*r"}, "^", "/", "|")) asserts.Equal("^/dir/|^/dir/di\\*r/", BuildRegexp([]string{"/dir", "/dir/di*r"}, "^", "/", "|"))
} }
func TestBuildConcat(t *testing.T) {
asserts := assert.New(t)
asserts.Equal("CONCAT(1,2)", BuildConcat("1", "2", "mysql"))
asserts.Equal("1||2", BuildConcat("1", "2", "sqlite3"))
}

@ -0,0 +1,12 @@
package util
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestExists(t *testing.T) {
asserts := assert.New(t)
asserts.True(Exists("io_test.go"))
asserts.False(Exists("io_test.js"))
}

@ -0,0 +1,101 @@
package util
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestBuildLogger(t *testing.T) {
asserts := assert.New(t)
asserts.NotPanics(func() {
BuildLogger("error")
})
asserts.NotPanics(func() {
BuildLogger("warning")
})
asserts.NotPanics(func() {
BuildLogger("info")
})
asserts.NotPanics(func() {
BuildLogger("?")
})
asserts.NotPanics(func() {
BuildLogger("debug")
})
}
func TestLog(t *testing.T) {
asserts := assert.New(t)
asserts.NotNil(Log())
logger = nil
asserts.NotNil(Log())
}
func TestLogger_Debug(t *testing.T) {
asserts := assert.New(t)
l := Logger{
level: LevelDebug,
}
asserts.NotPanics(func() {
l.Debug("123")
})
l.level = LevelError
asserts.NotPanics(func() {
l.Debug("123")
})
}
func TestLogger_Info(t *testing.T) {
asserts := assert.New(t)
l := Logger{
level: LevelDebug,
}
asserts.NotPanics(func() {
l.Info("123")
})
l.level = LevelError
asserts.NotPanics(func() {
l.Info("123")
})
}
func TestLogger_Warning(t *testing.T) {
asserts := assert.New(t)
l := Logger{
level: LevelDebug,
}
asserts.NotPanics(func() {
l.Warning("123")
})
l.level = LevelError
asserts.NotPanics(func() {
l.Warning("123")
})
}
func TestLogger_Error(t *testing.T) {
asserts := assert.New(t)
l := Logger{
level: LevelDebug,
}
asserts.NotPanics(func() {
l.Error("123")
})
l.level = -1
asserts.NotPanics(func() {
l.Error("123")
})
}
func TestLogger_Panic(t *testing.T) {
asserts := assert.New(t)
l := Logger{
level: LevelDebug,
}
asserts.Panics(func() {
l.Panic("123")
})
l.level = -1
asserts.NotPanics(func() {
l.Error("123")
})
}

@ -12,3 +12,10 @@ func TestDotPathToStandardPath(t *testing.T) {
asserts.Equal("/目录", DotPathToStandardPath("目录")) asserts.Equal("/目录", DotPathToStandardPath("目录"))
asserts.Equal("/目录/目录2", DotPathToStandardPath("目录,目录2")) asserts.Equal("/目录/目录2", DotPathToStandardPath("目录,目录2"))
} }
func TestFillSlash(t *testing.T) {
asserts := assert.New(t)
asserts.Equal("/", FillSlash("/"))
asserts.Equal("/", FillSlash(""))
asserts.Equal("/123/", FillSlash("/123"))
}

Loading…
Cancel
Save