You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models_test.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func Test_parsePostgreSQLHostPort(t *testing.T) {
  14. tests := []struct {
  15. HostPort string
  16. Host string
  17. Port string
  18. }{
  19. {
  20. HostPort: "127.0.0.1:1234",
  21. Host: "127.0.0.1",
  22. Port: "1234",
  23. },
  24. {
  25. HostPort: "127.0.0.1",
  26. Host: "127.0.0.1",
  27. Port: "5432",
  28. },
  29. {
  30. HostPort: "[::1]:1234",
  31. Host: "[::1]",
  32. Port: "1234",
  33. },
  34. {
  35. HostPort: "[::1]",
  36. Host: "[::1]",
  37. Port: "5432",
  38. },
  39. {
  40. HostPort: "/tmp/pg.sock:1234",
  41. Host: "/tmp/pg.sock",
  42. Port: "1234",
  43. },
  44. {
  45. HostPort: "/tmp/pg.sock",
  46. Host: "/tmp/pg.sock",
  47. Port: "5432",
  48. },
  49. }
  50. for _, test := range tests {
  51. host, port := parsePostgreSQLHostPort(test.HostPort)
  52. assert.Equal(t, test.Host, host)
  53. assert.Equal(t, test.Port, port)
  54. }
  55. }
  56. func Test_getPostgreSQLConnectionString(t *testing.T) {
  57. tests := []struct {
  58. Host string
  59. Port string
  60. User string
  61. Passwd string
  62. Name string
  63. Param string
  64. SSLMode string
  65. Output string
  66. }{
  67. {
  68. Host: "/tmp/pg.sock",
  69. Port: "4321",
  70. User: "testuser",
  71. Passwd: "space space !#$%^^%^```-=?=",
  72. Name: "gitea",
  73. Param: "",
  74. SSLMode: "false",
  75. Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/giteasslmode=false&host=/tmp/pg.sock",
  76. },
  77. {
  78. Host: "localhost",
  79. Port: "1234",
  80. User: "pgsqlusername",
  81. Passwd: "I love Gitea!",
  82. Name: "gitea",
  83. Param: "",
  84. SSLMode: "true",
  85. Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/giteasslmode=true",
  86. },
  87. }
  88. for _, test := range tests {
  89. connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
  90. assert.Equal(t, test.Output, connStr)
  91. }
  92. }
  93. func TestDumpDatabase(t *testing.T) {
  94. assert.NoError(t, PrepareTestDatabase())
  95. dir, err := ioutil.TempDir(os.TempDir(), "dump")
  96. assert.NoError(t, err)
  97. for _, dbType := range supportedDatabases {
  98. assert.NoError(t, DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
  99. }
  100. }