db_sync_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. "go-common/app/admin/ep/saga/model"
  7. "go-common/app/admin/ep/saga/service/utils"
  8. "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestDaoCommit(t *testing.T) {
  11. convey.Convey("test dao commit", t, func(ctx convey.C) {
  12. var (
  13. commit *model.StatisticsCommits
  14. err error
  15. total int
  16. )
  17. ctx.Convey("When project not exist", func(ctx convey.C) {
  18. total, err = d.HasCommit(55555, "55555")
  19. ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
  20. ctx.So(err, convey.ShouldBeNil)
  21. ctx.So(total, convey.ShouldEqual, 0)
  22. })
  23. err = d.DelCommit(55555, "55555")
  24. ctx.Convey("DelCommit err should be nil.", func(ctx convey.C) {
  25. ctx.So(err, convey.ShouldBeNil)
  26. })
  27. })
  28. ctx.Convey("When project exist", func(ctx convey.C) {
  29. var (
  30. commitID = "11111"
  31. PorjID = 11111
  32. )
  33. ctx.Convey("create commit", func(ctx convey.C) {
  34. info := &model.StatisticsCommits{
  35. CommitID: commitID,
  36. ProjectID: PorjID,
  37. Title: "test",
  38. }
  39. err = d.CreateCommit(info)
  40. ctx.Convey("CreateCommit err should be nil.", func(ctx convey.C) {
  41. ctx.So(err, convey.ShouldBeNil)
  42. })
  43. })
  44. ctx.Convey("has commit after create", func(ctx convey.C) {
  45. total, err = d.HasCommit(PorjID, commitID)
  46. ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
  47. ctx.So(err, convey.ShouldBeNil)
  48. ctx.So(total, convey.ShouldEqual, 1)
  49. })
  50. })
  51. ctx.Convey("query commit", func(ctx convey.C) {
  52. commit, err = d.QueryCommitByID(PorjID, commitID)
  53. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  54. ctx.So(err, convey.ShouldBeNil)
  55. ctx.So(commit.Title, convey.ShouldEqual, "test")
  56. })
  57. })
  58. ctx.Convey("create second temp commit", func(ctx convey.C) {
  59. temp := &model.StatisticsCommits{
  60. CommitID: "33333",
  61. ProjectID: 33333,
  62. Title: "temp",
  63. }
  64. err = d.CreateCommit(temp)
  65. ctx.Convey("CreateCommit again err should be nil.", func(ctx convey.C) {
  66. ctx.So(err, convey.ShouldBeNil)
  67. })
  68. })
  69. ctx.Convey("has commit when exist two rows", func(ctx convey.C) {
  70. total, err = d.HasCommit(PorjID, commitID)
  71. ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
  72. ctx.So(err, convey.ShouldBeNil)
  73. ctx.So(total, convey.ShouldEqual, 1)
  74. })
  75. })
  76. ctx.Convey("update commit", func(ctx convey.C) {
  77. updateInfo := &model.StatisticsCommits{
  78. CommitID: commitID,
  79. ProjectID: PorjID,
  80. Title: "update",
  81. }
  82. err = d.UpdateCommit(PorjID, commitID, updateInfo)
  83. ctx.Convey("UpdateCommit err should be nil.", func(ctx convey.C) {
  84. ctx.So(err, convey.ShouldBeNil)
  85. })
  86. })
  87. ctx.Convey("query commit after update", func(ctx convey.C) {
  88. commit, err = d.QueryCommitByID(PorjID, commitID)
  89. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  90. ctx.So(err, convey.ShouldBeNil)
  91. ctx.So(commit.Title, convey.ShouldEqual, "update")
  92. })
  93. })
  94. ctx.Convey("update temp commit", func(ctx convey.C) {
  95. updateInfo := &model.StatisticsCommits{
  96. CommitID: "33333",
  97. ProjectID: 33333,
  98. Title: "update temp",
  99. }
  100. err = d.UpdateCommit(33333, "33333", updateInfo)
  101. ctx.Convey("UpdateCommit temp err should be nil.", func(ctx convey.C) {
  102. ctx.So(err, convey.ShouldBeNil)
  103. })
  104. })
  105. ctx.Convey("query temp commit after update", func(ctx convey.C) {
  106. commit, err = d.QueryCommitByID(33333, "33333")
  107. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  108. ctx.So(err, convey.ShouldBeNil)
  109. ctx.So(commit.Title, convey.ShouldEqual, "update temp")
  110. })
  111. })
  112. ctx.Convey("delete commit", func(ctx convey.C) {
  113. err = d.DelCommit(PorjID, commitID)
  114. ctx.Convey("DelCommit commit exist.", func(ctx convey.C) {
  115. ctx.So(err, convey.ShouldBeNil)
  116. })
  117. })
  118. ctx.Convey("has commit after delete", func(ctx convey.C) {
  119. total, err = d.HasCommit(PorjID, commitID)
  120. ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
  121. ctx.So(err, convey.ShouldBeNil)
  122. ctx.So(total, convey.ShouldEqual, 0)
  123. })
  124. })
  125. ctx.Convey("has temp commit when not delete", func(ctx convey.C) {
  126. total, err = d.HasCommit(33333, "33333")
  127. ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
  128. ctx.So(err, convey.ShouldBeNil)
  129. ctx.So(total, convey.ShouldEqual, 1)
  130. })
  131. })
  132. ctx.Convey("delete temp commit", func(ctx convey.C) {
  133. err = d.DelCommit(33333, "33333")
  134. ctx.Convey("DelCommit commit exist.", func(ctx convey.C) {
  135. ctx.So(err, convey.ShouldBeNil)
  136. })
  137. })
  138. ctx.Convey("has tem commit after delete", func(ctx convey.C) {
  139. total, err = d.HasCommit(33333, "33333")
  140. ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
  141. ctx.So(err, convey.ShouldBeNil)
  142. ctx.So(total, convey.ShouldEqual, 0)
  143. })
  144. })
  145. })
  146. })
  147. }
  148. func TestDaoCommitSpe(t *testing.T) {
  149. convey.Convey("test dao commit include special char", t, func(ctx convey.C) {
  150. var err error
  151. commitSpe := &model.StatisticsCommits{
  152. CommitID: "22222",
  153. ProjectID: 22222,
  154. Title: "🇭相关",
  155. }
  156. ctx.Convey("create commit", func(ctx convey.C) {
  157. err = d.CreateCommit(commitSpe)
  158. ctx.Convey("CreateCommit err should not be nil.", func(ctx convey.C) {
  159. ctx.So(err, convey.ShouldNotBeNil)
  160. ctx.So(err.Error(), convey.ShouldContainSubstring, "Incorrect string value")
  161. })
  162. })
  163. ctx.Convey("create commit after to ascii", func(ctx convey.C) {
  164. commitSpe.Title = strconv.QuoteToASCII(commitSpe.Title)
  165. err = d.CreateCommit(commitSpe)
  166. ctx.Convey("CreateCommit err should be nil after to ascii.", func(ctx convey.C) {
  167. ctx.So(err, convey.ShouldBeNil)
  168. })
  169. })
  170. ctx.Convey("delete commit", func(ctx convey.C) {
  171. err = d.DelCommit(22222, "22222")
  172. ctx.Convey("DelCommit err should be nil.", func(ctx convey.C) {
  173. ctx.So(err, convey.ShouldBeNil)
  174. })
  175. })
  176. })
  177. }
  178. func TestQueryPipelinesByTime(t *testing.T) {
  179. convey.Convey("QueryPipelinesByTime", t, func(ctx convey.C) {
  180. var (
  181. req = &model.PipelineDataReq{
  182. Branch: "master",
  183. State: "success",
  184. User: "wangweizhen",
  185. }
  186. since = `2018-12-01 00:00:00`
  187. until = `2018-12-02 00:00:00`
  188. )
  189. ctx.Convey("query pipelines info", func(ctx convey.C) {
  190. total, statNum, _, err := d.QueryPipelinesByTime(682, req, since, until)
  191. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  192. ctx.So(err, convey.ShouldBeNil)
  193. ctx.So(total, convey.ShouldBeGreaterThan, 1)
  194. ctx.So(statNum, convey.ShouldBeGreaterThan, 1)
  195. })
  196. })
  197. })
  198. }
  199. func TestMRByProjectID(t *testing.T) {
  200. convey.Convey("QueryMRByProjectID", t, func(ctx convey.C) {
  201. since, until := utils.CalSyncTime()
  202. ctx.Convey("query MRByProjectID info", func(ctx convey.C) {
  203. mrs, err := d.MRByProjectID(context.TODO(), 682, since, until)
  204. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  205. ctx.So(err, convey.ShouldBeNil)
  206. ctx.So(len(mrs), convey.ShouldBeGreaterThan, 0)
  207. })
  208. })
  209. })
  210. }
  211. func TestDaoBranchSpe(t *testing.T) {
  212. convey.Convey("test dao branch include special char", t, func(ctx convey.C) {
  213. var (
  214. branch = &model.StatisticsBranches{
  215. ProjectID: 666,
  216. ProjectName: "六六大顺",
  217. CommitID: "6661",
  218. BranchName: "666",
  219. Protected: false,
  220. Merged: false,
  221. DevelopersCanPush: false,
  222. DevelopersCanMerge: false,
  223. IsDeleted: true,
  224. }
  225. total int
  226. err error
  227. )
  228. ctx.Convey("create branch", func(ctx convey.C) {
  229. err = d.CreateBranch(context.TODO(), branch)
  230. ctx.Convey("CreateBranch err should not be nil.", func(ctx convey.C) {
  231. ctx.So(err, convey.ShouldBeNil)
  232. })
  233. })
  234. ctx.Convey("has branch", func(ctx convey.C) {
  235. total, err = d.HasBranch(context.TODO(), 666, "666")
  236. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  237. ctx.So(err, convey.ShouldBeNil)
  238. ctx.So(total, convey.ShouldBeGreaterThan, 0)
  239. })
  240. })
  241. ctx.Convey("update branch", func(ctx convey.C) {
  242. err = d.UpdateBranch(context.TODO(), 666, "666", branch)
  243. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  244. ctx.So(err, convey.ShouldBeNil)
  245. })
  246. })
  247. })
  248. }