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 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 := map[string]struct {
  10. HostPort string
  11. Host string
  12. Port string
  13. }{
  14. "host-port": {
  15. HostPort: "127.0.0.1:1234",
  16. Host: "127.0.0.1",
  17. Port: "1234",
  18. },
  19. "no-port": {
  20. HostPort: "127.0.0.1",
  21. Host: "127.0.0.1",
  22. Port: "5432",
  23. },
  24. "ipv6-port": {
  25. HostPort: "[::1]:1234",
  26. Host: "::1",
  27. Port: "1234",
  28. },
  29. "ipv6-no-port": {
  30. HostPort: "[::1]",
  31. Host: "::1",
  32. Port: "5432",
  33. },
  34. "unix-socket": {
  35. HostPort: "/tmp/pg.sock:1234",
  36. Host: "/tmp/pg.sock",
  37. Port: "1234",
  38. },
  39. "unix-socket-no-port": {
  40. HostPort: "/tmp/pg.sock",
  41. Host: "/tmp/pg.sock",
  42. Port: "5432",
  43. },
  44. }
  45. for k, test := range tests {
  46. t.Run(k, func(t *testing.T) {
  47. t.Log(test.HostPort)
  48. host, port := parsePostgreSQLHostPort(test.HostPort)
  49. assert.Equal(t, test.Host, host)
  50. assert.Equal(t, test.Port, port)
  51. })
  52. }
  53. }
  54. func Test_getPostgreSQLConnectionString(t *testing.T) {
  55. tests := []struct {
  56. Host string
  57. User string
  58. Passwd string
  59. Name string
  60. SSLMode string
  61. Output string
  62. }{
  63. {
  64. Host: "/tmp/pg.sock",
  65. User: "testuser",
  66. Passwd: "space space !#$%^^%^```-=?=",
  67. Name: "gitea",
  68. SSLMode: "false",
  69. Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
  70. },
  71. {
  72. Host: "localhost",
  73. User: "pgsqlusername",
  74. Passwd: "I love Gitea!",
  75. Name: "gitea",
  76. SSLMode: "true",
  77. Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
  78. },
  79. {
  80. Host: "localhost:1234",
  81. User: "user",
  82. Passwd: "pass",
  83. Name: "gitea?param=1",
  84. Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
  85. },
  86. }
  87. for _, test := range tests {
  88. connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
  89. assert.Equal(t, test.Output, connStr)
  90. }
  91. }