client_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package api
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/library/ecode"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. var client ArchiveClient
  9. func init() {
  10. var err error
  11. client, err = NewClient(nil)
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. func TestTypes(t *testing.T) {
  17. convey.Convey("Types", t, func(ctx convey.C) {
  18. var c = context.Background()
  19. ctx.Convey("When everything is correct", func(ctx convey.C) {
  20. reply, err := client.Types(c, &NoArgRequest{})
  21. ctx.So(err, convey.ShouldBeNil)
  22. for k, v := range reply.Types {
  23. ctx.Printf("key:%d id:%d name:%s pid:%d\n", k, v.ID, v.Name, v.Pid)
  24. }
  25. })
  26. })
  27. }
  28. func TestArc(t *testing.T) {
  29. convey.Convey("TestArc", t, func(ctx convey.C) {
  30. var c = context.Background()
  31. ctx.Convey("When everything is correct", func(ctx convey.C) {
  32. reply, err := client.Arc(c, &ArcRequest{Aid: 10100696})
  33. ctx.So(err, convey.ShouldBeNil)
  34. ctx.Printf("%+v\n", reply.Arc)
  35. })
  36. ctx.Convey("When error", func(ctx convey.C) {
  37. reply, err := client.Arc(context.TODO(), &ArcRequest{Aid: 99999999999})
  38. ctx.So(err, convey.ShouldEqual, ecode.NothingFound)
  39. ctx.So(reply, convey.ShouldBeNil)
  40. })
  41. })
  42. }
  43. func TestArcs(t *testing.T) {
  44. convey.Convey("TestArcs", t, func(ctx convey.C) {
  45. var c = context.Background()
  46. ctx.Convey("When everything is correct", func(ctx convey.C) {
  47. reply, err := client.Arcs(c, &ArcsRequest{Aids: []int64{10100696}})
  48. ctx.So(err, convey.ShouldBeNil)
  49. ctx.Printf("%+v\n", reply.Arcs)
  50. })
  51. ctx.Convey("When empty", func(ctx convey.C) {
  52. reply, err := client.Arcs(c, &ArcsRequest{Aids: []int64{99999999999}})
  53. // 批量接口 err=nil arcs=nil
  54. ctx.So(err, convey.ShouldBeNil)
  55. ctx.So(reply.Arcs, convey.ShouldBeNil)
  56. })
  57. })
  58. }
  59. func TestView(t *testing.T) {
  60. convey.Convey("TestView", t, func(ctx convey.C) {
  61. var c = context.Background()
  62. ctx.Convey("When everything is correct", func(ctx convey.C) {
  63. reply, err := client.View(c, &ViewRequest{Aid: 10100696})
  64. ctx.So(err, convey.ShouldBeNil)
  65. ctx.Printf("arc:%+v\n", reply.Arc)
  66. ctx.Printf("pages:%+v\n", reply.Pages)
  67. })
  68. ctx.Convey("When empty", func(ctx convey.C) {
  69. reply, err := client.View(c, &ViewRequest{Aid: 99999999999})
  70. ctx.So(err, convey.ShouldNotBeNil)
  71. ctx.So(reply, convey.ShouldBeNil)
  72. })
  73. })
  74. }
  75. func TestViews(t *testing.T) {
  76. convey.Convey("TestViews", t, func(ctx convey.C) {
  77. var c = context.Background()
  78. ctx.Convey("When everything is correct", func(ctx convey.C) {
  79. reply, err := client.Views(c, &ViewsRequest{Aids: []int64{10100696}})
  80. ctx.So(err, convey.ShouldBeNil)
  81. ctx.Printf("%+v\n", reply.Views)
  82. })
  83. ctx.Convey("When empty", func(ctx convey.C) {
  84. arcs, err := client.Views(c, &ViewsRequest{Aids: []int64{99999999999}})
  85. ctx.So(err, convey.ShouldBeNil)
  86. ctx.So(arcs.Views, convey.ShouldBeNil)
  87. })
  88. })
  89. }