github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/db/tpl/tpl_analyze_test.go (about) 1 package tpl 2 3 import "testing" 4 5 //go test -coverprofile=cover.out github.com/zzkkff/lib4go/db/tpl 6 // cover -func=cover.out 7 8 func TestAnalyzeTPL(t *testing.T) { 9 10 input := make(map[string]interface{}) 11 input["name"] = "colin" 12 input["name2"] = "colin2" 13 input["name3_"] = "name3_" 14 f := func() string { 15 return ":" 16 } 17 18 //通用参数解析 19 tpls := map[string][]interface{}{ 20 `1where dual`: []interface{}{`1where dual`, 0}, 21 `2where name=@=`: []interface{}{`2where name=@=`, 0}, 22 `3where name=@(2`: []interface{}{`3where name=@(2`, 0}, 23 `4where name=@!`: []interface{}{`4where name=@!`, 0}, 24 `5where name=@@`: []interface{}{`5where name=@@`, 0}, 25 `6where name=@w`: []interface{}{`6where name=:`, 1}, 26 `7where name=@w id=@id`: []interface{}{`7where name=: id=:`, 2}, 27 `8where name=@w\r\n id=@id`: []interface{}{`8where name=:\r\n id=:`, 2}, 28 `9where id in(#ids)`: []interface{}{`9where id in(NULL)`, 0}, 29 `10where name='#name'`: []interface{}{`10where name='colin'`, 0}, 30 `11where id=0 &name`: []interface{}{`11where id=0 and name=:`, 1}, 31 `12where id=0 &id`: []interface{}{`12where id=0 `, 0}, 32 `13where id=0 |name`: []interface{}{`13where id=0 or name=:`, 1}, 33 `14where id=0 |id`: []interface{}{`14where id=0 `, 0}, 34 `15where id=0 !id`: []interface{}{`15where id=0 !id`, 0}, 35 `16set name=colin~id`: []interface{}{`16set name=colin`, 0}, 36 `17set id=0~name`: []interface{}{`17set id=0,name=:`, 1}, 37 /*add by champly 2016年11月9日11:53:35*/ 38 `18where name=@name3_`: []interface{}{`18where name=:`, 1}, 39 /*end*/ 40 } 41 42 for tpl, except := range tpls { 43 actual, params, _ := AnalyzeTPL(tpl, input, f) 44 if actual != except[0].(string) || len(params) != except[1].(int) { 45 t.Errorf("AnalyzeTPL解析参数有误:except:%s actual:%s", except[0].(string), actual) 46 } 47 } 48 49 //正确参数解析 50 tpl := "select seq_wxaccountmenu_auto_id.nextval from where name=@name2" 51 except := "select seq_wxaccountmenu_auto_id.nextval from where name=:" 52 actual, params, _ := AnalyzeTPL(tpl, input, f) 53 if actual != except || len(params) != 1 || params[0].(string) != input["name2"] { 54 t.Error("AnalyzeTPL解析参数有误") 55 } 56 57 //值不存在 58 tpl = "select seq_wxaccountmenu_auto_id.nextval from where name=@id" 59 except = "select seq_wxaccountmenu_auto_id.nextval from where name=:" 60 actual, params, _ = AnalyzeTPL(tpl, input, f) 61 if actual != except || len(params) != 1 || params[0] != nil { 62 t.Error("AnalyzeTPL解析参数有误") 63 } 64 65 //多个相同属性 66 tpl = "select seq_wxaccountmenu_auto_id.nextval from where name=@id and id=@id" 67 except = "select seq_wxaccountmenu_auto_id.nextval from where name=: and id=:" 68 actual, params, _ = AnalyzeTPL(tpl, input, f) 69 if actual != except || len(params) != 2 || params[0] != nil || params[1] != nil { 70 t.Error("AnalyzeTPL解析参数有误") 71 } 72 73 /*add by champly 2016年11月9日11:54:52*/ 74 // 多个不同的参数 75 tpl = "select seq_wxaccountmenu_auto_id.nextbal from where name=@name and name2='#name2' &name3_ |name ~name" 76 except = "select seq_wxaccountmenu_auto_id.nextbal from where name=: and name2='colin2' and name3_=: or name=: ,name=:" 77 actual, params, _ = AnalyzeTPL(tpl, input, f) 78 if actual != except || len(params) != 4 || params[0].(string) != input["name"] || params[1].(string) != input["name3_"] || params[2].(string) != input["name"] || params[3].(string) != input["name"] { 79 t.Error("AnalyzeTPL解析参数有误") 80 } 81 /*end*/ 82 83 }