Skip to content

Commit

Permalink
add uint test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjjian committed Apr 7, 2022
1 parent fc7a0f8 commit 53012e7
Showing 1 changed file with 260 additions and 0 deletions.
260 changes: 260 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,263 @@ func TestParserSQLRefIdNotFound(t *testing.T) {
t.Errorf("actual error is [%s]", err.Error())
}
}

func testParserQuery(t *testing.T, skipError bool, xmlData string, expect []string) {
actual, err := ParseXMLQuery(xmlData, skipError)
if err != nil {
t.Errorf("parse error: %v", err)
return
}
if len(actual) != len(expect) {
t.Errorf("the length of actual is not the same as the length of expected, actual length is %d, expect is %d",
len(actual), len(expect))
return
}
for i := range actual {
if actual[i] != expect[i] {
t.Errorf("\nexpect[%d]: [%s]\nactual[%d]: [%s]", i, expect, i, actual)
}
}

}

func TestParserQueryFullFile(t *testing.T) {
testParserQuery(t, false,
`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Test">
<sql id="sometable">
fruits
</sql>
<sql id="somewhere">
WHERE
category = #{category}
</sql>
<sql id="someinclude">
FROM
<include refid="${include_target}"/>
<include refid="somewhere"/>
</sql>
<select id="testParameters">
SELECT
name,
category,
price
FROM
fruits
WHERE
category = #{category}
AND price > ${price}
</select>
<select id="testInclude">
SELECT
name,
category,
price
<include refid="someinclude">
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>
<select id="testIf">
SELECT
name,
category,
price
FROM
fruits
WHERE
1=1
<if test="category != null and category !=''">
AND category = #{category}
</if>
<if test="price != null and price !=''">
AND price = ${price}
<if test="price >= 400">
AND name = 'Fuji'
</if>
</if>
</select>
<select id="testTrim">
SELECT
name,
category,
price
FROM
fruits
<trim prefix="WHERE" prefixOverrides="AND|OR">
OR category = 'apple'
OR price = 200
</trim>
</select>
<select id="testWhere">
SELECT
name,
category,
price
FROM
fruits
<where>
AND category = 'apple'
<if test="price != null and price !=''">
AND price = ${price}
</if>
</where>
</select>
<update id="testSet">
UPDATE
fruits
<set>
<if test="category != null and category !=''">
category = #{category},
</if>
<if test="price != null and price !=''">
price = ${price},
</if>
</set>
WHERE
name = #{name}
</update>
<select id="testChoose">
SELECT
name,
category,
price
FROM
fruits
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<when test="category == 'banana'">
AND category = #{category}
<if test="price != null and price !=''">
AND price = ${price}
</if>
</when>
<otherwise>
AND category = 'apple'
</otherwise>
</choose>
</where>
</select>
<select id="testForeach">
SELECT
name,
category,
price
FROM
fruits
<where>
category = 'apple' AND
<foreach collection="apples" item="name" open="(" close=")" separator="OR">
<if test="name == 'Jonathan' or name == 'Fuji'">
name = #{name}
</if>
</foreach>
</where>
</select>
<insert id="testInsertMulti">
INSERT INTO
fruits
(
name,
category,
price
)
VALUES
<foreach collection="fruits" item="fruit" separator=",">
(
#{fruit.name},
#{fruit.category},
${fruit.price}
)
</foreach>
</insert>
<select id="testBind">
<bind name="likeName" value="'%' + name + '%'"/>
SELECT
name,
category,
price
FROM
fruits
WHERE
name like #{likeName}
</select>
</mapper>`,
[]string{
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `category`=? AND `price`>?",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `category`=?",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE 1=1 AND `category`=? AND `price`=? AND `name`=\"Fuji\"",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `category`=\"apple\" OR `price`=200",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `category`=\"apple\" AND `price`=?",
"UPDATE `fruits` SET `category`=?, `price`=? WHERE `name`=?",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `name`=? AND `category`=? AND `price`=? AND `category`=\"apple\"",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `category`=\"apple\" AND (`name`=? OR `name`=?)",
"INSERT INTO `fruits` (`name`,`category`,`price`) VALUES (?,?,?),(?,?,?)",
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `name` LIKE ?",
})
}

func TestParserQueryHasInvalidQuery(t *testing.T) {
_, err := ParseXMLQuery(
`
<mapper namespace="Test">
<sql id="someinclude">
*
</sql>
<select id="testBind">
<bind name="likeName" value="'%' + name + '%'"/>
SELECT
name,
category,
price
FROM
fruits
WHERE
name like #{likeName}
</select>
<select id="select" resultType="map">
select
<include refid="someinclude2" />
from t
</select>
</mapper>`, false)
if err == nil {
t.Errorf("expect has error, but no error")
}
if err.Error() != "sql someinclude2 is not exist" {
t.Errorf("actual error is [%s]", err.Error())
}
}

func TestParserQueryHasInvalidQueryButSkip(t *testing.T) {
testParserQuery(t, true,
`
<mapper namespace="Test">
<sql id="someinclude">
*
</sql>
<select id="testBind">
<bind name="likeName" value="'%' + name + '%'"/>
SELECT
name,
category,
price
FROM
fruits
WHERE
name like #{likeName}
</select>
<select id="select" resultType="map">
select
<include refid="someinclude2" />
from t
</select>
</mapper>`, []string{
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `name` LIKE ?",
})
}

0 comments on commit 53012e7

Please sign in to comment.