wallet_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package dao
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "testing"
  7. "time"
  8. "go-common/app/service/live/wallet/conf"
  9. "go-common/library/log"
  10. . "github.com/smartystreets/goconvey/convey"
  11. "go-common/app/service/live/wallet/model"
  12. "math/rand"
  13. )
  14. var (
  15. once sync.Once
  16. d *Dao
  17. ctx = context.Background()
  18. testUID int64
  19. r *rand.Rand
  20. )
  21. func initConf() {
  22. if err := conf.Init(); err != nil {
  23. panic(err)
  24. }
  25. log.Init(conf.Conf.Log)
  26. defer log.Close()
  27. }
  28. func TestMain(m *testing.M) {
  29. conf.ConfPath = "../cmd/live-wallet-test.toml"
  30. once.Do(startService)
  31. os.Exit(m.Run())
  32. }
  33. func startService() {
  34. initConf()
  35. d = New(conf.Conf)
  36. r = rand.New(rand.NewSource(time.Now().UnixNano()))
  37. testUID = r.Int63n(100000000)
  38. log.Info("test uid:%ld", testUID)
  39. time.Sleep(time.Second * 1)
  40. }
  41. func TestInitWallet(t *testing.T) {
  42. Convey("Init Wallet", t, func() {
  43. once.Do(startService)
  44. _, err := d.InitWallet(ctx, testUID, 0, 0, 0)
  45. So(err, ShouldBeNil)
  46. })
  47. }
  48. func TestMelonseed(t *testing.T) {
  49. Convey("Melon seed", t, func() {
  50. once.Do(startService)
  51. var wallet *model.Melonseed
  52. wallet, err := d.Melonseed(ctx, testUID)
  53. So(err, ShouldBeNil)
  54. So(wallet.Gold, ShouldEqual, 0)
  55. })
  56. }
  57. func TestDetail(t *testing.T) {
  58. Convey("Detail", t, func() {
  59. once.Do(startService)
  60. var detail *model.Detail
  61. detail, err := d.Detail(ctx, testUID)
  62. So(err, ShouldBeNil)
  63. So(detail.Gold, ShouldEqual, 0)
  64. So(detail.Silver, ShouldEqual, 0)
  65. So(detail.SilverPayCnt, ShouldEqual, 0)
  66. So(detail.IapGold, ShouldEqual, 0)
  67. So(detail.GoldRechargeCnt, ShouldEqual, 0)
  68. So(detail.GoldPayCnt, ShouldEqual, 0)
  69. })
  70. }
  71. func TestDao_AddGold(t *testing.T) {
  72. Convey("Add Gold", t, func() {
  73. once.Do(startService)
  74. var testUID = r.Int63n(100000000)
  75. var odetail *model.Detail
  76. odetail, err := d.Detail(ctx, testUID)
  77. So(err, ShouldBeNil)
  78. num := 1000
  79. affect, err := d.AddGold(ctx, testUID, num)
  80. So(err, ShouldBeNil)
  81. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  82. var ndetail *model.Detail
  83. ndetail, err = d.Detail(ctx, testUID)
  84. So(err, ShouldBeNil)
  85. So(ndetail.Gold-odetail.Gold, ShouldEqual, num)
  86. So(ndetail.GoldRechargeCnt, ShouldEqual, odetail.GoldRechargeCnt)
  87. })
  88. }
  89. func TestDao_RechargeGold(t *testing.T) {
  90. Convey("recharge gold", t, func() {
  91. once.Do(startService)
  92. var testUID = r.Int63n(100000000)
  93. var odetail *model.Detail
  94. odetail, err := d.Detail(ctx, testUID)
  95. So(err, ShouldBeNil)
  96. num := 1000
  97. affect, err := d.RechargeGold(ctx, testUID, num)
  98. So(err, ShouldBeNil)
  99. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  100. var ndetail *model.Detail
  101. ndetail, err = d.Detail(ctx, testUID)
  102. So(err, ShouldBeNil)
  103. So(ndetail.Gold-odetail.Gold, ShouldEqual, num)
  104. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, 0)
  105. So(ndetail.GoldRechargeCnt-odetail.GoldRechargeCnt, ShouldEqual, num)
  106. })
  107. }
  108. func TestDao_RechargeIapGold(t *testing.T) {
  109. Convey("recharge iap gold", t, func() {
  110. once.Do(startService)
  111. var testUID = r.Int63n(100000000)
  112. var odetail *model.Detail
  113. odetail, err := d.Detail(ctx, testUID)
  114. So(err, ShouldBeNil)
  115. num := 1000
  116. affect, err := d.RechargeIapGold(ctx, testUID, num)
  117. So(err, ShouldBeNil)
  118. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  119. var ndetail *model.Detail
  120. ndetail, err = d.Detail(ctx, testUID)
  121. So(err, ShouldBeNil)
  122. So(ndetail.Gold-odetail.Gold, ShouldEqual, 0)
  123. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, num)
  124. So(ndetail.GoldRechargeCnt-odetail.GoldRechargeCnt, ShouldEqual, num)
  125. })
  126. }
  127. func TestDao_AddIapGold(t *testing.T) {
  128. Convey("Add iap Gold", t, func() {
  129. once.Do(startService)
  130. var testUID = r.Int63n(100000000)
  131. var odetail *model.Detail
  132. odetail, err := d.Detail(ctx, testUID)
  133. So(err, ShouldBeNil)
  134. num := 1000
  135. affect, err := d.AddIapGold(ctx, testUID, num)
  136. So(err, ShouldBeNil)
  137. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  138. var ndetail *model.Detail
  139. ndetail, err = d.Detail(ctx, testUID)
  140. So(err, ShouldBeNil)
  141. So(ndetail.Gold-odetail.Gold, ShouldEqual, 0)
  142. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, num)
  143. So(ndetail.GoldRechargeCnt, ShouldEqual, odetail.GoldRechargeCnt)
  144. })
  145. }
  146. func TestDao_AddSilver(t *testing.T) {
  147. Convey("Add silver", t, func() {
  148. once.Do(startService)
  149. var testUID = r.Int63n(100000000)
  150. var odetail *model.Detail
  151. odetail, err := d.Detail(ctx, testUID)
  152. So(err, ShouldBeNil)
  153. num := 1000
  154. affect, err := d.AddSilver(ctx, testUID, num)
  155. So(err, ShouldBeNil)
  156. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  157. var ndetail *model.Detail
  158. ndetail, err = d.Detail(ctx, testUID)
  159. So(err, ShouldBeNil)
  160. So(ndetail.Gold-odetail.Gold, ShouldEqual, 0)
  161. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, 0)
  162. So(ndetail.Silver-odetail.Silver, ShouldEqual, num)
  163. })
  164. }
  165. func TestDao_ConsumeGold(t *testing.T) {
  166. Convey("Consume Gold", t, func() {
  167. once.Do(startService)
  168. var testUID = r.Int63n(100000000)
  169. var odetail *model.Detail
  170. odetail, err := d.Detail(ctx, testUID)
  171. So(err, ShouldBeNil)
  172. num := 1000
  173. affect, err := d.RechargeGold(ctx, testUID, num)
  174. So(err, ShouldBeNil)
  175. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  176. pay := 500
  177. affect, err = d.ConsumeGold(ctx, testUID, pay)
  178. So(err, ShouldBeNil)
  179. So(affect, ShouldEqual, 1)
  180. var ndetail *model.Detail
  181. ndetail, err = d.Detail(ctx, testUID)
  182. So(err, ShouldBeNil)
  183. So(ndetail.Gold-odetail.Gold, ShouldEqual, num-pay)
  184. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, 0)
  185. So(ndetail.GoldRechargeCnt-odetail.GoldRechargeCnt, ShouldEqual, num)
  186. So(ndetail.GoldPayCnt-odetail.GoldPayCnt, ShouldEqual, pay)
  187. })
  188. }
  189. func TestDao_ConsumeIapGold(t *testing.T) {
  190. Convey("Consume Iap Gold", t, func() {
  191. once.Do(startService)
  192. var testUID = r.Int63n(100000000)
  193. var odetail *model.Detail
  194. odetail, err := d.Detail(ctx, testUID)
  195. So(err, ShouldBeNil)
  196. num := 1000
  197. affect, err := d.RechargeIapGold(ctx, testUID, num)
  198. So(err, ShouldBeNil)
  199. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  200. pay := 500
  201. affect, err = d.ConsumeIapGold(ctx, testUID, pay)
  202. So(err, ShouldBeNil)
  203. So(affect, ShouldEqual, 1)
  204. var ndetail *model.Detail
  205. ndetail, err = d.Detail(ctx, testUID)
  206. So(err, ShouldBeNil)
  207. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, num-pay)
  208. So(ndetail.Gold-odetail.Gold, ShouldEqual, 0)
  209. So(ndetail.GoldRechargeCnt-odetail.GoldRechargeCnt, ShouldEqual, num)
  210. So(ndetail.GoldPayCnt-odetail.GoldPayCnt, ShouldEqual, pay)
  211. })
  212. }
  213. func TestDao_ConsumeSilver(t *testing.T) {
  214. Convey("Consume Silver", t, func() {
  215. once.Do(startService)
  216. var testUID = r.Int63n(100000000)
  217. var odetail *model.Detail
  218. odetail, err := d.Detail(ctx, testUID)
  219. So(err, ShouldBeNil)
  220. num := 1000
  221. affect, err := d.AddSilver(ctx, testUID, num)
  222. So(err, ShouldBeNil)
  223. So(affect, ShouldBeGreaterThanOrEqualTo, 1) // 插入affect=1 更新affect=2
  224. pay := 500
  225. affect, err = d.ConsumeSilver(ctx, testUID, pay)
  226. So(err, ShouldBeNil)
  227. So(affect, ShouldEqual, 1)
  228. var ndetail *model.Detail
  229. ndetail, err = d.Detail(ctx, testUID)
  230. So(err, ShouldBeNil)
  231. So(ndetail.IapGold-odetail.IapGold, ShouldEqual, 0)
  232. So(ndetail.Gold-odetail.Gold, ShouldEqual, 0)
  233. So(ndetail.Silver-odetail.Silver, ShouldEqual, num-pay)
  234. So(ndetail.GoldRechargeCnt-odetail.GoldRechargeCnt, ShouldEqual, 0)
  235. So(ndetail.GoldPayCnt-odetail.GoldPayCnt, ShouldEqual, 0)
  236. So(ndetail.SilverPayCnt-odetail.SilverPayCnt, ShouldEqual, pay)
  237. })
  238. }