template_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package gen
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "testing"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/template"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGenTemplates(t *testing.T) {
  11. err := util.InitTemplates(category, templates)
  12. assert.Nil(t, err)
  13. dir, err := util.GetTemplateDir(category)
  14. assert.Nil(t, err)
  15. file := filepath.Join(dir, "model-new.tpl")
  16. data, err := ioutil.ReadFile(file)
  17. assert.Nil(t, err)
  18. assert.Equal(t, string(data), template.New)
  19. }
  20. func TestRevertTemplate(t *testing.T) {
  21. name := "model-new.tpl"
  22. err := util.InitTemplates(category, templates)
  23. assert.Nil(t, err)
  24. dir, err := util.GetTemplateDir(category)
  25. assert.Nil(t, err)
  26. file := filepath.Join(dir, name)
  27. data, err := ioutil.ReadFile(file)
  28. assert.Nil(t, err)
  29. modifyData := string(data) + "modify"
  30. err = util.CreateTemplate(category, name, modifyData)
  31. assert.Nil(t, err)
  32. data, err = ioutil.ReadFile(file)
  33. assert.Nil(t, err)
  34. assert.Equal(t, string(data), modifyData)
  35. assert.Nil(t, RevertTemplate(name))
  36. data, err = ioutil.ReadFile(file)
  37. assert.Nil(t, err)
  38. assert.Equal(t, template.New, string(data))
  39. }
  40. func TestClean(t *testing.T) {
  41. name := "model-new.tpl"
  42. err := util.InitTemplates(category, templates)
  43. assert.Nil(t, err)
  44. assert.Nil(t, Clean())
  45. dir, err := util.GetTemplateDir(category)
  46. assert.Nil(t, err)
  47. file := filepath.Join(dir, name)
  48. _, err = ioutil.ReadFile(file)
  49. assert.NotNil(t, err)
  50. }
  51. func TestUpdate(t *testing.T) {
  52. name := "model-new.tpl"
  53. err := util.InitTemplates(category, templates)
  54. assert.Nil(t, err)
  55. dir, err := util.GetTemplateDir(category)
  56. assert.Nil(t, err)
  57. file := filepath.Join(dir, name)
  58. data, err := ioutil.ReadFile(file)
  59. assert.Nil(t, err)
  60. modifyData := string(data) + "modify"
  61. err = util.CreateTemplate(category, name, modifyData)
  62. assert.Nil(t, err)
  63. data, err = ioutil.ReadFile(file)
  64. assert.Nil(t, err)
  65. assert.Equal(t, string(data), modifyData)
  66. assert.Nil(t, Update())
  67. data, err = ioutil.ReadFile(file)
  68. assert.Nil(t, err)
  69. assert.Equal(t, template.New, string(data))
  70. }