gen_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package gen
  2. import (
  3. "database/sql"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tal-tech/go-zero/core/logx"
  11. "github.com/tal-tech/go-zero/core/stringx"
  12. "github.com/tal-tech/go-zero/tools/goctl/config"
  13. "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
  14. )
  15. var (
  16. source = "CREATE TABLE `test_user_info` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `nanosecond` bigint NOT NULL DEFAULT '0',\n `data` varchar(255) DEFAULT '',\n `content` json DEFAULT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `nanosecond_unique` (`nanosecond`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
  17. )
  18. func TestCacheModel(t *testing.T) {
  19. logx.Disable()
  20. _ = Clean()
  21. dir, _ := filepath.Abs("./testmodel")
  22. cacheDir := filepath.Join(dir, "cache")
  23. noCacheDir := filepath.Join(dir, "nocache")
  24. defer func() {
  25. _ = os.RemoveAll(dir)
  26. }()
  27. g, err := NewDefaultGenerator(cacheDir, &config.Config{
  28. NamingFormat: "GoZero",
  29. })
  30. assert.Nil(t, err)
  31. err = g.StartFromDDL(source, true)
  32. assert.Nil(t, err)
  33. assert.True(t, func() bool {
  34. _, err := os.Stat(filepath.Join(cacheDir, "TestUserInfoModel.go"))
  35. return err == nil
  36. }())
  37. g, err = NewDefaultGenerator(noCacheDir, &config.Config{
  38. NamingFormat: "gozero",
  39. })
  40. assert.Nil(t, err)
  41. err = g.StartFromDDL(source, false)
  42. assert.Nil(t, err)
  43. assert.True(t, func() bool {
  44. _, err := os.Stat(filepath.Join(noCacheDir, "testuserinfomodel.go"))
  45. return err == nil
  46. }())
  47. }
  48. func TestNamingModel(t *testing.T) {
  49. logx.Disable()
  50. _ = Clean()
  51. dir, _ := filepath.Abs("./testmodel")
  52. camelDir := filepath.Join(dir, "camel")
  53. snakeDir := filepath.Join(dir, "snake")
  54. defer func() {
  55. _ = os.RemoveAll(dir)
  56. }()
  57. g, err := NewDefaultGenerator(camelDir, &config.Config{
  58. NamingFormat: "GoZero",
  59. })
  60. assert.Nil(t, err)
  61. err = g.StartFromDDL(source, true)
  62. assert.Nil(t, err)
  63. assert.True(t, func() bool {
  64. _, err := os.Stat(filepath.Join(camelDir, "TestUserInfoModel.go"))
  65. return err == nil
  66. }())
  67. g, err = NewDefaultGenerator(snakeDir, &config.Config{
  68. NamingFormat: "go_zero",
  69. })
  70. assert.Nil(t, err)
  71. err = g.StartFromDDL(source, true)
  72. assert.Nil(t, err)
  73. assert.True(t, func() bool {
  74. _, err := os.Stat(filepath.Join(snakeDir, "test_user_info_model.go"))
  75. return err == nil
  76. }())
  77. }
  78. func TestWrapWithRawString(t *testing.T) {
  79. assert.Equal(t, "``", wrapWithRawString(""))
  80. assert.Equal(t, "``", wrapWithRawString("``"))
  81. assert.Equal(t, "`a`", wrapWithRawString("a"))
  82. assert.Equal(t, "` `", wrapWithRawString(" "))
  83. }
  84. func TestFields(t *testing.T) {
  85. type Student struct {
  86. ID int64 `db:"id"`
  87. Name string `db:"name"`
  88. Age sql.NullInt64 `db:"age"`
  89. Score sql.NullFloat64 `db:"score"`
  90. CreateTime time.Time `db:"create_time"`
  91. UpdateTime sql.NullTime `db:"update_time"`
  92. }
  93. var (
  94. studentFieldNames = builderx.RawFieldNames(&Student{})
  95. studentRows = strings.Join(studentFieldNames, ",")
  96. studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
  97. studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  98. )
  99. assert.Equal(t, []string{"`id`", "`name`", "`age`", "`score`", "`create_time`", "`update_time`"}, studentFieldNames)
  100. assert.Equal(t, "`id`,`name`,`age`,`score`,`create_time`,`update_time`", studentRows)
  101. assert.Equal(t, "`name`,`age`,`score`", studentRowsExpectAutoSet)
  102. assert.Equal(t, "`name`=?,`age`=?,`score`=?", studentRowsWithPlaceHolder)
  103. }