附近小店
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_test.go 8.6 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestGetUserPortrait(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 != apiGetUserPortrait {
  15. t.Fatalf("Except to path '%s',get '%s'", apiGetUserPortrait, 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": "20170611",
  40. "visit_uv_new": {
  41. "province": [
  42. {
  43. "id": 31,
  44. "name": "广东省",
  45. "value": 215
  46. }
  47. ],
  48. "city": [
  49. {
  50. "id": 3102,
  51. "name": "广州",
  52. "value": 78
  53. }
  54. ],
  55. "genders": [
  56. {
  57. "id": 1,
  58. "name": "男",
  59. "value": 2146
  60. }
  61. ],
  62. "platforms": [
  63. {
  64. "id": 1,
  65. "name": "iPhone",
  66. "value": 27642
  67. }
  68. ],
  69. "devices": [
  70. {
  71. "name": "OPPO R9",
  72. "value": 61
  73. }
  74. ],
  75. "ages": [
  76. {
  77. "id": 1,
  78. "name": "17岁以下",
  79. "value": 151
  80. }
  81. ]
  82. },
  83. "visit_uv": {
  84. "province": [
  85. {
  86. "id": 31,
  87. "name": "广东省",
  88. "value": 1341
  89. }
  90. ],
  91. "city": [
  92. {
  93. "id": 3102,
  94. "name": "广州",
  95. "value": 234
  96. }
  97. ],
  98. "genders": [
  99. {
  100. "id": 1,
  101. "name": "男",
  102. "value": 14534
  103. }
  104. ],
  105. "platforms": [
  106. {
  107. "id": 1,
  108. "name": "iPhone",
  109. "value": 21750
  110. }
  111. ],
  112. "devices": [
  113. {
  114. "name": "OPPO R9",
  115. "value": 617
  116. }
  117. ],
  118. "ages": [
  119. {
  120. "id": 1,
  121. "name": "17岁以下",
  122. "value": 3156
  123. }
  124. ]
  125. }
  126. }`
  127. if _, err := w.Write([]byte(raw)); err != nil {
  128. t.Fatal(err)
  129. }
  130. }))
  131. defer ts.Close()
  132. _, err := getUserPortrait("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetUserPortrait)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. }
  137. func TestGetVisitDistribution(t *testing.T) {
  138. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  139. if r.Method != "POST" {
  140. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  141. }
  142. path := r.URL.EscapedPath()
  143. if path != apiGetVisitDistribution {
  144. t.Fatalf("Except to path '%s',get '%s'", apiGetVisitDistribution, path)
  145. }
  146. if err := r.ParseForm(); err != nil {
  147. t.Fatal(err)
  148. }
  149. if r.Form.Get("access_token") == "" {
  150. t.Fatalf("access_token can not be empty")
  151. }
  152. params := make(map[string]interface{})
  153. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  154. t.Fatal(err)
  155. }
  156. param, ok := params["begin_date"]
  157. if !ok || param == "" {
  158. t.Log("param begin_date can not be empty")
  159. t.Fail()
  160. }
  161. param, ok = params["end_date"]
  162. if !ok || param == "" {
  163. t.Log("param end_date can not be empty")
  164. t.Fail()
  165. }
  166. w.WriteHeader(http.StatusOK)
  167. raw := `{
  168. "ref_date": "20170313",
  169. "list": [
  170. {
  171. "index": "access_source_session_cnt",
  172. "item_list": [
  173. {
  174. "key": 10,
  175. "value": 5
  176. },
  177. {
  178. "key": 8,
  179. "value": 687
  180. },
  181. {
  182. "key": 7,
  183. "value": 10740
  184. },
  185. {
  186. "key": 6,
  187. "value": 1961
  188. },
  189. {
  190. "key": 5,
  191. "value": 677
  192. },
  193. {
  194. "key": 4,
  195. "value": 653
  196. },
  197. {
  198. "key": 3,
  199. "value": 1120
  200. },
  201. {
  202. "key": 2,
  203. "value": 10243
  204. },
  205. {
  206. "key": 1,
  207. "value": 116578
  208. }
  209. ]
  210. },
  211. {
  212. "index": "access_staytime_info",
  213. "item_list": [
  214. {
  215. "key": 8,
  216. "value": 16329
  217. },
  218. {
  219. "key": 7,
  220. "value": 19322
  221. },
  222. {
  223. "key": 6,
  224. "value": 21832
  225. },
  226. {
  227. "key": 5,
  228. "value": 19539
  229. },
  230. {
  231. "key": 4,
  232. "value": 29670
  233. },
  234. {
  235. "key": 3,
  236. "value": 19667
  237. },
  238. {
  239. "key": 2,
  240. "value": 11794
  241. },
  242. {
  243. "key": 1,
  244. "value": 4511
  245. }
  246. ]
  247. },
  248. {
  249. "index": "access_depth_info",
  250. "item_list": [
  251. {
  252. "key": 5,
  253. "value": 217
  254. },
  255. {
  256. "key": 4,
  257. "value": 3259
  258. },
  259. {
  260. "key": 3,
  261. "value": 32445
  262. },
  263. {
  264. "key": 2,
  265. "value": 63542
  266. },
  267. {
  268. "key": 1,
  269. "value": 43201
  270. }
  271. ]
  272. }
  273. ]
  274. }`
  275. if _, err := w.Write([]byte(raw)); err != nil {
  276. t.Fatal(err)
  277. }
  278. }))
  279. defer ts.Close()
  280. _, err := getVisitDistribution("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetVisitDistribution)
  281. if err != nil {
  282. t.Fatal(err)
  283. }
  284. }
  285. func TestGetVisitPage(t *testing.T) {
  286. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  287. if r.Method != "POST" {
  288. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  289. }
  290. path := r.URL.EscapedPath()
  291. if path != apiGetVisitPage {
  292. t.Fatalf("Except to path '%s',get '%s'", apiGetVisitPage, path)
  293. }
  294. if err := r.ParseForm(); err != nil {
  295. t.Fatal(err)
  296. }
  297. if r.Form.Get("access_token") == "" {
  298. t.Fatalf("access_token can not be empty")
  299. }
  300. params := make(map[string]interface{})
  301. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  302. t.Fatal(err)
  303. }
  304. param, ok := params["begin_date"]
  305. if !ok || param == "" {
  306. t.Log("param begin_date can not be empty")
  307. t.Fail()
  308. }
  309. param, ok = params["end_date"]
  310. if !ok || param == "" {
  311. t.Log("param end_date can not be empty")
  312. t.Fail()
  313. }
  314. w.WriteHeader(http.StatusOK)
  315. raw := `{
  316. "ref_date": "20170313",
  317. "list": [
  318. {
  319. "page_path": "pages/main/main.html",
  320. "page_visit_pv": 213429,
  321. "page_visit_uv": 55423,
  322. "page_staytime_pv": 8.139198,
  323. "entrypage_pv": 117922,
  324. "exitpage_pv": 61304,
  325. "page_share_pv": 180,
  326. "page_share_uv": 166
  327. },
  328. {
  329. "page_path": "pages/linedetail/linedetail.html",
  330. "page_visit_pv": 155030,
  331. "page_visit_uv": 42195,
  332. "page_staytime_pv": 35.462395,
  333. "entrypage_pv": 21101,
  334. "exitpage_pv": 47051,
  335. "page_share_pv": 47,
  336. "page_share_uv": 42
  337. },
  338. {
  339. "page_path": "pages/search/search.html",
  340. "page_visit_pv": 65011,
  341. "page_visit_uv": 24716,
  342. "page_staytime_pv": 6.889634,
  343. "entrypage_pv": 1811,
  344. "exitpage_pv": 3198,
  345. "page_share_pv": 0,
  346. "page_share_uv": 0
  347. },
  348. {
  349. "page_path": "pages/stationdetail/stationdetail.html",
  350. "page_visit_pv": 29953,
  351. "page_visit_uv": 9695,
  352. "page_staytime_pv": 7.558508,
  353. "entrypage_pv": 1386,
  354. "exitpage_pv": 2285,
  355. "page_share_pv": 0,
  356. "page_share_uv": 0
  357. },
  358. {
  359. "page_path": "pages/switch-city/switch-city.html",
  360. "page_visit_pv": 8928,
  361. "page_visit_uv": 4017,
  362. "page_staytime_pv": 9.22659,
  363. "entrypage_pv": 748,
  364. "exitpage_pv": 1613,
  365. "page_share_pv": 0,
  366. "page_share_uv": 0
  367. }
  368. ]
  369. }`
  370. if _, err := w.Write([]byte(raw)); err != nil {
  371. t.Fatal(err)
  372. }
  373. }))
  374. defer ts.Close()
  375. _, err := getVisitPage("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetVisitPage)
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. }
  380. func TestGetDailySummary(t *testing.T) {
  381. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  382. if r.Method != "POST" {
  383. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  384. }
  385. path := r.URL.EscapedPath()
  386. if path != apiGetDailySummary {
  387. t.Fatalf("Except to path '%s',get '%s'", apiGetDailySummary, path)
  388. }
  389. if err := r.ParseForm(); err != nil {
  390. t.Fatal(err)
  391. }
  392. if r.Form.Get("access_token") == "" {
  393. t.Fatalf("access_token can not be empty")
  394. }
  395. params := make(map[string]interface{})
  396. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  397. t.Fatal(err)
  398. }
  399. param, ok := params["begin_date"]
  400. if !ok || param == "" {
  401. t.Log("param begin_date can not be empty")
  402. t.Fail()
  403. }
  404. param, ok = params["end_date"]
  405. if !ok || param == "" {
  406. t.Log("param end_date can not be empty")
  407. t.Fail()
  408. }
  409. w.WriteHeader(http.StatusOK)
  410. raw := `{
  411. "list": [
  412. {
  413. "ref_date": "20170313",
  414. "visit_total": 391,
  415. "share_pv": 572,
  416. "share_uv": 383
  417. }
  418. ]
  419. }`
  420. if _, err := w.Write([]byte(raw)); err != nil {
  421. t.Fatal(err)
  422. }
  423. }))
  424. defer ts.Close()
  425. _, err := getDailySummary("mock-access-token", "mock-begin-date", "mock-end-date", ts.URL+apiGetDailySummary)
  426. if err != nil {
  427. t.Fatal(err)
  428. }
  429. }