Repository navigation
Expand file tree
/
Copy pathparser_regression_test.go
More file actions
135 lines (120 loc) · 3.31 KB
/
Copy pathparser_regression_test.go
File metadata and controls
135 lines (120 loc) · 3.31 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package parser
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/actiontech/mybatis-mapper-2-sql/ast"
"github.com/stretchr/testify/assert"
)
func TestParserSetWhereNoNullWHERE(t *testing.T) {
actual, err := ParseXML(`
<mapper namespace="Test">
<update id="updateToReverse">
UPDATE t_sample_record t
<set>t.cancelled_at = null</set>
<where>
<trim prefix="(" suffix=")">t.record_id=?</trim>
</where>
</update>
</mapper>`)
if !assert.NoError(t, err) {
return
}
assert.NotContains(t, actual, "nullWHERE")
assert.Contains(t, actual, "NULL")
assert.Contains(t, actual, "WHERE")
}
func TestParserWhereIfNoGlue(t *testing.T) {
actual, err := ParseXML(`
<mapper namespace="Test">
<select id="testWhereIfGlue">
SELECT 1 FROM t
<where>
<if test="a != null">a=?</if>
<if test="b != null">AND b=?</if>
</where>
</select>
</mapper>`)
if !assert.NoError(t, err) {
return
}
assert.NotContains(t, actual, "?AND")
assert.Contains(t, actual, "`a`=?")
assert.Contains(t, actual, "AND `b`=?")
}
func TestParserSelectUnchanged(t *testing.T) {
testParser(t,
`
<mapper namespace="Test">
<select id="testSelect">
SELECT name FROM fruits WHERE id = #{id}
</select>
</mapper>`,
"SELECT `name` FROM `fruits` WHERE `id`=?;",
)
}
func xmlTestDataDir(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !assert.True(t, ok) {
t.FailNow()
}
return filepath.Join(filepath.Dir(file), "testdata", "xml")
}
func readXMLTestFile(t *testing.T, name string) string {
t.Helper()
content, err := os.ReadFile(filepath.Join(xmlTestDataDir(t), name))
if !assert.NoError(t, err) {
t.FailNow()
}
return string(content)
}
func findStmtByStartLine(stmts []ast.StmtInfo, startLine uint64) (ast.StmtInfo, bool) {
for _, stmt := range stmts {
if stmt.StartLine == startLine {
return stmt, true
}
}
return ast.StmtInfo{}, false
}
func TestParseXMLsRegressionSetWhereMapper(t *testing.T) {
content := readXMLTestFile(t, "regression_set_where_mapper.xml")
stmts, err := ParseXMLs([]XmlFile{{FilePath: "regression_set_where_mapper.xml", Content: content}})
if !assert.NoError(t, err) {
return
}
assert.Len(t, stmts, 23)
updateToReverse, ok := findStmtByStartLine(stmts, 1294)
if !assert.True(t, ok) {
return
}
assert.NotContains(t, updateToReverse.SQL, "nullWHERE")
assert.Contains(t, updateToReverse.SQL, "NULL")
assert.Contains(t, updateToReverse.SQL, "WHERE")
for _, stmt := range stmts {
assert.NotContains(t, stmt.SQL, "nullWHERE")
}
}
func TestParseXMLsRegressionBasicMapper(t *testing.T) {
content := readXMLTestFile(t, "regression_basic_mapper.xml")
stmts, err := ParseXMLs([]XmlFile{{FilePath: "regression_basic_mapper.xml", Content: content}})
if !assert.NoError(t, err) {
return
}
assert.Len(t, stmts, 10)
for _, stmt := range stmts {
assert.NotEmpty(t, strings.TrimSpace(stmt.SQL))
assert.NotContains(t, stmt.SQL, "nullWHERE")
}
testParameters, ok := findStmtByStartLine(stmts, 16)
if assert.True(t, ok) {
assert.Contains(t, testParameters.SQL, "`category`=?")
assert.Contains(t, testParameters.SQL, "`price`>?")
}
testChoose, ok := findStmtByStartLine(stmts, 96)
if assert.True(t, ok) {
assert.Contains(t, testChoose.SQL, "`category`=\"apple\"")
}
}