builder_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package builderx
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/go-xorm/builder"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type mockedUser struct {
  9. // 自增id
  10. ID string `db:"id" json:"id,omitempty"`
  11. // 姓名
  12. UserName string `db:"user_name" json:"userName,omitempty"`
  13. // 1男,2女
  14. Sex int `db:"sex" json:"sex,omitempty"`
  15. UUID string `db:"uuid" uuid:"uuid,omitempty"`
  16. Age int `db:"age" json:"age"`
  17. }
  18. var (
  19. userFieldsWithRawStringQuote = RawFieldNames(mockedUser{})
  20. userFieldsWithoutRawStringQuote = FieldNames(mockedUser{})
  21. )
  22. func TestFieldNames(t *testing.T) {
  23. t.Run("old", func(t *testing.T) {
  24. var u mockedUser
  25. out := FieldNames(&u)
  26. expected := []string{"id", "user_name", "sex", "uuid", "age"}
  27. assert.Equal(t, expected, out)
  28. })
  29. t.Run("new", func(t *testing.T) {
  30. var u mockedUser
  31. out := RawFieldNames(&u)
  32. expected := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"}
  33. assert.Equal(t, expected, out)
  34. })
  35. }
  36. func TestNewEq(t *testing.T) {
  37. u := &mockedUser{
  38. ID: "123456",
  39. UserName: "wahaha",
  40. }
  41. out := NewEq(u)
  42. fmt.Println(out)
  43. actual := builder.Eq{"id": "123456", "user_name": "wahaha"}
  44. assert.Equal(t, out, actual)
  45. }
  46. // @see https://github.com/go-xorm/builder
  47. func TestBuilderSql(t *testing.T) {
  48. u := &mockedUser{
  49. ID: "123123",
  50. }
  51. fields := RawFieldNames(u)
  52. eq := NewEq(u)
  53. sql, args, err := builder.Select(fields...).From("user").Where(eq).ToSQL()
  54. fmt.Println(sql, args, err)
  55. actualSQL := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE id=?"
  56. actualArgs := []interface{}{"123123"}
  57. assert.Equal(t, sql, actualSQL)
  58. assert.Equal(t, args, actualArgs)
  59. }
  60. func TestBuildSqlDefaultValue(t *testing.T) {
  61. eq := builder.Eq{}
  62. eq["age"] = 0
  63. eq["user_name"] = ""
  64. t.Run("raw", func(t *testing.T) {
  65. sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(eq).ToSQL()
  66. fmt.Println(sql, args, err)
  67. actualSQL := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE age=? AND user_name=?"
  68. actualArgs := []interface{}{0, ""}
  69. assert.Equal(t, sql, actualSQL)
  70. assert.Equal(t, args, actualArgs)
  71. })
  72. t.Run("withour raw quote", func(t *testing.T) {
  73. sql, args, err := builder.Select(userFieldsWithoutRawStringQuote...).From("user").Where(eq).ToSQL()
  74. fmt.Println(sql, args, err)
  75. actualSQL := "SELECT id,user_name,sex,uuid,age FROM user WHERE age=? AND user_name=?"
  76. actualArgs := []interface{}{0, ""}
  77. assert.Equal(t, sql, actualSQL)
  78. assert.Equal(t, args, actualArgs)
  79. })
  80. }
  81. func TestBuilderSqlIn(t *testing.T) {
  82. u := &mockedUser{
  83. Age: 18,
  84. }
  85. gtU := NewGt(u)
  86. in := builder.In("id", []string{"1", "2", "3"})
  87. sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(in).And(gtU).ToSQL()
  88. fmt.Println(sql, args, err)
  89. actualSQL := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE id IN (?,?,?) AND age>?"
  90. actualArgs := []interface{}{"1", "2", "3", 18}
  91. assert.Equal(t, sql, actualSQL)
  92. assert.Equal(t, args, actualArgs)
  93. }
  94. func TestBuildSqlLike(t *testing.T) {
  95. like := builder.Like{"name", "wang"}
  96. sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(like).ToSQL()
  97. fmt.Println(sql, args, err)
  98. actualSQL := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE name LIKE ?"
  99. actualArgs := []interface{}{"%wang%"}
  100. assert.Equal(t, sql, actualSQL)
  101. assert.Equal(t, args, actualArgs)
  102. }