run_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package charge
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "time"
  8. model "go-common/app/job/main/growup/model/charge"
  9. xtime "go-common/library/time"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func Test_AvCharge(t *testing.T) {
  13. Convey("Test av weekly and monthly charge\n", t, func() {
  14. c := context.Background()
  15. deleteAll(c)
  16. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  17. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  18. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  19. err := s.runVideo(c, d, nil)
  20. So(err, ShouldBeNil)
  21. checkAvWeeklyCharge(c, ac1, 30, 10, 10)
  22. checkAvWeeklyCharge(c, ac2, 30, 10, 10)
  23. checkAvMonthlyCharge(c, ac1, 30, 10, 10)
  24. checkAvMonthlyCharge(c, ac2, 30, 10, 10)
  25. ac1 = insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 10)
  26. ac2 = insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 20)
  27. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  28. err = s.runVideo(c, d, nil)
  29. So(err, ShouldBeNil)
  30. checkAvWeeklyCharge(c, ac1, 60, 20, 20)
  31. checkAvWeeklyCharge(c, ac2, 90, 30, 30)
  32. checkAvMonthlyCharge(c, ac1, 60, 20, 20)
  33. checkAvMonthlyCharge(c, ac2, 90, 30, 30)
  34. ac1 = insertAvDailyCharge(c, "2018-06-05", "2018-06-01 15:02:03", 1101, 110, 15)
  35. d, _ = time.ParseInLocation("2006-01-02", "2018-06-05", time.Local)
  36. err = s.runVideo(c, d, nil)
  37. So(err, ShouldBeNil)
  38. checkAvWeeklyCharge(c, ac1, 45, 15, 15)
  39. checkAvMonthlyCharge(c, ac1, 105, 35, 35)
  40. })
  41. }
  42. func Test_AvChargeDateStatis(t *testing.T) {
  43. Convey("Test av charge date statis\n", t, func() {
  44. c := context.Background()
  45. deleteAll(c)
  46. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 10)
  47. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 10)
  48. insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1103, 110, 3500)
  49. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  50. err := s.runVideo(c, d, nil)
  51. So(err, ShouldBeNil)
  52. checkAvChargeDateStatis(c, 0, 2, 1, 3520, 12, d, "av_charge_daily_statis")
  53. checkAvChargeDateStatis(c, 1, 1, 1, 3520, 12, d, "av_charge_daily_statis")
  54. weekD := getStartWeeklyDate(d)
  55. checkAvChargeDateStatis(c, 0, 2, 1, 3520, 12, weekD, "av_charge_weekly_statis")
  56. checkAvChargeDateStatis(c, 1, 1, 1, 3520, 12, weekD, "av_charge_weekly_statis")
  57. monthD := getStartMonthlyDate(d)
  58. checkAvChargeDateStatis(c, 0, 2, 1, 3520, 12, monthD, "av_charge_monthly_statis")
  59. checkAvChargeDateStatis(c, 1, 1, 1, 3520, 12, monthD, "av_charge_monthly_statis")
  60. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 1000)
  61. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 1000)
  62. insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 110, 4000)
  63. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  64. err = s.runVideo(c, d, nil)
  65. So(err, ShouldBeNil)
  66. checkAvChargeDateStatis(c, 0, 2, 1, 6000, 12, d, "av_charge_daily_statis")
  67. checkAvChargeDateStatis(c, 1, 1, 1, 6000, 12, d, "av_charge_daily_statis")
  68. weekD = getStartWeeklyDate(d)
  69. charge := 6000 + 3520
  70. checkAvChargeDateStatis(c, 0, 2, 1, charge, 12, weekD, "av_charge_weekly_statis")
  71. checkAvChargeDateStatis(c, 1, 0, 1, charge, 12, weekD, "av_charge_weekly_statis")
  72. checkAvChargeDateStatis(c, 2, 1, 1, charge, 12, weekD, "av_charge_weekly_statis")
  73. monthD = getStartMonthlyDate(d)
  74. checkAvChargeDateStatis(c, 0, 2, 1, charge, 12, monthD, "av_charge_monthly_statis")
  75. checkAvChargeDateStatis(c, 1, 0, 1, charge, 12, monthD, "av_charge_monthly_statis")
  76. checkAvChargeDateStatis(c, 2, 1, 1, charge, 12, monthD, "av_charge_monthly_statis")
  77. insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1101, 110, 15000)
  78. insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1102, 110, 15000)
  79. insertAvDailyCharge(c, "2018-06-10", "2018-06-01 15:02:03", 1103, 110, 40000)
  80. d, _ = time.ParseInLocation("2006-01-02", "2018-06-10", time.Local)
  81. err = s.runVideo(c, d, nil)
  82. So(err, ShouldBeNil)
  83. checkAvChargeDateStatis(c, 0, 0, 1, 70000, 12, d, "av_charge_daily_statis")
  84. checkAvChargeDateStatis(c, 1, 0, 1, 70000, 12, d, "av_charge_daily_statis")
  85. checkAvChargeDateStatis(c, 3, 2, 1, 70000, 12, d, "av_charge_daily_statis")
  86. checkAvChargeDateStatis(c, 4, 1, 1, 70000, 12, d, "av_charge_daily_statis")
  87. weekD = getStartWeeklyDate(d)
  88. checkAvChargeDateStatis(c, 0, 0, 1, 70000, 12, weekD, "av_charge_weekly_statis")
  89. checkAvChargeDateStatis(c, 1, 0, 1, 70000, 12, weekD, "av_charge_weekly_statis")
  90. checkAvChargeDateStatis(c, 3, 2, 1, 70000, 12, weekD, "av_charge_weekly_statis")
  91. checkAvChargeDateStatis(c, 4, 1, 1, 70000, 12, weekD, "av_charge_weekly_statis")
  92. monthD = getStartMonthlyDate(d)
  93. checkAvChargeDateStatis(c, 0, 0, 1, 79520, 12, monthD, "av_charge_monthly_statis")
  94. checkAvChargeDateStatis(c, 1, 0, 1, 79520, 12, monthD, "av_charge_monthly_statis")
  95. checkAvChargeDateStatis(c, 3, 2, 1, 79520, 12, monthD, "av_charge_monthly_statis")
  96. checkAvChargeDateStatis(c, 4, 1, 1, 79520, 12, monthD, "av_charge_monthly_statis")
  97. insertAvDailyCharge(c, "2018-07-10", "2018-06-01 15:02:03", 1101, 110, 100)
  98. d, _ = time.ParseInLocation("2006-01-02", "2018-07-10", time.Local)
  99. err = s.runVideo(c, d, nil)
  100. So(err, ShouldBeNil)
  101. monthD = getStartMonthlyDate(d)
  102. checkAvChargeDateStatis(c, 0, 1, 1, 100, 12, monthD, "av_charge_monthly_statis")
  103. checkAvChargeDateStatis(c, 1, 0, 1, 100, 12, monthD, "av_charge_monthly_statis")
  104. })
  105. }
  106. func Test_AvChargeStatis(t *testing.T) {
  107. Convey("Test av charge statis\n", t, func() {
  108. c := context.Background()
  109. deleteAll(c)
  110. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1101, 110, 1000)
  111. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 15:02:03", 1102, 110, 1000)
  112. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  113. err := s.runVideo(c, d, nil)
  114. So(err, ShouldBeNil)
  115. checkAvChargeStatis(c, ac1, 1000)
  116. checkAvChargeStatis(c, ac2, 1000)
  117. ac1 = insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1101, 110, 1000)
  118. ac2 = insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1102, 110, 1000)
  119. ac3 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 15:02:03", 1103, 110, 1000)
  120. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  121. err = s.runVideo(c, d, nil)
  122. So(err, ShouldBeNil)
  123. checkAvChargeStatis(c, ac1, 2000)
  124. checkAvChargeStatis(c, ac2, 2000)
  125. checkAvChargeStatis(c, ac3, 1000)
  126. ac2 = insertAvDailyCharge(c, "2018-07-03", "2018-06-01 15:02:03", 1102, 110, 1000)
  127. ac3 = insertAvDailyCharge(c, "2018-07-03", "2018-06-01 15:02:03", 1103, 110, 1000)
  128. ac4 := insertAvDailyCharge(c, "2018-07-03", "2018-06-01 15:02:03", 1104, 110, 1000)
  129. d, _ = time.ParseInLocation("2006-01-02", "2018-07-03", time.Local)
  130. err = s.runVideo(c, d, nil)
  131. So(err, ShouldBeNil)
  132. checkAvChargeStatis(c, ac1, 2000)
  133. checkAvChargeStatis(c, ac2, 3000)
  134. checkAvChargeStatis(c, ac3, 2000)
  135. checkAvChargeStatis(c, ac4, 1000)
  136. })
  137. }
  138. // up_charge
  139. func Test_UpCharge(t *testing.T) {
  140. Convey("Test up daily weekly monthly charge\n", t, func() {
  141. c := context.Background()
  142. deleteAll(c)
  143. ac1 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1101, 110, 10)
  144. ac2 := insertAvDailyCharge(c, "2018-06-01", "2018-06-01 20:02:03", 1102, 110, 10)
  145. d, _ := time.ParseInLocation("2006-01-02", "2018-06-01", time.Local)
  146. err := s.runVideo(c, d, nil)
  147. So(err, ShouldBeNil)
  148. acs := []*model.AvCharge{ac1, ac2}
  149. checkUpDailyCharge(c, acs, 2, 20, "up_daily_charge", "2018-06-01")
  150. checkUpWeeklyCharge(c, acs, 2, 20)
  151. checkUpMonthlyCharge(c, acs, 2, 20)
  152. ac11 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 20:02:03", 1101, 110, 10)
  153. ac21 := insertAvDailyCharge(c, "2018-06-02", "2018-06-01 20:02:03", 1102, 110, 10)
  154. d, _ = time.ParseInLocation("2006-01-02", "2018-06-02", time.Local)
  155. err = s.runVideo(c, d, nil)
  156. So(err, ShouldBeNil)
  157. acs = append(acs, []*model.AvCharge{ac11, ac21}...)
  158. checkUpDailyCharge(c, []*model.AvCharge{ac11, ac21}, 2, 20, "up_daily_charge", "2018-06-01")
  159. checkUpWeeklyCharge(c, acs, 2, 40)
  160. checkUpMonthlyCharge(c, acs, 2, 40)
  161. ac12 := insertAvDailyCharge(c, "2018-06-05", "2018-06-01 20:02:03", 1101, 110, 10)
  162. d, _ = time.ParseInLocation("2006-01-02", "2018-06-05", time.Local)
  163. err = s.runVideo(c, d, nil)
  164. So(err, ShouldBeNil)
  165. acs = append(acs, []*model.AvCharge{ac12}...)
  166. checkUpDailyCharge(c, []*model.AvCharge{ac12}, 1, 10, "up_daily_charge", "2018-06-05")
  167. checkUpWeeklyCharge(c, []*model.AvCharge{ac12}, 1, 10)
  168. checkUpMonthlyCharge(c, acs, 2, 50)
  169. ac13 := insertAvDailyCharge(c, "2018-07-05", "2018-06-01 20:02:03", 1101, 110, 10)
  170. d, _ = time.ParseInLocation("2006-01-02", "2018-07-05", time.Local)
  171. err = s.runVideo(c, d, nil)
  172. So(err, ShouldBeNil)
  173. checkUpDailyCharge(c, []*model.AvCharge{ac13}, 1, 10, "up_daily_charge", "2018-07-05")
  174. checkUpWeeklyCharge(c, []*model.AvCharge{ac13}, 1, 10)
  175. checkUpMonthlyCharge(c, []*model.AvCharge{ac13}, 1, 10)
  176. })
  177. }
  178. func checkAvWeeklyCharge(c context.Context, ac *model.AvCharge, totalPlayCount, incCharge, totalCharge int64) {
  179. avWeeklyCharge, err := s.GetAvCharge(c, startWeeklyDate, s.dao.AvWeeklyCharge)
  180. So(err, ShouldBeNil)
  181. var aw *model.AvCharge
  182. for _, av := range avWeeklyCharge {
  183. if av.AvID == ac.AvID {
  184. aw = av
  185. break
  186. }
  187. }
  188. So(ac.AvID, ShouldEqual, aw.AvID)
  189. So(ac.MID, ShouldEqual, aw.MID)
  190. So(ac.TagID, ShouldEqual, aw.TagID)
  191. So(ac.IsOriginal, ShouldEqual, aw.IsOriginal)
  192. So(ac.UploadTime, ShouldEqual, aw.UploadTime)
  193. So(totalPlayCount, ShouldEqual, aw.TotalPlayCount)
  194. So(totalCharge, ShouldEqual, aw.TotalCharge)
  195. So(incCharge, ShouldEqual, aw.IncCharge)
  196. So(xtime.Time(getStartWeeklyDate(ac.Date.Time()).Unix()), ShouldEqual, aw.Date)
  197. }
  198. func checkAvMonthlyCharge(c context.Context, ac *model.AvCharge, totalPlayCount, incCharge, totalCharge int64) {
  199. avCharge, err := s.GetAvCharge(c, startMonthlyDate, s.dao.AvMonthlyCharge)
  200. So(err, ShouldBeNil)
  201. var aw *model.AvCharge
  202. for _, av := range avCharge {
  203. if av.AvID == ac.AvID {
  204. aw = av
  205. break
  206. }
  207. }
  208. So(ac.AvID, ShouldEqual, aw.AvID)
  209. So(ac.MID, ShouldEqual, aw.MID)
  210. So(ac.TagID, ShouldEqual, aw.TagID)
  211. So(ac.IsOriginal, ShouldEqual, aw.IsOriginal)
  212. So(ac.UploadTime, ShouldEqual, aw.UploadTime)
  213. So(totalPlayCount, ShouldEqual, aw.TotalPlayCount)
  214. So(totalCharge, ShouldEqual, aw.TotalCharge)
  215. So(incCharge, ShouldEqual, aw.IncCharge)
  216. So(xtime.Time(getStartMonthlyDate(ac.Date.Time()).Unix()), ShouldEqual, aw.Date)
  217. }
  218. func checkAvChargeStatis(c context.Context, ac *model.AvCharge, totalCharge int64) {
  219. ai := &model.AvChargeStatis{}
  220. err := s.dao.QueryRow(c, fmt.Sprintf("select av_id,mid,tag_id,is_original,upload_time,total_charge from av_charge_statis where av_id = %d", ac.AvID)).Scan(
  221. &ai.AvID, &ai.MID, &ai.TagID, &ai.IsOriginal, &ai.UploadTime, &ai.TotalCharge)
  222. So(err, ShouldBeNil)
  223. So(ac.AvID, ShouldEqual, ai.AvID)
  224. So(ac.MID, ShouldEqual, ai.MID)
  225. So(ac.TagID, ShouldEqual, ai.TagID)
  226. So(ac.IsOriginal, ShouldEqual, ai.IsOriginal)
  227. So(ac.UploadTime, ShouldEqual, ai.UploadTime)
  228. So(totalCharge, ShouldEqual, ai.TotalCharge)
  229. }
  230. func checkUpDailyCharge(c context.Context, acs []*model.AvCharge, count, total int64, table, date string) {
  231. if len(acs) == 0 {
  232. return
  233. }
  234. if date == "" {
  235. date = acs[0].Date.Time().Format(_layout)
  236. }
  237. var incCharge int64
  238. for _, ac := range acs {
  239. incCharge += ac.IncCharge
  240. }
  241. up := &model.UpCharge{}
  242. err := s.dao.QueryRow(c, fmt.Sprintf("select av_count,inc_charge, total_charge from %s where mid = %d and date = '%s'", table, acs[0].MID, date)).Scan(
  243. &up.AvCount, &up.IncCharge, &up.TotalCharge)
  244. So(err, ShouldBeNil)
  245. So(total, ShouldEqual, up.TotalCharge)
  246. So(incCharge, ShouldEqual, up.IncCharge)
  247. }
  248. func checkUpWeeklyCharge(c context.Context, avs []*model.AvCharge, count, total int64) {
  249. m := make(map[string][]*model.AvCharge)
  250. for _, av := range avs {
  251. d := getStartWeeklyDate(av.Date.Time()).Format(_layout)
  252. if _, ok := m[d]; !ok {
  253. m[d] = make([]*model.AvCharge, 0)
  254. }
  255. m[d] = append(m[d], av)
  256. }
  257. for date, avs := range m {
  258. checkUpDailyCharge(c, avs, count, total, "up_weekly_charge", date)
  259. }
  260. }
  261. func checkUpMonthlyCharge(c context.Context, avs []*model.AvCharge, count, total int64) {
  262. m := make(map[string][]*model.AvCharge)
  263. for _, av := range avs {
  264. d := getStartMonthlyDate(av.Date.Time()).Format(_layout)
  265. if _, ok := m[d]; !ok {
  266. m[d] = make([]*model.AvCharge, 0)
  267. }
  268. m[d] = append(m[d], av)
  269. }
  270. for date, avs := range m {
  271. checkUpDailyCharge(c, avs, count, total, "up_monthly_charge", date)
  272. }
  273. }
  274. func checkAvChargeDateStatis(c context.Context, section, avs, categoryID, charge, count int, d time.Time, table string) {
  275. xd := xtime.Time(d.Unix())
  276. ads := &model.DateStatis{}
  277. err := s.dao.QueryRow(c, fmt.Sprintf("select avs,charge,cdate from %s where money_section = %d and category_id = %d and cdate = '%s'", table, section, categoryID, d.Format(_layout))).Scan(
  278. &ads.Count, &ads.Charge, &ads.CDate)
  279. So(err, ShouldBeNil)
  280. So(charge, ShouldEqual, ads.Charge)
  281. So(xd, ShouldEqual, ads.CDate)
  282. So(avs, ShouldEqual, ads.Count)
  283. var ccount int64
  284. err = s.dao.QueryRow(c, fmt.Sprintf("select count(*) from %s where category_id = %d and cdate = '%s'", table, categoryID, d.Format(_layout))).Scan(&ccount)
  285. So(err, ShouldBeNil)
  286. So(count, ShouldEqual, ccount)
  287. }
  288. func deleteAll(c context.Context) {
  289. s.dao.Exec(c, "truncate av_daily_charge_06")
  290. s.dao.Exec(c, "truncate av_daily_charge_07")
  291. s.dao.Exec(c, "truncate av_weekly_charge")
  292. s.dao.Exec(c, "truncate av_monthly_charge")
  293. s.dao.Exec(c, "truncate av_charge_statis")
  294. s.dao.Exec(c, "truncate up_daily_charge")
  295. s.dao.Exec(c, "truncate up_weekly_charge")
  296. s.dao.Exec(c, "truncate up_monthly_charge")
  297. s.dao.Exec(c, "truncate av_charge_daily_statis")
  298. s.dao.Exec(c, "truncate av_charge_weekly_statis")
  299. s.dao.Exec(c, "truncate av_charge_monthly_statis")
  300. }
  301. func getAvDailyChargeStruct(date string, uploadDate string, avID, mid, charge int64) *model.AvCharge {
  302. d, _ := time.ParseInLocation(_layout, date, time.Local)
  303. upD, _ := time.ParseInLocation(_layoutSec, uploadDate, time.Local)
  304. ac := &model.AvCharge{
  305. AvID: avID,
  306. MID: mid,
  307. TagID: 1,
  308. IsOriginal: 1,
  309. DanmakuCount: charge,
  310. CommentCount: charge,
  311. CollectCount: charge,
  312. CoinCount: charge,
  313. ShareCount: charge,
  314. ElecPayCount: charge,
  315. TotalPlayCount: charge * int64(3),
  316. WebPlayCount: charge,
  317. AppPlayCount: charge,
  318. H5PlayCount: charge,
  319. LvUnknown: charge,
  320. Lv0: charge,
  321. Lv1: charge,
  322. Lv2: charge,
  323. Lv3: charge,
  324. Lv4: charge,
  325. Lv5: charge,
  326. Lv6: charge,
  327. VScore: charge,
  328. IncCharge: charge,
  329. TotalCharge: 0,
  330. Date: xtime.Time(d.Unix()),
  331. UploadTime: xtime.Time(upD.Unix()),
  332. DBState: _dbInsert,
  333. }
  334. return ac
  335. }
  336. func insertAvDailyCharge(c context.Context, date string, uploadDate string, avID, mid, charge int64) *model.AvCharge {
  337. s.dao.Exec(c, fmt.Sprintf("insert into task_status(type,date,status) values(1,'%s',1) on duplicate key update date=values(date)", date))
  338. ac := getAvDailyChargeStruct(date, uploadDate, avID, mid, charge)
  339. _, err := s.avChargeBatchInsert(c, []*model.AvCharge{ac}, fmt.Sprintf("av_daily_charge_%s", strings.Split(date, "-")[1]))
  340. So(err, ShouldBeNil)
  341. return ac
  342. }