dao_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "path/filepath"
  6. "strings"
  7. "go-common/app/interface/openplatform/article/conf"
  8. artmdl "go-common/app/interface/openplatform/article/model"
  9. "go-common/library/cache/redis"
  10. _ "github.com/go-sql-driver/mysql"
  11. . "github.com/smartystreets/goconvey/convey"
  12. "gopkg.in/h2non/gock.v1"
  13. )
  14. var (
  15. dataMID = int64(1)
  16. noDataMID = int64(10000)
  17. _noData = int64(1000000)
  18. d *Dao
  19. categories = []*artmdl.Category{
  20. &artmdl.Category{Name: "游戏", ID: 1},
  21. &artmdl.Category{Name: "动漫", ID: 2},
  22. }
  23. art = artmdl.Article{
  24. Meta: &artmdl.Meta{
  25. ID: 100,
  26. Category: categories[0],
  27. Title: "隐藏于时区记忆中的,是希望还是绝望!",
  28. Summary: "说起日本校服,第一个浮现在我们脑海中的必然是那象征着青春阳光 蓝白色相称的水手服啦. 拉色短裙配上洁白的直袜",
  29. BannerURL: "http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg",
  30. TemplateID: 1,
  31. State: 0,
  32. Author: &artmdl.Author{Mid: 123, Name: "爱蜜莉雅", Face: "http://i1.hdslb.com/bfs/face/5c6109964e78a84021299cdf71739e21cd7bc208.jpg"},
  33. Reprint: 0,
  34. ImageURLs: []string{"http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg", "http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg", "http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg"},
  35. OriginImageURLs: []string{"http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg", "http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg", "http://i2.hdslb.com/bfs/archive/b5727f244d5c7a34c1c0e78f49765d09ff30c129.jpg"},
  36. PublishTime: 1495784507,
  37. Tags: []*artmdl.Tag{},
  38. Stats: &artmdl.Stats{Favorite: 100, Like: 10, View: 500, Dislike: 1, Share: 99},
  39. },
  40. Content: "content",
  41. }
  42. )
  43. func CleanCache() {
  44. c := context.TODO()
  45. pool := redis.NewPool(conf.Conf.Redis)
  46. pool.Get(c).Do("FLUSHDB")
  47. }
  48. func init() {
  49. dir, _ := filepath.Abs("../cmd/convey-test.toml")
  50. flag.Set("conf", dir)
  51. conf.Init()
  52. d = New(conf.Conf)
  53. d.httpClient.SetTransport(gock.DefaultTransport)
  54. }
  55. func WithDao(f func(d *Dao)) func() {
  56. return func() {
  57. Reset(func() { CleanCache() })
  58. f(d)
  59. }
  60. }
  61. func WithMysql(f func(d *Dao)) func() {
  62. return func() {
  63. Reset(func() { CleanCache() })
  64. f(d)
  65. }
  66. }
  67. func WithCleanCache(f func()) func() {
  68. return func() {
  69. Reset(func() { CleanCache() })
  70. f()
  71. }
  72. }
  73. func httpMock(method, url string) *gock.Request {
  74. r := gock.New(url)
  75. r.Method = strings.ToUpper(method)
  76. return r
  77. }
  78. func ctx() context.Context {
  79. return context.Background()
  80. }