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.

69 line
2.3KB

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