forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_test.go
172 lines (158 loc) · 4.41 KB
/
path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package filesystem
import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
)
func TestFileSystem_IsFileExist(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
}}
// 存在
{
path := "/1.txt"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
mock.ExpectQuery("SELECT(.+)").WithArgs(1, "1.txt").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "1.txt"))
exist, file := fs.IsFileExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.True(exist)
asserts.Equal(uint(1), file.ID)
}
// 文件不存在
{
path := "/1.txt"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
mock.ExpectQuery("SELECT(.+)").WithArgs(1, "1.txt").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
exist, _ := fs.IsFileExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.False(exist)
}
// 父目录不存在
{
path := "/1.txt"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
exist, _ := fs.IsFileExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.False(exist)
}
}
func TestFileSystem_IsPathExist(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
}}
// 查询根目录
{
path := "/"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
exist, folder := fs.IsPathExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.True(exist)
asserts.Equal(uint(1), folder.ID)
}
// 深层路径
{
path := "/1/2/3"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "1").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
// 2
mock.ExpectQuery("SELECT(.+)").
WithArgs(2, 1, "2").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
// 3
mock.ExpectQuery("SELECT(.+)").
WithArgs(3, 1, "3").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(4, 1))
exist, folder := fs.IsPathExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.True(exist)
asserts.Equal(uint(4), folder.ID)
}
// 深层路径 重设根目录为/1
{
path := "/2/3"
fs.Root = &model.Folder{Name: "1", Model: gorm.Model{ID: 2}, OwnerID: 1}
// 2
mock.ExpectQuery("SELECT(.+)").
WithArgs(2, 1, "2").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
// 3
mock.ExpectQuery("SELECT(.+)").
WithArgs(3, 1, "3").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(4, 1))
exist, folder := fs.IsPathExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.True(exist)
asserts.Equal(uint(4), folder.ID)
fs.Root = nil
}
// 深层 不存在
{
path := "/1/2/3"
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "1").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
// 2
mock.ExpectQuery("SELECT(.+)").
WithArgs(2, 1, "2").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
// 3
mock.ExpectQuery("SELECT(.+)").
WithArgs(3, 1, "3").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
exist, folder := fs.IsPathExist(path)
asserts.NoError(mock.ExpectationsWereMet())
asserts.False(exist)
asserts.Nil(folder)
}
}
func TestFileSystem_IsChildFileExist(t *testing.T) {
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
ID: 1,
},
}}
folder := model.Folder{
Model: gorm.Model{ID: 1},
Name: "123",
Position: "/",
}
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, "321").
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(2, "321"))
exist, childFile := fs.IsChildFileExist(&folder, "321")
asserts.NoError(mock.ExpectationsWereMet())
asserts.True(exist)
asserts.Equal("/123", childFile.Position)
}