dsn_test.go 3.6 KB

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