code.gitea.io/gitea@v1.19.3/modules/setting/database_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_parsePostgreSQLHostPort(t *testing.T) {
    13  	tests := []struct {
    14  		HostPort string
    15  		Host     string
    16  		Port     string
    17  	}{
    18  		{
    19  			HostPort: "127.0.0.1:1234",
    20  			Host:     "127.0.0.1",
    21  			Port:     "1234",
    22  		},
    23  		{
    24  			HostPort: "127.0.0.1",
    25  			Host:     "127.0.0.1",
    26  			Port:     "5432",
    27  		},
    28  		{
    29  			HostPort: "[::1]:1234",
    30  			Host:     "[::1]",
    31  			Port:     "1234",
    32  		},
    33  		{
    34  			HostPort: "[::1]",
    35  			Host:     "[::1]",
    36  			Port:     "5432",
    37  		},
    38  		{
    39  			HostPort: "/tmp/pg.sock:1234",
    40  			Host:     "/tmp/pg.sock",
    41  			Port:     "1234",
    42  		},
    43  		{
    44  			HostPort: "/tmp/pg.sock",
    45  			Host:     "/tmp/pg.sock",
    46  			Port:     "5432",
    47  		},
    48  	}
    49  	for _, test := range tests {
    50  		host, port := parsePostgreSQLHostPort(test.HostPort)
    51  		assert.Equal(t, test.Host, host)
    52  		assert.Equal(t, test.Port, port)
    53  	}
    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  
    89  	for _, test := range tests {
    90  		connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
    91  		assert.Equal(t, test.Output, connStr)
    92  	}
    93  }