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