github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/db/tpl/tpl_sqlite_test.go (about)

     1  package tpl
     2  
     3  import "testing"
     4  
     5  func TestSqliteTPLGetContext(t *testing.T) {
     6  	sqlite := SqliteTPLContext{}
     7  	input := make(map[string]interface{})
     8  	input["id"] = 1
     9  	input["name"] = "colin"
    10  
    11  	//正确参数解析
    12  	tpl := "select seq_wxaccountmenu_auto_id.nextval from where id=@id and name=@name"
    13  	except := "select seq_wxaccountmenu_auto_id.nextval from where id=? and name=?"
    14  	actual, params := sqlite.GetSQLContext(tpl, input)
    15  	if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] {
    16  		t.Error("GetSQLContext解析参数有误")
    17  	}
    18  
    19  	//正确参数解析o
    20  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where id=@id \r\nand name=@name"
    21  	except = "select seq_wxaccountmenu_auto_id.nextval from where id=? \r\nand name=?"
    22  	actual, params = sqlite.GetSQLContext(tpl, input)
    23  	if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] {
    24  		t.Error("GetSQLContext解析参数有误")
    25  	}
    26  
    27  	/*add by champly 2016年11月9日15:02:44*/
    28  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where id=#id \r\nand name=@name"
    29  	except = "select seq_wxaccountmenu_auto_id.nextval from where id=1 \r\nand name=?"
    30  	actual, params = sqlite.GetSQLContext(tpl, input)
    31  	if actual != except || len(params) != 1 || params[0] != input["name"] {
    32  		t.Error("GetSQLContext解析参数有误")
    33  	}
    34  
    35  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where |id \r\nand &name"
    36  	except = "select seq_wxaccountmenu_auto_id.nextval from where or id=? \r\nand and name=?"
    37  	actual, params = sqlite.GetSQLContext(tpl, input)
    38  	if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] {
    39  		t.Error("GetSQLContext解析参数有误")
    40  	}
    41  
    42  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where ~id~name~test"
    43  	except = "select seq_wxaccountmenu_auto_id.nextval from where ,id=?,name=?"
    44  	actual, params = sqlite.GetSQLContext(tpl, input)
    45  	if actual != except || len(params) != 2 || params[0] != input["id"] || params[1] != input["name"] {
    46  		t.Error("GetSQLContext解析参数有误")
    47  	}
    48  
    49  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where ~!@#$|&"
    50  	except = "select seq_wxaccountmenu_auto_id.nextval from where ~!@#$|&"
    51  	actual, params = sqlite.GetSQLContext(tpl, input)
    52  	if actual != except || len(params) != 0 {
    53  		t.Error("GetSQLContext解析参数有误")
    54  	}
    55  
    56  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where id=@id and id=@id and name=@name"
    57  	except = "select seq_wxaccountmenu_auto_id.nextval from where id=? and id=? and name=?"
    58  	actual, params = sqlite.GetSQLContext(tpl, input)
    59  	if actual != except || len(params) != 3 || params[0] != input["id"] || params[1] != input["id"] || params[2] != input["name"] {
    60  		t.Error("GetSQLContext解析参数有误")
    61  	}
    62  
    63  	tpl = "select seq_wxaccountmenu_auto_id.nextval from where id=@id and id=@id and name=@names"
    64  	except = "select seq_wxaccountmenu_auto_id.nextval from where id=? and id=? and name=?"
    65  	actual, params = sqlite.GetSQLContext(tpl, input)
    66  	if actual != except || len(params) != 3 || params[0] != input["id"] || params[1] != input["id"] || params[2] != nil {
    67  		t.Error("GetSQLContext解析参数有误")
    68  	}
    69  	/*end*/
    70  
    71  }
    72  
    73  func TestSqliteTPLGetSPContext(t *testing.T) {
    74  	sqlite := SqliteTPLContext{}
    75  	input := make(map[string]interface{})
    76  	input["id"] = 1
    77  	input["name"] = "colin"
    78  	input["name_"] = "name_"
    79  
    80  	//正确参数解析
    81  	tpl := "order_create(@id,@name,@colin)"
    82  	except := "order_create(?,?,?)"
    83  	actual, params := sqlite.GetSPContext(tpl, input)
    84  	if actual != except || len(params) != 3 || params[0] != input["id"] || params[1] != input["name"] || params[2] != nil {
    85  		t.Error("GetSPContext解析参数有误")
    86  	}
    87  
    88  	/*add by champly 2016年11月10日09:25:45*/
    89  	tpl = "order_create(&id&colin)"
    90  	except = "order_create(and id=?)"
    91  	actual, params = sqlite.GetSPContext(tpl, input)
    92  	if actual != except || len(params) != 1 || params[0] != input["id"] {
    93  		t.Error("GetSPContext解析参数有误")
    94  	}
    95  
    96  	tpl = "order_create(|id|colin)"
    97  	except = "order_create(or id=?)"
    98  	actual, params = sqlite.GetSPContext(tpl, input)
    99  	if actual != except || len(params) != 1 || params[0] != input["id"] {
   100  		t.Error("GetSPContext解析参数有误")
   101  	}
   102  
   103  	tpl = "order_create(#id#colin)"
   104  	except = "order_create(1NULL)"
   105  	actual, params = sqlite.GetSPContext(tpl, input)
   106  	if actual != except || len(params) != 0 {
   107  		t.Error("GetSPContext解析参数有误")
   108  	}
   109  
   110  	tpl = "order_create(~id~colin)"
   111  	except = "order_create(,id=?)"
   112  	actual, params = sqlite.GetSPContext(tpl, input)
   113  	if actual != except || len(params) != 1 || params[0] != input["id"] {
   114  		t.Error("GetSPContext解析参数有误")
   115  	}
   116  
   117  	tpl = "order_create(~@#|&!)"
   118  	except = "order_create(~@#|&!)"
   119  	actual, params = sqlite.GetSPContext(tpl, input)
   120  	if actual != except || len(params) != 0 {
   121  		t.Error("GetSPContext解析参数有误")
   122  	}
   123  
   124  	tpl = "order_create(name_=@name_,name_=@name__)"
   125  	except = "order_create(name_=?,name_=?)"
   126  	actual, params = sqlite.GetSPContext(tpl, input)
   127  	if actual != except || len(params) != 2 || params[0] != input["name_"] || params[1] != nil {
   128  		t.Error("GetSPContext解析参数有误")
   129  	}
   130  
   131  	tpl = "order_create(name_=@name_,name_=@name__"
   132  	except = "order_create(name_=?,name_=?"
   133  	actual, params = sqlite.GetSPContext(tpl, input)
   134  	if actual != except || len(params) != 2 || params[0] != input["name_"] || params[1] != nil {
   135  		t.Error("GetSPContext解析参数有误")
   136  	}
   137  
   138  	tpl = "order_create(@id,@name,@colin)"
   139  	except = "order_create(?,?,?)"
   140  	actual, params = sqlite.GetSPContext(tpl, nil)
   141  	if actual != except || len(params) != 3 || params[0] != nil || params[1] != nil || params[2] != nil {
   142  		t.Error("GetSPContext解析参数有误")
   143  	}
   144  	/*end*/
   145  }
   146  
   147  func TestSqliteTPLReplace(t *testing.T) {
   148  	orcl := SqliteTPLContext{}
   149  	input := make([]interface{}, 0, 2)
   150  
   151  	tpl := "begin order_create(?,?,?);end;"
   152  	except := "begin order_create(NULL,NULL,NULL);end;"
   153  	actual := orcl.Replace(tpl, input)
   154  	if actual != except {
   155  		t.Error("Replace解析参数有误", actual)
   156  	}
   157  
   158  	tpl = ""
   159  	except = ""
   160  	actual = orcl.Replace(tpl, input)
   161  	if actual != except {
   162  		t.Error("Replace解析参数有误", actual)
   163  	}
   164  
   165  	input = append(input, 1)
   166  	input = append(input, "colin")
   167  
   168  	tpl = "begin order_create(?,?,?);end;"
   169  	except = "begin order_create('1','colin',NULL);end;"
   170  	actual = orcl.Replace(tpl, input)
   171  	if actual != except {
   172  		t.Error("Replace解析参数有误", actual)
   173  	}
   174  
   175  	tpl = "begin order_create(?);end;"
   176  	except = "begin order_create('1');end;"
   177  	actual = orcl.Replace(tpl, input)
   178  	if actual != except {
   179  		t.Error("Replace解析参数有误", actual)
   180  	}
   181  
   182  	/*change by champly 2016年11月10日10:06:51*/
   183  	// tpl = "begin order_create(?,?,?);end;"
   184  	// except = "begin order_create('1','colin',NULL);end;"
   185  	// actual = orcl.Replace(tpl, input)
   186  	// if actual != except {
   187  	// 	t.Error("Replace解析参数有误", actual)
   188  	// }
   189  	/*end*/
   190  
   191  	tpl = "begin order_create(?,'?234');end;"
   192  	except = "begin order_create('1','?234');end;"
   193  	actual = orcl.Replace(tpl, input)
   194  	if actual != except {
   195  		t.Error("Replace解析参数有误", actual)
   196  	}
   197  
   198  	/*add by champly 2016年11月10日10:08:52*/
   199  	tpl = "begin order_create(?,?;end;"
   200  	except = "begin order_create('1','colin';end;"
   201  	actual = orcl.Replace(tpl, input)
   202  	if actual != except {
   203  		t.Error("Replace解析参数有误", actual)
   204  	}
   205  
   206  	tpl = "begin order_create(?,?);end;"
   207  	except = "begin order_create('1',?);end;"
   208  	actual = orcl.Replace(tpl, input)
   209  	if actual != except {
   210  		t.Error("Replace解析参数有误", actual)
   211  	}
   212  
   213  	tpl = "begin order_create(?,? );end;"
   214  	except = "begin order_create('1','colin' );end;"
   215  	actual = orcl.Replace(tpl, input)
   216  	if actual != except {
   217  		t.Error("Replace解析参数有误", actual)
   218  	}
   219  
   220  	tpl = "begin order_create(?,?"
   221  	except = "begin order_create('1','colin'"
   222  	actual = orcl.Replace(tpl, input)
   223  	if actual != except {
   224  		t.Error("Replace解析参数有误", actual)
   225  	}
   226  
   227  	tpl = "begin order_create(?,??"
   228  	except = "begin order_create('1',?'colin'"
   229  	actual = orcl.Replace(tpl, input)
   230  	if actual != except {
   231  		t.Error("Replace解析参数有误", actual)
   232  	}
   233  
   234  	tpl = "begin order_create(?,??@"
   235  	except = "begin order_create('1',??@"
   236  	actual = orcl.Replace(tpl, input)
   237  	if actual != except {
   238  		t.Error("Replace解析参数有误", actual)
   239  	}
   240  
   241  	tpl = "begin order_create(?,? "
   242  	except = "begin order_create('1','colin' "
   243  	actual = orcl.Replace(tpl, input)
   244  	if actual != except {
   245  		t.Error("Replace解析参数有误", actual)
   246  	}
   247  	/*end*/
   248  }