附近小店
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_retain_test.go 4.1 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestGetMonthlyRetain(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 != apiGetMonthlyRetain {
  15. t.Fatalf("Except to path '%s',get '%s'", apiGetMonthlyRetain, 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. "ref_date": "201702",
  40. "visit_uv_new": [
  41. {
  42. "key": 0,
  43. "value": 346249
  44. }
  45. ],
  46. "visit_uv": [
  47. {
  48. "key": 0,
  49. "value": 346249
  50. }
  51. ]
  52. }`
  53. if _, err := w.Write([]byte(raw)); err != nil {
  54. t.Fatal(err)
  55. }
  56. }))
  57. defer ts.Close()
  58. _, err := getRetain("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetMonthlyRetain)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. }
  63. func TestGetWeeklyRetain(t *testing.T) {
  64. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  65. if r.Method != "POST" {
  66. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  67. }
  68. path := r.URL.EscapedPath()
  69. if path != apiGetWeeklyRetain {
  70. t.Fatalf("Except to path '%s',get '%s'", apiGetWeeklyRetain, path)
  71. }
  72. if err := r.ParseForm(); err != nil {
  73. t.Fatal(err)
  74. }
  75. if r.Form.Get("access_token") == "" {
  76. t.Fatalf("access_token can not be empty")
  77. }
  78. params := make(map[string]interface{})
  79. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  80. t.Fatal(err)
  81. }
  82. param, ok := params["begin_date"]
  83. if !ok || param == "" {
  84. t.Log("param begin_date can not be empty")
  85. t.Fail()
  86. }
  87. param, ok = params["end_date"]
  88. if !ok || param == "" {
  89. t.Log("param end_date can not be empty")
  90. t.Fail()
  91. }
  92. w.WriteHeader(http.StatusOK)
  93. raw := `{
  94. "ref_date": "20170306-20170312",
  95. "visit_uv_new": [
  96. {
  97. "key": 0,
  98. "value": 0
  99. },
  100. {
  101. "key": 1,
  102. "value": 16853
  103. }
  104. ],
  105. "visit_uv": [
  106. {
  107. "key": 0,
  108. "value": 0
  109. },
  110. {
  111. "key": 1,
  112. "value": 99310
  113. }
  114. ]
  115. }`
  116. if _, err := w.Write([]byte(raw)); err != nil {
  117. t.Fatal(err)
  118. }
  119. }))
  120. defer ts.Close()
  121. _, err := getRetain("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetWeeklyRetain)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. }
  126. func TestGetDailyRetain(t *testing.T) {
  127. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  128. if r.Method != "POST" {
  129. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  130. }
  131. path := r.URL.EscapedPath()
  132. if path != apiGetDailyRetain {
  133. t.Fatalf("Except to path '%s',get '%s'", apiGetDailyRetain, path)
  134. }
  135. if err := r.ParseForm(); err != nil {
  136. t.Fatal(err)
  137. }
  138. if r.Form.Get("access_token") == "" {
  139. t.Fatalf("access_token can not be empty")
  140. }
  141. params := make(map[string]interface{})
  142. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  143. t.Fatal(err)
  144. }
  145. param, ok := params["begin_date"]
  146. if !ok || param == "" {
  147. t.Log("param begin_date can not be empty")
  148. t.Fail()
  149. }
  150. param, ok = params["end_date"]
  151. if !ok || param == "" {
  152. t.Log("param end_date can not be empty")
  153. t.Fail()
  154. }
  155. w.WriteHeader(http.StatusOK)
  156. raw := `{
  157. "ref_date": "20170313",
  158. "visit_uv_new": [
  159. {
  160. "key": 0,
  161. "value": 5464
  162. }
  163. ],
  164. "visit_uv": [
  165. {
  166. "key": 0,
  167. "value": 55500
  168. }
  169. ]
  170. }`
  171. if _, err := w.Write([]byte(raw)); err != nil {
  172. t.Fatal(err)
  173. }
  174. }))
  175. defer ts.Close()
  176. _, err := getRetain("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetDailyRetain)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. }