service_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "testing"
  7. "go-common/app/admin/ep/saga/conf"
  8. "go-common/app/admin/ep/saga/model"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "github.com/xanzy/go-gitlab"
  11. )
  12. var (
  13. srv *Service
  14. )
  15. func TestMain(m *testing.M) {
  16. var err error
  17. flag.Set("conf", "../cmd/saga-admin-test.toml")
  18. if err = conf.Init(); err != nil {
  19. panic(err)
  20. }
  21. srv = New()
  22. os.Exit(m.Run())
  23. }
  24. // TestInsertDB ...
  25. func TestInsertDB(t *testing.T) {
  26. Convey("insertDB", t, func() {
  27. p := &gitlab.Project{
  28. ID: 11,
  29. Name: "test",
  30. Description: "[主站 android java] test",
  31. WebURL: "http://gitlab.bilibili.co/platform/go-common",
  32. SSHURLToRepo: "git@gitlab.bilibili.co:platform/go-common.git",
  33. DefaultBranch: "master",
  34. Namespace: &gitlab.ProjectNamespace{
  35. Name: "mytest",
  36. Kind: "group",
  37. },
  38. }
  39. err := srv.insertDB(p)
  40. So(err, ShouldBeNil)
  41. projectInfo, err := srv.dao.ProjectInfoByID(p.ID)
  42. So(err, ShouldBeNil)
  43. So(projectInfo.ProjectID, ShouldEqual, 11)
  44. So(projectInfo.Name, ShouldEqual, "test")
  45. So(projectInfo.WebURL, ShouldEqual, "http://gitlab.bilibili.co/platform/go-common")
  46. So(projectInfo.Repo, ShouldEqual, "git@gitlab.bilibili.co:platform/go-common.git")
  47. So(projectInfo.DefaultBranch, ShouldEqual, "master")
  48. So(projectInfo.Department, ShouldEqual, "主站")
  49. So(projectInfo.Business, ShouldEqual, "android")
  50. So(projectInfo.Language, ShouldEqual, "java")
  51. So(projectInfo.SpaceName, ShouldEqual, "mytest")
  52. So(projectInfo.SpaceKind, ShouldEqual, "group")
  53. })
  54. }
  55. // TestResolveDes ...
  56. func TestResolveDes(t *testing.T) {
  57. Convey("resolveDes", t, func() {
  58. s := "[主站 android java] test"
  59. department, business, language, parseFail := parseDes(s)
  60. So(department, ShouldEqual, "主站")
  61. So(business, ShouldEqual, "android")
  62. So(language, ShouldEqual, "java")
  63. So(parseFail, ShouldEqual, false)
  64. })
  65. }
  66. // TestCollectProject ...
  67. func TestCollectProject(t *testing.T) {
  68. Convey("CollectProject", t, func() {
  69. err := srv.CollectProject(context.Background())
  70. So(err, ShouldBeNil)
  71. })
  72. }
  73. // TestPushMsg ...
  74. func TestPushMsg(t *testing.T) {
  75. Convey("TestPushMsg", t, func() {
  76. var err error
  77. result := &model.SyncResult{}
  78. for i := 0; i < 10; i++ {
  79. errData := &model.FailData{
  80. ChildID: i,
  81. }
  82. result.FailData = append(result.FailData, errData)
  83. }
  84. err = srv.WechatFailData(model.DataTypeJob, 888, result, nil)
  85. So(err, ShouldBeNil)
  86. })
  87. }
  88. func TestServiceSaveAggregateBranchDatabase(t *testing.T) {
  89. Convey("test service aggregate branch include special char", t, func(ctx C) {
  90. var (
  91. branch = &model.AggregateBranches{
  92. ID: 4,
  93. ProjectID: 666,
  94. ProjectName: "六六大顺",
  95. BranchName: "666",
  96. BranchUserName: "吴维",
  97. BranchMaster: "wuwei",
  98. Behind: 1111,
  99. Ahead: 2222,
  100. LatestSyncTime: nil,
  101. LatestUpdateTime: nil,
  102. IsDeleted: true,
  103. }
  104. //total int
  105. err error
  106. )
  107. Convey("SaveAggregateBranchDatabase", func(ctx C) {
  108. err = srv.SaveAggregateBranchDatabase(context.TODO(), branch)
  109. Convey("Then err should be nil.", func(ctx C) {
  110. So(err, ShouldBeNil)
  111. })
  112. })
  113. })
  114. }