github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/db/tpl/tpl_analyze_test.go (about) 1 package tpl 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 //go test -coverprofile=cover.out github.com/zzkkff/library/db/tpl 9 // cover -func=cover.out 10 11 func TestAnalyzeTPL(t *testing.T) { 12 13 input := make(map[string]interface{}) 14 input["name"] = "colin" 15 input["name2"] = "colin2" 16 input["name3_"] = "name3_" 17 f := func() string { 18 return ":" 19 } 20 21 //通用参数解析 22 tpls := map[string][]interface{}{ 23 `1where dual`: []interface{}{`1where dual`, 0}, 24 `2where name=@=`: []interface{}{`2where name=@=`, 0}, 25 `3where name=@(2`: []interface{}{`3where name=@(2`, 0}, 26 `4where name=@!`: []interface{}{`4where name=@!`, 0}, 27 `5where name=@@`: []interface{}{`5where name=@@`, 0}, 28 `6where name=@w`: []interface{}{`6where name=:`, 1}, 29 `7where name=@w id=@id`: []interface{}{`7where name=: id=:`, 2}, 30 `8where name=@w\r\n id=@id`: []interface{}{`8where name=:\r\n id=:`, 2}, 31 `9where id in(#ids)`: []interface{}{`9where id in(NULL)`, 0}, 32 `10where name='#name'`: []interface{}{`10where name='colin'`, 0}, 33 `11where id=0 &name`: []interface{}{`11where id=0 and name=:`, 1}, 34 `12where id=0 &id`: []interface{}{`12where id=0 `, 0}, 35 `13where id=0 |name`: []interface{}{`13where id=0 or name=:`, 1}, 36 `14where id=0 |id`: []interface{}{`14where id=0 `, 0}, 37 `15where id=0 !id`: []interface{}{`15where id=0 !id`, 0}, 38 `16set name=colin~id`: []interface{}{`16set name=colin`, 0}, 39 `17set id=0~name`: []interface{}{`17set id=0,name=:`, 1}, 40 /*add by champly 2016年11月9日11:53:35*/ 41 `18where name=@name3_`: []interface{}{`18where name=:`, 1}, 42 `19where name=@t.name3_`: []interface{}{`19where name=:`, 1}, 43 `20where name=$name3_`: []interface{}{`20where name=name3_`, 0}, 44 `21where ?name`: []interface{}{`21where name like '%:%'`, 1}, 45 // `22where >name`: []interface{}{`22where name > :`, 1}, 46 // `23where <name`: []interface{}{`23where name < :`, 1}, 47 `24where \<name`: []interface{}{`24where <name`, 0}, 48 `25where \@name`: []interface{}{`25where @name`, 0}, 49 `26where &t.name`: []interface{}{`26where t.name=:`, 1}, 50 `27where |t.name`: []interface{}{`27where t.name=:`, 1}, 51 `28where &t.sex`: []interface{}{`28where `, 0}, 52 `29where name=@t.name`: []interface{}{`29where name=:`, 1}, 53 `30where name=#t.name`: []interface{}{`30where name=colin`, 0}, 54 // `31where >t.name`: []interface{}{`31where and t.name > :`, 1}, 55 `32where ~t.name`: []interface{}{`32where ,t.name=:`, 1}, 56 `33where &p.name`: []interface{}{`33where p.name=:`, 1}, 57 `34where [p.name`: []interface{}{`34where p.name>=:`, 1}, 58 `35where ]p.name`: []interface{}{`35where p.name<=:`, 1}, 59 /*end*/ 60 } 61 62 for tpl, except := range tpls { 63 actual, params, _ := AnalyzeTPL(tpl, input, f) 64 if actual != except[0].(string) || len(params) != except[1].(int) { 65 t.Errorf("AnalyzeTPL解析参数有误:except:%s actual:%s", except[0].(string), actual) 66 } 67 } 68 69 //正确参数解析 70 tpl := "select seq_wxaccountmenu_auto_id.nextval from where name=@name2" 71 except := "select seq_wxaccountmenu_auto_id.nextval from where name=:" 72 actual, params, _ := AnalyzeTPL(tpl, input, f) 73 if actual != except || len(params) != 1 || params[0].(string) != input["name2"] { 74 t.Error("AnalyzeTPL解析参数有误") 75 } 76 77 //值不存在 78 tpl = "select seq_wxaccountmenu_auto_id.nextval from where name=@id" 79 except = "select seq_wxaccountmenu_auto_id.nextval from where name=:" 80 actual, params, _ = AnalyzeTPL(tpl, input, f) 81 if actual != except || len(params) != 1 || params[0] != nil { 82 t.Error("AnalyzeTPL解析参数有误") 83 } 84 85 //多个相同属性 86 tpl = "select seq_wxaccountmenu_auto_id.nextval from where name=@id and id=@id" 87 except = "select seq_wxaccountmenu_auto_id.nextval from where name=: and id=:" 88 actual, params, _ = AnalyzeTPL(tpl, input, f) 89 if actual != except || len(params) != 2 || params[0] != nil || params[1] != nil { 90 t.Error("AnalyzeTPL解析参数有误") 91 } 92 93 /*add by champly 2016年11月9日11:54:52*/ 94 // 多个不同的参数 95 tpl = "select seq_wxaccountmenu_auto_id.nextbal from where name=@name and name2='#name2' &name3_ |name ~name [name ]name" 96 except = "select seq_wxaccountmenu_auto_id.nextbal from where name=: and name2='colin2' and name3_=: or name=: ,name=: and name>=: and name<=:" 97 actual, params, _ = AnalyzeTPL(tpl, input, f) 98 99 if actual != except || len(params) != 6 || params[0].(string) != input["name"] || params[1].(string) != input["name3_"] || params[2].(string) != input["name"] || params[3].(string) != input["name"] { 100 t.Error("AnalyzeTPL解析参数有误") 101 } 102 /*end*/ 103 104 } 105 106 func TestAnalyzeTPL2(t *testing.T) { 107 input := map[string]interface{}{ 108 "gender": "女", 109 "name": "", 110 } 111 f := func() string { 112 return "?" 113 } 114 115 tpl := `select * from students where gender = @gender ?name` 116 117 actual, params, _ := AnalyzeTPL(tpl, input, f) 118 fmt.Println(actual, params) 119 }