reply_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. "go-common/app/interface/main/feedback/model"
  9. "go-common/library/database/sql"
  10. "github.com/bouk/monkey"
  11. "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestDaoTxAddReply(t *testing.T) {
  14. convey.Convey("TxAddReply", t, func(ctx convey.C) {
  15. var (
  16. tx, _ = d.BeginTran(context.Background())
  17. r = &model.Reply{}
  18. )
  19. ctx.Convey("When everything is correct", func(ctx convey.C) {
  20. id, err := d.TxAddReply(tx, r)
  21. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  22. ctx.So(err, convey.ShouldBeNil)
  23. ctx.So(id, convey.ShouldNotBeNil)
  24. })
  25. })
  26. ctx.Convey("When tx.Exec gets error", func(ctx convey.C) {
  27. guard := monkey.PatchInstanceMethod(reflect.TypeOf(tx), "Exec",
  28. func(_ *sql.Tx, _ string, _ ...interface{}) (xsql.Result, error) {
  29. return nil, fmt.Errorf("tx.Exec Error")
  30. })
  31. defer guard.Unpatch()
  32. _, err := d.TxAddReply(tx, r)
  33. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  34. ctx.So(err, convey.ShouldNotBeNil)
  35. })
  36. })
  37. ctx.Reset(func() {
  38. tx.Rollback()
  39. })
  40. })
  41. }
  42. func TestDaoAddReply(t *testing.T) {
  43. convey.Convey("AddReply", t, func(ctx convey.C) {
  44. var (
  45. c = context.TODO()
  46. r = &model.Reply{}
  47. )
  48. ctx.Convey("When everything is correct", func(ctx convey.C) {
  49. id, err := d.AddReply(c, r)
  50. ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
  51. ctx.So(err, convey.ShouldBeNil)
  52. ctx.So(id, convey.ShouldNotBeNil)
  53. })
  54. })
  55. ctx.Convey("When d.inReply.Exec gets error", func(ctx convey.C) {
  56. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.inReply), "Exec",
  57. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (xsql.Result, error) {
  58. return nil, fmt.Errorf("d.inReply.Exec Error")
  59. })
  60. defer guard.Unpatch()
  61. _, err := d.AddReply(c, r)
  62. ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
  63. ctx.So(err, convey.ShouldNotBeNil)
  64. })
  65. })
  66. })
  67. }
  68. func TestDaoReplys(t *testing.T) {
  69. convey.Convey("Replys", t, func(ctx convey.C) {
  70. var (
  71. c = context.TODO()
  72. ssnID = int64(3131)
  73. offset = int(1)
  74. limit = int(10)
  75. )
  76. ctx.Convey("When everything is correct", func(ctx convey.C) {
  77. rs, err := d.Replys(c, ssnID, offset, limit)
  78. ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
  79. ctx.So(err, convey.ShouldBeNil)
  80. ctx.SkipSo(rs, convey.ShouldNotBeNil)
  81. })
  82. })
  83. ctx.Convey("When d.selReply.Query gets error", func(ctx convey.C) {
  84. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReply), "Query",
  85. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
  86. return nil, fmt.Errorf("d.selReply.Query Error")
  87. })
  88. defer guard.Unpatch()
  89. _, err := d.Replys(c, ssnID, offset, limit)
  90. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  91. ctx.So(err, convey.ShouldNotBeNil)
  92. })
  93. })
  94. })
  95. }
  96. func TestDaoWebReplys(t *testing.T) {
  97. convey.Convey("WebReplys", t, func(ctx convey.C) {
  98. var (
  99. c = context.TODO()
  100. ssnID = int64(3131)
  101. mid = int64(1313)
  102. )
  103. ctx.Convey("When everything is correct", func(ctx convey.C) {
  104. rs, err := d.WebReplys(c, ssnID, mid)
  105. ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
  106. ctx.So(err, convey.ShouldBeNil)
  107. ctx.SkipSo(rs, convey.ShouldNotBeNil)
  108. })
  109. })
  110. ctx.Convey("When d.selReplyBySid.Query gets error", func(ctx convey.C) {
  111. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReplyBySid), "Query",
  112. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
  113. return nil, fmt.Errorf("d.selReplyBySid.Query Error")
  114. })
  115. defer guard.Unpatch()
  116. _, err := d.WebReplys(c, ssnID, mid)
  117. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  118. ctx.So(err, convey.ShouldNotBeNil)
  119. })
  120. })
  121. })
  122. }
  123. func TestDaoReplysByMid(t *testing.T) {
  124. convey.Convey("ReplysByMid", t, func(ctx convey.C) {
  125. var (
  126. c = context.TODO()
  127. mid = int64(11424224)
  128. offset = int(1)
  129. limit = int(10)
  130. )
  131. ctx.Convey("ReplysByMid", func(ctx convey.C) {
  132. rs, err := d.ReplysByMid(c, mid, offset, limit)
  133. ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
  134. ctx.So(err, convey.ShouldBeNil)
  135. ctx.SkipSo(rs, convey.ShouldNotBeNil)
  136. })
  137. })
  138. ctx.Convey("When d.selReplyByMid.Query gets error", func(ctx convey.C) {
  139. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.selReplyByMid), "Query",
  140. func(_ *sql.Stmt, _ context.Context, _ ...interface{}) (*sql.Rows, error) {
  141. return nil, fmt.Errorf("d.selReplyByMid.Query Error")
  142. })
  143. defer guard.Unpatch()
  144. _, err := d.ReplysByMid(c, mid, offset, limit)
  145. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  146. ctx.So(err, convey.ShouldNotBeNil)
  147. })
  148. })
  149. })
  150. }