gift_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package v1
  2. import (
  3. "context"
  4. "go-common/app/interface/live/app-room/api/http/v1"
  5. "go-common/app/interface/live/app-room/model"
  6. "go-common/library/net/metadata"
  7. "strconv"
  8. "testing"
  9. "time"
  10. . "github.com/smartystreets/goconvey/convey"
  11. v1pb "go-common/app/interface/live/app-room/api/http/v1"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestV1DailyBag(t *testing.T) {
  15. convey.Convey("DailyBag", t, func(c convey.C) {
  16. var (
  17. ctx = metadata.NewContext(context.Background(), metadata.MD{
  18. metadata.Mid: int64(88895029),
  19. })
  20. req = &v1pb.DailyBagReq{}
  21. )
  22. c.Convey("When everything gose positive", func(c convey.C) {
  23. resp, err := s.DailyBag(ctx, req)
  24. c.Convey("Then err should be nil.resp should not be nil.", func(c convey.C) {
  25. c.So(err, convey.ShouldBeNil)
  26. c.So(resp, convey.ShouldNotBeNil)
  27. })
  28. })
  29. })
  30. }
  31. func Test_RealGoldRecharge(t *testing.T) {
  32. Convey("normal", t, func() {
  33. So(1000, ShouldEqual, realRechargeGold(1000))
  34. So(2000, ShouldEqual, realRechargeGold(1300))
  35. So(2000, ShouldEqual, realRechargeGold(1700))
  36. So(1000, ShouldEqual, realRechargeGold(1))
  37. So(1000, ShouldEqual, realRechargeGold(233))
  38. So(2000, ShouldEqual, realRechargeGold(1398))
  39. ts, _ := time.Parse("2006-01-02", "2018-12-01")
  40. So(1, ShouldEqual, day(ts))
  41. So(201812, ShouldEqual, yearMonthNum(ts))
  42. ts, _ = time.Parse("2006-01-02", "2018-11-30")
  43. So(30, ShouldEqual, day(ts))
  44. So(201811, ShouldEqual, yearMonthNum(ts))
  45. })
  46. }
  47. func getContextWithMid(mid int64) context.Context {
  48. md := metadata.MD{
  49. metadata.Mid: mid,
  50. }
  51. return metadata.NewContext(context.Background(), md)
  52. }
  53. func TestGiftService_NeedTipRecharge_Silver(t *testing.T) {
  54. Convey("silver", t, func() {
  55. Convey("day empty", func() {
  56. ctx := getContextWithMid(1)
  57. testGiftService.conf.Gift.RechargeTip.SilverTipDays = []int{}
  58. resp, err := testGiftService.NeedTipRecharge(ctx, &v1.NeedTipRechargeReq{
  59. From: v1.From_Silver,
  60. NeedGold: 0,
  61. Platform: "android",
  62. })
  63. So(err, ShouldBeNil)
  64. So(resp.Show, ShouldEqual, 0)
  65. })
  66. Convey("day hit and has coupon", func() {
  67. mid := int64(2)
  68. yearMonth, _ := strconv.ParseInt(time.Now().Format("200601"), 10, 64)
  69. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, yearMonth)
  70. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, StopPush)
  71. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  72. if w.CouponBalance < 1 {
  73. t.Logf("own not enouth: %v", w.CouponBalance)
  74. return
  75. }
  76. testGiftService.conf.Gift.RechargeTip.SilverTipDays = append(testGiftService.conf.Gift.RechargeTip.SilverTipDays, day(time.Now()))
  77. t.Logf("%v", testGiftService.conf.Gift.RechargeTip.SilverTipDays)
  78. ctx := getContextWithMid(mid)
  79. resp, err := testGiftService.NeedTipRecharge(ctx, &v1.NeedTipRechargeReq{
  80. From: v1.From_Silver,
  81. NeedGold: 0,
  82. Platform: "android",
  83. })
  84. So(err, ShouldBeNil)
  85. t.Logf("resp:%+v", resp)
  86. So(resp.Show, ShouldEqual, 1)
  87. })
  88. Convey("day hit and has coupon and set stop push", func() {
  89. mid := int64(2)
  90. yearMonth, _ := strconv.ParseInt(time.Now().Format("200601"), 10, 64)
  91. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, yearMonth)
  92. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, StopPush)
  93. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  94. if w.CouponBalance < 1 {
  95. t.Logf("own not enouth: %v", w.CouponBalance)
  96. return
  97. }
  98. testGiftService.conf.Gift.RechargeTip.SilverTipDays = append(testGiftService.conf.Gift.RechargeTip.SilverTipDays, day(time.Now()))
  99. t.Logf("%v", testGiftService.conf.Gift.RechargeTip.SilverTipDays)
  100. ctx := getContextWithMid(mid)
  101. _, err := testGiftService.TipRechargeAction(ctx, &v1.TipRechargeActionReq{
  102. From: v1.From_Silver,
  103. Action: v1.UserAction_StopPush,
  104. })
  105. So(err, ShouldBeNil)
  106. resp, err := testGiftService.NeedTipRecharge(ctx, &v1.NeedTipRechargeReq{
  107. From: v1.From_Silver,
  108. NeedGold: 0,
  109. Platform: "android",
  110. })
  111. So(err, ShouldBeNil)
  112. t.Logf("resp:%+v", resp)
  113. So(resp.Show, ShouldEqual, 0)
  114. })
  115. })
  116. }
  117. func TestGiftService_NeedTipRecharge_Gold(t *testing.T) {
  118. Convey("normal", t, func() {
  119. mid := int64(2)
  120. ctx := getContextWithMid(mid)
  121. testGiftService.dao.DelUserConf(context.Background(), mid, model.GoldTarget, HasShow)
  122. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  123. bpGold := int64(w.BcoinBalance * 1000)
  124. t.Logf("own : %d", bpGold)
  125. if bpGold < 3*1000 {
  126. t.Logf("own not enouth: %d", bpGold)
  127. return
  128. }
  129. lw, _ := testGiftService.dao.LiveWallet(context.Background(), mid, "android")
  130. if lw.GoldPayCnt > 0 {
  131. t.Logf("goldPayCnt enouth: %d", lw.GoldPayCnt)
  132. return
  133. }
  134. req := &v1.NeedTipRechargeReq{
  135. From: v1.From_Gold,
  136. NeedGold: 666,
  137. Platform: "android",
  138. }
  139. resp, err := testGiftService.NeedTipRecharge(ctx, req)
  140. So(err, ShouldBeNil)
  141. t.Logf("resp:%+v", resp)
  142. So(resp.Show, ShouldEqual, 1)
  143. So(resp.Bp, ShouldEqual, w.BcoinBalance)
  144. So(resp.BpCoupon, ShouldEqual, w.CouponBalance)
  145. So(resp.RechargeGold, ShouldEqual, 1000)
  146. })
  147. }
  148. func TestGiftService_NeedTipRecharge(t *testing.T) {
  149. Convey("silver condition", t, func() {
  150. Convey("day empty", func() {
  151. testGiftService.dao.DelUserConf(context.Background(), 1, model.SilverTarget, StopPush)
  152. testGiftService.conf.Gift.RechargeTip.SilverTipDays = []int{}
  153. resp, err := testGiftService.silverNeedTipRecharge(context.Background(), 1, &v1.NeedTipRechargeReq{
  154. From: v1.From_Silver,
  155. NeedGold: 0,
  156. Platform: "android",
  157. })
  158. So(err, ShouldBeNil)
  159. So(resp.Show, ShouldEqual, 0)
  160. })
  161. Convey("day hit and has coupon", func() {
  162. mid := int64(3)
  163. yearMonth, _ := strconv.ParseInt(time.Now().Format("200601"), 10, 64)
  164. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, yearMonth)
  165. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, StopPush)
  166. time.Sleep(time.Millisecond * 10) // 因为是异步设置所以需要一点时间延迟
  167. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  168. if w.CouponBalance < 1 {
  169. t.Logf("own not enouth: %v", w.CouponBalance)
  170. return
  171. }
  172. testGiftService.conf.Gift.RechargeTip.SilverTipDays = append(testGiftService.conf.Gift.RechargeTip.SilverTipDays, day(time.Now()))
  173. t.Logf("%v", testGiftService.conf.Gift.RechargeTip.SilverTipDays)
  174. resp, err := testGiftService.silverNeedTipRecharge(context.Background(), mid, &v1.NeedTipRechargeReq{
  175. From: v1.From_Silver,
  176. NeedGold: 0,
  177. Platform: "android",
  178. })
  179. So(err, ShouldBeNil)
  180. t.Logf("resp:%+v", resp)
  181. So(resp.Show, ShouldEqual, 1)
  182. time.Sleep(time.Millisecond * 20) // 因为是异步设置所以需要一点时间延迟
  183. resp, err = testGiftService.silverNeedTipRecharge(context.Background(), mid, &v1.NeedTipRechargeReq{
  184. From: v1.From_Silver,
  185. NeedGold: 0,
  186. Platform: "android",
  187. })
  188. So(err, ShouldBeNil)
  189. t.Logf("resp:%+v", resp)
  190. So(resp.Show, ShouldEqual, 0)
  191. })
  192. Convey("day hit and has no coupon", func() {
  193. mid := int64(20)
  194. yearMonth, _ := strconv.ParseInt(time.Now().Format("200601"), 10, 64)
  195. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, yearMonth)
  196. testGiftService.dao.DelUserConf(context.Background(), mid, model.SilverTarget, StopPush)
  197. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  198. if w.CouponBalance > 1 {
  199. t.Logf("own enouth: %v", w.CouponBalance)
  200. return
  201. }
  202. testGiftService.conf.Gift.RechargeTip.SilverTipDays = append(testGiftService.conf.Gift.RechargeTip.SilverTipDays, day(time.Now()))
  203. t.Logf("%v", testGiftService.conf.Gift.RechargeTip.SilverTipDays)
  204. resp, err := testGiftService.silverNeedTipRecharge(context.Background(), mid, &v1.NeedTipRechargeReq{
  205. From: v1.From_Silver,
  206. NeedGold: 0,
  207. Platform: "android",
  208. })
  209. So(err, ShouldBeNil)
  210. t.Logf("resp:%+v", resp)
  211. So(resp.Show, ShouldEqual, 0)
  212. })
  213. })
  214. Convey("gold condition", t, func() {
  215. mid := int64(3)
  216. testGiftService.dao.DelUserConf(context.Background(), mid, model.GoldTarget, HasShow)
  217. w, _ := testGiftService.dao.PayCenterWallet(context.Background(), mid, "android")
  218. bpGold := int64(w.BcoinBalance * 1000)
  219. t.Logf("own : %d", bpGold)
  220. if bpGold < 3*1000 {
  221. t.Logf("own not enouth: %d", bpGold)
  222. return
  223. }
  224. lw, _ := testGiftService.dao.LiveWallet(context.Background(), mid, "android")
  225. if lw.GoldPayCnt > 0 {
  226. t.Logf("goldPayCnt enouth: %d", lw.GoldPayCnt)
  227. return
  228. }
  229. req := &v1.NeedTipRechargeReq{
  230. From: v1.From_Gold,
  231. NeedGold: 1,
  232. Platform: "android",
  233. }
  234. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*300)
  235. resp, err := testGiftService.goldNeedTipRecharge(ctx, mid, req)
  236. So(err, ShouldBeNil)
  237. t.Logf("resp:%+v", resp)
  238. So(resp.Show, ShouldEqual, 1)
  239. So(resp.Bp, ShouldEqual, w.BcoinBalance)
  240. So(resp.BpCoupon, ShouldEqual, w.CouponBalance)
  241. So(resp.RechargeGold, ShouldEqual, 1000)
  242. cancel()
  243. time.Sleep(time.Millisecond * 20) // 因为是异步设置所以需要一点时间延迟
  244. testGiftService.dao.DelUserConf(context.Background(), mid, model.GoldTarget, HasShow)
  245. req.NeedGold = 1230
  246. ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond*300)
  247. resp, err = testGiftService.goldNeedTipRecharge(ctx, mid, req)
  248. cancel()
  249. So(err, ShouldBeNil)
  250. t.Logf("resp:%+v", resp)
  251. So(resp.Show, ShouldEqual, 1)
  252. So(resp.Bp, ShouldEqual, w.BcoinBalance)
  253. So(resp.BpCoupon, ShouldEqual, w.CouponBalance)
  254. So(resp.RechargeGold, ShouldEqual, 2000)
  255. time.Sleep(time.Millisecond * 10) // 因为是异步设置所以需要一点时间延迟
  256. testGiftService.dao.DelUserConf(context.Background(), mid, model.GoldTarget, HasShow)
  257. req.NeedGold = bpGold + 1
  258. ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond*300)
  259. resp, err = testGiftService.goldNeedTipRecharge(ctx, mid, req)
  260. cancel()
  261. So(err, ShouldBeNil)
  262. t.Logf("resp:%+v", resp)
  263. So(resp.Show, ShouldEqual, 0)
  264. })
  265. }