mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Add some Unit-Tests (#14500)
* fix url * modules/auth/pa: coverage: 40#.0% * modules/base coverage: 67.6% -> 89.9% * modules/cache coverage: 0% -> 12.0% * modules/convert coverage: 27.1% -> 29.7%
This commit is contained in:
		
							
								
								
									
										20
									
								
								modules/auth/pam/pam_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								modules/auth/pam/pam_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					// +build pam
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Copyright 2021 The Gitea Authors. All rights reserved.
 | 
				
			||||||
 | 
					// Use of this source code is governed by a MIT-style
 | 
				
			||||||
 | 
					// license that can be found in the LICENSE file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package pam
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestPamAuth(t *testing.T) {
 | 
				
			||||||
 | 
						result, err := Auth("gitea", "user1", "false-pwd")
 | 
				
			||||||
 | 
						assert.Error(t, err)
 | 
				
			||||||
 | 
						assert.EqualValues(t, "Authentication failure", err.Error())
 | 
				
			||||||
 | 
						assert.Len(t, result, 0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -17,8 +17,8 @@ func TestNaturalSortLess(t *testing.T) {
 | 
				
			|||||||
	test("v1.20.0", "v1.2.0", false)
 | 
						test("v1.20.0", "v1.2.0", false)
 | 
				
			||||||
	test("v1.20.0", "v1.29.0", true)
 | 
						test("v1.20.0", "v1.29.0", true)
 | 
				
			||||||
	test("v1.20.0", "v1.20.0", false)
 | 
						test("v1.20.0", "v1.20.0", false)
 | 
				
			||||||
	test("abc", "bcd", "abc" < "bcd")
 | 
						test("abc", "bcd", true)
 | 
				
			||||||
	test("a-1-a", "a-1-b", true)
 | 
						test("a-1-a", "a-1-b", true)
 | 
				
			||||||
	test("2", "12", true)
 | 
						test("2", "12", true)
 | 
				
			||||||
	test("a", "ab", "a" < "ab")
 | 
						test("a", "ab", true)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -270,7 +270,7 @@ func Int64sContains(intsSlice []int64, a int64) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// IsLetter reports whether the rune is a letter (category L).
 | 
					// IsLetter reports whether the rune is a letter (category L).
 | 
				
			||||||
// https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
 | 
					// https://github.com/golang/go/blob/c3b4918/src/go/scanner/scanner.go#L342
 | 
				
			||||||
func IsLetter(ch rune) bool {
 | 
					func IsLetter(ch rune) bool {
 | 
				
			||||||
	return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
 | 
						return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,10 @@
 | 
				
			|||||||
package base
 | 
					package base
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/base64"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -53,11 +56,48 @@ func TestBasicAuthDecode(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func TestBasicAuthEncode(t *testing.T) {
 | 
					func TestBasicAuthEncode(t *testing.T) {
 | 
				
			||||||
	assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
 | 
						assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
 | 
				
			||||||
 | 
						assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----"))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: Test PBKDF2()
 | 
					func TestVerifyTimeLimitCode(t *testing.T) {
 | 
				
			||||||
// TODO: Test VerifyTimeLimitCode()
 | 
						tc := []struct {
 | 
				
			||||||
// TODO: Test CreateTimeLimitCode()
 | 
							data    string
 | 
				
			||||||
 | 
							minutes int
 | 
				
			||||||
 | 
							code    string
 | 
				
			||||||
 | 
							valid   bool
 | 
				
			||||||
 | 
						}{{
 | 
				
			||||||
 | 
							data:    "data",
 | 
				
			||||||
 | 
							minutes: 2,
 | 
				
			||||||
 | 
							code:    testCreateTimeLimitCode(t, "data", 2),
 | 
				
			||||||
 | 
							valid:   true,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							data:    "abc123-ß",
 | 
				
			||||||
 | 
							minutes: 1,
 | 
				
			||||||
 | 
							code:    testCreateTimeLimitCode(t, "abc123-ß", 1),
 | 
				
			||||||
 | 
							valid:   true,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							data:    "data",
 | 
				
			||||||
 | 
							minutes: 2,
 | 
				
			||||||
 | 
							code:    "2021012723240000005928251dac409d2c33a6eb82c63410aaad569bed",
 | 
				
			||||||
 | 
							valid:   false,
 | 
				
			||||||
 | 
						}}
 | 
				
			||||||
 | 
						for _, test := range tc {
 | 
				
			||||||
 | 
							actualValid := VerifyTimeLimitCode(test.data, test.minutes, test.code)
 | 
				
			||||||
 | 
							assert.Equal(t, test.valid, actualValid, "data: '%s' code: '%s' should be valid: %t", test.data, test.code, test.valid)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func testCreateTimeLimitCode(t *testing.T, data string, m int) string {
 | 
				
			||||||
 | 
						result0 := CreateTimeLimitCode(data, m, nil)
 | 
				
			||||||
 | 
						result1 := CreateTimeLimitCode(data, m, time.Now().Format("200601021504"))
 | 
				
			||||||
 | 
						result2 := CreateTimeLimitCode(data, m, time.Unix(time.Now().Unix()+int64(time.Minute)*int64(m), 0).Format("200601021504"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert.Equal(t, result0, result1)
 | 
				
			||||||
 | 
						assert.NotEqual(t, result0, result2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert.True(t, len(result0) != 0)
 | 
				
			||||||
 | 
						return result0
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFileSize(t *testing.T) {
 | 
					func TestFileSize(t *testing.T) {
 | 
				
			||||||
	var size int64 = 512
 | 
						var size int64 = 512
 | 
				
			||||||
@@ -76,6 +116,12 @@ func TestFileSize(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, "2.0 EiB", FileSize(size))
 | 
						assert.Equal(t, "2.0 EiB", FileSize(size))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestPrettyNumber(t *testing.T) {
 | 
				
			||||||
 | 
						assert.Equal(t, "23,342,432", PrettyNumber(23342432))
 | 
				
			||||||
 | 
						assert.Equal(t, "0", PrettyNumber(0))
 | 
				
			||||||
 | 
						assert.Equal(t, "-100,000", PrettyNumber(-100000))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestSubtract(t *testing.T) {
 | 
					func TestSubtract(t *testing.T) {
 | 
				
			||||||
	toFloat64 := func(n interface{}) float64 {
 | 
						toFloat64 := func(n interface{}) float64 {
 | 
				
			||||||
		switch v := n.(type) {
 | 
							switch v := n.(type) {
 | 
				
			||||||
@@ -168,6 +214,13 @@ func TestInt64sToMap(t *testing.T) {
 | 
				
			|||||||
	)
 | 
						)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestInt64sContains(t *testing.T) {
 | 
				
			||||||
 | 
						assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
 | 
				
			||||||
 | 
						assert.Equal(t, true, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1))
 | 
				
			||||||
 | 
						assert.Equal(t, true, Int64sContains([]int64{2323}, 2323))
 | 
				
			||||||
 | 
						assert.Equal(t, false, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestIsLetter(t *testing.T) {
 | 
					func TestIsLetter(t *testing.T) {
 | 
				
			||||||
	assert.True(t, IsLetter('a'))
 | 
						assert.True(t, IsLetter('a'))
 | 
				
			||||||
	assert.True(t, IsLetter('e'))
 | 
						assert.True(t, IsLetter('e'))
 | 
				
			||||||
@@ -181,6 +234,8 @@ func TestIsLetter(t *testing.T) {
 | 
				
			|||||||
	assert.False(t, IsLetter('-'))
 | 
						assert.False(t, IsLetter('-'))
 | 
				
			||||||
	assert.False(t, IsLetter('1'))
 | 
						assert.False(t, IsLetter('1'))
 | 
				
			||||||
	assert.False(t, IsLetter('$'))
 | 
						assert.False(t, IsLetter('$'))
 | 
				
			||||||
 | 
						assert.False(t, IsLetter(0x00))
 | 
				
			||||||
 | 
						assert.False(t, IsLetter(0x93))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
 | 
					func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
 | 
				
			||||||
@@ -197,11 +252,19 @@ Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
 | 
				
			|||||||
Comment Comment Comment --><svg></svg>`)))
 | 
					Comment Comment Comment --><svg></svg>`)))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// IsRepresentableAsText
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestIsTextFile(t *testing.T) {
 | 
					func TestIsTextFile(t *testing.T) {
 | 
				
			||||||
	assert.True(t, IsTextFile([]byte{}))
 | 
						assert.True(t, IsTextFile([]byte{}))
 | 
				
			||||||
	assert.True(t, IsTextFile([]byte("lorem ipsum")))
 | 
						assert.True(t, IsTextFile([]byte("lorem ipsum")))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIsImageFile(t *testing.T) {
 | 
				
			||||||
 | 
						png, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC")
 | 
				
			||||||
 | 
						assert.True(t, IsImageFile(png))
 | 
				
			||||||
 | 
						assert.False(t, IsImageFile([]byte("plain text")))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestIsSVGImageFile(t *testing.T) {
 | 
					func TestIsSVGImageFile(t *testing.T) {
 | 
				
			||||||
	assert.True(t, IsSVGImageFile([]byte("<svg></svg>")))
 | 
						assert.True(t, IsSVGImageFile([]byte("<svg></svg>")))
 | 
				
			||||||
	assert.True(t, IsSVGImageFile([]byte("    <svg></svg>")))
 | 
						assert.True(t, IsSVGImageFile([]byte("    <svg></svg>")))
 | 
				
			||||||
@@ -248,6 +311,33 @@ func TestIsSVGImageFile(t *testing.T) {
 | 
				
			|||||||
	<foo></foo>`)))
 | 
						<foo></foo>`)))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIsPDFFile(t *testing.T) {
 | 
				
			||||||
 | 
						pdf, _ := base64.StdEncoding.DecodeString("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe")
 | 
				
			||||||
 | 
						assert.True(t, IsPDFFile(pdf))
 | 
				
			||||||
 | 
						assert.False(t, IsPDFFile([]byte("plain text")))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIsVideoFile(t *testing.T) {
 | 
				
			||||||
 | 
						mp4, _ := base64.StdEncoding.DecodeString("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA")
 | 
				
			||||||
 | 
						assert.True(t, IsVideoFile(mp4))
 | 
				
			||||||
 | 
						assert.False(t, IsVideoFile([]byte("plain text")))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIsAudioFile(t *testing.T) {
 | 
				
			||||||
 | 
						mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl")
 | 
				
			||||||
 | 
						assert.True(t, IsAudioFile(mp3))
 | 
				
			||||||
 | 
						assert.False(t, IsAudioFile([]byte("plain text")))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO: Test EntryIcon
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSetupGiteaRoot(t *testing.T) {
 | 
				
			||||||
 | 
						_ = os.Setenv("GITEA_ROOT", "test")
 | 
				
			||||||
 | 
						assert.EqualValues(t, "test", SetupGiteaRoot())
 | 
				
			||||||
 | 
						_ = os.Setenv("GITEA_ROOT", "")
 | 
				
			||||||
 | 
						assert.NotEqual(t, "test", SetupGiteaRoot())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFormatNumberSI(t *testing.T) {
 | 
					func TestFormatNumberSI(t *testing.T) {
 | 
				
			||||||
	assert.Equal(t, "125", FormatNumberSI(int(125)))
 | 
						assert.Equal(t, "125", FormatNumberSI(int(125)))
 | 
				
			||||||
	assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
 | 
						assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
 | 
				
			||||||
@@ -255,6 +345,3 @@ func TestFormatNumberSI(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
 | 
						assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
 | 
				
			||||||
	assert.Equal(t, "", FormatNumberSI("test"))
 | 
						assert.Equal(t, "", FormatNumberSI("test"))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
// TODO: IsImageFile(), currently no idea how to test
 | 
					 | 
				
			||||||
// TODO: IsPDFFile(), currently no idea how to test
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										151
									
								
								modules/cache/cache_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										151
									
								
								modules/cache/cache_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,151 @@
 | 
				
			|||||||
 | 
					// Copyright 2021 The Gitea Authors. All rights reserved.
 | 
				
			||||||
 | 
					// Use of this source code is governed by a MIT-style
 | 
				
			||||||
 | 
					// license that can be found in the LICENSE file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"code.gitea.io/gitea/modules/setting"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func createTestCache() {
 | 
				
			||||||
 | 
						conn, _ = newCache(setting.Cache{
 | 
				
			||||||
 | 
							Adapter: "memory",
 | 
				
			||||||
 | 
							TTL:     time.Minute,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestNewContext(t *testing.T) {
 | 
				
			||||||
 | 
						assert.NoError(t, NewContext())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						setting.CacheService.Cache = setting.Cache{Enabled: true, Adapter: "redis", Conn: "some random string"}
 | 
				
			||||||
 | 
						con, err := newCache(setting.Cache{
 | 
				
			||||||
 | 
							Adapter:  "rand",
 | 
				
			||||||
 | 
							Conn:     "false conf",
 | 
				
			||||||
 | 
							Interval: 100,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(t, err)
 | 
				
			||||||
 | 
						assert.Nil(t, con)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestGetCache(t *testing.T) {
 | 
				
			||||||
 | 
						createTestCache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert.NotNil(t, GetCache())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestGetString(t *testing.T) {
 | 
				
			||||||
 | 
						createTestCache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err := GetString("key", func() (string, error) {
 | 
				
			||||||
 | 
							return "", fmt.Errorf("some error")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, "", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetString("key", func() (string, error) {
 | 
				
			||||||
 | 
							return "", nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, "", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetString("key", func() (string, error) {
 | 
				
			||||||
 | 
						// 	return "some data", nil
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.Equal(t, "", data)
 | 
				
			||||||
 | 
						// Remove("key")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetString("key", func() (string, error) {
 | 
				
			||||||
 | 
							return "some data", nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, "some data", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetString("key", func() (string, error) {
 | 
				
			||||||
 | 
						// 	return "", fmt.Errorf("some error")
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.Equal(t, "some data", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO: uncommented code works in IDE but not with go test
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestGetInt(t *testing.T) {
 | 
				
			||||||
 | 
						createTestCache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err := GetInt("key", func() (int, error) {
 | 
				
			||||||
 | 
							return 0, fmt.Errorf("some error")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, 0, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetInt("key", func() (int, error) {
 | 
				
			||||||
 | 
							return 0, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, 0, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetInt("key", func() (int, error) {
 | 
				
			||||||
 | 
						// 	return 100, nil
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.Equal(t, 0, data)
 | 
				
			||||||
 | 
						// Remove("key")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetInt("key", func() (int, error) {
 | 
				
			||||||
 | 
							return 100, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Equal(t, 100, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetInt("key", func() (int, error) {
 | 
				
			||||||
 | 
						// 	return 0, fmt.Errorf("some error")
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.Equal(t, 100, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO: uncommented code works in IDE but not with go test
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func TestGetInt64(t *testing.T) {
 | 
				
			||||||
 | 
						createTestCache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err := GetInt64("key", func() (int64, error) {
 | 
				
			||||||
 | 
							return 0, fmt.Errorf("some error")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(t, err)
 | 
				
			||||||
 | 
						assert.EqualValues(t, 0, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetInt64("key", func() (int64, error) {
 | 
				
			||||||
 | 
							return 0, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.EqualValues(t, 0, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetInt64("key", func() (int64, error) {
 | 
				
			||||||
 | 
						// 	return 100, nil
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.EqualValues(t, 0, data)
 | 
				
			||||||
 | 
						// Remove("key")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err = GetInt64("key", func() (int64, error) {
 | 
				
			||||||
 | 
							return 100, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.EqualValues(t, 100, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// data, err = GetInt64("key", func() (int, error) {
 | 
				
			||||||
 | 
						// 	return 0, fmt.Errorf("some error")
 | 
				
			||||||
 | 
						// })
 | 
				
			||||||
 | 
						// assert.NoError(t, err)
 | 
				
			||||||
 | 
						// assert.EqualValues(t, 100, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO: uncommented code works in IDE but not with go test
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										39
									
								
								modules/convert/utils_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								modules/convert/utils_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					// Copyright 2021 The Gitea Authors. All rights reserved.
 | 
				
			||||||
 | 
					// Use of this source code is governed by a MIT-style
 | 
				
			||||||
 | 
					// license that can be found in the LICENSE file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package convert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_ "github.com/mattn/go-sqlite3"
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestToCorrectPageSize(t *testing.T) {
 | 
				
			||||||
 | 
						assert.EqualValues(t, 30, ToCorrectPageSize(0))
 | 
				
			||||||
 | 
						assert.EqualValues(t, 30, ToCorrectPageSize(-10))
 | 
				
			||||||
 | 
						assert.EqualValues(t, 20, ToCorrectPageSize(20))
 | 
				
			||||||
 | 
						assert.EqualValues(t, 50, ToCorrectPageSize(100))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestToGitServiceType(t *testing.T) {
 | 
				
			||||||
 | 
						tc := []struct {
 | 
				
			||||||
 | 
							typ  string
 | 
				
			||||||
 | 
							enum int
 | 
				
			||||||
 | 
						}{{
 | 
				
			||||||
 | 
							typ: "github", enum: 2,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							typ: "gitea", enum: 3,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							typ: "gitlab", enum: 4,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							typ: "gogs", enum: 5,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							typ: "trash", enum: 1,
 | 
				
			||||||
 | 
						}}
 | 
				
			||||||
 | 
						for _, test := range tc {
 | 
				
			||||||
 | 
							assert.EqualValues(t, test.enum, ToGitServiceType(test.typ))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user