gosora/query_gen/utils_test.go
Azareal ea1037bd63 track favicon stats
experiment with tracking average route performance
temporary error route stub
optimise dumprequest
add DisableAnalytics config setting
fix double hyphens in slugs being mistaken for sql injection
more querygen tests

You wil need to run the updater / patcher for this commit.
2020-02-26 20:34:38 +10:00

72 lines
2.4 KiB
Go

package qgen
import (
"strings"
"testing"
)
type MT struct {
Type int
Contents string
}
func expectTokens(t *testing.T, whs []DBWhere, tokens ...MT) {
i := 0
for _, wh := range whs {
for _, expr := range wh.Expr {
if expr.Type != tokens[i].Type || expr.Contents != tokens[i].Contents {
t.Fatalf("token mismatch: %+v - %+v\n", expr, tokens[i])
}
i++
}
}
}
func TestProcessWhere(t *testing.T) {
whs := processWhere("uid = ?")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenSub, "?"})
whs = processWhere("uid = 1")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "1"})
whs = processWhere("uid = 0")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "0"})
whs = processWhere("uid = '1'")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "1"})
whs = processWhere("uid = 't'")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "t"})
whs = processWhere("uid = ''")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, ""})
whs = processWhere("uid = '")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, ""})
whs = processWhere("uid=?")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenSub, "?"})
whs = processWhere("uid=1")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "1"})
whs = processWhere("uid=0")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "0"})
whs = processWhere("uid=20")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenNumber, "20"})
whs = processWhere("uid='1'")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "1"})
whs = processWhere("uid='t'")
expectTokens(t, whs, MT{TokenColumn, "uid"}, MT{TokenOp, "="}, MT{TokenString, "t"})
whs = processWhere("uid")
expectTokens(t, whs, MT{TokenColumn, "uid"})
}
func TestMySQLBuildWhere(t *testing.T) {
a := &MysqlAdapter{Name: "mysql", Buffer: make(map[string]DBStmt)}
reap := func(wh, ex string) {
sb := &strings.Builder{}
a.buildWhere(wh, sb)
res := sb.String()
if res != ex {
t.Fatalf("build where mismatch: '%+v' - '%+v'\n", ex, res)
}
}
reap("uid = 0", " WHERE `uid`= 0 ")
reap("uid = '0'", " WHERE `uid`= '0'")
reap("uid=0", " WHERE `uid`= 0 ")
}