github.com/lingyao2333/mo-zero@v1.4.1/core/stores/sqlx/utils_test.go (about)

     1  package sqlx
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestEscape(t *testing.T) {
    12  	s := "a\x00\n\r\\'\"\x1ab"
    13  
    14  	out := escape(s)
    15  
    16  	assert.Equal(t, `a\x00\n\r\\\'\"\x1ab`, out)
    17  }
    18  
    19  func TestDesensitize(t *testing.T) {
    20  	datasource := "user:pass@tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
    21  	datasource = desensitize(datasource)
    22  	assert.False(t, strings.Contains(datasource, "user"))
    23  	assert.False(t, strings.Contains(datasource, "pass"))
    24  	assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
    25  }
    26  
    27  func TestDesensitize_WithoutAccount(t *testing.T) {
    28  	datasource := "tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
    29  	datasource = desensitize(datasource)
    30  	assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
    31  }
    32  
    33  func TestFormat(t *testing.T) {
    34  	tests := []struct {
    35  		name   string
    36  		query  string
    37  		args   []interface{}
    38  		expect string
    39  		hasErr bool
    40  	}{
    41  		{
    42  			name:   "mysql normal",
    43  			query:  "select name, age from users where bool=? and phone=?",
    44  			args:   []interface{}{true, "133"},
    45  			expect: "select name, age from users where bool=1 and phone='133'",
    46  		},
    47  		{
    48  			name:   "mysql normal",
    49  			query:  "select name, age from users where bool=? and phone=?",
    50  			args:   []interface{}{false, "133"},
    51  			expect: "select name, age from users where bool=0 and phone='133'",
    52  		},
    53  		{
    54  			name:   "pg normal",
    55  			query:  "select name, age from users where bool=$1 and phone=$2",
    56  			args:   []interface{}{true, "133"},
    57  			expect: "select name, age from users where bool=1 and phone='133'",
    58  		},
    59  		{
    60  			name:   "pg normal reverse",
    61  			query:  "select name, age from users where bool=$2 and phone=$1",
    62  			args:   []interface{}{"133", false},
    63  			expect: "select name, age from users where bool=0 and phone='133'",
    64  		},
    65  		{
    66  			name:   "pg error not number",
    67  			query:  "select name, age from users where bool=$a and phone=$1",
    68  			args:   []interface{}{"133", false},
    69  			hasErr: true,
    70  		},
    71  		{
    72  			name:   "pg error more args",
    73  			query:  "select name, age from users where bool=$2 and phone=$1 and nickname=$3",
    74  			args:   []interface{}{"133", false},
    75  			hasErr: true,
    76  		},
    77  		{
    78  			name:   "oracle normal",
    79  			query:  "select name, age from users where bool=:1 and phone=:2",
    80  			args:   []interface{}{true, "133"},
    81  			expect: "select name, age from users where bool=1 and phone='133'",
    82  		},
    83  		{
    84  			name:   "oracle normal reverse",
    85  			query:  "select name, age from users where bool=:2 and phone=:1",
    86  			args:   []interface{}{"133", false},
    87  			expect: "select name, age from users where bool=0 and phone='133'",
    88  		},
    89  		{
    90  			name:   "oracle error not number",
    91  			query:  "select name, age from users where bool=:a and phone=:1",
    92  			args:   []interface{}{"133", false},
    93  			hasErr: true,
    94  		},
    95  		{
    96  			name:   "oracle error more args",
    97  			query:  "select name, age from users where bool=:2 and phone=:1 and nickname=:3",
    98  			args:   []interface{}{"133", false},
    99  			hasErr: true,
   100  		},
   101  		{
   102  			name:   "select with date",
   103  			query:  "select * from user where date='2006-01-02 15:04:05' and name=:1",
   104  			args:   []interface{}{"foo"},
   105  			expect: "select * from user where date='2006-01-02 15:04:05' and name='foo'",
   106  		},
   107  		{
   108  			name:   "select with date and escape",
   109  			query:  `select * from user where date=' 2006-01-02 15:04:05 \'' and name=:1`,
   110  			args:   []interface{}{"foo"},
   111  			expect: `select * from user where date=' 2006-01-02 15:04:05 \'' and name='foo'`,
   112  		},
   113  		{
   114  			name:   "select with date and bad arg",
   115  			query:  `select * from user where date='2006-01-02 15:04:05 \'' and name=:a`,
   116  			args:   []interface{}{"foo"},
   117  			hasErr: true,
   118  		},
   119  		{
   120  			name:   "select with date and escape error",
   121  			query:  `select * from user where date='2006-01-02 15:04:05 \`,
   122  			args:   []interface{}{"foo"},
   123  			hasErr: true,
   124  		},
   125  	}
   126  
   127  	for _, test := range tests {
   128  		test := test
   129  		t.Run(test.name, func(t *testing.T) {
   130  			t.Parallel()
   131  
   132  			actual, err := format(test.query, test.args...)
   133  			if test.hasErr {
   134  				assert.NotNil(t, err)
   135  			} else {
   136  				assert.Nil(t, err)
   137  				assert.Equal(t, test.expect, actual)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestWriteValue(t *testing.T) {
   144  	var buf strings.Builder
   145  	tm := time.Now()
   146  	writeValue(&buf, &tm)
   147  	assert.Equal(t, "'"+tm.String()+"'", buf.String())
   148  
   149  	buf.Reset()
   150  	writeValue(&buf, tm)
   151  	assert.Equal(t, "'"+tm.String()+"'", buf.String())
   152  }