github.com/jtzjtz/kit@v1.0.2/sql/sqlquery_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestQuery(t *testing.T) {
     9  	sql, err := Query("")
    10  	if err != nil {
    11  		t.Fatal(err)
    12  	}
    13  	t.Log(sql)
    14  }
    15  
    16  func TestQuery1(t *testing.T) {
    17  	sql, err := Query("id = ? AND name LIKE ? AND age > ?", "1", "%zhangsan%", 19)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	t.Log(sql)
    22  }
    23  
    24  func TestQuery2(t *testing.T) {
    25  	// full_name LIKE '%\'%f\') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%\')%'
    26  	sql, err := Query("full_name LIKE ?", "'%f') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%')")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	t.Log(sql)
    31  }
    32  
    33  func TestQuery3(t *testing.T) {
    34  	// create_time = '2021-01-04 14:08:35'
    35  	sql, err := Query("create_time = ?", time.Now())
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	t.Log(sql)
    40  }
    41  
    42  func TestQuery4(t *testing.T) {
    43  	// name = 'zhangsan' AND ( age < 20 OR  age > 25) AND email LIKE '%xxx@qq.com%'
    44  	sql, err := Query("name = ? AND ( age < ? OR  age > ?) AND email LIKE ?", "zhangsan", 20, int32(25), "%xxx@qq.com")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	t.Log(sql)
    49  }
    50  
    51  func TestQuery5(t *testing.T) {
    52  	// name = 'zhangsan' AND ( age < 20 OR  age > 25) AND email LIKE '%xxx@qq.com%'
    53  	sql, err := Query("price = ?", 20.11)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	t.Log(sql)
    58  }
    59  
    60  func TestQuery6(t *testing.T) {
    61  	// full_name LIKE '%\'%f\') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%\')%'
    62  	sql, err := Query("full_name LIKE ?", []byte("'%f') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%')"))
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	t.Log(sql)
    67  }