reply_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "go-common/app/admin/main/reply/model"
  8. xtime "go-common/library/time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func deleteReply(d *Dao, oid int64, rpid int64) error {
  12. _delSQL := "Delete from reply_%d where id = ?"
  13. _, err := d.db.Exec(context.Background(), fmt.Sprintf(_delSQL, hit(oid)), rpid)
  14. if err != nil {
  15. return err
  16. }
  17. return nil
  18. }
  19. func TestDataReply(t *testing.T) {
  20. d := _d
  21. c := context.Background()
  22. now := time.Now()
  23. r := model.Reply{
  24. ID: 2,
  25. Oid: 86,
  26. Type: 1,
  27. Mid: 2233,
  28. Floor: 1,
  29. CTime: xtime.Time(now.Unix()),
  30. MTime: xtime.Time(now.Unix()),
  31. }
  32. d.InsertReply(c, &r)
  33. defer deleteReply(d, r.Oid, r.ID)
  34. r2 := model.Reply{
  35. ID: 1,
  36. Oid: 6,
  37. Type: 1,
  38. Mid: 2233,
  39. Floor: 1,
  40. CTime: xtime.Time(now.Unix()),
  41. MTime: xtime.Time(now.Unix()),
  42. }
  43. d.InsertReply(c, &r2)
  44. defer deleteReply(d, r2.Oid, r2.ID)
  45. Convey("reply test", t, WithDao(func(d *Dao) {
  46. Convey("get reply", WithDao(func(d *Dao) {
  47. rp, err := d.Reply(c, 86, r.ID)
  48. So(err, ShouldBeNil)
  49. So(rp, ShouldNotBeNil)
  50. So(rp.ID, ShouldEqual, r.ID)
  51. So(rp.Mid, ShouldEqual, 2233)
  52. }))
  53. Convey("increase reply RCount", WithDao(func(d *Dao) {
  54. t, err := d.BeginTran(c)
  55. So(err, ShouldBeNil)
  56. now = time.Now()
  57. rows, err := d.TxIncrReplyRCount(t, 86, r.ID, now)
  58. So(err, ShouldBeNil)
  59. So(rows, ShouldBeGreaterThan, 0)
  60. err = t.Commit()
  61. So(err, ShouldBeNil)
  62. rp, err := d.Reply(c, 86, r.ID)
  63. So(err, ShouldBeNil)
  64. So(rp, ShouldNotBeNil)
  65. So(rp.RCount, ShouldEqual, 1)
  66. So(rp.MTime, ShouldEqual, now.Unix())
  67. Convey("decrease reply RCount", WithDao(func(d *Dao) {
  68. t, err := d.BeginTran(c)
  69. So(err, ShouldBeNil)
  70. now = time.Now()
  71. rows, err := d.TxDecrReplyRCount(t, 86, r.ID, now)
  72. So(err, ShouldBeNil)
  73. So(rows, ShouldBeGreaterThan, 0)
  74. err = t.Commit()
  75. So(err, ShouldBeNil)
  76. rp, err := d.Reply(c, 86, r.ID)
  77. So(err, ShouldBeNil)
  78. So(rp, ShouldNotBeNil)
  79. So(rp.RCount, ShouldEqual, 0)
  80. So(rp.MTime, ShouldEqual, now.Unix())
  81. }))
  82. }))
  83. Convey("set reply attr", WithDao(func(d *Dao) {
  84. t, err := d.BeginTran(c)
  85. So(err, ShouldBeNil)
  86. now = time.Now()
  87. rows, err := d.TxUpReplyAttr(t, 86, r.ID, 3, now)
  88. So(err, ShouldBeNil)
  89. So(rows, ShouldBeGreaterThan, 0)
  90. err = t.Commit()
  91. So(err, ShouldBeNil)
  92. rp, err := d.Reply(c, 86, r.ID)
  93. So(err, ShouldBeNil)
  94. So(rp, ShouldNotBeNil)
  95. So(rp.Attr, ShouldEqual, 3)
  96. So(rp.MTime, ShouldEqual, now.Unix())
  97. }))
  98. Convey("set reply state", WithDao(func(d *Dao) {
  99. t, err := d.BeginTran(c)
  100. So(err, ShouldBeNil)
  101. now = time.Now()
  102. rows, err := d.TxUpdateReplyState(t, 86, r.ID, 2, now)
  103. So(err, ShouldBeNil)
  104. So(rows, ShouldBeGreaterThan, 0)
  105. err = t.Commit()
  106. So(err, ShouldBeNil)
  107. rp, err := d.Reply(c, 86, r.ID)
  108. So(err, ShouldBeNil)
  109. So(rp, ShouldNotBeNil)
  110. So(rp.State, ShouldEqual, 2)
  111. So(rp.MTime, ShouldEqual, now.Unix())
  112. }))
  113. Convey("get replies", WithDao(func(d *Dao) {
  114. rpMap, err := d.Replies(c, []int64{6, 86}, []int64{1, 2})
  115. So(err, ShouldBeNil)
  116. So(len(rpMap), ShouldEqual, 2)
  117. So(rpMap[1].ID, ShouldEqual, 1)
  118. So(rpMap[1].Oid, ShouldEqual, 6)
  119. So(rpMap[1].Mid, ShouldEqual, 2233)
  120. So(rpMap[2].ID, ShouldEqual, 2)
  121. So(rpMap[2].Oid, ShouldEqual, 86)
  122. So(rpMap[2].Mid, ShouldEqual, 2233)
  123. }))
  124. Convey("export replies test", WithDao(func(d *Dao) {
  125. var (
  126. ctime time.Time
  127. etime time.Time
  128. err error
  129. )
  130. ctime, err = time.Parse("2006-01-02", "2008-03-03")
  131. So(err, ShouldBeNil)
  132. etime, err = time.Parse("2006-01-02", "2018-03-03")
  133. So(err, ShouldBeNil)
  134. _, err = d.ExportReplies(c, 3400, 0, 3, "", ctime, etime)
  135. So(err, ShouldBeNil)
  136. }))
  137. }))
  138. }