token_test.go 6.5 KB
Newer Older
martianzhang's avatar
martianzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2018 Xiaomi, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ast

import (
	"fmt"
	"testing"

	"github.com/XiaoMi/soar/common"

	"github.com/kr/pretty"
)

martianzhang's avatar
martianzhang 已提交
28
func TestTokenize(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
29
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
30 31 32 33 34 35 36 37 38
	err := common.GoldenDiff(func() {
		for _, sql := range common.TestSQLs {
			fmt.Println(sql)
			fmt.Println(Tokenize(sql))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
	}
martianzhang's avatar
martianzhang 已提交
39
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
40 41 42
}

func TestTokenizer(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
43
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
44 45 46 47 48 49 50 51 52 53 54
	sqls := []string{
		"select c1,c2,c3 from t1,t2 join t3 on t1.c1=t2.c1 and t1.c3=t3.c1 where id>1000",
		"select sourcetable, if(f.lastcontent = ?, f.lastupdate, f.lastcontent) as lastactivity, f.totalcount as activity, type.class as type, (f.nodeoptions & ?) as nounsubscribe from node as f inner join contenttype as type on type.contenttypeid = f.contenttypeid inner join subscribed as sd on sd.did = f.nodeid and sd.userid = ? union all select f.name as title, f.userid as keyval, ? as sourcetable, ifnull(f.lastpost, f.joindate) as lastactivity, f.posts as activity, ? as type, ? as nounsubscribe from user as f inner join userlist as ul on ul.relationid = f.userid and ul.userid = ? where ul.type = ? and ul.aq = ? order by title limit ?",
		"select c1 from t1 where id>=1000", // test ">="
		"select SQL_CALC_FOUND_ROWS col from tbl where id>1000",
		"SELECT * FROM tb WHERE id=?;",
		"SELECT * FROM tb WHERE id is null;",
		"SELECT * FROM tb WHERE id is not null;",
		"SELECT * FROM tb WHERE id between 1 and 3;",
		"alter table inventory add index idx_store_film` (`store_id`,`film_id`);",
	}
martianzhang's avatar
martianzhang 已提交
55 56 57 58 59 60 61
	err := common.GoldenDiff(func() {
		for _, sql := range sqls {
			pretty.Println(Tokenizer(sql))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
62
	}
martianzhang's avatar
martianzhang 已提交
63
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
64 65 66
}

func TestGetQuotedString(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
67
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
	var str = []string{
		`"hello world"`,
		"`hello world`",
		`'hello world'`,
		"hello world",
		`'hello \'world'`,
		`"hello \"wor\"ld"`,
		`"hello \"world"`,
		`""`,
		`''`,
		"``",
		`'hello 'world'`,
		`"hello "world"`,
	}
martianzhang's avatar
martianzhang 已提交
82 83 84 85 86 87 88
	err := common.GoldenDiff(func() {
		for _, s := range str {
			fmt.Printf("orignal: %s\nquoted: %s\n", s, getQuotedString(s))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
89
	}
martianzhang's avatar
martianzhang 已提交
90
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
91 92 93
}

func TestCompress(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
94
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
95 96 97 98 99 100 101 102
	err := common.GoldenDiff(func() {
		for _, sql := range common.TestSQLs {
			fmt.Println(sql)
			fmt.Println(Compress(sql))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
103
	}
martianzhang's avatar
martianzhang 已提交
104
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
105 106 107
}

func TestFormat(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
108
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
109 110 111 112 113 114 115 116
	err := common.GoldenDiff(func() {
		for _, sql := range common.TestSQLs {
			fmt.Println(sql)
			fmt.Println(format(sql))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
117
	}
martianzhang's avatar
martianzhang 已提交
118
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
119 120 121
}

func TestSplitStatement(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
122
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
123 124 125 126 127 128 129 130
	bufs := [][]byte{
		[]byte("select * from test;hello"),
		[]byte("select 'asd;fas', col from test;hello"),
		[]byte("-- select * from test;hello"),
		[]byte("#select * from test;hello"),
		[]byte("select * /*comment*/from test;hello"),
		[]byte("select * /*comment;*/from test;hello"),
		[]byte(`select * /*comment
martianzhang's avatar
martianzhang 已提交
131 132
		;*/
		from test;hello`),
martianzhang's avatar
martianzhang 已提交
133
		[]byte(`select * from test`),
martianzhang's avatar
martianzhang 已提交
134 135 136 137 138 139
		// https://github.com/XiaoMi/soar/issues/66
		[]byte(`/*comment*/`),
		[]byte(`/*comment*/;`),
		[]byte(`--`),
		[]byte(`-- comment`),
		[]byte(`# comment`),
martianzhang's avatar
martianzhang 已提交
140
		// https://github.com/XiaoMi/soar/issues/116
martianzhang's avatar
martianzhang 已提交
141 142 143 144
		[]byte(`select
*
-- comment
from tb
martianzhang's avatar
martianzhang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
where col = 1`),
		[]byte(`select
* --
from tb
where col = 1`),
		[]byte(`select
* #
from tb
where col = 1`),
		[]byte(`select
*
--
from tb
martianzhang's avatar
martianzhang 已提交
158
where col = 1`),
martianzhang's avatar
martianzhang 已提交
159 160 161 162
		[]byte(`select * from
-- comment
tb;
select col from tb where col = 1;`),
martianzhang's avatar
martianzhang 已提交
163 164 165 166 167 168
		// https://github.com/XiaoMi/soar/issues/120
		[]byte(`
-- comment
select col from tb;
select col from tb;
`),
martianzhang's avatar
martianzhang 已提交
169 170 171
	}
	buf2s := [][]byte{
		[]byte("select * from test\\Ghello"),
172
		[]byte("select 'hello\\Gworld', col from test\\Ghello"),
martianzhang's avatar
martianzhang 已提交
173 174 175 176 177 178 179 180
		[]byte("-- select * from test\\Ghello"),
		[]byte("#select * from test\\Ghello"),
		[]byte("select * /*comment*/from test\\Ghello"),
		[]byte("select * /*comment;*/from test\\Ghello"),
		[]byte(`select * /*comment
        \\G*/
        from test\\Ghello`),
	}
martianzhang's avatar
martianzhang 已提交
181 182 183 184 185 186 187 188 189 190 191 192
	err := common.GoldenDiff(func() {
		for i, buf := range bufs {
			sql, _, _ := SplitStatement(buf, []byte(common.Config.Delimiter))
			fmt.Println(i, sql)
		}
		for i, buf := range buf2s {
			sql, _, _ := SplitStatement(buf, []byte(common.Config.Delimiter))
			fmt.Println(i, sql)
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
193
	}
martianzhang's avatar
martianzhang 已提交
194
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
195 196 197
}

func TestLeftNewLines(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
198
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
199 200 201 202 203 204 205 206
	bufs := [][]byte{
		[]byte(`
		select * from test;hello`),
		[]byte(`select * /*comment
        ;*/
        from test;hello`),
		[]byte(`select * from test`),
	}
martianzhang's avatar
martianzhang 已提交
207 208 209 210 211 212 213
	err := common.GoldenDiff(func() {
		for _, buf := range bufs {
			fmt.Println(LeftNewLines(buf))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
214
	}
martianzhang's avatar
martianzhang 已提交
215
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
216 217 218
}

func TestNewLines(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
219
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
220 221 222 223 224 225 226 227
	bufs := [][]byte{
		[]byte(`
		select * from test;hello`),
		[]byte(`select * /*comment
        ;*/
        from test;hello`),
		[]byte(`select * from test`),
	}
martianzhang's avatar
martianzhang 已提交
228 229 230 231 232 233 234
	err := common.GoldenDiff(func() {
		for _, buf := range bufs {
			fmt.Println(NewLines(buf))
		}
	}, t.Name(), update)
	if nil != err {
		t.Fatal(err)
martianzhang's avatar
martianzhang 已提交
235
	}
martianzhang's avatar
martianzhang 已提交
236
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
237
}