gen_test.go 3.8 KB

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