mirror of
https://github.com/neilotoole/sq.git
synced 2024-12-26 01:34:43 +03:00
c778c8ced5
* test linting * test linting * test linting * test linting
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
//nolint:lll
|
|
package mysql
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
)
|
|
|
|
var KindFromDBTypeName = kindFromDBTypeName
|
|
|
|
func TestPlaceholders(t *testing.T) {
|
|
testCases := []struct {
|
|
numCols int
|
|
numRows int
|
|
want string
|
|
}{
|
|
{numCols: 0, numRows: 0, want: ""},
|
|
{numCols: 1, numRows: 1, want: "(?)"},
|
|
{numCols: 2, numRows: 1, want: "(?, ?)"},
|
|
{numCols: 1, numRows: 2, want: "(?), (?)"},
|
|
{numCols: 2, numRows: 2, want: "(?, ?), (?, ?)"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
got := placeholders(tc.numCols, tc.numRows)
|
|
require.Equal(t, tc.want, got)
|
|
}
|
|
}
|
|
|
|
func TestHasErrCode(t *testing.T) {
|
|
var err error
|
|
err = &mysql.MySQLError{
|
|
Number: 1146,
|
|
Message: "I'm not here",
|
|
}
|
|
|
|
require.True(t, hasErrCode(err, errNumTableNotExist))
|
|
|
|
// Test that a wrapped error works
|
|
err = errz.Err(err)
|
|
require.True(t, hasErrCode(err, errNumTableNotExist))
|
|
}
|
|
|
|
func TestDSNFromLocation(t *testing.T) {
|
|
testCases := []struct {
|
|
loc string
|
|
parseTime bool
|
|
wantDSN string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest",
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?parseTime=true",
|
|
parseTime: true,
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?parseTime=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?parseTime=true",
|
|
parseTime: true,
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowOldPasswords=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowOldPasswords=true",
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowOldPasswords=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowOldPasswords=true&parseTime=true",
|
|
parseTime: true,
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowOldPasswords=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowOldPasswords=true",
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowCleartextPasswords=true&allowOldPasswords=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowCleartextPasswords=true&allowOldPasswords=true",
|
|
},
|
|
{
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?parseTime=true&allowCleartextPasswords=true&allowOldPasswords=true",
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowCleartextPasswords=true&allowOldPasswords=true&parseTime=true",
|
|
parseTime: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
t.Run(tutil.Name(tc.loc, tc.parseTime), func(t *testing.T) {
|
|
src := &source.Source{
|
|
Handle: "@testhandle",
|
|
Type: Type,
|
|
Location: tc.loc,
|
|
}
|
|
|
|
gotDSN, gotErr := dsnFromLocation(src, tc.parseTime)
|
|
if tc.wantErr {
|
|
require.Error(t, gotErr)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, gotErr)
|
|
require.Equal(t, tc.wantDSN, gotDSN)
|
|
})
|
|
}
|
|
}
|