yintai2/app/common/dao/base_test.go

74 lines
3.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dao
import "testing"
func TestEzSort(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "empty", in: "", want: ""},
{name: "spaces", in: " ", want: ""},
{name: "tabs", in: "\t\t", want: ""},
{name: "comma only", in: ",", want: ""},
{name: "direction only", in: "desc", want: ""},
{name: "column only", in: "pinned", want: ""},
{name: "one column desc", in: "pinned desc", want: "`pinned` DESC"},
{name: "one column asc", in: "weight asc", want: "`weight` ASC"},
{name: "direction uppercase", in: "pinned DESC", want: "`pinned` DESC"},
{name: "direction mixed case", in: "pinned DeSc", want: "`pinned` DESC"},
{name: "column case kept", in: "Pinned desc", want: "`Pinned` DESC"},
{name: "underscore column", in: "_pinned desc", want: "`_pinned` DESC"},
{name: "digit in column", in: "weight2 desc", want: "`weight2` DESC"},
{name: "extra inner spaces", in: "pinned desc", want: "`pinned` DESC"},
{name: "leading and trailing spaces", in: " pinned desc ", want: "`pinned` DESC"},
{name: "tab between column and direction", in: "pinned\tdesc", want: "`pinned` DESC"},
{name: "two columns", in: "pinned desc, weight desc", want: "`pinned` DESC,`weight` DESC"},
{name: "mixed directions", in: "pinned asc, weight desc", want: "`pinned` ASC,`weight` DESC"},
{name: "no space after comma", in: "pinned desc,weight desc", want: "`pinned` DESC,`weight` DESC"},
{name: "spaces around comma", in: " pinned desc , weight ASC ", want: "`pinned` DESC,`weight` ASC"},
{name: "three columns", in: "a desc, b asc, c_2 DESC", want: "`a` DESC,`b` ASC,`c_2` DESC"},
{name: "trailing comma", in: "pinned desc,", want: ""},
{name: "leading comma", in: ",pinned desc", want: ""},
{name: "double comma", in: "pinned desc,,weight desc", want: ""},
{name: "second missing direction", in: "pinned desc, weight", want: ""},
{name: "first missing direction", in: "pinned, weight desc", want: ""},
{name: "extra token", in: "pinned desc extra", want: ""},
{name: "extra token on second", in: "pinned desc, weight desc extra", want: ""},
{name: "direction glued to column", in: "pinned_desc", want: ""},
{name: "column starts with digit", in: "1pinned desc", want: ""},
{name: "dot qualifier", in: "yt_page.id desc", want: ""},
{name: "hyphen", in: "pinned-at desc", want: ""},
{name: "unicode column", in: "置顶 desc", want: ""},
{name: "fullwidth comma", in: "pinned desc,weight desc", want: ""},
{name: "star", in: "* desc", want: ""},
{name: "function call", in: "count(*) desc", want: ""},
{name: "parentheses", in: "id) desc", want: ""},
{name: "single quote", in: "id' desc", want: ""},
{name: "double quote", in: "id\" desc", want: ""},
{name: "backslash", in: "id\\ desc", want: ""},
{name: "null byte", in: "id\x00 desc", want: ""},
{name: "semicolon", in: "id desc; drop table yt_page", want: ""},
{name: "comment", in: "id desc--", want: ""},
{name: "hash comment", in: "id desc#", want: ""},
{name: "comma expression", in: "id DESC,extractvalue(1,concat(0x7e,user()))", want: ""},
{name: "backtick breakout", in: "id`,extractvalue(1,concat(0x7e,user())) DESC", want: ""},
{name: "space before breakout", in: "id desc, extractvalue(1,concat(0x7e,user()))", want: ""},
{name: "newline payload", in: "id desc\nextractvalue()", want: ""},
{name: "invalid direction", in: "pinned drop", want: ""},
{name: "direction with junk", in: "pinned desc,", want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ezSort(tt.in); got != tt.want {
t.Fatalf("ezSort(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}