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.0KB

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