Merge pull request #1239 from vvisionnn/master

Keep updated at column when rename
pull/1257/head
AaronLiu 3 years ago committed by GitHub
commit 7bdbf3e754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -266,7 +266,7 @@ func GetFilesByUploadSession(sessionID string, uid uint) (*File, error) {
// Rename 重命名文件 // Rename 重命名文件
func (file *File) Rename(new string) error { func (file *File) Rename(new string) error {
return DB.Model(&file).Update("name", new).Error return DB.Model(&file).UpdateColumn("name", new).Error
} }
// UpdatePicInfo 更新文件的图像信息 // UpdatePicInfo 更新文件的图像信息

@ -2,11 +2,12 @@ package model
import ( import (
"errors" "errors"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
"time"
) )
func TestFile_Create(t *testing.T) { func TestFile_Create(t *testing.T) {
@ -425,10 +426,11 @@ func TestGetFilesByUploadSession(t *testing.T) {
func TestFile_Updates(t *testing.T) { func TestFile_Updates(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
file := File{Model: gorm.Model{ID: 1}} file := File{Model: gorm.Model{ID: 1}}
// rename // rename
{ {
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WithArgs("newName", sqlmock.AnyArg(), 1).WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").WithArgs("newName", 1).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
err := file.Rename("newName") err := file.Rename("newName")
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())

@ -298,10 +298,7 @@ func (folder *Folder) MoveFolderTo(dirs []uint, dstFolder *Folder) error {
// Rename 重命名目录 // Rename 重命名目录
func (folder *Folder) Rename(new string) error { func (folder *Folder) Rename(new string) error {
if err := DB.Model(&folder).Update("name", new).Error; err != nil { return DB.Model(&folder).UpdateColumn("name", new).Error
return err
}
return nil
} }
/* /*

@ -584,3 +584,39 @@ func TestTraceRoot(t *testing.T) {
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())
} }
} }
func TestFolder_Rename(t *testing.T) {
asserts := assert.New(t)
folder := Folder{
Model: gorm.Model{
ID: 1,
},
Name: "test_name",
OwnerID: 1,
Position: "/test",
}
// 成功
{
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)folders(.+)SET(.+)").
WithArgs("test_name_new", 1).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
err := folder.Rename("test_name_new")
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
}
// 出现错误
{
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)folders(.+)SET(.+)").
WithArgs("test_name_new", 1).
WillReturnError(errors.New("error"))
mock.ExpectRollback()
err := folder.Rename("test_name_new")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
}

@ -3,11 +3,12 @@ package filesystem
import ( import (
"context" "context"
"errors" "errors"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
testMock "github.com/stretchr/testify/mock"
"os" "os"
"testing" "testing"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
testMock "github.com/stretchr/testify/mock"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
@ -676,8 +677,8 @@ func TestFileSystem_Rename(t *testing.T) {
WithArgs(10, 1). WithArgs(10, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)files(.+)"). mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
WithArgs("new.txt", sqlmock.AnyArg(), 10). WithArgs("new.txt", 10).
WillReturnResult(sqlmock.NewResult(1, 1)) WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt") err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")
@ -702,8 +703,8 @@ func TestFileSystem_Rename(t *testing.T) {
WithArgs(10, 1). WithArgs(10, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)files(.+)"). mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
WithArgs("new.txt", sqlmock.AnyArg(), 10). WithArgs("new.txt", 10).
WillReturnError(errors.New("error")) WillReturnError(errors.New("error"))
mock.ExpectRollback() mock.ExpectRollback()
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt") err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")
@ -718,8 +719,8 @@ func TestFileSystem_Rename(t *testing.T) {
WithArgs(10, 1). WithArgs(10, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)folders(.+)"). mock.ExpectExec("UPDATE(.+)folders(.+)SET(.+)").
WithArgs("new", sqlmock.AnyArg(), 10). WithArgs("new", 10).
WillReturnResult(sqlmock.NewResult(1, 1)) WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
err := fs.Rename(ctx, []uint{10}, []uint{}, "new") err := fs.Rename(ctx, []uint{10}, []uint{}, "new")
@ -744,8 +745,8 @@ func TestFileSystem_Rename(t *testing.T) {
WithArgs(10, 1). WithArgs(10, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)folders(.+)"). mock.ExpectExec("UPDATE(.+)folders(.+)SET(.+)").
WithArgs("new", sqlmock.AnyArg(), 10). WithArgs("new", 10).
WillReturnError(errors.New("error")) WillReturnError(errors.New("error"))
mock.ExpectRollback() mock.ExpectRollback()
err := fs.Rename(ctx, []uint{10}, []uint{}, "new") err := fs.Rename(ctx, []uint{10}, []uint{}, "new")

Loading…
Cancel
Save