dsn_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 The Gogs 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. package dbutil
  5. import (
  6. "fmt"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "gogs.io/gogs/internal/conf"
  11. )
  12. func TestParsePostgreSQLHostPort(t *testing.T) {
  13. tests := []struct {
  14. info string
  15. expHost string
  16. expPort string
  17. }{
  18. {info: "127.0.0.1:1234", expHost: "127.0.0.1", expPort: "1234"},
  19. {info: "127.0.0.1", expHost: "127.0.0.1", expPort: "5432"},
  20. {info: "[::1]:1234", expHost: "[::1]", expPort: "1234"},
  21. {info: "[::1]", expHost: "[::1]", expPort: "5432"},
  22. {info: "/tmp/pg.sock:1234", expHost: "/tmp/pg.sock", expPort: "1234"},
  23. {info: "/tmp/pg.sock", expHost: "/tmp/pg.sock", expPort: "5432"},
  24. }
  25. for _, test := range tests {
  26. t.Run("", func(t *testing.T) {
  27. host, port := ParsePostgreSQLHostPort(test.info)
  28. assert.Equal(t, test.expHost, host)
  29. assert.Equal(t, test.expPort, port)
  30. })
  31. }
  32. }
  33. func TestParseMSSQLHostPort(t *testing.T) {
  34. tests := []struct {
  35. info string
  36. expHost string
  37. expPort string
  38. }{
  39. {info: "127.0.0.1:1234", expHost: "127.0.0.1", expPort: "1234"},
  40. {info: "127.0.0.1,1234", expHost: "127.0.0.1", expPort: "1234"},
  41. {info: "127.0.0.1", expHost: "127.0.0.1", expPort: "1433"},
  42. }
  43. for _, test := range tests {
  44. t.Run("", func(t *testing.T) {
  45. host, port := ParseMSSQLHostPort(test.info)
  46. assert.Equal(t, test.expHost, host)
  47. assert.Equal(t, test.expPort, port)
  48. })
  49. }
  50. }
  51. func TestNewDSN(t *testing.T) {
  52. t.Run("bad dialect", func(t *testing.T) {
  53. _, err := NewDSN(conf.DatabaseOpts{
  54. Type: "bad_dialect",
  55. })
  56. assert.Equal(t, "unrecognized dialect: bad_dialect", fmt.Sprintf("%v", err))
  57. })
  58. tests := []struct {
  59. name string
  60. opts conf.DatabaseOpts
  61. wantDSN string
  62. }{
  63. {
  64. name: "mysql: unix",
  65. opts: conf.DatabaseOpts{
  66. Type: "mysql",
  67. Host: "/tmp/mysql.sock",
  68. Name: "gogs",
  69. User: "gogs",
  70. Password: "pa$$word",
  71. },
  72. wantDSN: "gogs:pa$$word@unix(/tmp/mysql.sock)/gogs?charset=utf8mb4&parseTime=true",
  73. },
  74. {
  75. name: "mysql: tcp",
  76. opts: conf.DatabaseOpts{
  77. Type: "mysql",
  78. Host: "localhost:3306",
  79. Name: "gogs",
  80. User: "gogs",
  81. Password: "pa$$word",
  82. },
  83. wantDSN: "gogs:pa$$word@tcp(localhost:3306)/gogs?charset=utf8mb4&parseTime=true",
  84. },
  85. {
  86. name: "postgres: unix",
  87. opts: conf.DatabaseOpts{
  88. Type: "postgres",
  89. Host: "/tmp/pg.sock",
  90. Name: "gogs",
  91. Schema: "test",
  92. User: "gogs@local",
  93. Password: "pa$$word",
  94. SSLMode: "disable",
  95. },
  96. wantDSN: "user='gogs@local' password='pa$$word' host='/tmp/pg.sock' port='5432' dbname='gogs' sslmode='disable' search_path='test' application_name='gogs'",
  97. },
  98. {
  99. name: "postgres: tcp",
  100. opts: conf.DatabaseOpts{
  101. Type: "postgres",
  102. Host: "127.0.0.1",
  103. Name: "gogs",
  104. Schema: "test",
  105. User: "gogs@local",
  106. Password: "pa$$word",
  107. SSLMode: "disable",
  108. },
  109. wantDSN: "user='gogs@local' password='pa$$word' host='127.0.0.1' port='5432' dbname='gogs' sslmode='disable' search_path='test' application_name='gogs'",
  110. },
  111. {
  112. name: "mssql",
  113. opts: conf.DatabaseOpts{
  114. Type: "mssql",
  115. Host: "127.0.0.1",
  116. Name: "gogs",
  117. User: "gogs@local",
  118. Password: "pa$$word",
  119. },
  120. wantDSN: "server=127.0.0.1; port=1433; database=gogs; user id=gogs@local; password=pa$$word;",
  121. },
  122. {
  123. name: "sqlite3",
  124. opts: conf.DatabaseOpts{
  125. Type: "sqlite3",
  126. Path: "/tmp/gogs.db",
  127. },
  128. wantDSN: "file:/tmp/gogs.db?cache=shared&mode=rwc",
  129. },
  130. }
  131. for _, test := range tests {
  132. t.Run(test.name, func(t *testing.T) {
  133. dsn, err := NewDSN(test.opts)
  134. require.NoError(t, err)
  135. assert.Equal(t, test.wantDSN, dsn)
  136. })
  137. }
  138. }