代码拉取完成,页面将自动刷新
同步操作将从 unsafe-rust/sq 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。