-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsql_source_test.go
103 lines (92 loc) · 2.73 KB
/
sql_source_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
100
101
102
103
package gobatis
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestStaticSqlSource_getBoundSql(t *testing.T) {
sss := &staticSqlSource{
sqlStr: "select * from t_gap where id = #{id} and gap = #{gap}",
paramMappings: make([]string, 0),
}
bs := sss.getBoundSql(map[string]interface{}{
"id": 1,
"gap": 10,
})
expc := "select * from t_gap where id = ? and gap = ?"
assert.Equal(t, bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assert.Equal(t, bs.params["id"], 1, "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assert.Equal(t, bs.params["gap"], 10, "test failed, actual:"+fmt.Sprintf("%d", bs.params["gap"]))
}
func TestDynamicSqlSource_getBoundSql(t *testing.T) {
params := map[string]interface{}{
"name": "Sean",
"age": 18,
"code": 18,
"array": []map[string]interface{}{{"idea": "11"}, {"idea": "22"}, {"idea": "33"}},
"array1": []string{"11", "22", "33"},
"array2": []s{{A: "aa"}, {A: "bb"}, {A: "cc"}},
}
msn := &mixedSqlNode{
sqlNodes: []iSqlNode{
&textSqlNode{
content: "select 1 from t_gap ",
},
&whereSqlNode{
sqlNodes: []iSqlNode{
&trimSqlNode{
prefixOverrides: "and",
sqlNodes: []iSqlNode{
&ifSqlNode{
test: "age == 18",
sqlNode: &textSqlNode{
content: "and age = #{age}",
},
},
&ifSqlNode{
test: "name == 'Sean'",
sqlNode: &textSqlNode{
content: "and name = #{name}",
},
},
},
},
&chooseNode{
sqlNodes: []iSqlNode{
&ifSqlNode{
test: "code == 18",
sqlNode: &textSqlNode{
content: "and code = 'cctv'",
},
},
},
},
},
},
&foreachSqlNode{
sqlNode: &mixedSqlNode{
sqlNodes: []iSqlNode{
&textSqlNode{
content: "#{ item.A }",
},
},
},
item: "item",
open: "and id in (",
close: ")",
separator: ",",
collection: "array2",
},
},
}
ds := dynamicSqlSource{
sqlNode: msn,
}
bs := ds.getBoundSql(params)
expc := "select 1 from t_gap where age = ? and name = ? and code = 'cctv' and id in ( ? , ? , ? )"
assert.Equal(t, bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assert.Equal(t, bs.params["name"], "Sean", "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assert.Equal(t, bs.extParams["_ls_item_p_item0.A"], "aa", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item0.A"]))
assert.Equal(t, bs.extParams["_ls_item_p_item1.A"], "bb", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item1.A"]))
assert.Equal(t, bs.extParams["_ls_item_p_item2.A"], "cc", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item2.A"]))
}