2020-08-06 20:58:47 +03:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-04-08 21:09:27 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var GetTableColumnNames = getTableColumnNames
|
|
|
|
|
|
|
|
func TestPlaceholders(t *testing.T) {
|
2020-08-12 21:24:01 +03:00
|
|
|
testCases := []struct {
|
|
|
|
numCols int
|
|
|
|
numRows int
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{numCols: 0, numRows: 0, want: ""},
|
|
|
|
{numCols: 1, numRows: 1, want: "($1)"},
|
|
|
|
{numCols: 2, numRows: 1, want: "($1, $2)"},
|
|
|
|
{numCols: 1, numRows: 2, want: "($1), ($2)"},
|
|
|
|
{numCols: 2, numRows: 2, want: "($1, $2), ($3, $4)"},
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
got := placeholders(tc.numCols, tc.numRows)
|
|
|
|
require.Equal(t, tc.want, got)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplacePlaceholders(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"": "",
|
|
|
|
"hello": "hello",
|
|
|
|
"?": "$1",
|
|
|
|
"??": "$1$2",
|
|
|
|
" ? ": " $1 ",
|
|
|
|
"(?, ?)": "($1, $2)",
|
|
|
|
"(?, ?, ?)": "($1, $2, $3)",
|
|
|
|
" (? , ? , ?) ": " ($1 , $2 , $3) ",
|
|
|
|
}
|
|
|
|
|
|
|
|
for input, want := range testCases {
|
|
|
|
got := replacePlaceholders(input)
|
|
|
|
require.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 08:16:10 +03:00
|
|
|
|
|
|
|
func Test_idSanitize(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
`tbl_name`: `"tbl_name"`,
|
|
|
|
}
|
|
|
|
|
|
|
|
for input, want := range testCases {
|
|
|
|
got := idSanitize(input)
|
|
|
|
require.Equal(t, want, got)
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 21:09:27 +03:00
|
|
|
|
|
|
|
func TestIsErrTooManyConnections(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = &pgconn.PgError{Code: "53300"}
|
|
|
|
require.True(t, isErrTooManyConnections(err))
|
|
|
|
|
|
|
|
// Test with a wrapped error
|
|
|
|
err = errz.Err(err)
|
|
|
|
require.True(t, isErrTooManyConnections(err))
|
|
|
|
}
|