db_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. User: "gogs@local",
  91. Password: "pa$$word",
  92. SSLMode: "disable",
  93. },
  94. expDSN: "postgres://gogs%40local:pa%24%24word@:5432/gogs?sslmode=disable&host=/tmp/pg.sock",
  95. },
  96. {
  97. name: "postgres: tcp",
  98. opts: conf.DatabaseOpts{
  99. Type: "postgres",
  100. Host: "127.0.0.1",
  101. Name: "gogs",
  102. User: "gogs@local",
  103. Password: "pa$$word",
  104. SSLMode: "disable",
  105. },
  106. expDSN: "postgres://gogs%40local:pa%24%24word@127.0.0.1:5432/gogs?sslmode=disable",
  107. },
  108. {
  109. name: "mssql",
  110. opts: conf.DatabaseOpts{
  111. Type: "mssql",
  112. Host: "127.0.0.1",
  113. Name: "gogs",
  114. User: "gogs@local",
  115. Password: "pa$$word",
  116. },
  117. expDSN: "server=127.0.0.1; port=1433; database=gogs; user id=gogs@local; password=pa$$word;",
  118. },
  119. {
  120. name: "sqlite3",
  121. opts: conf.DatabaseOpts{
  122. Type: "sqlite3",
  123. Path: "/tmp/gogs.db",
  124. },
  125. expDSN: "file:/tmp/gogs.db?cache=shared&mode=rwc",
  126. },
  127. }
  128. for _, test := range tests {
  129. t.Run(test.name, func(t *testing.T) {
  130. dsn, err := parseDSN(test.opts)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. assert.Equal(t, test.expDSN, dsn)
  135. })
  136. }
  137. }