wechat_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package wechat
  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/dao"
  9. "go-common/app/admin/ep/saga/model"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. var (
  13. mydao *dao.Dao
  14. wechat *Wechat
  15. ctx = context.Background()
  16. )
  17. func TestMain(m *testing.M) {
  18. var err error
  19. flag.Set("conf", "../../cmd/saga-admin-test.toml")
  20. if err = conf.Init(); err != nil {
  21. panic(err)
  22. }
  23. mydao = dao.New()
  24. defer mydao.Close()
  25. wechat = New(mydao)
  26. os.Exit(m.Run())
  27. }
  28. func TestAddRequireVisible(t *testing.T) {
  29. var (
  30. err error
  31. userMap = make(map[string]model.RequireVisibleUser)
  32. )
  33. Convey("TEST addRequireVisible", t, func() {
  34. err = wechat.addRequireVisible(ctx, "000000")
  35. So(err, ShouldNotBeNil)
  36. err = wechat.addRequireVisible(ctx, "001134")
  37. So(err, ShouldBeNil)
  38. err = mydao.RequireVisibleUsersRedis(ctx, &userMap)
  39. So(err, ShouldBeNil)
  40. So(userMap, ShouldContainKey, "001134")
  41. })
  42. }
  43. func TestAlreadyInCache(t *testing.T) {
  44. var (
  45. err error
  46. result bool
  47. contactInfo model.ContactInfo
  48. )
  49. contactInfo = model.ContactInfo{
  50. ID: "111",
  51. UserName: "zhangsan",
  52. UserID: "222",
  53. NickName: "xiaolizi",
  54. VisibleSaga: true,
  55. }
  56. Convey("TEST alreadyInCache", t, func() {
  57. result, err = wechat.alreadyInCache(ctx, "000")
  58. So(err, ShouldBeNil)
  59. So(result, ShouldEqual, false)
  60. So(mydao.SetRequireVisibleUsersRedis(ctx, &contactInfo), ShouldBeNil)
  61. result, err = wechat.alreadyInCache(ctx, "222")
  62. So(err, ShouldBeNil)
  63. So(result, ShouldEqual, true)
  64. })
  65. }
  66. func TestSyncContacts(t *testing.T) {
  67. var (
  68. err error
  69. contactInfo = &model.ContactInfo{
  70. UserID: "E10021",
  71. UserName: "eyotang",
  72. NickName: "ben大神点C",
  73. }
  74. modify = &model.ContactInfo{
  75. UserID: "000328",
  76. UserName: "eyotang",
  77. NickName: "ben大神点C",
  78. VisibleSaga: false,
  79. }
  80. target *model.ContactInfo
  81. almostEqual bool
  82. )
  83. Convey("TEST sync after add incorrect", t, func() {
  84. err = wechat.dao.CreateContact(contactInfo)
  85. So(err, ShouldBeNil)
  86. target, err = wechat.dao.QueryUserByID(contactInfo.UserID)
  87. So(err, ShouldBeNil)
  88. almostEqual = contactInfo.AlmostEqual(target)
  89. So(almostEqual, ShouldBeTrue)
  90. err = wechat.SyncContacts(ctx)
  91. So(err, ShouldBeNil)
  92. target, err = wechat.dao.QueryUserByID(contactInfo.UserID)
  93. So(err, ShouldNotBeNil)
  94. })
  95. Convey("TEST aync after change", t, func() {
  96. contactInfo, err = wechat.dao.QueryUserByID(modify.UserID)
  97. So(err, ShouldBeNil)
  98. modify.ID = contactInfo.ID
  99. err = wechat.dao.UptContact(contactInfo)
  100. So(err, ShouldBeNil)
  101. err = wechat.SyncContacts(ctx)
  102. So(err, ShouldBeNil)
  103. target, err = wechat.dao.QueryUserByID(modify.UserID)
  104. So(err, ShouldBeNil)
  105. So(target.VisibleSaga, ShouldBeTrue)
  106. So(target.UserName, ShouldNotEqual, "eyotang")
  107. })
  108. }
  109. func TestPushMsg(t *testing.T) {
  110. var err error
  111. userName := []string{"wuwei"}
  112. content := "测试发送企业微信"
  113. Convey("TEST push message", t, func() {
  114. err = wechat.PushMsg(ctx, userName, content)
  115. So(err, ShouldBeNil)
  116. })
  117. }
  118. func TestAnalysisContacts(t *testing.T) {
  119. var err error
  120. Convey("TEST analysis contacts", t, func() {
  121. err = wechat.AnalysisContacts(ctx)
  122. So(err, ShouldBeNil)
  123. })
  124. }