utils_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package sqlx
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestEscape(t *testing.T) {
  8. s := "a\x00\n\r\\'\"\x1ab"
  9. out := escape(s)
  10. assert.Equal(t, `a\x00\n\r\\\'\"\x1ab`, out)
  11. }
  12. func TestDesensitize(t *testing.T) {
  13. datasource := "user:pass@tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
  14. datasource = desensitize(datasource)
  15. assert.False(t, strings.Contains(datasource, "user"))
  16. assert.False(t, strings.Contains(datasource, "pass"))
  17. assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
  18. }
  19. func TestDesensitize_WithoutAccount(t *testing.T) {
  20. datasource := "tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
  21. datasource = desensitize(datasource)
  22. assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
  23. }
  24. func TestFormatForPrint(t *testing.T) {
  25. tests := []struct {
  26. name string
  27. query string
  28. args []interface{}
  29. expect string
  30. }{
  31. {
  32. name: "no args",
  33. query: "select user, name from table where id=?",
  34. expect: `select user, name from table where id=?`,
  35. },
  36. {
  37. name: "one arg",
  38. query: "select user, name from table where id=?",
  39. args: []interface{}{"kevin"},
  40. expect: `select user, name from table where id=? ["kevin"]`,
  41. },
  42. }
  43. for _, test := range tests {
  44. t.Run(test.name, func(t *testing.T) {
  45. actual := formatForPrint(test.query, test.args...)
  46. assert.Equal(t, test.expect, actual)
  47. })
  48. }
  49. }