2019-11-12 00:48:20 +00:00
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2020-02-25 00:01:36 +00:00
package schemas
2019-11-12 00:48:20 +00:00
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
2020-03-06 07:48:32 +00:00
func TestAlwaysQuoteTo ( t * testing . T ) {
var (
quoter = Quoter { '[' , ']' , AlwaysReserve }
kases = [ ] struct {
expected string
value string
} {
{ "[mytable]" , "mytable" } ,
{ "[mytable]" , "`mytable`" } ,
{ "[mytable]" , ` [mytable] ` } ,
{ ` ["mytable"] ` , ` "mytable" ` } ,
2020-09-06 08:22:34 +00:00
{ ` [mytable].* ` , ` [mytable].* ` } ,
2020-03-06 07:48:32 +00:00
{ "[myschema].[mytable]" , "myschema.mytable" } ,
{ "[myschema].[mytable]" , "`myschema`.mytable" } ,
{ "[myschema].[mytable]" , "myschema.`mytable`" } ,
{ "[myschema].[mytable]" , "`myschema`.`mytable`" } ,
{ "[myschema].[mytable]" , ` [myschema].mytable ` } ,
{ "[myschema].[mytable]" , ` myschema.[mytable] ` } ,
{ "[myschema].[mytable]" , ` [myschema].[mytable] ` } ,
{ ` ["myschema].[mytable"] ` , ` "myschema.mytable" ` } ,
{ "[message_user] AS [sender]" , "`message_user` AS `sender`" } ,
{ "[myschema].[mytable] AS [table]" , "myschema.mytable AS table" } ,
2020-03-25 12:28:11 +00:00
{ " [mytable]" , " mytable" } ,
{ " [mytable]" , " mytable" } ,
{ "[mytable] " , "mytable " } ,
{ "[mytable] " , "mytable " } ,
{ " [mytable] " , " mytable " } ,
{ " [mytable] " , " mytable " } ,
2020-03-06 07:48:32 +00:00
}
)
for _ , v := range kases {
t . Run ( v . value , func ( t * testing . T ) {
buf := & strings . Builder { }
2021-12-07 03:43:50 +00:00
err := quoter . QuoteTo ( buf , v . value )
assert . NoError ( t , err )
2020-03-06 07:48:32 +00:00
assert . EqualValues ( t , v . expected , buf . String ( ) )
} )
2019-11-12 00:48:20 +00:00
}
2020-03-06 07:48:32 +00:00
}
2019-11-12 00:48:20 +00:00
2020-03-06 07:48:32 +00:00
func TestReversedQuoteTo ( t * testing . T ) {
var (
quoter = Quoter { '[' , ']' , func ( s string ) bool {
2021-12-07 03:43:50 +00:00
return s == "mytable"
2020-03-06 07:48:32 +00:00
} }
kases = [ ] struct {
expected string
value string
} {
{ "[mytable]" , "mytable" } ,
{ "[mytable]" , "`mytable`" } ,
{ "[mytable]" , ` [mytable] ` } ,
2020-09-06 08:22:34 +00:00
{ "[mytable].*" , ` [mytable].* ` } ,
2020-03-06 07:48:32 +00:00
{ ` "mytable" ` , ` "mytable" ` } ,
{ "myschema.[mytable]" , "myschema.mytable" } ,
{ "myschema.[mytable]" , "`myschema`.mytable" } ,
{ "myschema.[mytable]" , "myschema.`mytable`" } ,
{ "myschema.[mytable]" , "`myschema`.`mytable`" } ,
{ "myschema.[mytable]" , ` [myschema].mytable ` } ,
{ "myschema.[mytable]" , ` myschema.[mytable] ` } ,
{ "myschema.[mytable]" , ` [myschema].[mytable] ` } ,
{ ` "myschema.mytable" ` , ` "myschema.mytable" ` } ,
{ "message_user AS sender" , "`message_user` AS `sender`" } ,
{ "myschema.[mytable] AS table" , "myschema.mytable AS table" } ,
}
)
for _ , v := range kases {
t . Run ( v . value , func ( t * testing . T ) {
buf := & strings . Builder { }
quoter . QuoteTo ( buf , v . value )
assert . EqualValues ( t , v . expected , buf . String ( ) )
} )
}
}
2020-02-25 00:01:36 +00:00
2020-03-06 07:48:32 +00:00
func TestNoQuoteTo ( t * testing . T ) {
var (
quoter = Quoter { '[' , ']' , AlwaysNoReserve }
kases = [ ] struct {
expected string
value string
} {
{ "mytable" , "mytable" } ,
{ "mytable" , "`mytable`" } ,
{ "mytable" , ` [mytable] ` } ,
2020-09-06 08:22:34 +00:00
{ "mytable.*" , ` [mytable].* ` } ,
2020-03-06 07:48:32 +00:00
{ ` "mytable" ` , ` "mytable" ` } ,
{ "myschema.mytable" , "myschema.mytable" } ,
{ "myschema.mytable" , "`myschema`.mytable" } ,
{ "myschema.mytable" , "myschema.`mytable`" } ,
{ "myschema.mytable" , "`myschema`.`mytable`" } ,
{ "myschema.mytable" , ` [myschema].mytable ` } ,
{ "myschema.mytable" , ` myschema.[mytable] ` } ,
{ "myschema.mytable" , ` [myschema].[mytable] ` } ,
{ ` "myschema.mytable" ` , ` "myschema.mytable" ` } ,
{ "message_user AS sender" , "`message_user` AS `sender`" } ,
{ "myschema.mytable AS table" , "myschema.mytable AS table" } ,
}
)
for _ , v := range kases {
t . Run ( v . value , func ( t * testing . T ) {
buf := & strings . Builder { }
2021-12-07 03:43:50 +00:00
err := quoter . QuoteTo ( buf , v . value )
assert . NoError ( t , err )
2020-03-06 07:48:32 +00:00
assert . EqualValues ( t , v . expected , buf . String ( ) )
} )
}
2019-11-12 00:48:20 +00:00
}
2020-02-26 12:45:10 +00:00
func TestJoin ( t * testing . T ) {
cols := [ ] string { "f1" , "f2" , "f3" }
2020-03-06 07:48:32 +00:00
quoter := Quoter { '[' , ']' , AlwaysReserve }
assert . EqualValues ( t , "[a],[b]" , quoter . Join ( [ ] string { "a" , " b" } , "," ) )
2020-02-26 12:45:10 +00:00
2020-09-06 08:22:34 +00:00
assert . EqualValues ( t , "[a].*,[b].[c]" , quoter . Join ( [ ] string { "a.*" , " b.c" } , "," ) )
2020-02-26 12:45:10 +00:00
assert . EqualValues ( t , "[f1], [f2], [f3]" , quoter . Join ( cols , ", " ) )
2020-03-06 07:48:32 +00:00
quoter . IsReserved = AlwaysNoReserve
2020-02-26 12:45:10 +00:00
assert . EqualValues ( t , "f1, f2, f3" , quoter . Join ( cols , ", " ) )
}
2020-02-27 00:34:16 +00:00
func TestStrings ( t * testing . T ) {
2020-09-06 08:22:34 +00:00
cols := [ ] string { "f1" , "f2" , "t3.f3" , "t4.*" }
2020-03-06 07:48:32 +00:00
quoter := Quoter { '[' , ']' , AlwaysReserve }
2020-02-27 00:34:16 +00:00
quotedCols := quoter . Strings ( cols )
2020-09-06 08:22:34 +00:00
assert . EqualValues ( t , [ ] string { "[f1]" , "[f2]" , "[t3].[f3]" , "[t4].*" } , quotedCols )
2020-02-27 00:34:16 +00:00
}
2020-02-27 01:30:06 +00:00
func TestTrim ( t * testing . T ) {
2020-02-27 05:49:43 +00:00
var kases = map [ string ] string {
"[table_name]" : "table_name" ,
"[schema].[table_name]" : "schema.table_name" ,
}
for src , dst := range kases {
assert . EqualValues ( t , src , CommonQuoter . Trim ( src ) )
2020-03-06 07:48:32 +00:00
assert . EqualValues ( t , dst , Quoter { '[' , ']' , AlwaysReserve } . Trim ( src ) )
2020-02-27 05:49:43 +00:00
}
2020-02-27 01:30:06 +00:00
}
2020-03-09 02:17:37 +00:00
func TestReplace ( t * testing . T ) {
q := Quoter { '[' , ']' , AlwaysReserve }
var kases = [ ] struct {
source string
expected string
} {
{
"SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?" ,
"SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE [TABLE_SCHEMA] = ? AND [TABLE_NAME] = ? AND [COLUMN_NAME] = ?" ,
} ,
{
"SELECT 'abc```test```''', `a` FROM b" ,
"SELECT 'abc```test```''', [a] FROM b" ,
} ,
{
"UPDATE table SET `a` = ~ `a`, `b`='abc`'" ,
"UPDATE table SET [a] = ~ [a], [b]='abc`'" ,
} ,
2021-08-24 05:46:08 +00:00
{
"INSERT INTO `insert_where` (`height`,`name`,`repo_id`,`width`,`index`) SELECT $1,$2,$3,$4,coalesce(MAX(`index`),0)+1 FROM `insert_where` WHERE (`repo_id`=$5)" ,
"INSERT INTO [insert_where] ([height],[name],[repo_id],[width],[index]) SELECT $1,$2,$3,$4,coalesce(MAX([index]),0)+1 FROM [insert_where] WHERE ([repo_id]=$5)" ,
} ,
2020-03-09 02:17:37 +00:00
}
for _ , kase := range kases {
t . Run ( kase . source , func ( t * testing . T ) {
assert . EqualValues ( t , kase . expected , q . Replace ( kase . source ) )
} )
}
}