ut_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "github.com/smartystreets/goconvey/convey"
  7. "gopkg.in/h2non/gock.v1"
  8. )
  9. func TestDaoParseUTFiles(t *testing.T) {
  10. convey.Convey("ParseUTFiles", t, func(ctx convey.C) {
  11. var (
  12. c = context.Background()
  13. url = "http://uat-i0.hdslb.com/bfs/test/03af5d0707e4c6ec41a3eb797c8e9897f124515c.html"
  14. )
  15. gock.Off()
  16. d.client.SetTransport(http.DefaultClient.Transport)
  17. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  18. pkgs, err := d.ParseUTFiles(c, url)
  19. //t.Logf("the pkg[0] is PKG: %s, Coverage: %v, HTMLURL: %s", pkgs[0].PKG, pkgs[0].Coverage, pkgs[0].HTMLURL)
  20. ctx.Convey("Then err should be nil.pkgs should not be nil.", func(ctx convey.C) {
  21. ctx.So(pkgs, convey.ShouldNotBeNil)
  22. ctx.So(err, convey.ShouldBeNil)
  23. })
  24. })
  25. })
  26. }
  27. func TestDaoGitCommitInfo(t *testing.T) {
  28. convey.Convey("GitCommitInfo", t, func(ctx convey.C) {
  29. var (
  30. c = context.Background()
  31. commitID = "ae1377033a11ca85a19bca365af32a5b0ebea31c"
  32. )
  33. gock.Off()
  34. d.client.SetTransport(http.DefaultClient.Transport)
  35. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  36. commit, err := d.GitLabCommits(c, commitID)
  37. ctx.Convey("Then err should be nil. gitcommit should not be nil.", func(ctx convey.C) {
  38. ctx.So(err, convey.ShouldBeNil)
  39. ctx.So(commit, convey.ShouldNotBeNil)
  40. })
  41. })
  42. })
  43. }
  44. func TestDaoSendWechatToUsers(t *testing.T) {
  45. convey.Convey("SendWechatToUsers", t, func(ctx convey.C) {
  46. var (
  47. c = context.Background()
  48. users = []string{"zhaobingqing"}
  49. msg = "给你我的小心心❤"
  50. )
  51. d.client.SetTransport(gock.DefaultTransport)
  52. ctx.Convey("When everything gose postive", func(ctx convey.C) {
  53. httpMock("POST", _sagaWechatURL+"/message/send").Reply(200).JSON(`{"code":0,"message":"0"}`)
  54. err := d.SendWechatToUsers(c, users, msg)
  55. ctx.Convey("Then err should be nil", func(ctx convey.C) {
  56. ctx.So(err, convey.ShouldBeNil)
  57. })
  58. })
  59. ctx.Convey("When http status != 200", func(ctx convey.C) {
  60. httpMock("POST", _sagaWechatURL+"/message/send").Reply(404)
  61. err := d.SendWechatToUsers(c, users, msg)
  62. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  63. ctx.So(err, convey.ShouldNotBeNil)
  64. })
  65. })
  66. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  67. httpMock("POST", _sagaWechatURL+"/message/send").Reply(200).JSON(`{"code":-401,"message":"0"}`)
  68. err := d.SendWechatToUsers(c, users, msg)
  69. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  70. ctx.So(err, convey.ShouldNotBeNil)
  71. })
  72. })
  73. ctx.Reset(func() {
  74. gock.Off()
  75. d.client.SetTransport(http.DefaultClient.Transport)
  76. })
  77. })
  78. }
  79. func TestDaoSendWechatToGroup(t *testing.T) {
  80. convey.Convey("SendWechatToGroup", t, func(ctx convey.C) {
  81. var (
  82. msg = "信息测试~@002157"
  83. c = context.Background()
  84. )
  85. d.client.SetTransport(gock.DefaultTransport)
  86. ctx.Convey("When everything gose postive", func(ctx convey.C) {
  87. httpMock("POST", _sagaWechatURL+"/appchat/send").Reply(200).JSON(`{"code":0,"message":"0"}`)
  88. err := d.SendWechatToGroup(c, d.c.WeChat.ChatID, msg)
  89. ctx.Convey("Then err should be nil", func(ctx convey.C) {
  90. ctx.So(err, convey.ShouldBeNil)
  91. })
  92. })
  93. ctx.Convey("When http status != 200", func(ctx convey.C) {
  94. httpMock("POST", _sagaWechatURL+"/appchat/send").Reply(404)
  95. err := d.SendWechatToGroup(c, d.c.WeChat.ChatID, msg)
  96. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  97. ctx.So(err, convey.ShouldNotBeNil)
  98. })
  99. })
  100. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  101. httpMock("POST", _sagaWechatURL+"/appchat/send").Reply(200).JSON(`{"code":-401,"message":"0"}`)
  102. err := d.SendWechatToGroup(c, d.c.WeChat.ChatID, msg)
  103. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  104. ctx.So(err, convey.ShouldNotBeNil)
  105. })
  106. })
  107. ctx.Reset(func() {
  108. gock.Off()
  109. d.client.SetTransport(http.DefaultClient.Transport)
  110. })
  111. })
  112. }
  113. func TestDaoGetCoverage(t *testing.T) {
  114. convey.Convey("GetCoverage", t, func(ctx convey.C) {
  115. var (
  116. c = context.Background()
  117. commitID = "8d2f1b49661c7089e2b595eafff326033a138c23"
  118. pkg = "go-common/app/admin/main/apm"
  119. )
  120. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  121. cov, err := d.GetCoverage(c, commitID, pkg)
  122. ctx.Convey("Then err should be nil.cov should not be nil.", func(ctx convey.C) {
  123. // t.Logf("the cov of %s is %.2f", pkg, cov)
  124. ctx.So(cov, convey.ShouldNotBeNil)
  125. ctx.So(err, convey.ShouldBeNil)
  126. })
  127. })
  128. ctx.Convey("When the pkg is empty string", func(ctx convey.C) {
  129. pkg = ""
  130. _, err := d.GetCoverage(c, commitID, pkg)
  131. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  132. ctx.So(err, convey.ShouldNotBeNil)
  133. })
  134. })
  135. })
  136. }