builder_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package builder
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. type mockedUser struct {
  7. ID string `db:"id" json:"id,omitempty"`
  8. UserName string `db:"user_name" json:"userName,omitempty"`
  9. Sex int `db:"sex" json:"sex,omitempty"`
  10. UUID string `db:"uuid" uuid:"uuid,omitempty"`
  11. Age int `db:"age" json:"age"`
  12. }
  13. func TestFieldNames(t *testing.T) {
  14. t.Run("new", func(t *testing.T) {
  15. var u mockedUser
  16. out := RawFieldNames(&u)
  17. expected := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"}
  18. assert.Equal(t, expected, out)
  19. })
  20. }
  21. type mockedUserWithOptions struct {
  22. ID string `db:"id" json:"id,omitempty"`
  23. UserName string `db:"user_name,type=varchar,length=255" json:"userName,omitempty"`
  24. Sex int `db:"sex" json:"sex,omitempty"`
  25. UUID string `db:",type=varchar,length=16" uuid:"uuid,omitempty"`
  26. Age int `db:"age" json:"age"`
  27. }
  28. func TestFieldNamesWithTagOptions(t *testing.T) {
  29. t.Run("new", func(t *testing.T) {
  30. var u mockedUserWithOptions
  31. out := RawFieldNames(&u)
  32. expected := []string{"`id`", "`user_name`", "`sex`", "`UUID`", "`age`"}
  33. assert.Equal(t, expected, out)
  34. })
  35. }
  36. type mockedUserWithDashTag struct {
  37. ID string `db:"id" json:"id,omitempty"`
  38. UserName string `db:"user_name" json:"userName,omitempty"`
  39. Mobile string `db:"-" json:"mobile,omitempty"`
  40. }
  41. func TestFieldNamesWithDashTag(t *testing.T) {
  42. t.Run("new", func(t *testing.T) {
  43. var u mockedUserWithDashTag
  44. out := RawFieldNames(&u)
  45. expected := []string{"`id`", "`user_name`"}
  46. assert.Equal(t, expected, out)
  47. })
  48. }
  49. type mockedUserWithDashTagAndOptions struct {
  50. ID string `db:"id" json:"id,omitempty"`
  51. UserName string `db:"user_name,type=varchar,length=255" json:"userName,omitempty"`
  52. Mobile string `db:"-,type=varchar,length=255" json:"mobile,omitempty"`
  53. }
  54. func TestFieldNamesWithDashTagAndOptions(t *testing.T) {
  55. t.Run("new", func(t *testing.T) {
  56. var u mockedUserWithDashTagAndOptions
  57. out := RawFieldNames(&u)
  58. expected := []string{"`id`", "`user_name`"}
  59. assert.Equal(t, expected, out)
  60. })
  61. }
  62. func TestPostgreSqlJoin(t *testing.T) {
  63. // Test with empty input array
  64. var input []string
  65. var expectedOutput string
  66. assert.Equal(t, expectedOutput, PostgreSqlJoin(input))
  67. // Test with single element input array
  68. input = []string{"foo"}
  69. expectedOutput = "foo = $2"
  70. assert.Equal(t, expectedOutput, PostgreSqlJoin(input))
  71. // Test with multiple elements input array
  72. input = []string{"foo", "bar", "baz"}
  73. expectedOutput = "foo = $2, bar = $3, baz = $4"
  74. assert.Equal(t, expectedOutput, PostgreSqlJoin(input))
  75. }
  76. type testStruct struct {
  77. Foo string `db:"foo"`
  78. Bar int `db:"bar"`
  79. Baz bool `db:"-"`
  80. }
  81. func TestRawFieldNames(t *testing.T) {
  82. // Test with a struct without tags
  83. in := struct {
  84. Foo string
  85. Bar int
  86. }{}
  87. expectedOutput := []string{"`Foo`", "`Bar`"}
  88. assert.ElementsMatch(t, expectedOutput, RawFieldNames(in))
  89. // Test pg without db tag
  90. expectedOutput = []string{"Foo", "Bar"}
  91. assert.ElementsMatch(t, expectedOutput, RawFieldNames(in, true))
  92. // Test with a struct with tags
  93. input := testStruct{}
  94. expectedOutput = []string{"`foo`", "`bar`"}
  95. assert.ElementsMatch(t, expectedOutput, RawFieldNames(input))
  96. // Test with nil input (pointer)
  97. var nilInput *testStruct
  98. assert.Panics(t, func() {
  99. RawFieldNames(nilInput)
  100. }, "RawFieldNames should panic with nil input")
  101. // Test with non-struct input
  102. inputInt := 42
  103. assert.Panics(t, func() {
  104. RawFieldNames(inputInt)
  105. }, "RawFieldNames should panic with non-struct input")
  106. // Test with PostgreSQL flag
  107. input = testStruct{}
  108. expectedOutput = []string{"foo", "bar"}
  109. assert.ElementsMatch(t, expectedOutput, RawFieldNames(input, true))
  110. }