github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/db/tpl/tpl_orcl_test.go (about) 1 package tpl 2 3 import "testing" 4 5 func TestORCTPLgetSPName(t *testing.T) { 6 orcl := ATTPLContext{name: "oracle", prefix: ":"} 7 input := map[string]string{ 8 "order_create(:1,:2,:3)": "begin order_create(:1,:2,:3);end;", 9 "order_create(:1,:2,:3);": "begin order_create(:1,:2,:3);end;", 10 "order_create(:1,:2,:3),": "begin order_create(:1,:2,:3);end;", 11 ";order_create(:1,:2,:3)": "begin order_create(:1,:2,:3);end;", 12 ",order_create(:1,:2,:3)": "begin order_create(:1,:2,:3);end;", 13 ";order_create(:1,:2,:3),": "begin order_create(:1,:2,:3);end;", 14 "#order_create(:1,:2,:3)": "begin #order_create(:1,:2,:3);end;", 15 "": "begin ;end;", 16 } 17 for i, except := range input { 18 if orcl.getSPName(i) != except { 19 t.Error("getSPName 解析参数有误") 20 } 21 } 22 } 23 func TestORCTPLGetContext(t *testing.T) { 24 orcl := ATTPLContext{name: "oracle", prefix: ":"} 25 input := make(map[string]interface{}) 26 input["id"] = 1 27 input["name"] = "colin" 28 input["condtion"] = "(id>1)" 29 input["ids"] = `"1","2","3"` 30 31 //正确参数解析 32 tpl := "where id=@id and name=@name" 33 except := "where id=:1 and name=:2" 34 actual, params := orcl.GetSQLContext(tpl, input) 35 if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] { 36 t.Error("GetSQLContext解析参数有误") 37 } 38 39 //正确参数解析 40 tpl = "where id=@id \r\nand name=@name" 41 except = "where id=:1 \r\nand name=:2" 42 actual, params = orcl.GetSQLContext(tpl, input) 43 if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] { 44 t.Error("GetSQLContext解析参数有误") 45 } 46 47 /*change by champly 2016年11月9日11:55:31*/ 48 // //正确参数解析【重复代码】 49 // tpl = "where id=@id \r\nand name=@name" 50 // except = "where id=:1 \r\nand name=:2" 51 // actual, params = orcl.GetSQLContext(tpl, input) 52 // if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] { 53 // t.Error("GetSQLContext解析参数有误") 54 // } 55 /*end*/ 56 57 //正确参数解析 58 tpl = "where id=@id \r\nand name=@name &name" 59 except = "where id=:1 \r\nand name=:2 and name=:3" 60 actual, params = orcl.GetSQLContext(tpl, input) 61 if actual != except || len(params) != 3 || params[0] != input["id"] || params[1] != input["name"] { 62 t.Error("GetSQLContext解析参数有误") 63 } 64 65 //正确参数解析 66 tpl = "where id=@id \r\nand name=@name |name &name" 67 except = "where id=:1 \r\nand name=:2 or name=:3 and name=:4" 68 actual, params = orcl.GetSQLContext(tpl, input) 69 if actual != except || len(params) != 4 || params[0] != input["id"] || params[1] != input["name"] { 70 t.Error("GetSQLContext解析参数有误") 71 } 72 73 //正确参数解析 74 tpl = "where id=@id \r\nand name=@name |name &name #condtion" 75 except = "where id=:1 \r\nand name=:2 or name=:3 and name=:4 (id>1)" 76 actual, params = orcl.GetSQLContext(tpl, input) 77 if actual != except || len(params) != 4 || params[0] != input["id"] || params[1] != input["name"] { 78 t.Error("GetSQLContext解析参数有误") 79 } 80 81 //正确参数解析 82 tpl = "where id=@id and name=@name |name &name id in (#ids)" 83 except = `where id=:1 and name=:2 or name=:3 and name=:4 id in ("1","2","3")` 84 actual, params = orcl.GetSQLContext(tpl, input) 85 if actual != except || len(params) != 4 || params[0] != input["id"] || params[1] != input["name"] { 86 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 87 } 88 89 //正确参数解析 90 tpl = "update order_main set date=sysdate ~name ~status where id=@id" 91 except = `update order_main set date=sysdate ,name=:1 where id=:2` 92 actual, params = orcl.GetSQLContext(tpl, input) 93 if actual != except || len(params) != 2 || params[1] != input["id"] || params[0] != input["name"] { 94 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 95 } 96 97 /*add by champly 2016年11月9日11:55:57*/ 98 // 没有参数的解析 99 tpl = "where status=@status" 100 except = `where status=:1` 101 actual, params = orcl.GetSQLContext(tpl, input) 102 if actual != except || len(params) != 1 || params[0] != nil { 103 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 104 } 105 106 // 没有参数的解析 107 tpl = "udpate order_main set status=#status" 108 except = `udpate order_main set status=NULL` 109 actual, params = orcl.GetSQLContext(tpl, input) 110 if actual != except || len(params) != 0 { 111 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 112 } 113 114 // 没有参数的解析 115 tpl = "udpate order_main set status=|status &status ~status" 116 except = `udpate order_main set status= ` 117 actual, params = orcl.GetSQLContext(tpl, input) 118 if actual != except || len(params) != 0 { 119 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 120 } 121 122 // 特殊字符的解析 123 tpl = "udpate order_main set status=@!^&*|s" 124 except = `udpate order_main set status=@!^&*` 125 actual, params = orcl.GetSQLContext(tpl, input) 126 if actual != except || len(params) != 0 { 127 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 128 } 129 130 // 解析空字符串 131 tpl = "" 132 except = `` 133 actual, params = orcl.GetSQLContext(tpl, input) 134 if actual != except || len(params) != 0 { 135 t.Errorf("GetSQLContext解析参数有误:actual:%s,except:%s", actual, except) 136 } 137 /*end*/ 138 } 139 140 /* 141 func TestORCLNICEName(t *testing.T) { 142 orcl := ATTPLContext{name: "oracle", prefix: ":"} 143 input := make(map[string]interface{}) 144 145 input["id"] = 1 146 input["name"] = "colin" 147 tpl := "&t.id&t.name)," 148 except := "t.id=:1 and t.name=:2" 149 actual, params := orcl.GetSPContext(tpl, input) 150 if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] { 151 t.Error("GetSPContext解析参数有误", actual) 152 } 153 } 154 */ 155 func TestORCTPLGetSPContext(t *testing.T) { 156 orcl := ATTPLContext{name: "oracle", prefix: ":"} 157 input := make(map[string]interface{}) 158 159 input["id"] = 1 160 input["name"] = "colin" 161 //正确参数解析 162 tpl := "order_create(@id,@name,@colin)" 163 except := "begin order_create(:1,:2,:3);end;" 164 actual, params := orcl.GetSPContext(tpl, input) 165 if actual != except || len(params) != 3 || params[0] != input["id"] || params[1] != input["name"] || params[2] != nil { 166 t.Error("GetSPContext解析参数有误") 167 } 168 169 //正确参数解析 170 tpl = "order_create(@id,@name)," 171 except = "begin order_create(:1,:2);end;" 172 actual, params = orcl.GetSPContext(tpl, input) 173 if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] { 174 t.Error("GetSPContext解析参数有误", actual) 175 } 176 177 /*add by champly 2016年11月9日11:56:24*/ 178 // sql为空解析 179 tpl = "" 180 except = "begin ;end;" 181 actual, params = orcl.GetSPContext(tpl, input) 182 if actual != except || len(params) != 0 { 183 t.Error("GetSPContext解析参数有误", actual) 184 } 185 /*end*/ 186 } 187 func TestORCTPLReplace(t *testing.T) { 188 orcl := ATTPLContext{name: "oracle", prefix: ":"} 189 input := make([]interface{}, 0, 2) 190 191 tpl := "begin order_create(:1,:2,:3);end;" 192 except := "begin order_create(NULL,NULL,NULL);end;" 193 actual := orcl.Replace(tpl, input) 194 if actual != except { 195 t.Error("Replace解析参数有误:actual:", actual) 196 } 197 198 tpl = "" 199 except = "" 200 actual = orcl.Replace(tpl, input) 201 if actual != except { 202 t.Error("Replace解析参数有误", actual) 203 } 204 205 input = append(input, 1) 206 input = append(input, "colin") 207 208 tpl = "begin order_create(:1,:2,:3);end;" 209 except = "begin order_create('1','colin',NULL);end;" 210 actual = orcl.Replace(tpl, input) 211 if actual != except { 212 t.Error("Replace解析参数有误", actual) 213 } 214 215 tpl = "begin order_create(:11,:2,:3);end;" 216 except = "begin order_create(NULL,'colin',NULL);end;" 217 actual = orcl.Replace(tpl, input) 218 if actual != except { 219 t.Error("Replace解析参数有误", actual) 220 } 221 222 /*add by champly 2016年11月9日14:23:20*/ 223 tpl = "begin name=:1 where id=:2;end;" 224 except = "begin name='1' where id='colin';end;" 225 actual = orcl.Replace(tpl, input) 226 if actual != except { 227 t.Error("Replace解析参数有误", actual) 228 } 229 230 tpl = "" 231 except = "" 232 actual = orcl.Replace(tpl, input) 233 if actual != except { 234 t.Error("Replace解析参数有误", actual) 235 } 236 237 tpl = "begin name=:1 where id=:2;end;" 238 except = "begin name=:1 where id=:2;end;" 239 actual = orcl.Replace(tpl, nil) 240 if actual != except { 241 t.Error("Replace解析参数有误", actual) 242 } 243 244 tpl = "begin name=:1 where id=:2|;end;" 245 except = "begin name='1' where id='colin'|;end;" 246 actual = orcl.Replace(tpl, input) 247 if actual != except { 248 t.Error("Replace解析参数有误", actual) 249 } 250 251 tpl = "begin name=:1a where id=:2a|;end;" 252 except = "begin name=:1a where id=:2a|;end;" 253 actual = orcl.Replace(tpl, input) 254 if actual != except { 255 t.Error("Replace解析参数有误", actual) 256 } 257 258 // 匹配结尾 259 tpl = "begin name=:1a where id=:2" 260 except = "begin name=:1a where id='colin'" 261 actual = orcl.Replace(tpl, input) 262 if actual != except { 263 t.Error("Replace解析参数有误", actual) 264 } 265 266 tpl = "begin name=:1 where id=:2 " 267 except = "begin name='1' where id='colin' " 268 actual = orcl.Replace(tpl, input) 269 if actual != except { 270 t.Error("Replace解析参数有误", actual) 271 } 272 273 }