dsn_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 TestNewDSN(t *testing.T) {
  31. t.Run("bad dialect", func(t *testing.T) {
  32. _, err := NewDSN(conf.DatabaseOpts{
  33. Type: "bad_dialect",
  34. })
  35. assert.Equal(t, "unrecognized dialect: bad_dialect", fmt.Sprintf("%v", err))
  36. })
  37. tests := []struct {
  38. name string
  39. opts conf.DatabaseOpts
  40. wantDSN string
  41. }{
  42. {
  43. name: "mysql: unix",
  44. opts: conf.DatabaseOpts{
  45. Type: "mysql",
  46. Host: "/tmp/mysql.sock",
  47. Name: "gogs",
  48. User: "gogs",
  49. Password: "pa$$word",
  50. },
  51. wantDSN: "gogs:pa$$word@unix(/tmp/mysql.sock)/gogs?charset=utf8mb4&parseTime=true",
  52. },
  53. {
  54. name: "mysql: tcp",
  55. opts: conf.DatabaseOpts{
  56. Type: "mysql",
  57. Host: "localhost:3306",
  58. Name: "gogs",
  59. User: "gogs",
  60. Password: "pa$$word",
  61. },
  62. wantDSN: "gogs:pa$$word@tcp(localhost:3306)/gogs?charset=utf8mb4&parseTime=true",
  63. },
  64. {
  65. name: "postgres: unix",
  66. opts: conf.DatabaseOpts{
  67. Type: "postgres",
  68. Host: "/tmp/pg.sock",
  69. Name: "gogs",
  70. Schema: "test",
  71. User: "gogs@local",
  72. Password: "pa$$word",
  73. SSLMode: "disable",
  74. },
  75. wantDSN: "user='gogs@local' password='pa$$word' host='/tmp/pg.sock' port='5432' dbname='gogs' sslmode='disable' search_path='test' application_name='gogs'",
  76. },
  77. {
  78. name: "postgres: tcp",
  79. opts: conf.DatabaseOpts{
  80. Type: "postgres",
  81. Host: "127.0.0.1",
  82. Name: "gogs",
  83. Schema: "test",
  84. User: "gogs@local",
  85. Password: "pa$$word",
  86. SSLMode: "disable",
  87. },
  88. wantDSN: "user='gogs@local' password='pa$$word' host='127.0.0.1' port='5432' dbname='gogs' sslmode='disable' search_path='test' application_name='gogs'",
  89. },
  90. {
  91. name: "sqlite3",
  92. opts: conf.DatabaseOpts{
  93. Type: "sqlite3",
  94. Path: "/tmp/gogs.db",
  95. },
  96. wantDSN: "file:/tmp/gogs.db?cache=shared&mode=rwc",
  97. },
  98. }
  99. for _, test := range tests {
  100. t.Run(test.name, func(t *testing.T) {
  101. dsn, err := NewDSN(test.opts)
  102. require.NoError(t, err)
  103. assert.Equal(t, test.wantDSN, dsn)
  104. })
  105. }
  106. }