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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuoteTo(t *testing.T) {
|
2020-02-25 00:01:36 +00:00
|
|
|
var quoter = Quoter{"[", "]"}
|
2019-11-12 00:48:20 +00:00
|
|
|
|
|
|
|
test := func(t *testing.T, expected string, value string) {
|
|
|
|
buf := &strings.Builder{}
|
2020-02-25 00:01:36 +00:00
|
|
|
quoter.QuoteTo(buf, value)
|
2019-11-12 00:48:20 +00:00
|
|
|
assert.EqualValues(t, expected, buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
test(t, "[mytable]", "mytable")
|
|
|
|
test(t, "[mytable]", "`mytable`")
|
|
|
|
test(t, "[mytable]", `[mytable]`)
|
|
|
|
|
|
|
|
test(t, `["mytable"]`, `"mytable"`)
|
|
|
|
|
|
|
|
test(t, "[myschema].[mytable]", "myschema.mytable")
|
|
|
|
test(t, "[myschema].[mytable]", "`myschema`.mytable")
|
|
|
|
test(t, "[myschema].[mytable]", "myschema.`mytable`")
|
|
|
|
test(t, "[myschema].[mytable]", "`myschema`.`mytable`")
|
|
|
|
test(t, "[myschema].[mytable]", `[myschema].mytable`)
|
|
|
|
test(t, "[myschema].[mytable]", `myschema.[mytable]`)
|
|
|
|
test(t, "[myschema].[mytable]", `[myschema].[mytable]`)
|
|
|
|
|
|
|
|
test(t, `["myschema].[mytable"]`, `"myschema.mytable"`)
|
|
|
|
|
2020-02-25 00:01:36 +00:00
|
|
|
test(t, "[message_user] AS [sender]", "`message_user` AS `sender`")
|
|
|
|
|
|
|
|
assert.EqualValues(t, "[a],[b]", quoter.Join([]string{"a", " b"}, ","))
|
|
|
|
|
2019-11-12 00:48:20 +00:00
|
|
|
buf := &strings.Builder{}
|
2020-02-25 00:01:36 +00:00
|
|
|
quoter = Quoter{"", ""}
|
|
|
|
quoter.QuoteTo(buf, "noquote")
|
2019-11-12 00:48:20 +00:00
|
|
|
assert.EqualValues(t, "noquote", buf.String())
|
|
|
|
}
|
2020-02-26 12:45:10 +00:00
|
|
|
|
|
|
|
func TestJoin(t *testing.T) {
|
|
|
|
cols := []string{"f1", "f2", "f3"}
|
|
|
|
quoter := Quoter{"[", "]"}
|
|
|
|
|
|
|
|
assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", "))
|
|
|
|
|
|
|
|
quoter = Quoter{"", ""}
|
|
|
|
assert.EqualValues(t, "f1, f2, f3", quoter.Join(cols, ", "))
|
|
|
|
}
|
2020-02-27 00:34:16 +00:00
|
|
|
|
|
|
|
func TestStrings(t *testing.T) {
|
|
|
|
cols := []string{"f1", "f2", "t3.f3"}
|
|
|
|
quoter := Quoter{"[", "]"}
|
|
|
|
|
|
|
|
quotedCols := quoter.Strings(cols)
|
|
|
|
assert.EqualValues(t, []string{"[f1]", "[f2]", "[t3].[f3]"}, quotedCols)
|
|
|
|
}
|
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))
|
|
|
|
assert.EqualValues(t, dst, Quoter{"[", "]"}.Trim(src))
|
|
|
|
}
|
2020-02-27 01:30:06 +00:00
|
|
|
}
|