package_initialize_router_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPackageInitializeRouter_Injection(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. AppName string
  13. GroupName string
  14. ModuleName string
  15. PackageName string
  16. FunctionName string
  17. RouterGroupName string
  18. }
  19. tests := []struct {
  20. name string
  21. fields fields
  22. wantErr bool
  23. }{
  24. {
  25. name: "测试 InitCustomerRouter 注入",
  26. fields: fields{
  27. Type: TypePackageInitializeRouter,
  28. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go"),
  29. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router"`,
  30. AppName: "RouterGroupApp",
  31. GroupName: "Example",
  32. ModuleName: "exampleRouter",
  33. PackageName: "router",
  34. FunctionName: "InitCustomerRouter",
  35. RouterGroupName: "privateGroup",
  36. },
  37. wantErr: false,
  38. },
  39. {
  40. name: "测试 InitFileUploadAndDownloadRouter 注入",
  41. fields: fields{
  42. Type: TypePackageInitializeRouter,
  43. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go"),
  44. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router"`,
  45. AppName: "RouterGroupApp",
  46. GroupName: "Example",
  47. ModuleName: "exampleRouter",
  48. PackageName: "router",
  49. FunctionName: "InitFileUploadAndDownloadRouter",
  50. RouterGroupName: "privateGroup",
  51. },
  52. wantErr: false,
  53. },
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. a := &PackageInitializeRouter{
  58. Type: tt.fields.Type,
  59. Path: tt.fields.Path,
  60. ImportPath: tt.fields.ImportPath,
  61. AppName: tt.fields.AppName,
  62. GroupName: tt.fields.GroupName,
  63. ModuleName: tt.fields.ModuleName,
  64. PackageName: tt.fields.PackageName,
  65. FunctionName: tt.fields.FunctionName,
  66. RouterGroupName: tt.fields.RouterGroupName,
  67. LeftRouterGroupName: "privateGroup",
  68. RightRouterGroupName: "publicGroup",
  69. }
  70. file, err := a.Parse(a.Path, nil)
  71. if err != nil {
  72. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  73. }
  74. a.Injection(file)
  75. err = a.Format(a.Path, nil, file)
  76. if (err != nil) != tt.wantErr {
  77. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  78. }
  79. })
  80. }
  81. }
  82. func TestPackageInitializeRouter_Rollback(t *testing.T) {
  83. type fields struct {
  84. Type Type
  85. Path string
  86. ImportPath string
  87. AppName string
  88. GroupName string
  89. ModuleName string
  90. PackageName string
  91. FunctionName string
  92. RouterGroupName string
  93. }
  94. tests := []struct {
  95. name string
  96. fields fields
  97. wantErr bool
  98. }{
  99. {
  100. name: "测试 InitCustomerRouter 回滚",
  101. fields: fields{
  102. Type: TypePackageInitializeRouter,
  103. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go"),
  104. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router"`,
  105. AppName: "RouterGroupApp",
  106. GroupName: "Example",
  107. ModuleName: "exampleRouter",
  108. PackageName: "router",
  109. FunctionName: "InitCustomerRouter",
  110. RouterGroupName: "privateGroup",
  111. },
  112. wantErr: false,
  113. },
  114. {
  115. name: "测试 InitFileUploadAndDownloadRouter 回滚",
  116. fields: fields{
  117. Type: TypePackageInitializeRouter,
  118. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go"),
  119. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router"`,
  120. AppName: "RouterGroupApp",
  121. GroupName: "Example",
  122. ModuleName: "exampleRouter",
  123. PackageName: "router",
  124. FunctionName: "InitFileUploadAndDownloadRouter",
  125. RouterGroupName: "privateGroup",
  126. },
  127. wantErr: false,
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. a := &PackageInitializeRouter{
  133. Type: tt.fields.Type,
  134. Path: tt.fields.Path,
  135. ImportPath: tt.fields.ImportPath,
  136. AppName: tt.fields.AppName,
  137. GroupName: tt.fields.GroupName,
  138. ModuleName: tt.fields.ModuleName,
  139. PackageName: tt.fields.PackageName,
  140. FunctionName: tt.fields.FunctionName,
  141. RouterGroupName: tt.fields.RouterGroupName,
  142. }
  143. file, err := a.Parse(a.Path, nil)
  144. if err != nil {
  145. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  146. }
  147. a.Rollback(file)
  148. err = a.Format(a.Path, nil, file)
  149. if (err != nil) != tt.wantErr {
  150. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  151. }
  152. })
  153. }
  154. }