Skip to content

Commit dea7a5c

Browse files
authored
just add some unit tests (#16291)
* code.gitea.io/gitea/routers/utils coverage: 100.0% * code.gitea.io/gitea/routers/install 0% -> 5.0% * ConvertUtf8ToUtf8mb4: make sure DBType is mysql
1 parent add74fb commit dea7a5c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

models/convert.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/modules/setting"
11+
12+
"xorm.io/xorm/schemas"
1113
)
1214

1315
// ConvertUtf8ToUtf8mb4 converts database and tables from utf8 to utf8mb4 if it's mysql and set ROW_FORMAT=dynamic
1416
func ConvertUtf8ToUtf8mb4() error {
17+
if x.Dialect().URI().DBType != schemas.MYSQL {
18+
return nil
19+
}
20+
1521
_, err := x.Exec(fmt.Sprintf("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", setting.Database.Name))
1622
if err != nil {
1723
return err

routers/install/routes_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package install
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestRoutes(t *testing.T) {
14+
routes := Routes()
15+
assert.NotNil(t, routes)
16+
assert.Len(t, routes.R.Routes(), 1)
17+
assert.EqualValues(t, "/", routes.R.Routes()[0].Pattern)
18+
assert.Nil(t, routes.R.Routes()[0].SubRoutes)
19+
assert.Len(t, routes.R.Routes()[0].Handlers, 2)
20+
}

routers/utils/utils_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,41 @@ func TestIsExternalURL(t *testing.T) {
6262
"//try.gitea.io/test?param=false"),
6363
newTest(false,
6464
"/hey/hey/hey#3244"),
65+
newTest(true,
66+
"://missing protocol scheme"),
6567
} {
6668
assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
6769
}
6870
}
71+
72+
func TestSanitizeFlashErrorString(t *testing.T) {
73+
tests := []struct {
74+
name string
75+
arg string
76+
want string
77+
}{
78+
{
79+
name: "no error",
80+
arg: "",
81+
want: "",
82+
},
83+
{
84+
name: "normal error",
85+
arg: "can not open file: \"abc.exe\"",
86+
want: "can not open file: "abc.exe"",
87+
},
88+
{
89+
name: "line break error",
90+
arg: "some error:\n\nawesome!",
91+
want: "some error:<br><br>awesome!",
92+
},
93+
}
94+
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
if got := SanitizeFlashErrorString(tt.arg); got != tt.want {
98+
t.Errorf("SanitizeFlashErrorString() = '%v', want '%v'", got, tt.want)
99+
}
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)