You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.2KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. )
  7. func TestKfList(t *testing.T) {
  8. SetupConfig()
  9. now := int32(time.Now().Unix())
  10. s := kfCache{}
  11. r := s.kfGetInfo("孙鹏") //will trigger load info
  12. AssertEqual(t, r.Acc, "kf2001@gh_f09231355c68", "kf account str mismatch")
  13. AssertEqual(t, r.ID, 2001, "kf account id should be 2001")
  14. avatarurl := "http://mmbiz.qpic.cn/mmbiz_jpg/WiaVAicTdo7zMrD65vOsqRVQL6YlSPkzhp6y8ksIjd3yOfYu2xe9w7jAbdBIFoic3Bh4fWkvUzOW29u9FN0tDXG2Q/300?wx_fmt=jpeg"
  15. AssertEqual(t, r.Avatar, avatarurl, "kf avatar url mismatch")
  16. //get kfacc by name
  17. id := s.kfAcc("孙鹏")
  18. AssertEqual(t, id, "kf2001@gh_f09231355c68", "kf account id should be kf2001@gh_f09231355c68")
  19. //test cache
  20. AssertEqual(t, s.Expire > s.UpdateAt, true, "expire should after update")
  21. AssertEqual(t, s.UpdateAt >= now, true, "expire should after update")
  22. orig := s
  23. s.kfRenewList()
  24. s.kfRenewList()
  25. AssertEqual(t, orig.Expire, s.Expire, "time stamp should still be the same")
  26. }
  27. func TestJsonStructure(t *testing.T) {
  28. sample := `
  29. {
  30. "kf_list": [
  31. {
  32. "kf_account": "kf2001@gh_f09231355c68",
  33. "kf_headimgurl": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMrD65vOsqRVQL6YlSPkzhp6y8ksIjd3yOfYu2xe9w7jAbdBIFoic3Bh4fWkvUzOW29u9FN0tDXG2Q\/300?wx_fmt=jpeg",
  34. "kf_id": 2001,
  35. "kf_nick": "孙鹏",
  36. "kf_wx": "lawipac"
  37. },
  38. {
  39. "kf_account": "kf2002@gh_f09231355c68",
  40. "kf_headimgurl": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMZVRDmRUIkaV0Uiardfw9VbvqicgtWAbM6jYKaDdScKAtahICB204fCiaz8Ucb3VyGIjKEWicIjhMzUw\/300?wx_fmt=jpeg",
  41. "kf_id": 2002,
  42. "kf_nick": "理事",
  43. "invite_wx": "x707184846",
  44. "invite_expire_time": 1495195006,
  45. "invite_status": "expired"
  46. }
  47. ]
  48. }
  49. `
  50. k := kfUserList{}
  51. err := json.Unmarshal([]byte(sample), &k)
  52. AssertEqual(t, err, nil, "decode json should have no error")
  53. AssertEqual(t, len(k.KfList), 2, "should have 2 kf users")
  54. AssertEqual(t, k.KfList[0].Acc, "kf2001@gh_f09231355c68", "first acc mismatch")
  55. AssertEqual(t, k.KfList[1].Acc, "kf2002@gh_f09231355c68", "second acc mismatch")
  56. }