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 重命名文件
func (file *File) Rename(new string) error {
return DB.Model(&file).Update("name", new).Error
return DB.Model(&file).UpdateColumn("name", new).Error
}
// UpdatePicInfo 更新文件的图像信息

@ -2,11 +2,12 @@ package model
import (
"errors"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestFile_Create(t *testing.T) {
@ -425,10 +426,11 @@ func TestGetFilesByUploadSession(t *testing.T) {
func TestFile_Updates(t *testing.T) {
asserts := assert.New(t)
file := File{Model: gorm.Model{ID: 1}}
// rename
{
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()
err := file.Rename("newName")
asserts.NoError(mock.ExpectationsWereMet())

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

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

Loading…
Cancel
Save