subject_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "go-common/app/admin/main/reply/model"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func deleteSubject(d *Dao, oid int64, typ int32) error {
  11. _delSQL := "Delete from reply_subject_%d where oid=? and type=?"
  12. _, err := d.db.Exec(context.Background(), fmt.Sprintf(_delSQL, subHit(oid)), oid, typ)
  13. if err != nil {
  14. return err
  15. }
  16. return nil
  17. }
  18. func TestSubjectAttr(t *testing.T) {
  19. var (
  20. sub = &model.Subject{
  21. Oid: 1,
  22. Type: 1,
  23. }
  24. now = time.Now()
  25. c = context.Background()
  26. )
  27. Convey("get and update a subject", t, WithDao(func(d *Dao) {
  28. _, err := d.AddSubject(c, sub.Mid, sub.Oid, sub.Type, sub.State, now)
  29. So(err, ShouldBeNil)
  30. sub.AttrSet(model.AttrYes, model.SubAttrMonitor)
  31. _, err = d.UpSubjectAttr(c, sub.Oid, sub.Type, sub.Attr, now)
  32. So(err, ShouldBeNil)
  33. sub, err = d.Subject(c, sub.Oid, sub.Type)
  34. So(err, ShouldBeNil)
  35. So(sub.AttrVal(model.SubAttrMonitor), ShouldEqual, model.AttrYes)
  36. }))
  37. Convey("get and update a subject", t, WithDao(func(d *Dao) {
  38. _, err := d.AddSubject(c, sub.Mid, sub.Oid, sub.Type, sub.State, now)
  39. So(err, ShouldBeNil)
  40. sub.AttrSet(model.AttrNo, model.SubAttrMonitor)
  41. _, err = d.UpSubjectAttr(c, sub.Oid, sub.Type, sub.Attr, now)
  42. So(err, ShouldBeNil)
  43. sub, err = d.Subject(c, sub.Oid, sub.Type)
  44. So(err, ShouldBeNil)
  45. So(sub.AttrVal(model.SubAttrMonitor), ShouldEqual, model.AttrNo)
  46. }))
  47. }
  48. func TestSubjectState(t *testing.T) {
  49. var (
  50. sub = &model.Subject{
  51. Oid: 1,
  52. Type: 1,
  53. }
  54. now = time.Now()
  55. c = context.Background()
  56. )
  57. Convey("get and update a subject", t, WithDao(func(d *Dao) {
  58. _, err := d.AddSubject(c, sub.Mid, sub.Oid, sub.Type, sub.State, now)
  59. So(err, ShouldBeNil)
  60. _, err = d.UpSubjectState(c, sub.Oid, sub.Type, model.SubStateForbid, now)
  61. So(err, ShouldBeNil)
  62. sub, err = d.Subject(c, sub.Oid, sub.Type)
  63. So(sub.State, ShouldEqual, model.SubStateForbid)
  64. }))
  65. }
  66. func TestSubjectCount(t *testing.T) {
  67. d := _d
  68. var (
  69. sub = &model.Subject{
  70. Oid: 2,
  71. Type: 3,
  72. }
  73. now = time.Now()
  74. c = context.Background()
  75. )
  76. _, err := d.AddSubject(c, sub.Mid, sub.Oid, sub.Type, sub.State, now)
  77. if err != nil {
  78. t.Logf("add subject failed!%v", err)
  79. t.FailNow()
  80. }
  81. defer deleteSubject(d, 2, 3)
  82. Convey("increase and decrease subject", t, WithDao(func(d *Dao) {
  83. Convey("increase subject rcount", WithDao(func(d *Dao) {
  84. t, err := d.BeginTran(c)
  85. So(err, ShouldBeNil)
  86. rows, err := d.TxIncrSubRCount(t, 2, 3, now)
  87. So(err, ShouldBeNil)
  88. So(rows, ShouldBeGreaterThan, 0)
  89. err = t.Commit()
  90. So(err, ShouldBeNil)
  91. sub, err = d.Subject(c, sub.Oid, sub.Type)
  92. So(err, ShouldBeNil)
  93. So(sub.RCount, ShouldEqual, 1)
  94. }))
  95. Convey("decrease subject rcount", WithDao(func(d *Dao) {
  96. t, err := d.BeginTran(c)
  97. So(err, ShouldBeNil)
  98. rows, err := d.TxDecrSubRCount(t, 2, 3, now)
  99. So(err, ShouldBeNil)
  100. So(rows, ShouldBeGreaterThan, 0)
  101. err = t.Commit()
  102. So(err, ShouldBeNil)
  103. sub, err = d.Subject(c, sub.Oid, sub.Type)
  104. So(err, ShouldBeNil)
  105. So(sub.RCount, ShouldEqual, 0)
  106. }))
  107. Convey("increase subject acount", WithDao(func(d *Dao) {
  108. t, err := d.BeginTran(c)
  109. So(err, ShouldBeNil)
  110. rows, err := d.TxIncrSubACount(t, 2, 3, 123, now)
  111. So(err, ShouldBeNil)
  112. So(rows, ShouldBeGreaterThan, 0)
  113. err = t.Commit()
  114. So(err, ShouldBeNil)
  115. sub, err = d.Subject(c, sub.Oid, sub.Type)
  116. So(err, ShouldBeNil)
  117. So(sub.ACount, ShouldEqual, 123)
  118. }))
  119. Convey("decrease subject acount", WithDao(func(d *Dao) {
  120. t, err := d.BeginTran(c)
  121. So(err, ShouldBeNil)
  122. rows, err := d.TxSubDecrACount(t, 2, 3, 23, now)
  123. So(err, ShouldBeNil)
  124. So(rows, ShouldBeGreaterThan, 0)
  125. err = t.Commit()
  126. So(err, ShouldBeNil)
  127. sub, err = d.Subject(c, sub.Oid, sub.Type)
  128. So(err, ShouldBeNil)
  129. So(sub.ACount, ShouldEqual, 100)
  130. }))
  131. }))
  132. }