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.

database_test.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_parsePostgreSQLHostPort(t *testing.T) {
  9. tests := []struct {
  10. HostPort string
  11. Host string
  12. Port string
  13. }{
  14. {
  15. HostPort: "127.0.0.1:1234",
  16. Host: "127.0.0.1",
  17. Port: "1234",
  18. },
  19. {
  20. HostPort: "127.0.0.1",
  21. Host: "127.0.0.1",
  22. Port: "5432",
  23. },
  24. {
  25. HostPort: "[::1]:1234",
  26. Host: "[::1]",
  27. Port: "1234",
  28. },
  29. {
  30. HostPort: "[::1]",
  31. Host: "[::1]",
  32. Port: "5432",
  33. },
  34. {
  35. HostPort: "/tmp/pg.sock:1234",
  36. Host: "/tmp/pg.sock",
  37. Port: "1234",
  38. },
  39. {
  40. HostPort: "/tmp/pg.sock",
  41. Host: "/tmp/pg.sock",
  42. Port: "5432",
  43. },
  44. }
  45. for _, test := range tests {
  46. host, port := parsePostgreSQLHostPort(test.HostPort)
  47. assert.Equal(t, test.Host, host)
  48. assert.Equal(t, test.Port, port)
  49. }
  50. }
  51. func Test_getPostgreSQLConnectionString(t *testing.T) {
  52. tests := []struct {
  53. Host string
  54. Port string
  55. User string
  56. Passwd string
  57. Name string
  58. Param string
  59. SSLMode string
  60. Output string
  61. }{
  62. {
  63. Host: "/tmp/pg.sock",
  64. Port: "4321",
  65. User: "testuser",
  66. Passwd: "space space !#$%^^%^```-=?=",
  67. Name: "gitea",
  68. Param: "",
  69. SSLMode: "false",
  70. Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/giteasslmode=false&host=/tmp/pg.sock",
  71. },
  72. {
  73. Host: "localhost",
  74. Port: "1234",
  75. User: "pgsqlusername",
  76. Passwd: "I love Gitea!",
  77. Name: "gitea",
  78. Param: "",
  79. SSLMode: "true",
  80. Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/giteasslmode=true",
  81. },
  82. }
  83. for _, test := range tests {
  84. connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
  85. assert.Equal(t, test.Output, connStr)
  86. }
  87. }