package_module_enter_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPackageModuleEnter_Rollback(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. StructName string
  13. AppName string
  14. GroupName string
  15. ModuleName string
  16. PackageName string
  17. ServiceName string
  18. }
  19. tests := []struct {
  20. name string
  21. fields fields
  22. wantErr bool
  23. }{
  24. {
  25. name: "测试 FileUploadAndDownloadRouter 回滚",
  26. fields: fields{
  27. Type: TypePackageRouterModuleEnter,
  28. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "router", "example", "enter.go"),
  29. ImportPath: `api "github.com/flipped-aurora/gin-vue-admin/server/api/v1"`,
  30. StructName: "FileUploadAndDownloadRouter",
  31. AppName: "ApiGroupApp",
  32. GroupName: "ExampleApiGroup",
  33. ModuleName: "exaFileUploadAndDownloadApi",
  34. PackageName: "api",
  35. ServiceName: "FileUploadAndDownloadApi",
  36. },
  37. wantErr: false,
  38. },
  39. {
  40. name: "测试 FileUploadAndDownloadApi 回滚",
  41. fields: fields{
  42. Type: TypePackageApiModuleEnter,
  43. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "api", "v1", "example", "enter.go"),
  44. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/service"`,
  45. StructName: "FileUploadAndDownloadApi",
  46. AppName: "ServiceGroupApp",
  47. GroupName: "ExampleServiceGroup",
  48. ModuleName: "fileUploadAndDownloadService",
  49. PackageName: "service",
  50. ServiceName: "FileUploadAndDownloadService",
  51. },
  52. wantErr: false,
  53. },
  54. {
  55. name: "测试 FileUploadAndDownloadService 回滚",
  56. fields: fields{
  57. Type: TypePackageServiceModuleEnter,
  58. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "service", "example", "enter.go"),
  59. ImportPath: ``,
  60. StructName: "FileUploadAndDownloadService",
  61. AppName: "",
  62. GroupName: "",
  63. ModuleName: "",
  64. PackageName: "",
  65. ServiceName: "",
  66. },
  67. wantErr: false,
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. a := &PackageModuleEnter{
  73. Type: tt.fields.Type,
  74. Path: tt.fields.Path,
  75. ImportPath: tt.fields.ImportPath,
  76. StructName: tt.fields.StructName,
  77. AppName: tt.fields.AppName,
  78. GroupName: tt.fields.GroupName,
  79. ModuleName: tt.fields.ModuleName,
  80. PackageName: tt.fields.PackageName,
  81. ServiceName: tt.fields.ServiceName,
  82. }
  83. file, err := a.Parse(a.Path, nil)
  84. if err != nil {
  85. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  86. }
  87. a.Rollback(file)
  88. err = a.Format(a.Path, nil, file)
  89. if (err != nil) != tt.wantErr {
  90. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  91. }
  92. })
  93. }
  94. }
  95. func TestPackageModuleEnter_Injection(t *testing.T) {
  96. type fields struct {
  97. Type Type
  98. Path string
  99. ImportPath string
  100. StructName string
  101. AppName string
  102. GroupName string
  103. ModuleName string
  104. PackageName string
  105. ServiceName string
  106. }
  107. tests := []struct {
  108. name string
  109. fields fields
  110. wantErr bool
  111. }{
  112. {
  113. name: "测试 FileUploadAndDownloadRouter 注入",
  114. fields: fields{
  115. Type: TypePackageRouterModuleEnter,
  116. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "router", "example", "enter.go"),
  117. ImportPath: `api "github.com/flipped-aurora/gin-vue-admin/server/api/v1"`,
  118. StructName: "FileUploadAndDownloadRouter",
  119. AppName: "ApiGroupApp",
  120. GroupName: "ExampleApiGroup",
  121. ModuleName: "exaFileUploadAndDownloadApi",
  122. PackageName: "api",
  123. ServiceName: "FileUploadAndDownloadApi",
  124. },
  125. wantErr: false,
  126. },
  127. {
  128. name: "测试 FileUploadAndDownloadApi 注入",
  129. fields: fields{
  130. Type: TypePackageApiModuleEnter,
  131. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "api", "v1", "example", "enter.go"),
  132. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/service"`,
  133. StructName: "FileUploadAndDownloadApi",
  134. AppName: "ServiceGroupApp",
  135. GroupName: "ExampleServiceGroup",
  136. ModuleName: "fileUploadAndDownloadService",
  137. PackageName: "service",
  138. ServiceName: "FileUploadAndDownloadService",
  139. },
  140. wantErr: false,
  141. },
  142. {
  143. name: "测试 FileUploadAndDownloadService 注入",
  144. fields: fields{
  145. Type: TypePackageServiceModuleEnter,
  146. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "service", "example", "enter.go"),
  147. ImportPath: ``,
  148. StructName: "FileUploadAndDownloadService",
  149. AppName: "",
  150. GroupName: "",
  151. ModuleName: "",
  152. PackageName: "",
  153. ServiceName: "",
  154. },
  155. wantErr: false,
  156. },
  157. }
  158. for _, tt := range tests {
  159. t.Run(tt.name, func(t *testing.T) {
  160. a := &PackageModuleEnter{
  161. Type: tt.fields.Type,
  162. Path: tt.fields.Path,
  163. ImportPath: tt.fields.ImportPath,
  164. StructName: tt.fields.StructName,
  165. AppName: tt.fields.AppName,
  166. GroupName: tt.fields.GroupName,
  167. ModuleName: tt.fields.ModuleName,
  168. PackageName: tt.fields.PackageName,
  169. ServiceName: tt.fields.ServiceName,
  170. }
  171. file, err := a.Parse(a.Path, nil)
  172. if err != nil {
  173. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  174. }
  175. a.Injection(file)
  176. err = a.Format(a.Path, nil, file)
  177. if (err != nil) != tt.wantErr {
  178. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  179. }
  180. })
  181. }
  182. }