package_initialize_gorm_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPackageInitializeGorm_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: "测试 &example.ExaFileUploadAndDownload{} 注入",
  23. fields: fields{
  24. Type: TypePackageInitializeGorm,
  25. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  26. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  27. StructName: "ExaFileUploadAndDownload",
  28. PackageName: "example",
  29. IsNew: false,
  30. },
  31. },
  32. {
  33. name: "测试 &example.ExaCustomer{} 注入",
  34. fields: fields{
  35. Type: TypePackageInitializeGorm,
  36. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  37. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  38. StructName: "ExaCustomer",
  39. PackageName: "example",
  40. IsNew: false,
  41. },
  42. },
  43. {
  44. name: "测试 new(example.ExaFileUploadAndDownload) 注入",
  45. fields: fields{
  46. Type: TypePackageInitializeGorm,
  47. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  48. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  49. StructName: "ExaFileUploadAndDownload",
  50. PackageName: "example",
  51. IsNew: true,
  52. },
  53. },
  54. {
  55. name: "测试 new(example.ExaCustomer) 注入",
  56. fields: fields{
  57. Type: TypePackageInitializeGorm,
  58. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  59. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  60. StructName: "ExaCustomer",
  61. PackageName: "example",
  62. IsNew: true,
  63. },
  64. },
  65. }
  66. for _, tt := range tests {
  67. t.Run(tt.name, func(t *testing.T) {
  68. a := &PackageInitializeGorm{
  69. Type: tt.fields.Type,
  70. Path: tt.fields.Path,
  71. ImportPath: tt.fields.ImportPath,
  72. StructName: tt.fields.StructName,
  73. PackageName: tt.fields.PackageName,
  74. IsNew: tt.fields.IsNew,
  75. }
  76. file, err := a.Parse(a.Path, nil)
  77. if err != nil {
  78. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  79. }
  80. a.Injection(file)
  81. err = a.Format(a.Path, nil, file)
  82. if (err != nil) != tt.wantErr {
  83. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  84. }
  85. })
  86. }
  87. }
  88. func TestPackageInitializeGorm_Rollback(t *testing.T) {
  89. type fields struct {
  90. Type Type
  91. Path string
  92. ImportPath string
  93. StructName string
  94. PackageName string
  95. IsNew bool
  96. }
  97. tests := []struct {
  98. name string
  99. fields fields
  100. wantErr bool
  101. }{
  102. {
  103. name: "测试 &example.ExaFileUploadAndDownload{} 回滚",
  104. fields: fields{
  105. Type: TypePackageInitializeGorm,
  106. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  107. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  108. StructName: "ExaFileUploadAndDownload",
  109. PackageName: "example",
  110. IsNew: false,
  111. },
  112. },
  113. {
  114. name: "测试 &example.ExaCustomer{} 回滚",
  115. fields: fields{
  116. Type: TypePackageInitializeGorm,
  117. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  118. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  119. StructName: "ExaCustomer",
  120. PackageName: "example",
  121. IsNew: false,
  122. },
  123. },
  124. {
  125. name: "测试 new(example.ExaFileUploadAndDownload) 回滚",
  126. fields: fields{
  127. Type: TypePackageInitializeGorm,
  128. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  129. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  130. StructName: "ExaFileUploadAndDownload",
  131. PackageName: "example",
  132. IsNew: true,
  133. },
  134. },
  135. {
  136. name: "测试 new(example.ExaCustomer) 回滚",
  137. fields: fields{
  138. Type: TypePackageInitializeGorm,
  139. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
  140. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/model/example"`,
  141. StructName: "ExaCustomer",
  142. PackageName: "example",
  143. IsNew: true,
  144. },
  145. },
  146. }
  147. for _, tt := range tests {
  148. t.Run(tt.name, func(t *testing.T) {
  149. a := &PackageInitializeGorm{
  150. Type: tt.fields.Type,
  151. Path: tt.fields.Path,
  152. ImportPath: tt.fields.ImportPath,
  153. StructName: tt.fields.StructName,
  154. PackageName: tt.fields.PackageName,
  155. IsNew: tt.fields.IsNew,
  156. }
  157. file, err := a.Parse(a.Path, nil)
  158. if err != nil {
  159. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  160. }
  161. a.Rollback(file)
  162. err = a.Format(a.Path, nil, file)
  163. if (err != nil) != tt.wantErr {
  164. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  165. }
  166. })
  167. }
  168. }