test: new changes related to filesystem.CreateDirectory
This commit is contained in:
parent
2ab2662fcd
commit
aaf8a793ee
4 changed files with 115 additions and 24 deletions
|
@ -17,8 +17,9 @@ func TestFolder_Create(t *testing.T) {
|
|||
Name: "new folder",
|
||||
}
|
||||
|
||||
// 插入成功
|
||||
// 不存在,插入成功
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(5, 1))
|
||||
mock.ExpectCommit()
|
||||
fid, err := folder.Create()
|
||||
|
@ -28,12 +29,22 @@ func TestFolder_Create(t *testing.T) {
|
|||
|
||||
// 插入失败
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
fid, err = folder.Create()
|
||||
asserts.Error(err)
|
||||
asserts.Equal(uint(0), fid)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 存在,直接返回
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(5))
|
||||
mock.ExpectCommit()
|
||||
fid, err = folder.Create()
|
||||
asserts.NoError(err)
|
||||
asserts.Equal(uint(5), fid)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFolder_GetChild(t *testing.T) {
|
||||
|
|
|
@ -113,10 +113,15 @@ func TestGenericAfterUpload(t *testing.T) {
|
|||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WithArgs(1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 1
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WithArgs("我的文件", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
@ -127,22 +132,19 @@ func TestGenericAfterUpload(t *testing.T) {
|
|||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 路径不存在
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}),
|
||||
)
|
||||
err = GenericAfterUpload(ctx, &fs, file)
|
||||
asserts.Equal(ErrRootProtected, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 文件已存在
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WithArgs(1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 1
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WithArgs("我的文件", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("test.txt"),
|
||||
)
|
||||
|
@ -154,10 +156,15 @@ func TestGenericAfterUpload(t *testing.T) {
|
|||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WithArgs(1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 1
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WithArgs("我的文件", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name", "upload_session_id"}).AddRow("test.txt", "1"),
|
||||
)
|
||||
|
@ -169,10 +176,15 @@ func TestGenericAfterUpload(t *testing.T) {
|
|||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WithArgs(1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 1
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WithArgs("我的文件", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
|
|
|
@ -214,7 +214,7 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
|
|||
asserts.Equal(ErrFileExisted, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 存在同名目录
|
||||
// 存在同名目录,直接返回
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
|
@ -225,13 +225,37 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
|
|||
|
||||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("s"))
|
||||
mock.ExpectRollback()
|
||||
_, err = fs.CreateDirectory(ctx, "/ad/ab")
|
||||
asserts.Error(err)
|
||||
// ab
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs("ab", 2, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
|
||||
mock.ExpectCommit()
|
||||
res, err := fs.CreateDirectory(ctx, "/ad/ab")
|
||||
asserts.NoError(err)
|
||||
asserts.EqualValues(3, res.ID)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 成功创建
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// ad
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "ad").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs("ab", 2, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
_, err = fs.CreateDirectory(ctx, "/ad/ab")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 成功创建, 递归创建父目录
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
|
@ -239,20 +263,62 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
|
|||
// ad
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "ad").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
// 创建ad
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs("ad", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
// 创建ab
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs("ab", 2, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
_, err = fs.CreateDirectory(ctx, "/ad/ab")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 父目录不存在
|
||||
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
_, err = fs.CreateDirectory(ctx, "/ad")
|
||||
asserts.Equal(ErrRootProtected, err)
|
||||
// 底层创建失败
|
||||
// 成功创建
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// ad
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "ad").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
|
||||
// 创建ad
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs("ad", 1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1)).WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
_, err = fs.CreateDirectory(ctx, "/ad/ab")
|
||||
asserts.Error(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 直接创建根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
_, err = fs.CreateDirectory(ctx, "/")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
|
|
|
@ -147,6 +147,7 @@ func TestImportTask_Do(t *testing.T) {
|
|||
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
// 创建文件时查找父目录,仍然不存在
|
||||
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
|
||||
task.Do()
|
||||
|
||||
|
@ -177,6 +178,7 @@ func TestImportTask_Do(t *testing.T) {
|
|||
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
// 创建目录
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
mock.ExpectExec("INSERT(.+)folders(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
|
||||
mock.ExpectCommit()
|
||||
// 插入文件记录
|
||||
|
|
Loading…
Add table
Reference in a new issue