mysql_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package block
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. model "go-common/app/job/main/member/model/block"
  9. xsql "go-common/library/database/sql"
  10. "github.com/bouk/monkey"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestBlockhistoryIdx(t *testing.T) {
  14. convey.Convey("historyIdx", t, func(convCtx convey.C) {
  15. var (
  16. mid = int64(0)
  17. )
  18. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  19. p1 := historyIdx(mid)
  20. convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
  21. convCtx.So(p1, convey.ShouldNotBeNil)
  22. })
  23. })
  24. })
  25. }
  26. func TestBlockUserStatusList(t *testing.T) {
  27. convey.Convey("UserStatusList", t, func(convCtx convey.C) {
  28. var (
  29. c = context.Background()
  30. status model.BlockStatus
  31. startID = int64(0)
  32. limit = int(10)
  33. )
  34. rows, _ := d.db.Query(c, _userStatusList, status, startID, limit)
  35. convCtx.Convey("UserStatusList success", func(convCtx convey.C) {
  36. guard := monkey.PatchInstanceMethod(reflect.TypeOf(rows), "Scan", func(_ *xsql.Rows, _ ...interface{}) error {
  37. return nil
  38. })
  39. defer guard.Unpatch()
  40. maxID, mids, err := d.UserStatusList(c, status, startID, limit)
  41. convCtx.Convey("Then err should be nil.maxID,mids should be nil.", func(convCtx convey.C) {
  42. convCtx.So(err, convey.ShouldBeNil)
  43. convCtx.So(mids, convey.ShouldNotBeNil)
  44. convCtx.So(maxID, convey.ShouldNotBeNil)
  45. })
  46. })
  47. })
  48. }
  49. func TestBlockUserLastHistory(t *testing.T) {
  50. convey.Convey("UserLastHistory", t, func(convCtx convey.C) {
  51. var (
  52. c = context.Background()
  53. mid = int64(0)
  54. )
  55. convey.Convey("UserLastHistory success", func() {
  56. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  57. return nil
  58. })
  59. defer monkey.UnpatchAll()
  60. his, err := d.UserLastHistory(c, mid)
  61. convCtx.So(err, convey.ShouldBeNil)
  62. convCtx.So(his, convey.ShouldNotBeNil)
  63. })
  64. convey.Convey("UserLastHistory err", func() {
  65. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  66. return fmt.Errorf("row.Scan error")
  67. })
  68. defer monkey.UnpatchAll()
  69. his, err := d.UserLastHistory(c, mid)
  70. convCtx.So(err, convey.ShouldNotBeNil)
  71. convCtx.So(his, convey.ShouldNotBeNil)
  72. })
  73. convey.Convey("UserLastHistory no record", func() {
  74. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), fmt.Sprintf(_userLastHistory, historyIdx(mid)), 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  75. return sql.ErrNoRows
  76. })
  77. defer monkey.UnpatchAll()
  78. his, err := d.UserLastHistory(c, mid)
  79. convCtx.So(err, convey.ShouldBeNil)
  80. convCtx.So(his, convey.ShouldBeNil)
  81. })
  82. })
  83. }
  84. func TestBlockUserExtra(t *testing.T) {
  85. convey.Convey("UserExtra", t, func(convCtx convey.C) {
  86. var (
  87. c = context.Background()
  88. mid = int64(0)
  89. )
  90. convey.Convey("UserExtra success", func() {
  91. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  92. return nil
  93. })
  94. defer monkey.UnpatchAll()
  95. ex, err := d.UserExtra(c, mid)
  96. convCtx.So(err, convey.ShouldBeNil)
  97. convCtx.So(ex, convey.ShouldNotBeNil)
  98. })
  99. convey.Convey("UserExtra err", func() {
  100. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  101. return fmt.Errorf("row.Scan error")
  102. })
  103. defer monkey.UnpatchAll()
  104. ex, err := d.UserExtra(c, mid)
  105. convCtx.So(err, convey.ShouldNotBeNil)
  106. convCtx.So(ex, convey.ShouldNotBeNil)
  107. })
  108. convey.Convey("UserExtra no record", func() {
  109. monkey.PatchInstanceMethod(reflect.TypeOf(d.db.QueryRow(context.TODO(), _userExtra, 1)), "Scan", func(_ *xsql.Row, _ ...interface{}) error {
  110. return sql.ErrNoRows
  111. })
  112. defer monkey.UnpatchAll()
  113. ex, err := d.UserExtra(c, mid)
  114. convCtx.So(err, convey.ShouldBeNil)
  115. convCtx.So(ex, convey.ShouldBeNil)
  116. })
  117. })
  118. }
  119. func TestBlockTxUpsertUser(t *testing.T) {
  120. convey.Convey("TxUpsertUser", t, func(convCtx convey.C) {
  121. var (
  122. c = context.Background()
  123. tx, _ = d.BeginTX(c)
  124. mid = int64(0)
  125. status model.BlockStatus
  126. )
  127. convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
  128. count, err := d.TxUpsertUser(c, tx, mid, status)
  129. convCtx.Convey("Then err should be nil.count should not be nil.", func(convCtx convey.C) {
  130. convCtx.So(err, convey.ShouldBeNil)
  131. convCtx.So(count, convey.ShouldNotBeNil)
  132. })
  133. })
  134. convCtx.Reset(func() {
  135. tx.Commit()
  136. })
  137. })
  138. }
  139. func TestBlockInsertExtra(t *testing.T) {
  140. convey.Convey("InsertExtra", t, func(convCtx convey.C) {
  141. var (
  142. c = context.Background()
  143. ex = &model.DBExtra{}
  144. )
  145. convCtx.Convey("InsertExtra success", func(convCtx convey.C) {
  146. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  147. return nil, nil
  148. })
  149. defer monkey.UnpatchAll()
  150. err := d.InsertExtra(c, ex)
  151. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  152. convCtx.So(err, convey.ShouldBeNil)
  153. })
  154. })
  155. convCtx.Convey("InsertExtra err", func(convCtx convey.C) {
  156. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  157. return nil, fmt.Errorf("insert err")
  158. })
  159. defer monkey.UnpatchAll()
  160. err := d.InsertExtra(c, ex)
  161. convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
  162. convCtx.So(err, convey.ShouldNotBeNil)
  163. })
  164. })
  165. })
  166. }
  167. func TestBlockTxUpsertExtra(t *testing.T) {
  168. convey.Convey("TxUpsertExtra", t, func(convCtx convey.C) {
  169. var (
  170. c = context.Background()
  171. tx, _ = d.BeginTX(c)
  172. ex = &model.DBExtra{}
  173. )
  174. convCtx.Convey("TxUpsertExtra success", func(convCtx convey.C) {
  175. monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
  176. return nil, nil
  177. })
  178. defer monkey.UnpatchAll()
  179. err := d.TxUpsertExtra(c, tx, ex)
  180. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  181. convCtx.So(err, convey.ShouldBeNil)
  182. })
  183. })
  184. convCtx.Convey("TxUpsertExtra err", func(convCtx convey.C) {
  185. monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
  186. return nil, fmt.Errorf("insert err")
  187. })
  188. defer monkey.UnpatchAll()
  189. err := d.TxUpsertExtra(c, tx, ex)
  190. convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
  191. convCtx.So(err, convey.ShouldNotBeNil)
  192. })
  193. })
  194. convCtx.Reset(func() {
  195. tx.Commit()
  196. })
  197. })
  198. }
  199. func TestBlockTxInsertHistory(t *testing.T) {
  200. convey.Convey("TxInsertHistory", t, func(convCtx convey.C) {
  201. var (
  202. c = context.Background()
  203. tx, _ = d.BeginTX(c)
  204. h = &model.DBHistory{}
  205. )
  206. convCtx.Convey("TxUpsertExtra success", func(convCtx convey.C) {
  207. monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
  208. return nil, nil
  209. })
  210. defer monkey.UnpatchAll()
  211. err := d.TxInsertHistory(c, tx, h)
  212. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  213. convCtx.So(err, convey.ShouldBeNil)
  214. })
  215. })
  216. convCtx.Convey("TxUpsertExtra err", func(convCtx convey.C) {
  217. monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec", func(_ *xsql.Tx, _ string, _ ...interface{}) (sql.Result, error) {
  218. return nil, fmt.Errorf("insert err")
  219. })
  220. defer monkey.UnpatchAll()
  221. err := d.TxInsertHistory(c, tx, h)
  222. convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
  223. convCtx.So(err, convey.ShouldNotBeNil)
  224. })
  225. })
  226. convCtx.Reset(func() {
  227. tx.Commit()
  228. })
  229. })
  230. }
  231. func TestBlockUpsertAddBlockCount(t *testing.T) {
  232. convey.Convey("UpsertAddBlockCount", t, func(convCtx convey.C) {
  233. var (
  234. c = context.Background()
  235. mid = int64(0)
  236. )
  237. convCtx.Convey("UpsertAddBlockCount success", func(convCtx convey.C) {
  238. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  239. return nil, nil
  240. })
  241. defer monkey.UnpatchAll()
  242. err := d.UpsertAddBlockCount(c, mid)
  243. convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
  244. convCtx.So(err, convey.ShouldBeNil)
  245. })
  246. })
  247. convCtx.Convey("InsertExtra err", func(convCtx convey.C) {
  248. monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Exec", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (sql.Result, error) {
  249. return nil, fmt.Errorf("insert err")
  250. })
  251. defer monkey.UnpatchAll()
  252. err := d.UpsertAddBlockCount(c, mid)
  253. convCtx.Convey("Then err should not be nil.", func(convCtx convey.C) {
  254. convCtx.So(err, convey.ShouldNotBeNil)
  255. })
  256. })
  257. })
  258. }