tx_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dao
  2. import (
  3. "fmt"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "go-common/app/service/live/wallet/model"
  6. "testing"
  7. "time"
  8. )
  9. func TestDao_Tx1(t *testing.T) {
  10. Convey("commit wrong sample", t, func() {
  11. once.Do(startService)
  12. uid := int64(1)
  13. var wallet *model.Melonseed
  14. wallet, err := d.Melonseed(ctx, uid)
  15. t.Logf("uid gold : %d", wallet.Gold)
  16. og := wallet.Gold
  17. So(err, ShouldBeNil)
  18. So(1, ShouldEqual, 1)
  19. tx, err := d.db.Begin(ctx)
  20. So(err, ShouldBeNil)
  21. affect, err := d.AddGold(ctx, uid, 1)
  22. So(affect, ShouldBeGreaterThan, 0)
  23. So(err, ShouldBeNil)
  24. err = tx.Commit()
  25. So(err, ShouldBeNil)
  26. wallet, err = d.Melonseed(ctx, uid)
  27. t.Logf("uid gold : %d", wallet.Gold)
  28. So(err, ShouldBeNil)
  29. So(wallet.Gold-og, ShouldEqual, 1)
  30. })
  31. Convey("rollback wrong sample use db ins instead tx", t, func() {
  32. once.Do(startService)
  33. uid := int64(1)
  34. var wallet *model.Melonseed
  35. wallet, err := d.Melonseed(ctx, uid)
  36. t.Logf("uid gold : %d", wallet.Gold)
  37. og := wallet.Gold
  38. So(err, ShouldBeNil)
  39. So(1, ShouldEqual, 1)
  40. tx, err := d.db.Begin(ctx)
  41. So(err, ShouldBeNil)
  42. affect, err := d.AddGold(ctx, uid, 1)
  43. So(affect, ShouldBeGreaterThan, 0)
  44. So(err, ShouldBeNil)
  45. err = tx.Rollback()
  46. So(err, ShouldBeNil)
  47. wallet, err = d.Melonseed(ctx, uid)
  48. t.Logf("uid gold : %d", wallet.Gold)
  49. So(err, ShouldBeNil)
  50. So(wallet.Gold-og, ShouldEqual, 1)
  51. })
  52. Convey("commit", t, func() {
  53. once.Do(startService)
  54. uid := int64(1)
  55. var wallet *model.Melonseed
  56. wallet, err := d.Melonseed(ctx, uid)
  57. t.Logf("uid gold : %d", wallet.Gold)
  58. og := wallet.Gold
  59. So(err, ShouldBeNil)
  60. So(1, ShouldEqual, 1)
  61. tx, err := d.db.Begin(ctx)
  62. So(err, ShouldBeNil)
  63. res, err := tx.Exec(fmt.Sprintf("update user_wallet_%d set gold = gold + ? where uid = ?", uid%10), 1, uid)
  64. So(err, ShouldBeNil)
  65. affect, err := res.RowsAffected()
  66. So(affect, ShouldEqual, 1)
  67. So(err, ShouldBeNil)
  68. err = tx.Commit()
  69. So(err, ShouldBeNil)
  70. wallet, err = d.Melonseed(ctx, uid)
  71. t.Logf("uid gold : %d", wallet.Gold)
  72. So(err, ShouldBeNil)
  73. So(wallet.Gold-og, ShouldEqual, 1)
  74. })
  75. Convey("rollback", t, func() {
  76. once.Do(startService)
  77. uid := int64(1)
  78. var wallet *model.Melonseed
  79. wallet, err := d.Melonseed(ctx, uid)
  80. t.Logf("uid gold : %d", wallet.Gold)
  81. og := wallet.Gold
  82. So(err, ShouldBeNil)
  83. So(1, ShouldEqual, 1)
  84. tx, err := d.db.Begin(ctx)
  85. So(err, ShouldBeNil)
  86. res, err := tx.Exec(fmt.Sprintf("update user_wallet_%d set gold = gold + ? where uid = ?", uid%10), 1, uid)
  87. So(err, ShouldBeNil)
  88. affect, err := res.RowsAffected()
  89. So(affect, ShouldEqual, 1)
  90. So(err, ShouldBeNil)
  91. err = tx.Rollback()
  92. So(err, ShouldBeNil)
  93. wallet, err = d.Melonseed(ctx, uid)
  94. t.Logf("uid gold : %d", wallet.Gold)
  95. So(err, ShouldBeNil)
  96. So(wallet.Gold-og, ShouldEqual, 0)
  97. })
  98. }
  99. func TestDao_DoubleCoin(t *testing.T) {
  100. Convey("check1", t, func() {
  101. d := &model.DetailWithSnapShot{}
  102. So(model.NeedSnapshot(d, time.Now()), ShouldBeTrue)
  103. d.SnapShotTime = "2018-09-22 16:39:05"
  104. So(model.NeedSnapshot(d, time.Now()), ShouldBeTrue)
  105. now, _ := time.Parse("2006-01-02 15:04:05", "2018-09-22 16:39:04")
  106. t.Logf("today:%+v", now)
  107. So(model.NeedSnapshot(d, now), ShouldBeFalse)
  108. now = model.GetTodayTime(now)
  109. t.Logf("today:%+v", now)
  110. d.SnapShotTime = "2018-09-22 04:39:04"
  111. So(model.NeedSnapshot(d, now), ShouldBeFalse)
  112. d.SnapShotTime = "2018-09-21 04:39:04"
  113. So(model.NeedSnapshot(d, now), ShouldBeTrue)
  114. d.SnapShotTime = time.Now().Format("2006-01-02 15:04:05")
  115. t.Logf("today:%+v", d.SnapShotTime)
  116. So(model.TodayNeedSnapShot(d), ShouldBeFalse)
  117. d.SnapShotTime = time.Now().Add(-time.Second * 86400).Format("2006-01-02 15:04:05")
  118. t.Logf("today:%+v", d.SnapShotTime)
  119. So(model.TodayNeedSnapShot(d), ShouldBeTrue)
  120. })
  121. }