附近小店
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.

analysis_visit_trend_test.go 4.2 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestGetMonthlyVisitTrend(t *testing.T) {
  9. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. if r.Method != "POST" {
  11. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  12. }
  13. path := r.URL.EscapedPath()
  14. if path != apiGetMonthlyVisitTrend {
  15. t.Fatalf("Except to path '%s',get '%s'", apiGetMonthlyVisitTrend, path)
  16. }
  17. if err := r.ParseForm(); err != nil {
  18. t.Fatal(err)
  19. }
  20. if r.Form.Get("access_token") == "" {
  21. t.Fatalf("access_token can not be empty")
  22. }
  23. params := make(map[string]interface{})
  24. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  25. t.Fatal(err)
  26. }
  27. param, ok := params["begin_date"]
  28. if !ok || param == "" {
  29. t.Log("param begin_date can not be empty")
  30. t.Fail()
  31. }
  32. param, ok = params["end_date"]
  33. if !ok || param == "" {
  34. t.Log("param end_date can not be empty")
  35. t.Fail()
  36. }
  37. w.WriteHeader(http.StatusOK)
  38. raw := `{
  39. "list": [
  40. {
  41. "ref_date": "201703",
  42. "session_cnt": 126513,
  43. "visit_pv": 426113,
  44. "visit_uv": 48659,
  45. "visit_uv_new": 6726,
  46. "stay_time_session": 56.4112,
  47. "visit_depth": 2.0189
  48. }
  49. ]
  50. }`
  51. if _, err := w.Write([]byte(raw)); err != nil {
  52. t.Fatal(err)
  53. }
  54. }))
  55. defer ts.Close()
  56. _, err := getVisitTrend("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetMonthlyVisitTrend)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. }
  61. func TestGetWeeklyVisitTrend(t *testing.T) {
  62. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  63. if r.Method != "POST" {
  64. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  65. }
  66. path := r.URL.EscapedPath()
  67. if path != apiGetWeeklyVisitTrend {
  68. t.Fatalf("Except to path '%s',get '%s'", apiGetWeeklyVisitTrend, path)
  69. }
  70. if err := r.ParseForm(); err != nil {
  71. t.Fatal(err)
  72. }
  73. if r.Form.Get("access_token") == "" {
  74. t.Fatalf("access_token can not be empty")
  75. }
  76. params := make(map[string]interface{})
  77. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  78. t.Fatal(err)
  79. }
  80. param, ok := params["begin_date"]
  81. if !ok || param == "" {
  82. t.Log("param begin_date can not be empty")
  83. t.Fail()
  84. }
  85. param, ok = params["end_date"]
  86. if !ok || param == "" {
  87. t.Log("param end_date can not be empty")
  88. t.Fail()
  89. }
  90. w.WriteHeader(http.StatusOK)
  91. raw := `{
  92. "list": [
  93. {
  94. "ref_date": "20170306-20170312",
  95. "session_cnt": 986780,
  96. "visit_pv": 3251840,
  97. "visit_uv": 189405,
  98. "visit_uv_new": 45592,
  99. "stay_time_session": 54.5346,
  100. "visit_depth": 1.9735
  101. }
  102. ]
  103. }`
  104. if _, err := w.Write([]byte(raw)); err != nil {
  105. t.Fatal(err)
  106. }
  107. }))
  108. defer ts.Close()
  109. _, err := getVisitTrend("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetWeeklyVisitTrend)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. }
  114. func TestGetDailyVisitTrend(t *testing.T) {
  115. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  116. if r.Method != "POST" {
  117. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  118. }
  119. path := r.URL.EscapedPath()
  120. if path != apiGetDailyVisitTrend {
  121. t.Fatalf("Except to path '%s',get '%s'", apiGetDailyVisitTrend, path)
  122. }
  123. if err := r.ParseForm(); err != nil {
  124. t.Fatal(err)
  125. }
  126. if r.Form.Get("access_token") == "" {
  127. t.Fatalf("access_token can not be empty")
  128. }
  129. params := make(map[string]interface{})
  130. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  131. t.Fatal(err)
  132. }
  133. param, ok := params["begin_date"]
  134. if !ok || param == "" {
  135. t.Log("param begin_date can not be empty")
  136. t.Fail()
  137. }
  138. param, ok = params["end_date"]
  139. if !ok || param == "" {
  140. t.Log("param end_date can not be empty")
  141. t.Fail()
  142. }
  143. w.WriteHeader(http.StatusOK)
  144. raw := `{
  145. "list": [
  146. {
  147. "ref_date": "20170313",
  148. "session_cnt": 142549,
  149. "visit_pv": 472351,
  150. "visit_uv": 55500,
  151. "visit_uv_new": 5464,
  152. "stay_time_session": 0,
  153. "visit_depth": 1.9838
  154. }
  155. ]
  156. }`
  157. if _, err := w.Write([]byte(raw)); err != nil {
  158. t.Fatal(err)
  159. }
  160. }))
  161. defer ts.Close()
  162. _, err := getVisitTrend("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetDailyVisitTrend)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. }