plugin_initialize_gorm_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPluginInitializeGorm_Injection(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. StructName string
  13. PackageName string
  14. IsNew bool
  15. }
  16. tests := []struct {
  17. name string
  18. fields fields
  19. wantErr bool
  20. }{
  21. {
  22. name: "测试 &model.User{} 注入",
  23. fields: fields{
  24. Type: TypePluginInitializeGorm,
  25. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "initialize", "gorm.go"),
  26. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  27. StructName: "User",
  28. PackageName: "model",
  29. IsNew: false,
  30. },
  31. },
  32. {
  33. name: "测试 new(model.ExaCustomer) 注入",
  34. fields: fields{
  35. Type: TypePluginInitializeGorm,
  36. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "initialize", "gorm.go"),
  37. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  38. StructName: "User",
  39. PackageName: "model",
  40. IsNew: true,
  41. },
  42. },
  43. {
  44. name: "测试 new(model.SysUsers) 注入",
  45. fields: fields{
  46. Type: TypePluginInitializeGorm,
  47. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "initialize", "gorm.go"),
  48. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  49. StructName: "SysUser",
  50. PackageName: "model",
  51. IsNew: true,
  52. },
  53. },
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. a := &PluginInitializeGorm{
  58. Type: tt.fields.Type,
  59. Path: tt.fields.Path,
  60. ImportPath: tt.fields.ImportPath,
  61. StructName: tt.fields.StructName,
  62. PackageName: tt.fields.PackageName,
  63. IsNew: tt.fields.IsNew,
  64. }
  65. file, err := a.Parse(a.Path, nil)
  66. if err != nil {
  67. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  68. }
  69. a.Injection(file)
  70. err = a.Format(a.Path, nil, file)
  71. if (err != nil) != tt.wantErr {
  72. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  73. }
  74. })
  75. }
  76. }
  77. func TestPluginInitializeGorm_Rollback(t *testing.T) {
  78. type fields struct {
  79. Type Type
  80. Path string
  81. ImportPath string
  82. StructName string
  83. PackageName string
  84. IsNew bool
  85. }
  86. tests := []struct {
  87. name string
  88. fields fields
  89. wantErr bool
  90. }{
  91. {
  92. name: "测试 &model.User{} 回滚",
  93. fields: fields{
  94. Type: TypePluginInitializeGorm,
  95. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "initialize", "gorm.go"),
  96. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  97. StructName: "User",
  98. PackageName: "model",
  99. IsNew: false,
  100. },
  101. },
  102. {
  103. name: "测试 new(model.ExaCustomer) 回滚",
  104. fields: fields{
  105. Type: TypePluginInitializeGorm,
  106. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "initialize", "gorm.go"),
  107. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  108. StructName: "User",
  109. PackageName: "model",
  110. IsNew: true,
  111. },
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. a := &PluginInitializeGorm{
  117. Type: tt.fields.Type,
  118. Path: tt.fields.Path,
  119. ImportPath: tt.fields.ImportPath,
  120. StructName: tt.fields.StructName,
  121. PackageName: tt.fields.PackageName,
  122. IsNew: tt.fields.IsNew,
  123. }
  124. file, err := a.Parse(a.Path, nil)
  125. if err != nil {
  126. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  127. }
  128. a.Rollback(file)
  129. err = a.Format(a.Path, nil, file)
  130. if (err != nil) != tt.wantErr {
  131. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  132. }
  133. })
  134. }
  135. }