db_test.go 3.7 KB

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