瀏覽代碼

db: add tests for helper functions (#6084)

ᴜɴᴋɴᴡᴏɴ 4 年之前
父節點
當前提交
e186a3d2c9
共有 5 個文件被更改,包括 183 次插入41 次删除
  1. 4 4
      internal/db/db.go
  2. 148 0
      internal/db/db_test.go
  3. 30 0
      internal/db/main_test.go
  4. 0 33
      internal/db/models_test.go
  5. 1 4
      internal/db/ssh_key_test.go

+ 4 - 4
internal/db/db.go

@@ -27,8 +27,8 @@ import (
 // parsePostgreSQLHostPort parses given input in various forms defined in
 // https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
 // and returns proper host and port number.
-func parsePostgreSQLHostPort(info string) (string, string) {
-	host, port := "127.0.0.1", "5432"
+func parsePostgreSQLHostPort(info string) (host, port string) {
+	host, port = "127.0.0.1", "5432"
 	if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
 		idx := strings.LastIndex(info, ":")
 		host = info[:idx]
@@ -39,8 +39,8 @@ func parsePostgreSQLHostPort(info string) (string, string) {
 	return host, port
 }
 
-func parseMSSQLHostPort(info string) (string, string) {
-	host, port := "127.0.0.1", "1433"
+func parseMSSQLHostPort(info string) (host, port  string) {
+	host, port = "127.0.0.1", "1433"
 	if strings.Contains(info, ":") {
 		host = strings.Split(info, ":")[0]
 		port = strings.Split(info, ":")[1]

+ 148 - 0
internal/db/db_test.go

@@ -0,0 +1,148 @@
+// Copyright 2020 The Gogs 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 db
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+
+	"gogs.io/gogs/internal/conf"
+)
+
+func Test_parsePostgreSQLHostPort(t *testing.T) {
+	tests := []struct {
+		info    string
+		expHost string
+		expPort string
+	}{
+		{info: "127.0.0.1:1234", expHost: "127.0.0.1", expPort: "1234"},
+		{info: "127.0.0.1", expHost: "127.0.0.1", expPort: "5432"},
+		{info: "[::1]:1234", expHost: "[::1]", expPort: "1234"},
+		{info: "[::1]", expHost: "[::1]", expPort: "5432"},
+		{info: "/tmp/pg.sock:1234", expHost: "/tmp/pg.sock", expPort: "1234"},
+		{info: "/tmp/pg.sock", expHost: "/tmp/pg.sock", expPort: "5432"},
+	}
+	for _, test := range tests {
+		t.Run("", func(t *testing.T) {
+			host, port := parsePostgreSQLHostPort(test.info)
+			assert.Equal(t, test.expHost, host)
+			assert.Equal(t, test.expPort, port)
+		})
+	}
+}
+
+func Test_parseMSSQLHostPort(t *testing.T) {
+	tests := []struct {
+		info    string
+		expHost string
+		expPort string
+	}{
+		{info: "127.0.0.1:1234", expHost: "127.0.0.1", expPort: "1234"},
+		{info: "127.0.0.1,1234", expHost: "127.0.0.1", expPort: "1234"},
+		{info: "127.0.0.1", expHost: "127.0.0.1", expPort: "1433"},
+	}
+	for _, test := range tests {
+		t.Run("", func(t *testing.T) {
+			host, port := parseMSSQLHostPort(test.info)
+			assert.Equal(t, test.expHost, host)
+			assert.Equal(t, test.expPort, port)
+		})
+	}
+}
+
+func Test_parseDSN(t *testing.T) {
+	t.Run("bad dialect", func(t *testing.T) {
+		_, err := parseDSN(conf.DatabaseOpts{
+			Type: "bad_dialect",
+		})
+		assert.Equal(t, "unrecognized dialect: bad_dialect", fmt.Sprintf("%v", err))
+	})
+
+	tests := []struct {
+		name   string
+		opts   conf.DatabaseOpts
+		expDSN string
+	}{
+		{
+			name: "mysql: unix",
+			opts: conf.DatabaseOpts{
+				Type:     "mysql",
+				Host:     "/tmp/mysql.sock",
+				Name:     "gogs",
+				User:     "gogs",
+				Password: "pa$$word",
+			},
+			expDSN: "gogs:pa$$word@unix(/tmp/mysql.sock)/gogs?charset=utf8mb4&parseTime=true",
+		},
+		{
+			name: "mysql: tcp",
+			opts: conf.DatabaseOpts{
+				Type:     "mysql",
+				Host:     "localhost:3306",
+				Name:     "gogs",
+				User:     "gogs",
+				Password: "pa$$word",
+			},
+			expDSN: "gogs:pa$$word@tcp(localhost:3306)/gogs?charset=utf8mb4&parseTime=true",
+		},
+
+		{
+			name: "postgres: unix",
+			opts: conf.DatabaseOpts{
+				Type:     "postgres",
+				Host:     "/tmp/pg.sock",
+				Name:     "gogs",
+				User:     "gogs@local",
+				Password: "pa$$word",
+				SSLMode:  "disable",
+			},
+			expDSN: "postgres://gogs%40local:pa%24%24word@:5432/gogs?sslmode=disable&host=/tmp/pg.sock",
+		},
+		{
+			name: "postgres: tcp",
+			opts: conf.DatabaseOpts{
+				Type:     "postgres",
+				Host:     "127.0.0.1",
+				Name:     "gogs",
+				User:     "gogs@local",
+				Password: "pa$$word",
+				SSLMode:  "disable",
+			},
+			expDSN: "postgres://gogs%40local:pa%24%24word@127.0.0.1:5432/gogs?sslmode=disable",
+		},
+
+		{
+			name: "mssql",
+			opts: conf.DatabaseOpts{
+				Type:     "mssql",
+				Host:     "127.0.0.1",
+				Name:     "gogs",
+				User:     "gogs@local",
+				Password: "pa$$word",
+			},
+			expDSN: "server=127.0.0.1; port=1433; database=gogs; user id=gogs@local; password=pa$$word;",
+		},
+
+		{
+			name: "sqlite3",
+			opts: conf.DatabaseOpts{
+				Type: "sqlite3",
+				Path: "/tmp/gogs.db",
+			},
+			expDSN: "file:/tmp/gogs.db?cache=shared&mode=rwc",
+		},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			dsn, err := parseDSN(test.opts)
+			if err != nil {
+				t.Fatal(err)
+			}
+			assert.Equal(t, test.expDSN, dsn)
+		})
+	}
+}

+ 30 - 0
internal/db/main_test.go

@@ -0,0 +1,30 @@
+// Copyright 2020 The Gogs 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 db
+
+import (
+	"flag"
+	"fmt"
+	"os"
+	"testing"
+
+	log "unknwon.dev/clog/v2"
+
+	"gogs.io/gogs/internal/testutil"
+)
+
+func TestMain(m *testing.M) {
+	flag.Parse()
+	if !testing.Verbose() {
+		// Remove the primary logger and register a noop logger.
+		log.Remove(log.DefaultConsoleName)
+		err := log.New("noop", testutil.InitNoopLogger)
+		if err != nil {
+			fmt.Println(err)
+			os.Exit(1)
+		}
+	}
+	os.Exit(m.Run())
+}

+ 0 - 33
internal/db/models_test.go

@@ -1,33 +0,0 @@
-// Copyright 2016 The Gogs 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 db
-
-import (
-	"testing"
-
-	. "github.com/smartystreets/goconvey/convey"
-)
-
-func Test_parsePostgreSQLHostPort(t *testing.T) {
-	testSuites := []struct {
-		input      string
-		host, port string
-	}{
-		{"127.0.0.1:1234", "127.0.0.1", "1234"},
-		{"127.0.0.1", "127.0.0.1", "5432"},
-		{"[::1]:1234", "[::1]", "1234"},
-		{"[::1]", "[::1]", "5432"},
-		{"/tmp/pg.sock:1234", "/tmp/pg.sock", "1234"},
-		{"/tmp/pg.sock", "/tmp/pg.sock", "5432"},
-	}
-
-	Convey("Parse PostgreSQL host and port", t, func() {
-		for _, suite := range testSuites {
-			host, port := parsePostgreSQLHostPort(suite.input)
-			So(host, ShouldEqual, suite.host)
-			So(port, ShouldEqual, suite.port)
-		}
-	})
-}

+ 1 - 4
internal/db/ssh_key_test.go

@@ -14,11 +14,8 @@ import (
 	"gogs.io/gogs/internal/conf"
 )
 
-func init() {
-	conf.MustInit("")
-}
-
 func Test_SSHParsePublicKey(t *testing.T) {
+	conf.MustInit("")
 	testKeys := map[string]struct {
 		typeName string
 		length   int