diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-10 22:51:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-10 22:51:24 +0800 |
commit | e186a3d2c95aee3c652b010c2c08ed771590b86b (patch) | |
tree | 5b4f5637cef8a76cac979f2d225f540b141facea /internal/db | |
parent | 9a5b227f3ee2b2a3854d3aec022cc9a0cf0868b3 (diff) |
db: add tests for helper functions (#6084)
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/db.go | 8 | ||||
-rw-r--r-- | internal/db/db_test.go | 148 | ||||
-rw-r--r-- | internal/db/main_test.go | 30 | ||||
-rw-r--r-- | internal/db/models_test.go | 33 | ||||
-rw-r--r-- | internal/db/ssh_key_test.go | 5 |
5 files changed, 183 insertions, 41 deletions
diff --git a/internal/db/db.go b/internal/db/db.go index 85503533..e3796039 100644 --- a/internal/db/db.go +++ b/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] diff --git a/internal/db/db_test.go b/internal/db/db_test.go new file mode 100644 index 00000000..8c894125 --- /dev/null +++ b/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) + }) + } +} diff --git a/internal/db/main_test.go b/internal/db/main_test.go new file mode 100644 index 00000000..24393f5b --- /dev/null +++ b/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()) +} diff --git a/internal/db/models_test.go b/internal/db/models_test.go deleted file mode 100644 index 53f8b4f0..00000000 --- a/internal/db/models_test.go +++ /dev/null @@ -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) - } - }) -} diff --git a/internal/db/ssh_key_test.go b/internal/db/ssh_key_test.go index 9bf49413..dfd945b6 100644 --- a/internal/db/ssh_key_test.go +++ b/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 |