2022-12-18 10:18:35 +03:00
|
|
|
//nolint:lll
|
2020-08-06 20:58:47 +03:00
|
|
|
package mysql
|
|
|
|
|
2020-08-12 21:24:01 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
2022-12-18 10:18:35 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
2020-08-12 21:24:01 +03:00
|
|
|
|
2020-08-18 22:13:25 +03:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2020-08-12 21:24:01 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-18 22:13:25 +03:00
|
|
|
|
2021-03-13 20:08:53 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2020-08-12 21:24:01 +03:00
|
|
|
)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
var KindFromDBTypeName = kindFromDBTypeName
|
2020-08-12 21:24:01 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 22:13:25 +03:00
|
|
|
|
|
|
|
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
|
2023-05-22 18:08:14 +03:00
|
|
|
err = errw(err)
|
2020-08-18 22:13:25 +03:00
|
|
|
require.True(t, hasErrCode(err, errNumTableNotExist))
|
|
|
|
}
|
2021-03-13 20:08:53 +03:00
|
|
|
|
|
|
|
func TestDSNFromLocation(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2022-12-17 05:09:49 +03:00
|
|
|
loc string
|
|
|
|
parseTime bool
|
|
|
|
wantDSN string
|
|
|
|
wantErr bool
|
2021-03-13 20:08:53 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest",
|
|
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest",
|
|
|
|
},
|
|
|
|
{
|
2022-12-17 05:09:49 +03:00
|
|
|
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest",
|
|
|
|
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?parseTime=true",
|
|
|
|
parseTime: true,
|
2021-03-13 20:08:53 +03:00
|
|
|
},
|
|
|
|
{
|
2022-12-17 05:09:49 +03:00
|
|
|
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,
|
2021-03-13 20:08:53 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
2022-12-17 05:09:49 +03:00
|
|
|
t.Run(tutil.Name(tc.loc, tc.parseTime), func(t *testing.T) {
|
2021-03-13 20:08:53 +03:00
|
|
|
src := &source.Source{
|
|
|
|
Handle: "@testhandle",
|
|
|
|
Type: Type,
|
|
|
|
Location: tc.loc,
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:09:49 +03:00
|
|
|
gotDSN, gotErr := dsnFromLocation(src, tc.parseTime)
|
2021-03-13 20:08:53 +03:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, gotErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, gotErr)
|
|
|
|
require.Equal(t, tc.wantDSN, gotDSN)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|