dao_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package staff
  2. import (
  3. "flag"
  4. "go-common/app/admin/main/videoup/conf"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "testing"
  7. "context"
  8. "os"
  9. "strings"
  10. . "github.com/smartystreets/goconvey/convey"
  11. "gopkg.in/h2non/gock.v1"
  12. )
  13. func WithDao(f func(d *Dao)) func() {
  14. return func() {
  15. Reset(func() {})
  16. f(d)
  17. }
  18. }
  19. func httpMock(method, url string) *gock.Request {
  20. r := gock.New(url)
  21. r.Method = strings.ToUpper(method)
  22. d.httpClient.SetTransport(gock.DefaultTransport)
  23. return r
  24. }
  25. func TestMain(m *testing.M) {
  26. if os.Getenv("DEPLOY_ENV") != "" {
  27. flag.Set("app_id", "main.archive.videoup-admin")
  28. flag.Set("conf_token", "gRSfeavV7kJdY9875Gf29pbd2wrdKZ1a")
  29. flag.Set("tree_id", "2307")
  30. flag.Set("conf_version", "docker-1")
  31. flag.Set("deploy_env", "uat")
  32. flag.Set("conf_host", "config.bilibili.co")
  33. flag.Set("conf_path", "/tmp")
  34. flag.Set("region", "sh")
  35. flag.Set("zone", "sh001")
  36. } else {
  37. flag.Set("conf", "../../cmd/videoup-admin.toml")
  38. }
  39. flag.Parse()
  40. if err := conf.Init(); err != nil {
  41. panic(err)
  42. }
  43. d = New(conf.Conf)
  44. os.Exit(m.Run())
  45. }
  46. func TestStaffs(t *testing.T) {
  47. Convey("Staffs", t, WithDao(func(d *Dao) {
  48. httpMock("GET", d.staffURI).Reply(200).JSON(`{"code":0,"message":"0","ttl":1,"data":null}`)
  49. _, err := d.Staffs(context.TODO(), 1)
  50. So(err, ShouldBeNil)
  51. }))
  52. Convey("StaffApplyBatchSubmit", t, WithDao(func(d *Dao) {
  53. var ap *archive.StaffBatchParam
  54. httpMock("Do", d.staffURI).Reply(200).JSON(`{"code":0,"message":"0","ttl":1,"data":null}`)
  55. err := d.StaffApplyBatchSubmit(context.TODO(), ap)
  56. So(err, ShouldNotBeNil)
  57. }))
  58. }