redis_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/admin/ep/saga/model"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDaoWeixinTokenKeyRedis(t *testing.T) {
  9. convey.Convey("weixinTokenKeyRedis", t, func(ctx convey.C) {
  10. var (
  11. key = "111"
  12. )
  13. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  14. p1 := weixinTokenKeyRedis(key)
  15. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  16. ctx.So(p1, convey.ShouldEqual, "saga_weixin_token__111")
  17. })
  18. })
  19. })
  20. }
  21. func TestDaoAccessTokenRedis(t *testing.T) {
  22. convey.Convey("AccessTokenRedis", t, func(ctx convey.C) {
  23. var (
  24. c = context.Background()
  25. key = "111"
  26. token = "sdfgsdgfdg"
  27. )
  28. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  29. err := d.SetAccessTokenRedis(c, key, token, -1)
  30. ctx.Convey("Set Access Token. Then err should be nil. ", func(ctx convey.C) {
  31. ctx.So(err, convey.ShouldBeNil)
  32. })
  33. token, err := d.AccessTokenRedis(c, key)
  34. ctx.Convey("get access token. Then err should be nil.", func(ctx convey.C) {
  35. ctx.So(err, convey.ShouldBeNil)
  36. ctx.So(token, convey.ShouldEqual, token)
  37. })
  38. })
  39. })
  40. }
  41. func TestDaoRequireVisibleUsersRedis(t *testing.T) {
  42. convey.Convey("RequireVisibleUsersRedis", t, func(ctx convey.C) {
  43. var (
  44. c = context.Background()
  45. userID = "222"
  46. contactInfo = &model.ContactInfo{
  47. ID: "111",
  48. UserName: "zhanglin",
  49. UserID: userID,
  50. NickName: "mumuge",
  51. VisibleSaga: true,
  52. }
  53. userMap = make(map[string]model.RequireVisibleUser)
  54. )
  55. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  56. err := d.SetRequireVisibleUsersRedis(c, contactInfo)
  57. ctx.Convey("Set Visible Users. Then err should be nil. ", func(ctx convey.C) {
  58. ctx.So(err, convey.ShouldBeNil)
  59. })
  60. err = d.RequireVisibleUsersRedis(c, &userMap)
  61. ctx.Convey("get Visible Users. Then err should be nil.", func(ctx convey.C) {
  62. ctx.So(err, convey.ShouldBeNil)
  63. ctx.So(userMap[userID].UserName, convey.ShouldEqual, "zhanglin")
  64. ctx.So(userMap[userID].NickName, convey.ShouldEqual, "mumuge")
  65. })
  66. err = d.DeleteRequireVisibleUsersRedis(c)
  67. ctx.Convey("delete Visible Users. Then err should be nil.", func(ctx convey.C) {
  68. ctx.So(err, convey.ShouldBeNil)
  69. })
  70. })
  71. })
  72. }
  73. func TestDaoGetItemRedis(t *testing.T) {
  74. convey.Convey("GetItemRedis", t, func(ctx convey.C) {
  75. var (
  76. c = context.Background()
  77. key = "333"
  78. contactInfo = &model.ContactInfo{
  79. ID: "111",
  80. UserName: "zhanglin",
  81. UserID: "333",
  82. NickName: "mumuge",
  83. VisibleSaga: true,
  84. }
  85. getContactInfo *model.ContactInfo
  86. )
  87. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  88. err := d.SetItemRedis(c, key, contactInfo, 0)
  89. ctx.Convey("Set Item. Then err should be nil. ", func(ctx convey.C) {
  90. ctx.So(err, convey.ShouldBeNil)
  91. })
  92. err = d.ItemRedis(c, key, &getContactInfo)
  93. ctx.Convey("get Item. Then err should be nil.", func(ctx convey.C) {
  94. ctx.So(err, convey.ShouldBeNil)
  95. ctx.So(getContactInfo.UserName, convey.ShouldEqual, "zhanglin")
  96. ctx.So(getContactInfo.UserID, convey.ShouldEqual, "333")
  97. ctx.So(getContactInfo.NickName, convey.ShouldEqual, "mumuge")
  98. ctx.So(getContactInfo.VisibleSaga, convey.ShouldEqual, true)
  99. })
  100. })
  101. })
  102. }