wechat_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package wechat
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "testing"
  7. "go-common/app/tool/saga/conf"
  8. "go-common/app/tool/saga/dao"
  9. "go-common/app/tool/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-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.RequireVisibleUsers(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.SetRequireVisibleUsers(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: "004273",
  71. UserID: "E10021",
  72. UserName: "eyotang",
  73. NickName: "ben大神点C",
  74. }
  75. modify = &model.ContactInfo{
  76. UserID: "000328",
  77. UserName: "eyotang",
  78. NickName: "ben大神点C",
  79. VisibleSaga: false,
  80. }
  81. target *model.ContactInfo
  82. almostEqual bool
  83. )
  84. Convey("TEST sync after add incorrect", t, func() {
  85. err = wechat.dao.CreateContact(contactInfo)
  86. So(err, ShouldBeNil)
  87. target, err = wechat.dao.QueryUserByID(contactInfo.UserID)
  88. So(err, ShouldBeNil)
  89. almostEqual = contactInfo.AlmostEqual(target)
  90. So(almostEqual, ShouldBeTrue)
  91. err = wechat.SyncContacts(ctx)
  92. So(err, ShouldBeNil)
  93. target, err = wechat.dao.QueryUserByID(contactInfo.UserID)
  94. So(err, ShouldNotBeNil)
  95. })
  96. Convey("TEST aync after change", t, func() {
  97. contactInfo, err = wechat.dao.QueryUserByID(modify.UserID)
  98. So(err, ShouldBeNil)
  99. modify.ID = contactInfo.ID
  100. err = wechat.dao.UptContact(contactInfo)
  101. So(err, ShouldBeNil)
  102. err = wechat.SyncContacts(ctx)
  103. So(err, ShouldBeNil)
  104. target, err = wechat.dao.QueryUserByID(modify.UserID)
  105. So(err, ShouldBeNil)
  106. //So(target.VisibleSaga, ShouldBeTrue)
  107. So(target.UserName, ShouldNotEqual, "eyotang")
  108. })
  109. }