creative_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package datadao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/bouk/monkey"
  6. "go-common/library/net/http/blademaster"
  7. "net/url"
  8. "reflect"
  9. "testing"
  10. "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestDatadaoHTTPDataHandle(t *testing.T) {
  13. var (
  14. c = context.Background()
  15. params url.Values
  16. key = "archives"
  17. )
  18. convey.Convey("HTTPDataHandle", t, func(ctx convey.C) {
  19. type result struct {
  20. Code int `json:"code"`
  21. Data json.RawMessage `json:"data"`
  22. Message string `json:"message"`
  23. }
  24. res := new(result)
  25. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.bmClient), "Get", func(_ *blademaster.Client, _ context.Context, _, _ string, _ url.Values, _ interface{}) error {
  26. res.Code = 0
  27. res.Data = json.RawMessage("play")
  28. res.Message = "success"
  29. return nil
  30. })
  31. defer guard.Unpatch()
  32. _, err := d.HTTPDataHandle(c, params, key)
  33. ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
  34. ctx.So(err, convey.ShouldBeNil)
  35. ctx.So(res, convey.ShouldNotBeNil)
  36. })
  37. })
  38. }
  39. func TestDatadaogetURI(t *testing.T) {
  40. convey.Convey("getURI", t, func(ctx convey.C) {
  41. var (
  42. key = "archives"
  43. )
  44. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  45. uri, err := d.getURI(key)
  46. ctx.Convey("Then err should be nil.uri should not be nil.", func(ctx convey.C) {
  47. ctx.So(err, convey.ShouldBeNil)
  48. ctx.So(uri, convey.ShouldNotBeNil)
  49. })
  50. })
  51. })
  52. }