dao_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package live
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "strings"
  7. "testing"
  8. "go-common/app/interface/main/app-view/conf"
  9. "github.com/smartystreets/goconvey/convey"
  10. "gopkg.in/h2non/gock.v1"
  11. )
  12. var (
  13. d *Dao
  14. )
  15. func TestMain(m *testing.M) {
  16. if os.Getenv("DEPLOY_ENV") != "" {
  17. flag.Set("app_id", "main.app-svr.app-view")
  18. flag.Set("conf_token", "3a4CNLBhdFbRQPs7B4QftGvXHtJo92xw")
  19. flag.Set("tree_id", "4575")
  20. flag.Set("conf_version", "docker-1")
  21. flag.Set("deploy_env", "uat")
  22. flag.Set("conf_host", "config.bilibili.co")
  23. flag.Set("conf_path", "/tmp")
  24. flag.Set("region", "sh")
  25. flag.Set("zone", "sh001")
  26. } else {
  27. flag.Set("conf", "../../cmd/app-view-test.toml")
  28. }
  29. flag.Parse()
  30. if err := conf.Init(); err != nil {
  31. panic(err)
  32. }
  33. d = New(conf.Conf)
  34. os.Exit(m.Run())
  35. }
  36. func httpMock(method, url string) *gock.Request {
  37. r := gock.New(url)
  38. r.Method = strings.ToUpper(method)
  39. return r
  40. }
  41. func TestDaoLiving(t *testing.T) {
  42. convey.Convey("TestDaoLiving", t, func(ctx convey.C) {
  43. var (
  44. c = context.Background()
  45. )
  46. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  47. d.client.SetTransport(gock.DefaultTransport)
  48. defer gock.Off()
  49. httpMock("GET", d.list).Reply(200).JSON(`{"code":0,"data":[{"uid":"0","roomid":"1","title":"something","broadcast_type":1}]}`)
  50. ll, err := d.Living(c)
  51. ctx.Convey("Then ll should not be nil. err should be nil", func(ctx convey.C) {
  52. ctx.So(err, convey.ShouldBeNil)
  53. ctx.So(ll, convey.ShouldNotBeNil)
  54. })
  55. })
  56. })
  57. }
  58. func TestDaoBnj2019Conf(t *testing.T) {
  59. convey.Convey("TestDaoBnj2019Conf", t, func(ctx convey.C) {
  60. var (
  61. c = context.Background()
  62. )
  63. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  64. d.client.SetTransport(gock.DefaultTransport)
  65. defer gock.Off()
  66. httpMock("GET", d.bnj2019).Reply(200).JSON(`{"code":0,"data":{"grey_status":1,"grey_uids":"0,1"}}`)
  67. _, mids, err := d.Bnj2019Conf(c)
  68. ctx.Convey("Then mids should not be nil. err should be nil", func(ctx convey.C) {
  69. ctx.So(err, convey.ShouldBeNil)
  70. ctx.So(mids, convey.ShouldNotBeNil)
  71. })
  72. })
  73. })
  74. }