mirror of
https://github.com/cloudreve/cloudreve.git
synced 2025-11-02 03:17:56 +08:00
Test: delete objects related methods
This commit is contained in:
@ -139,3 +139,112 @@ func TestFileSystem_GetDownloadContent(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFileSystem_GroupFileByPolicy(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.Background()
|
||||
files := []model.File{
|
||||
model.File{
|
||||
PolicyID: 1,
|
||||
Name: "1_1.txt",
|
||||
},
|
||||
model.File{
|
||||
PolicyID: 2,
|
||||
Name: "2_1.txt",
|
||||
},
|
||||
model.File{
|
||||
PolicyID: 3,
|
||||
Name: "3_1.txt",
|
||||
},
|
||||
model.File{
|
||||
PolicyID: 2,
|
||||
Name: "2_2.txt",
|
||||
},
|
||||
model.File{
|
||||
PolicyID: 1,
|
||||
Name: "1_2.txt",
|
||||
},
|
||||
}
|
||||
fs := FileSystem{}
|
||||
policyGroup := fs.GroupFileByPolicy(ctx, files)
|
||||
asserts.Equal(map[uint][]*model.File{
|
||||
1: {&files[0], &files[4]},
|
||||
2: {&files[1], &files[3]},
|
||||
3: {&files[2]},
|
||||
}, policyGroup)
|
||||
}
|
||||
|
||||
func TestFileSystem_deleteGroupedFile(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.Background()
|
||||
fs := FileSystem{}
|
||||
files := []model.File{
|
||||
{
|
||||
PolicyID: 1,
|
||||
Name: "1_1.txt",
|
||||
SourceName: "1_1.txt",
|
||||
Policy: model.Policy{Model: gorm.Model{ID: 1}, Type: "local"},
|
||||
},
|
||||
{
|
||||
PolicyID: 2,
|
||||
Name: "2_1.txt",
|
||||
SourceName: "2_1.txt",
|
||||
Policy: model.Policy{Model: gorm.Model{ID: 1}, Type: "local"},
|
||||
},
|
||||
{
|
||||
PolicyID: 3,
|
||||
Name: "3_1.txt",
|
||||
SourceName: "3_1.txt",
|
||||
Policy: model.Policy{Model: gorm.Model{ID: 1}, Type: "local"},
|
||||
},
|
||||
{
|
||||
PolicyID: 2,
|
||||
Name: "2_2.txt",
|
||||
SourceName: "2_2.txt",
|
||||
Policy: model.Policy{Model: gorm.Model{ID: 1}, Type: "local"},
|
||||
},
|
||||
{
|
||||
PolicyID: 1,
|
||||
Name: "1_2.txt",
|
||||
SourceName: "1_2.txt",
|
||||
Policy: model.Policy{Model: gorm.Model{ID: 1}, Type: "local"},
|
||||
},
|
||||
}
|
||||
|
||||
// 全部失败
|
||||
{
|
||||
failed := fs.deleteGroupedFile(ctx, fs.GroupFileByPolicy(ctx, files))
|
||||
asserts.Equal(map[uint][]string{
|
||||
1: {"1_1.txt", "1_2.txt"},
|
||||
2: {"2_1.txt", "2_2.txt"},
|
||||
3: {"3_1.txt"},
|
||||
}, failed)
|
||||
}
|
||||
// 部分失败
|
||||
{
|
||||
file, err := os.Create("1_1.txt")
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
failed := fs.deleteGroupedFile(ctx, fs.GroupFileByPolicy(ctx, files))
|
||||
asserts.Equal(map[uint][]string{
|
||||
1: {"1_2.txt"},
|
||||
2: {"2_1.txt", "2_2.txt"},
|
||||
3: {"3_1.txt"},
|
||||
}, failed)
|
||||
}
|
||||
// 部分失败,包含整组未知存储策略导致的失败
|
||||
{
|
||||
file, err := os.Create("1_1.txt")
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
files[1].Policy.Type = "unknown"
|
||||
files[3].Policy.Type = "unknown"
|
||||
failed := fs.deleteGroupedFile(ctx, fs.GroupFileByPolicy(ctx, files))
|
||||
asserts.Equal(map[uint][]string{
|
||||
1: {"1_2.txt"},
|
||||
2: {"2_1.txt", "2_2.txt"},
|
||||
3: {"3_1.txt"},
|
||||
}, failed)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user