memcached_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/interface/openplatform/article/model"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func Test_ArticlesCache(t *testing.T) {
  9. c := context.TODO()
  10. Convey("add cache", t, WithDao(func(d *Dao) {
  11. err := d.AddArticlesMetaCache(c, art.Meta)
  12. So(err, ShouldBeNil)
  13. err = d.AddArticleContentCache(c, art.ID, art.Content)
  14. So(err, ShouldBeNil)
  15. err = d.AddArticleStatsCache(c, art.ID, art.Stats)
  16. So(err, ShouldBeNil)
  17. Convey("get meta cache", func() {
  18. _, err = d.ArticleMetaCache(c, art.ID)
  19. So(err, ShouldBeNil)
  20. cached, missed, err1 := d.ArticlesMetaCache(c, []int64{art.ID})
  21. So(err1, ShouldBeNil)
  22. So(missed, ShouldBeEmpty)
  23. So(cached, ShouldResemble, map[int64]*model.Meta{art.ID: art.Meta})
  24. })
  25. Convey("get content cache", func() {
  26. content, err1 := d.ArticleContentCache(c, art.ID)
  27. So(err1, ShouldBeNil)
  28. So(content, ShouldEqual, art.Content)
  29. })
  30. Convey("get no filter content cache", func() {
  31. err = d.AddArticleContentCache(c, art.ID, art.Content)
  32. So(err, ShouldBeNil)
  33. content, err := d.ArticleContentCache(c, art.ID)
  34. So(err, ShouldBeNil)
  35. So(content, ShouldEqual, art.Content)
  36. })
  37. Convey("get stats cache", func() {
  38. cached, missed, err := d.ArticlesStatsCache(c, []int64{art.ID})
  39. So(err, ShouldBeNil)
  40. So(missed, ShouldBeEmpty)
  41. So(cached, ShouldResemble, map[int64]*model.Stats{art.ID: art.Stats})
  42. })
  43. Convey("get stat cache", func() {
  44. res, err := d.ArticleStatsCache(c, art.ID)
  45. So(err, ShouldBeNil)
  46. So(res, ShouldResemble, art.Stats)
  47. })
  48. }))
  49. }
  50. func Test_AudioCache(t *testing.T) {
  51. c := context.TODO()
  52. card := model.AudioCard{ID: 1, Title: "audio"}
  53. Convey("add cache", t, WithDao(func(d *Dao) {
  54. err := d.AddAudioCardsCache(c, map[int64]*model.AudioCard{1: &card})
  55. So(err, ShouldBeNil)
  56. x, err := d.AudioCardsCache(c, []int64{card.ID})
  57. So(err, ShouldBeNil)
  58. So(x[card.ID], ShouldResemble, &card)
  59. }))
  60. }
  61. func Test_Hotspots(t *testing.T) {
  62. c := context.TODO()
  63. hots := []*model.Hotspot{&model.Hotspot{ID: 1, Tag: "tag"}}
  64. Convey("add cache", t, func() {
  65. err := d.AddCacheHotspots(c, hots)
  66. So(err, ShouldBeNil)
  67. res, err := d.CacheHotspots(c)
  68. So(err, ShouldBeNil)
  69. So(res, ShouldResemble, []*model.Hotspot{&model.Hotspot{ID: 1, Tag: "tag", TopArticles: []int64{}}})
  70. err = d.DelCacheHotspots(c)
  71. So(err, ShouldBeNil)
  72. // delete twice
  73. err = d.DelCacheHotspots(c)
  74. So(err, ShouldBeNil)
  75. res, err = d.CacheHotspots(c)
  76. So(err, ShouldBeNil)
  77. So(res, ShouldBeNil)
  78. })
  79. }