1 Star 0 Fork 1

winie/sq

forked from unsafe-rust/sq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
5_sql_builder_test.go 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
unsafe-rust 提交于 2021-03-28 12:21 . update
package sq
import (
"fmt"
"testing"
)
func TestExpr(t *testing.T) {
b := &SqlBuilder{
table: "users",
dialect: mustGetDialect("mysql"),
}
q := b.updateString(map[string]interface{}{
"id": 2,
"count": Expr("count + ?", 1),
})
//fmt.Println(q, b.args)
if q != "UPDATE `users` SET `count`=count + ?,`id`=?;" {
t.Error("Expr error,get:", q)
}
if len(b.args) != 2 {
t.Error("Expr args count error")
}
}
func TestSQLBuilder_queryString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
order: "id desc",
limit: "0",
offset: "10",
}
b.Where("id = ?", 1)
if b.queryString() != "SELECT * FROM `users` WHERE (id = ?) ORDER BY id desc LIMIT 0 OFFSET 10;" {
t.Error("sql builder query error", b.queryString())
}
fmt.Println(b.queryString())
}
func TestSQLBuilder_queryForceIndexString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
order: "id desc",
forceIndex: "idx_user",
limit: "0",
offset: "10",
}
b.Where("id = ?", 1)
if b.queryString() != "SELECT * FROM `users` force index(idx_user) WHERE (id = ?) ORDER BY id desc LIMIT 0 OFFSET 10;" {
t.Error("sql builder query error", b.queryString())
}
fmt.Println(b.queryString())
}
func TestSQLBuilder_insertString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
}
query := b.insertString(map[string]interface{}{
"id": 1,
"name": "test",
"email": "test@test.com",
"created_at": "2018-07-11 11:58:21",
"updated_at": "2018-07-11 11:58:21",
})
if query != "INSERT INTO `users` (`created_at`,`email`,`id`,`name`,`updated_at`) VALUES(?,?,?,?,?);" {
t.Error("sql builder insert error", query)
}
}
func TestSQLBuilder_updateString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
}
b.Where("id = ?", 1)
query := b.updateString(map[string]interface{}{
"name": "test",
"email": "test@test.com",
})
if query != "UPDATE `users` SET `email`=?,`name`=? WHERE (id = ?);" {
t.Error("sql builder update error", query)
}
fmt.Println(query, b.args)
}
func TestSQLBuilder_deleteString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
}
b.Where("id = ?", 1)
query := b.deleteString()
if query != "DELETE FROM `users` WHERE (id = ?);" {
t.Error("sql builder delete error", query)
}
}
func TestSQLBuilder_countString(t *testing.T) {
b := &SqlBuilder{
dialect: mustGetDialect("mysql"),
table: "users",
}
b.Where("id = ?", 1)
query := b.countString()
if query != "SELECT count(*) FROM `users` WHERE (id = ?);" {
t.Error("sql builder count error", query)
}
}
func TestSQLBuilder_Dialect(t *testing.T) {
testData := map[string]string{
"mysql": "INSERT INTO `users` (`created_at`,`email`,`id`,`name`,`updated_at`) VALUES(?,?,?,?,?);",
"postgres": `INSERT INTO "users" ("created_at","email","id","name","updated_at") VALUES(?,?,?,?,?);`,
"sqlite3": `INSERT INTO "users" ("created_at","email","id","name","updated_at") VALUES(?,?,?,?,?);`,
}
for k, v := range testData {
b := &SqlBuilder{
dialect: mustGetDialect(k),
table: "users",
}
query := b.insertString(map[string]interface{}{
"id": 1,
"name": "test",
"email": "test@test.com",
"created_at": "2018-07-11 11:58:21",
"updated_at": "2018-07-11 11:58:21",
})
if query != v {
t.Error(fmt.Sprintf("sql builder %s dialect insert error", k), query)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/winie_admin/sq.git
git@gitee.com:winie_admin/sq.git
winie_admin
sq
sq
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385