notice_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/admin/main/reply/model"
  7. xtime "go-common/library/time"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. var (
  11. mid int64 = 1
  12. nowTs = time.Now().Unix()
  13. lastID int64
  14. lastID2 int64
  15. )
  16. func Test_AddNotice(t *testing.T) {
  17. c := context.Background()
  18. nt := model.Notice{
  19. Plat: model.PlatAndroid,
  20. Condition: model.ConditionGT,
  21. Build: 1113,
  22. Title: "测试",
  23. Status: model.StatusOffline,
  24. Content: "测试内容",
  25. Link: "http://www.bilibili.com",
  26. StartTime: xtime.Time(nowTs),
  27. EndTime: xtime.Time(nowTs + 10*3600),
  28. ClientType: "android",
  29. }
  30. nt2 := model.Notice{
  31. Plat: model.PlatAndroid,
  32. Condition: model.ConditionLT,
  33. Build: 2233,
  34. Title: "测试2",
  35. Status: model.StatusOffline,
  36. Content: "测试内容2",
  37. Link: "http://www.bilibili.com",
  38. StartTime: xtime.Time(nowTs),
  39. EndTime: xtime.Time(nowTs + 10*3600),
  40. ClientType: "",
  41. }
  42. Convey("add notice", t, WithDao(func(d *Dao) {
  43. var err error
  44. lastID, err = d.CreateNotice(c, &nt)
  45. So(err, ShouldBeNil)
  46. So(lastID, ShouldBeGreaterThan, 0)
  47. lastID2, err = d.CreateNotice(c, &nt2)
  48. So(err, ShouldBeNil)
  49. So(lastID2, ShouldBeGreaterThan, 0)
  50. }))
  51. }
  52. func Test_ListNotice(t *testing.T) {
  53. c := context.Background()
  54. Convey("list notice", t, WithDao(func(d *Dao) {
  55. nts, err := d.ListNotice(c, 1, 100)
  56. So(err, ShouldBeNil)
  57. So(len(nts), ShouldBeGreaterThan, 0)
  58. So(nts[0].StartTime.Time().Unix(), ShouldBeGreaterThanOrEqualTo, nowTs)
  59. count, err := d.CountNotice(c)
  60. So(err, ShouldBeNil)
  61. So(count, ShouldBeGreaterThan, 1)
  62. }))
  63. }
  64. func Test_UpdateNotice(t *testing.T) {
  65. c := context.Background()
  66. Convey("update notice", t, WithDao(func(d *Dao) {
  67. data, err := d.Notice(c, uint32(lastID2))
  68. So(err, ShouldBeNil)
  69. So(data.Title, ShouldEqual, "测试2")
  70. data.ID = uint32(lastID2)
  71. data.Title = "测试3"
  72. rows, err := d.UpdateNotice(c, data)
  73. So(err, ShouldBeNil)
  74. So(rows, ShouldBeGreaterThan, 0)
  75. nt2, err := d.Notice(c, uint32(lastID2))
  76. So(err, ShouldBeNil)
  77. So(nt2.Title, ShouldEqual, "测试3")
  78. rows, err = d.UpdateNoticeStatus(c, model.StatusOnline, uint32(lastID))
  79. So(err, ShouldBeNil)
  80. So(rows, ShouldBeGreaterThan, 0)
  81. nts, err := d.RangeNotice(c, model.PlatAndroid, xtime.Time(nowTs)-3600, xtime.Time(nowTs+5*3600))
  82. So(err, ShouldBeNil)
  83. So(len(nts), ShouldBeGreaterThan, 0)
  84. var isFound bool
  85. for _, data = range nts {
  86. if data.ID == uint32(lastID) {
  87. isFound = true
  88. }
  89. }
  90. So(isFound, ShouldBeTrue)
  91. }))
  92. }
  93. func Test_DeleteNotice(t *testing.T) {
  94. c := context.Background()
  95. Convey("delete notice", t, WithDao(func(d *Dao) {
  96. rows, err := d.DeleteNotice(c, uint32(lastID))
  97. So(err, ShouldBeNil)
  98. So(rows, ShouldBeGreaterThan, 0)
  99. rows, err = d.DeleteNotice(c, uint32(lastID2))
  100. So(err, ShouldBeNil)
  101. So(rows, ShouldBeGreaterThan, 0)
  102. }))
  103. }