reply_content_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 deleteReplyContent(d *Dao, oid int64, rpid int64) error {
  12. _delSQL := "Delete from reply_content_%d where rpid = ?"
  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 TestReplyContent(t *testing.T) {
  20. _inContSQL := "INSERT IGNORE INTO reply_content_%d (rpid,message,ats,ip,plat,device,version,ctime,mtime) VALUES(?,?,?,?,?,?,?,?,?)"
  21. d := _d
  22. c := context.Background()
  23. now := time.Now()
  24. rc := model.ReplyContent{
  25. ID: 1,
  26. Message: "test",
  27. Device: "iphone",
  28. Version: "beta",
  29. CTime: xtime.Time(now.Unix()),
  30. MTime: xtime.Time(now.Unix()),
  31. }
  32. _, err := d.db.Exec(c, fmt.Sprintf(_inContSQL, hit(6)), rc.ID, rc.Message, rc.Ats, rc.IP, rc.Plat, rc.Device, rc.Version, rc.CTime, rc.MTime)
  33. if err != nil {
  34. t.Logf("insert reply content %v", err)
  35. t.FailNow()
  36. }
  37. defer deleteReplyContent(d, 6, 1)
  38. rc2 := model.ReplyContent{
  39. ID: 2,
  40. Message: "test2",
  41. Device: "iphone2",
  42. Version: "beta2",
  43. CTime: xtime.Time(now.Unix()),
  44. MTime: xtime.Time(now.Unix()),
  45. }
  46. _, err = d.db.Exec(c, fmt.Sprintf(_inContSQL, hit(86)), rc2.ID, rc2.Message, rc2.Ats, rc2.IP, rc2.Plat, rc2.Device, rc2.Version, rc2.CTime, rc2.MTime)
  47. if err != nil {
  48. t.Logf("insert reply content %v", err)
  49. t.FailNow()
  50. }
  51. defer deleteReplyContent(d, 86, 2)
  52. Convey("reply_content test", t, WithDao(func(d *Dao) {
  53. Convey("get update content", WithDao(func(d *Dao) {
  54. now = time.Now()
  55. rows, err := d.UpReplyContent(c, 86, 2, "test3", now)
  56. So(err, ShouldBeNil)
  57. So(rows, ShouldBeGreaterThan, 0)
  58. }))
  59. Convey("get reply_content", WithDao(func(d *Dao) {
  60. content, err := d.ReplyContent(c, 86, 2)
  61. So(err, ShouldBeNil)
  62. So(content, ShouldNotBeNil)
  63. So(content.ID, ShouldEqual, 2)
  64. So(content.CTime, ShouldEqual, now.Unix())
  65. So(content.Message, ShouldEqual, "test3")
  66. }))
  67. Convey("find reply_contents", WithDao(func(d *Dao) {
  68. cMap, err := d.ReplyContents(c, []int64{6, 86}, []int64{1, 2})
  69. So(err, ShouldBeNil)
  70. So(len(cMap), ShouldEqual, 2)
  71. So(cMap[1].ID, ShouldEqual, 1)
  72. So(cMap[1].Message, ShouldEqual, "test")
  73. So(cMap[2].ID, ShouldEqual, 2)
  74. So(cMap[2].Message, ShouldEqual, "test3")
  75. }))
  76. }))
  77. }