-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate_test.go
99 lines (89 loc) · 3.39 KB
/
translate_test.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package dingo
import (
"github.com/stretchr/testify/require"
"testing"
)
func Test_translate_sql_with_arg(t *testing.T) {
should := require.New(t)
translatedSql := Translate(
"SELECT * FROM account_010 WHERE passenger_id=:pid AND driver_id=:did")
should.Equal("SELECT * FROM account_010 WHERE passenger_id=? AND driver_id=?", translatedSql.sql)
should.Equal(map[string][]int{
"pid": {0},
"did": {1},
}, translatedSql.paramMap)
should.Equal(2, translatedSql.totalParamCount)
}
func Test_translate_sql_with_sub(t *testing.T) {
should := require.New(t)
translatedSql := Translate(
"SELECT * FROM account_:STR_district WHERE passenger_id=:pid AND driver_id=:did")
should.Equal("SELECT * FROM account_%v WHERE passenger_id=? AND driver_id=?", translatedSql.sql)
should.Equal(map[string][]int{
"pid": {1},
"did": {2},
"STR_district": {0},
}, translatedSql.paramMap)
should.Equal(1, translatedSql.strParamCount)
}
func Test_translate_UPDATE_COLUMNS(t *testing.T) {
should := require.New(t)
translatedSql := Translate(
"UPDATE account_:STR_district SET "+
"_modify_time=now(), :UPDATE_COLUMNS WHERE order_id=:oid", "passenger_count")
should.Equal("UPDATE account_%v SET _modify_time=now(), passenger_count=? WHERE order_id=?", translatedSql.sql)
should.Equal(map[string][]int{
"passenger_count": {1},
"oid": {2},
"STR_district": {0},
}, translatedSql.paramMap)
should.Equal(1, translatedSql.strParamCount)
}
func Test_translate_INSERT_COLUMNS(t *testing.T) {
should := require.New(t)
translatedSql := Translate(
`INSERT test :INSERT_COLUMNS`, "name")
should.Equal(`INSERT test (name) VALUES (?)`, translatedSql.sql)
should.Equal(map[string][]int{
"name": {0},
}, translatedSql.paramMap)
should.Equal(1, translatedSql.totalParamCount)
}
func Test_translate_sql_with_duplicated_args(t *testing.T) {
should := require.New(t)
translatedSql := Translate(
"SELECT * FROM account_010 WHERE passenger_id=:pid AND passenger_id=:pid")
should.Equal("SELECT * FROM account_010 WHERE passenger_id=? AND passenger_id=?", translatedSql.sql)
should.Equal(map[string][]int{
"pid": {0, 1},
}, translatedSql.paramMap)
should.Equal(2, translatedSql.totalParamCount)
}
func Test_translate_skip_quote(t *testing.T) {
should := require.New(t)
should.Equal("SELECT * FROM account_010 WHERE passenger_id=':pid'", Translate(
`SELECT * FROM account_010 WHERE passenger_id=':pid'`).sql)
should.Equal(`SELECT * FROM account_010 WHERE passenger_id='\':pid'`, Translate(
`SELECT * FROM account_010 WHERE passenger_id='\':pid'`).sql)
should.Equal(`SELECT * FROM account_010 WHERE passenger_id=":pid"`, Translate(
`SELECT * FROM account_010 WHERE passenger_id=":pid"`).sql)
should.Equal(`SELECT * FROM account_010 WHERE passenger_id="\":pid"`, Translate(
`SELECT * FROM account_010 WHERE passenger_id="\":pid"`).sql)
}
func Test_translate_column_group(t *testing.T) {
should := require.New(t)
should.Equal("(a, b) VALUES (?, ?) (c, d) VALUES (?, ?)", Translate(
`:INSERT_COLUMNS1 :INSERT_COLUMNS2`,
Columns("COLUMNS1", "a", "b"),
Columns("COLUMNS2", "c", "d")).sql)
}
func Test_translate_SELECT_COLUMNS(t *testing.T) {
should := require.New(t)
should.Equal("a, b", Translate(
`:SELECT_COLUMNS`, "a", "b").sql)
}
func Test_translate_HINT_COLUMNS(t *testing.T) {
should := require.New(t)
should.Equal(`/*{"a":"%v","b":"%v"}*/`, Translate(
`:HINT_COLUMNS`, "a", "b").sql)
}