mysql_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package block
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. model "go-common/app/service/main/member/model/block"
  7. "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDaohistoryIdx(t *testing.T) {
  10. convey.Convey("historyIdx", t, func(ctx convey.C) {
  11. var (
  12. mid = int64(46333)
  13. )
  14. ctx.Convey("When everything right", func(ctx convey.C) {
  15. shard := historyIdx(mid)
  16. ctx.Convey("Then shard should equal mid % 10.", func(ctx convey.C) {
  17. ctx.So(shard, convey.ShouldEqual, 3)
  18. })
  19. })
  20. })
  21. }
  22. func TestDaoUser(t *testing.T) {
  23. convey.Convey("User", t, func(ctx convey.C) {
  24. var (
  25. c = context.Background()
  26. mid = int64(46333)
  27. )
  28. ctx.Convey("When everything right", func(ctx convey.C) {
  29. user, err := d.User(c, mid)
  30. ctx.Convey("Then err should be nil.user should not be nil.", func(ctx convey.C) {
  31. ctx.So(err, convey.ShouldBeNil)
  32. ctx.So(user, convey.ShouldNotBeNil)
  33. ctx.So(user.MID, convey.ShouldEqual, mid)
  34. })
  35. })
  36. })
  37. }
  38. func TestDaoTxInsertHistory(t *testing.T) {
  39. convey.Convey("TxInsertHistory", t, func(ctx convey.C) {
  40. var (
  41. c = context.Background()
  42. tx, _ = d.db.Begin(c)
  43. his = &model.DBHistory{
  44. MID: 46333,
  45. AdminID: 2333,
  46. AdminName: "ut",
  47. Source: model.BlockSourceBlackHouse,
  48. Area: model.BlockAreaAlbum,
  49. Reason: "ut reason",
  50. Comment: "ut comment",
  51. Action: model.BlockActionLimit,
  52. StartTime: time.Now(),
  53. Duration: 60,
  54. Notify: false,
  55. CTime: time.Now(),
  56. MTime: time.Now(),
  57. }
  58. )
  59. ctx.Convey("When everything right", func(ctx convey.C) {
  60. err := d.TxInsertHistory(c, tx, his)
  61. ctx.So(err, convey.ShouldBeNil)
  62. err = tx.Commit()
  63. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  64. ctx.So(err, convey.ShouldBeNil)
  65. ctx.Convey("When get UserLastHistory", func(ctx convey.C) {
  66. his2, err := d.UserLastHistory(c, his.MID)
  67. ctx.Convey("Then err should be nil.his2 should resemble his.", func(ctx convey.C) {
  68. ctx.So(err, convey.ShouldBeNil)
  69. ctx.So(his2.MID, convey.ShouldEqual, his.MID)
  70. ctx.So(his2.Action, convey.ShouldEqual, his.Action)
  71. ctx.So(his2.StartTime.Unix(), convey.ShouldEqual, his.StartTime.Unix())
  72. ctx.So(his2.Duration, convey.ShouldEqual, his.Duration)
  73. })
  74. })
  75. })
  76. })
  77. })
  78. }
  79. func TestDaoTxUpdateUser(t *testing.T) {
  80. var (
  81. c = context.Background()
  82. tx, _ = d.db.Begin(c)
  83. mid = int64(46333)
  84. status = model.BlockStatusFalse
  85. )
  86. convey.Convey("TxUpdateUser", t, func(ctx convey.C) {
  87. ctx.Convey("When everything right", func(ctx convey.C) {
  88. err := d.TxUpdateUser(c, tx, mid, status)
  89. ctx.So(err, convey.ShouldBeNil)
  90. err = tx.Commit()
  91. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  92. ctx.So(err, convey.ShouldBeNil)
  93. })
  94. })
  95. })
  96. }
  97. func TestDaoUserStatusMapWithMIDs(t *testing.T) {
  98. convey.Convey("UserStatusMapWithMIDs", t, func(ctx convey.C) {
  99. var (
  100. c = context.Background()
  101. status = model.BlockStatusFalse
  102. mids = []int64{46333, 2, 35858}
  103. )
  104. ctx.Convey("When everything right", func(ctx convey.C) {
  105. midMap, err := d.UserStatusMapWithMIDs(c, status, mids)
  106. ctx.Convey("Then err should be nil.midMap should not be nil.", func(ctx convey.C) {
  107. ctx.So(err, convey.ShouldBeNil)
  108. ctx.So(midMap, convey.ShouldNotBeNil)
  109. })
  110. })
  111. })
  112. }
  113. func TestDaoUpdateAddBlockCount(t *testing.T) {
  114. convey.Convey("UpdateAddBlockCount", t, func(ctx convey.C) {
  115. var (
  116. c = context.TODO()
  117. mid = int64(46333)
  118. )
  119. ctx.Convey("When everything right", func(ctx convey.C) {
  120. err := d.UpdateAddBlockCount(c, mid)
  121. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  122. ctx.So(err, convey.ShouldBeNil)
  123. })
  124. })
  125. })
  126. }
  127. func TestUserDetails(t *testing.T) {
  128. convey.Convey("get user details", t, func(ctx convey.C) {
  129. var (
  130. c = context.Background()
  131. mid = int64(46333)
  132. )
  133. ctx.Convey("When get user details from db", func(ctx convey.C) {
  134. _, err := d.UserDetails(c, []int64{mid})
  135. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  136. ctx.So(err, convey.ShouldBeNil)
  137. })
  138. })
  139. })
  140. }