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