This repository has been archived on 2020-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
core/filter_test.go
Lunny Xiao 90aeac8d08
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
fix arg conversion (#61)
2019-09-28 05:59:35 +00:00

40 lines
1.4 KiB
Go

package core
import (
"testing"
"github.com/stretchr/testify/assert"
)
type quoterOnly struct {
Dialect
}
func (q *quoterOnly) Quote(item string) string {
return "[" + item + "]"
}
func TestQuoteFilter_Do(t *testing.T) {
f := QuoteFilter{}
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
res := f.Do(sql, new(quoterOnly), nil)
assert.EqualValues(t,
"SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE [TABLE_SCHEMA] = ? AND [TABLE_NAME] = ? AND [COLUMN_NAME] = ?",
res,
)
}
func TestSeqFilter(t *testing.T) {
var kases = map[string]string{
"SELECT * FROM TABLE1 WHERE a=? AND b=?": "SELECT * FROM TABLE1 WHERE a=$1 AND b=$2",
"SELECT 1, '???', '2006-01-02 15:04:05' FROM TABLE1 WHERE a=? AND b=?": "SELECT 1, '???', '2006-01-02 15:04:05' FROM TABLE1 WHERE a=$1 AND b=$2",
"select '1''?' from issue": "select '1''?' from issue",
"select '1\\??' from issue": "select '1\\??' from issue",
"select '1\\\\',? from issue": "select '1\\\\',$1 from issue",
"select '1\\''?',? from issue": "select '1\\''?',$1 from issue",
}
for sql, result := range kases {
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
}
}