redis_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. "go-common/app/tool/saga/model"
  7. "go-common/library/cache/redis"
  8. "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestDaoMergeTaskKey(t *testing.T) {
  11. convey.Convey("mergeTaskKey", t, func(ctx convey.C) {
  12. var (
  13. taskType = int(111)
  14. )
  15. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  16. p1 := mergeTaskKey(taskType)
  17. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  18. ctx.So(p1, convey.ShouldEqual, "saga_task_111")
  19. })
  20. })
  21. })
  22. }
  23. func TestDaoMrIIDKey(t *testing.T) {
  24. convey.Convey("mrIIDKey", t, func(ctx convey.C) {
  25. var (
  26. mrIID = int(222)
  27. )
  28. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  29. p1 := mrIIDKey(mrIID)
  30. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  31. ctx.So(p1, convey.ShouldEqual, "saga_mrIID_222")
  32. })
  33. })
  34. })
  35. }
  36. func TestDaoPingRedis(t *testing.T) {
  37. convey.Convey("pingRedis", t, func(ctx convey.C) {
  38. var (
  39. c = context.Background()
  40. )
  41. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  42. err := d.pingRedis(c)
  43. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  44. ctx.So(err, convey.ShouldBeNil)
  45. })
  46. })
  47. })
  48. }
  49. func TestDaoAddMRIID(t *testing.T) {
  50. convey.Convey("AddMRIID", t, func(ctx convey.C) {
  51. var (
  52. c = context.Background()
  53. mrIID = int(333)
  54. expire = int(1800)
  55. conn = d.redis.Get(c)
  56. )
  57. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  58. err := d.AddMRIID(c, mrIID, expire)
  59. mrID, err := redis.Int(conn.Do("GET", "saga_mrIID_333"))
  60. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  61. ctx.So(err, convey.ShouldBeNil)
  62. // 检查redis存储结果
  63. ctx.So(mrID, convey.ShouldEqual, mrIID)
  64. })
  65. })
  66. })
  67. }
  68. func TestDaoExistMRIID(t *testing.T) {
  69. convey.Convey("ExistMRIID", t, func(ctx convey.C) {
  70. var (
  71. c = context.Background()
  72. mrIID = int(444)
  73. conn = d.redis.Get(c)
  74. )
  75. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  76. conn.Do("DEL", "saga_mrIID_444")
  77. ok, err := d.ExistMRIID(c, mrIID)
  78. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  79. ctx.So(err, convey.ShouldBeNil)
  80. ctx.So(ok, convey.ShouldBeFalse)
  81. })
  82. })
  83. ctx.Convey("When MRIID exist", func(ctx convey.C) {
  84. conn.Do("SET", "saga_mrIID_444", 1)
  85. ok, err := d.ExistMRIID(c, mrIID)
  86. ctx.Convey("Then ok should not be nil.", func(ctx convey.C) {
  87. ctx.So(err, convey.ShouldBeNil)
  88. ctx.So(ok, convey.ShouldBeTrue)
  89. })
  90. })
  91. })
  92. }
  93. func TestDaoDeleteMRIID(t *testing.T) {
  94. convey.Convey("DeleteMRIID", t, func(ctx convey.C) {
  95. var (
  96. c = context.Background()
  97. mrIID = int(555)
  98. conn = d.redis.Get(c)
  99. )
  100. ctx.Convey("When data not exist", func(ctx convey.C) {
  101. err := d.DeleteMRIID(c, mrIID)
  102. value, _ := redis.Int(conn.Do("GET", "saga_mrIID_555"))
  103. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  104. ctx.So(err, convey.ShouldBeNil)
  105. ctx.So(value, convey.ShouldEqual, 0)
  106. })
  107. })
  108. ctx.Convey("When data exist", func(ctx convey.C) {
  109. redis.String(conn.Do("SET", "saga_mrIID_555", "Test"))
  110. err := d.DeleteMRIID(c, mrIID)
  111. value, _ := redis.Int(conn.Do("GET", "saga_mrIID_555"))
  112. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  113. ctx.So(err, convey.ShouldBeNil)
  114. ctx.So(value, convey.ShouldEqual, 0)
  115. })
  116. })
  117. })
  118. }
  119. func TestDaoPushMergeTask(t *testing.T) {
  120. convey.Convey("PushMergeTask", t, func(ctx convey.C) {
  121. var (
  122. c = context.Background()
  123. taskType = int(666)
  124. taskInfo = &model.TaskInfo{NoteID: 111, Event: nil, Repo: nil}
  125. conn = d.redis.Get(c)
  126. )
  127. bs, _ := json.Marshal(taskInfo)
  128. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  129. err := d.PushMergeTask(c, taskType, taskInfo)
  130. value, _ := redis.Bytes(conn.Do("LPOP", "saga_task_666"))
  131. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  132. ctx.So(err, convey.ShouldBeNil)
  133. // push 运行函数 pop 检验结果
  134. ctx.So(string(value), convey.ShouldEqual, string(bs))
  135. })
  136. })
  137. })
  138. }
  139. func TestDaoDeleteMergeTask(t *testing.T) {
  140. convey.Convey("DeleteMergeTask", t, func(ctx convey.C) {
  141. var (
  142. c = context.Background()
  143. taskType = int(777)
  144. taskInfo = &model.TaskInfo{NoteID: 777, Event: nil, Repo: nil}
  145. conn = d.redis.Get(c)
  146. )
  147. ctx.Convey("When data is not exist", func(ctx convey.C) {
  148. err := d.DeleteMergeTask(c, taskType, taskInfo)
  149. value, _ := redis.Int(conn.Do("LPOP", "saga_task_777"))
  150. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  151. ctx.So(err, convey.ShouldBeNil)
  152. ctx.So(value, convey.ShouldEqual, 0)
  153. })
  154. })
  155. ctx.Convey("When data is exist", func(ctx convey.C) {
  156. taskInfoJSON, _ := json.Marshal(taskInfo)
  157. redis.String(conn.Do("LPUSH", "saga_task_777", taskInfoJSON))
  158. err := d.DeleteMergeTask(c, taskType, taskInfo)
  159. value, _ := redis.Int(conn.Do("LPOP", "saga_task_777"))
  160. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  161. ctx.So(err, convey.ShouldBeNil)
  162. ctx.So(value, convey.ShouldEqual, 0)
  163. })
  164. })
  165. })
  166. }
  167. func TestDaoMergeTasks(t *testing.T) {
  168. convey.Convey("MergeTasks", t, func(ctx convey.C) {
  169. var (
  170. c = context.Background()
  171. taskType = int(888)
  172. conn = d.redis.Get(c)
  173. taskInfo = &model.TaskInfo{NoteID: 888, Event: nil, Repo: nil}
  174. )
  175. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  176. taskInfoJSON, _ := json.Marshal(taskInfo)
  177. conn.Do("LPUSH", "saga_task_888", taskInfoJSON)
  178. count, taskInfos, err := d.MergeTasks(c, taskType)
  179. taskInfoFirst, _ := json.Marshal(taskInfos[0])
  180. ctx.Convey("Then err should be nil.count,taskInfos should not be nil.", func(ctx convey.C) {
  181. ctx.So(err, convey.ShouldBeNil)
  182. ctx.So(string(taskInfoFirst), convey.ShouldEqual, string(taskInfoJSON))
  183. ctx.So(count, convey.ShouldNotEqual, 0)
  184. })
  185. })
  186. })
  187. }
  188. func TestDaoMergeInfoKey(t *testing.T) {
  189. convey.Convey("mergeInfoKey", t, func(ctx convey.C) {
  190. var (
  191. projID = int(999)
  192. branch = "test"
  193. )
  194. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  195. p1 := mergeInfoKey(projID, branch)
  196. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  197. ctx.So(p1, convey.ShouldEqual, "saga_mergeInfo_999_test")
  198. })
  199. })
  200. })
  201. }
  202. func TestDaoPathOwnerKey(t *testing.T) {
  203. convey.Convey("pathOwnerKey", t, func(ctx convey.C) {
  204. var (
  205. projID = int(1111)
  206. branch = "test"
  207. path = "."
  208. )
  209. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  210. p1 := pathOwnerKey(projID, branch, path)
  211. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  212. ctx.So(p1, convey.ShouldEqual, "saga_PathOwner_1111_test_.")
  213. })
  214. })
  215. })
  216. }
  217. func TestDaoPathReviewerKey(t *testing.T) {
  218. convey.Convey("pathReviewerKey", t, func(ctx convey.C) {
  219. var (
  220. projID = int(2222)
  221. branch = "test"
  222. path = "."
  223. )
  224. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  225. p1 := pathReviewerKey(projID, branch, path)
  226. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  227. ctx.So(p1, convey.ShouldEqual, "saga_PathReviewer_2222_test_.")
  228. })
  229. })
  230. })
  231. }
  232. func TestDaoAuthInfoKey(t *testing.T) {
  233. convey.Convey("authInfoKey", t, func(ctx convey.C) {
  234. var (
  235. projID = int(3333)
  236. mrIID = int(3333)
  237. )
  238. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  239. p1 := authInfoKey(projID, mrIID)
  240. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  241. ctx.So(p1, convey.ShouldEqual, "saga_auth_3333_3333")
  242. })
  243. })
  244. })
  245. }
  246. func TestDaoSetMergeInfo(t *testing.T) {
  247. convey.Convey("SetMergeInfo", t, func(ctx convey.C) {
  248. var (
  249. c = context.Background()
  250. projID = int(4444)
  251. branch = "test"
  252. mergeInfo = &model.MergeInfo{}
  253. conn = d.redis.Get(c)
  254. )
  255. redis.String(conn.Do(""))
  256. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  257. err := d.SetMergeInfo(c, projID, branch, mergeInfo)
  258. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  259. ctx.So(err, convey.ShouldBeNil)
  260. })
  261. })
  262. })
  263. }
  264. func TestDaoMergeInfo(t *testing.T) {
  265. convey.Convey("MergeInfo", t, func(ctx convey.C) {
  266. var (
  267. c = context.Background()
  268. projID = int(5555)
  269. branch = "test"
  270. conn = d.redis.Get(c)
  271. info = []byte(`{"NoteID":111,"Event":null,"Repo":null}`)
  272. )
  273. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  274. conn.Do("SET", "saga_mergeInfo_5555_test", info)
  275. ok, mergeInfo, err := d.MergeInfo(c, projID, branch)
  276. ctx.Convey("Then err should be nil.ok,mergeInfo should not be nil.", func(ctx convey.C) {
  277. ctx.So(err, convey.ShouldBeNil)
  278. ctx.So(mergeInfo, convey.ShouldNotBeNil)
  279. ctx.So(ok, convey.ShouldBeTrue)
  280. })
  281. })
  282. })
  283. }
  284. func TestDaoDeleteMergeInfo(t *testing.T) {
  285. convey.Convey("DeleteMergeInfo", t, func(ctx convey.C) {
  286. var (
  287. c = context.Background()
  288. projID = int(6666)
  289. branch = "test"
  290. conn = d.redis.Get(c)
  291. info = []byte(`{"NoteID":111,"Event":null,"Repo":null}`)
  292. )
  293. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  294. conn.Do("SET", "saga_mergeInfo_6666_test", info)
  295. err := d.DeleteMergeInfo(c, projID, branch)
  296. value, _ := redis.Int(conn.Do("GET", "saga_mergeInfo_6666_test"))
  297. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  298. ctx.So(err, convey.ShouldBeNil)
  299. ctx.So(value, convey.ShouldEqual, 0)
  300. })
  301. })
  302. })
  303. }
  304. func TestDaoPathAuthKey(t *testing.T) {
  305. convey.Convey("pathAuthKey", t, func(ctx convey.C) {
  306. var (
  307. projID = int(7777)
  308. branch = "test"
  309. path = "."
  310. )
  311. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  312. p1 := pathAuthKey(projID, branch, path)
  313. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  314. ctx.So(p1, convey.ShouldEqual, "saga_path_auth_7777_test_.")
  315. })
  316. })
  317. })
  318. }
  319. func TestDaoPathAuthR(t *testing.T) {
  320. convey.Convey("PathAuthR", t, func(ctx convey.C) {
  321. var (
  322. c = context.Background()
  323. projID = int(8888)
  324. branch = "test"
  325. path = "."
  326. conn = d.redis.Get(c)
  327. )
  328. redis.Int(conn.Do("SET", "saga_path_auth_8888_test_.", `{"Owners": ["c"]}`))
  329. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  330. authUsers, err := d.PathAuthR(c, projID, branch, path)
  331. authUsersJSON, _ := json.Marshal(authUsers)
  332. ctx.Convey("Then err should be nil.authUsers should not be nil.", func(ctx convey.C) {
  333. ctx.So(err, convey.ShouldBeNil)
  334. ctx.So(string(authUsersJSON), convey.ShouldEqual, `{"Owners":["c"],"Reviewers":null}`)
  335. ctx.So(authUsers, convey.ShouldNotBeNil)
  336. })
  337. })
  338. })
  339. }
  340. func TestDaoSetPathAuthR(t *testing.T) {
  341. convey.Convey("SetPathAuthR", t, func(ctx convey.C) {
  342. var (
  343. c = context.Background()
  344. projID = int(9999)
  345. branch = "test"
  346. path = "."
  347. authUsers = &model.AuthUsers{Owners: []string{"testOwner"}, Reviewers: []string{"testReviewers"}}
  348. conn = d.redis.Get(c)
  349. )
  350. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  351. err := d.SetPathAuthR(c, projID, branch, path, authUsers)
  352. value, _ := redis.String(conn.Do("GET", "saga_path_auth_9999_test_."))
  353. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  354. ctx.So(err, convey.ShouldBeNil)
  355. ctx.So(value, convey.ShouldEqual, `{"Owners":["testOwner"],"Reviewers":["testReviewers"]}`)
  356. })
  357. })
  358. })
  359. }
  360. func TestDaoDeletePathAuthR(t *testing.T) {
  361. convey.Convey("DeletePathAuthR", t, func(ctx convey.C) {
  362. var (
  363. c = context.Background()
  364. projID = int(88)
  365. branch = "test"
  366. path = "."
  367. authUsers = &model.AuthUsers{Owners: []string{"testOwner"}, Reviewers: []string{"testReviewers"}}
  368. conn = d.redis.Get(c)
  369. )
  370. ctx.Convey("When data not exist", func(ctx convey.C) {
  371. err := d.DeletePathAuthR(c, projID, branch, path)
  372. value, _ := redis.String(conn.Do("GET", "saga_path_auth_88_test_."))
  373. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  374. ctx.So(err, convey.ShouldBeNil)
  375. ctx.So(value, convey.ShouldEqual, "")
  376. })
  377. })
  378. ctx.Convey("When data exist", func(ctx convey.C) {
  379. err := d.SetPathAuthR(c, projID, branch, path, authUsers)
  380. value, _ := redis.String(conn.Do("GET", "saga_path_auth_88_test_."))
  381. ctx.Convey("Then err should be nil 1.", func(ctx convey.C) {
  382. ctx.So(err, convey.ShouldBeNil)
  383. ctx.So(value, convey.ShouldEqual, `{"Owners":["testOwner"],"Reviewers":["testReviewers"]}`)
  384. })
  385. err = d.DeletePathAuthR(c, projID, branch, path)
  386. value, _ = redis.String(conn.Do("GET", "saga_path_auth_88_test_."))
  387. ctx.Convey("Then err should be nil 2.", func(ctx convey.C) {
  388. ctx.So(err, convey.ShouldBeNil)
  389. ctx.So(value, convey.ShouldEqual, "")
  390. })
  391. })
  392. })
  393. }
  394. func TestDaoSetReportStatus(t *testing.T) {
  395. convey.Convey("SetReportStatus", t, func(ctx convey.C) {
  396. var (
  397. c = context.Background()
  398. projID = int(11111)
  399. mrIID = int(11111)
  400. result bool
  401. conn = d.redis.Get(c)
  402. )
  403. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  404. err := d.SetReportStatus(c, projID, mrIID, result)
  405. value, _ := redis.String(conn.Do("GET", "saga_auth_11111_11111"))
  406. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  407. ctx.So(err, convey.ShouldBeNil)
  408. ctx.So(value, convey.ShouldEqual, "false")
  409. })
  410. })
  411. })
  412. }
  413. func TestDaoReportStatus(t *testing.T) {
  414. convey.Convey("ReportStatus", t, func(ctx convey.C) {
  415. var (
  416. c = context.Background()
  417. projID = int(22222)
  418. mrIID = int(22222)
  419. conn = d.redis.Get(c)
  420. )
  421. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  422. conn.Do("SET", "saga_auth_22222_22222", "true")
  423. result, err := d.ReportStatus(c, projID, mrIID)
  424. ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
  425. ctx.So(err, convey.ShouldBeNil)
  426. ctx.So(result, convey.ShouldEqual, true)
  427. })
  428. })
  429. })
  430. }
  431. func TestDaoDeleteReportStatus(t *testing.T) {
  432. convey.Convey("DeleteReportStatus", t, func(ctx convey.C) {
  433. var (
  434. c = context.Background()
  435. projID = int(33333)
  436. mrIID = int(33333)
  437. conn = d.redis.Get(c)
  438. )
  439. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  440. conn.Do("SET", "saga_auth_33333_33333", "true")
  441. err := d.DeleteReportStatus(c, projID, mrIID)
  442. value, _ := redis.Int(conn.Do("GET", "saga_auth_33333_33333"))
  443. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  444. ctx.So(err, convey.ShouldBeNil)
  445. ctx.So(value, convey.ShouldEqual, 0)
  446. })
  447. })
  448. })
  449. }