forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting_test.go
196 lines (162 loc) · 6.19 KB
/
setting_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package model
import (
"database/sql"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
)
var mock sqlmock.Sqlmock
var mockDB *gorm.DB
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
DB, _ = gorm.Open("mysql", db)
mockDB = DB
defer db.Close()
m.Run()
}
func TestGetSettingByType(t *testing.T) {
cache.Store = cache.NewMemoStore()
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings := GetSettingByType([]string{"basic"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
rows = sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic2")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByType([]string{"basic", "basic2"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
//找不到
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByType([]string{"basic233"})
asserts.Equal(map[string]string{}, settings)
}
func TestGetSettingByNameWithDefault(t *testing.T) {
a := assert.New(t)
rows := sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings := GetSettingByNameWithDefault("123", "123321")
a.Equal("123321", settings)
}
func TestGetSettingByNames(t *testing.T) {
cache.Store = cache.NewMemoStore()
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings := GetSettingByNames("siteName", "siteDes")
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
asserts.NoError(mock.ExpectationsWereMet())
//找到其中一个设置时
rows = sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName2", "Cloudreve", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByNames("siteName2", "siteDes2333")
asserts.Equal(map[string]string{
"siteName2": "Cloudreve",
}, settings)
asserts.NoError(mock.ExpectationsWereMet())
//找不到设置时
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByNames("siteName2333", "siteDes2333")
asserts.Equal(map[string]string{}, settings)
asserts.NoError(mock.ExpectationsWereMet())
// 一个设置命中缓存
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WithArgs("siteDes2").WillReturnRows(sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteDes2", "Cloudreve2", "basic"))
settings = GetSettingByNames("siteName", "siteDes2")
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes2": "Cloudreve2",
}, settings)
asserts.NoError(mock.ExpectationsWereMet())
}
// TestGetSettingByName 测试GetSettingByName
func TestGetSettingByName(t *testing.T) {
cache.Store = cache.NewMemoStore()
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
siteName := GetSettingByName("siteName")
asserts.Equal("Cloudreve", siteName)
asserts.NoError(mock.ExpectationsWereMet())
// 第二次查询应返回缓存内容
siteNameCache := GetSettingByName("siteName")
asserts.Equal("Cloudreve", siteNameCache)
asserts.NoError(mock.ExpectationsWereMet())
// 找不到设置
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
siteName = GetSettingByName("siteName not exist")
asserts.Equal("", siteName)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestIsTrueVal(t *testing.T) {
asserts := assert.New(t)
asserts.True(IsTrueVal("1"))
asserts.True(IsTrueVal("true"))
asserts.False(IsTrueVal("0"))
asserts.False(IsTrueVal("false"))
}
func TestGetSiteURL(t *testing.T) {
asserts := assert.New(t)
// 正常
{
err := cache.Deletes([]string{"siteURL"}, "setting_")
asserts.NoError(err)
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://drive.cloudreve.org"))
siteURL := GetSiteURL()
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("https://drive.cloudreve.org", siteURL.String())
}
// 失败 返回默认值
{
err := cache.Deletes([]string{"siteURL"}, "setting_")
asserts.NoError(err)
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, ":][\\/\\]sdf"))
siteURL := GetSiteURL()
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("https://cloudreve.org", siteURL.String())
}
}
func TestGetIntSetting(t *testing.T) {
asserts := assert.New(t)
// 正常
{
cache.Set("setting_TestGetIntSetting", "10", 0)
res := GetIntSetting("TestGetIntSetting", 20)
asserts.Equal(10, res)
}
// 使用默认值
{
res := GetIntSetting("TestGetIntSetting_2", 20)
asserts.Equal(20, res)
}
}