hbase_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package weeklyhonor
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "reflect"
  7. "testing"
  8. "go-common/library/database/hbase.v2"
  9. "github.com/bouk/monkey"
  10. "github.com/smartystreets/goconvey/convey"
  11. "github.com/tsuna/gohbase/hrpc"
  12. )
  13. func TestWeeklyhonorreverseString(t *testing.T) {
  14. convey.Convey("reverseString", t, func(ctx convey.C) {
  15. var (
  16. s = ""
  17. )
  18. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  19. p1 := reverseString(s)
  20. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  21. ctx.So(p1, convey.ShouldNotBeNil)
  22. })
  23. })
  24. })
  25. }
  26. func TestWeeklyhonorhonorRowKey(t *testing.T) {
  27. convey.Convey("honorRowKey", t, func(ctx convey.C) {
  28. var (
  29. id = int64(0)
  30. date = ""
  31. )
  32. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  33. p1 := honorRowKey(id, date)
  34. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  35. ctx.So(p1, convey.ShouldNotBeNil)
  36. })
  37. })
  38. })
  39. }
  40. func TestWeeklyhonorHonorStat(t *testing.T) {
  41. var (
  42. c = context.Background()
  43. mid = int64(0)
  44. date = "20181112"
  45. val1 int32 = 500
  46. val2 int32 = -2
  47. cs = getMockCells(val1, val2)
  48. )
  49. guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.hbase), "GetStr", func(_ *hbase.Client, _ context.Context, _, _ string, _ ...func(hrpc.Call) error) (*hrpc.Result, error) {
  50. res := &hrpc.Result{
  51. Cells: cs,
  52. }
  53. return res, nil
  54. })
  55. defer guard.Unpatch()
  56. convey.Convey("honorStat", t, func() {
  57. stat, err := d.HonorStat(c, mid, date)
  58. convey.So(err, convey.ShouldBeNil)
  59. convey.So(stat, convey.ShouldNotBeNil)
  60. convey.So(stat.Play, convey.ShouldEqual, val1)
  61. convey.So(stat.FansInc, convey.ShouldEqual, val2)
  62. convey.So(stat.Rank0, convey.ShouldEqual, val1)
  63. })
  64. }
  65. func getMockCells(v1, v2 int32) []*hrpc.Cell {
  66. s1 := make([]byte, 0)
  67. buf1 := bytes.NewBuffer(s1)
  68. s2 := make([]byte, 0)
  69. buf2 := bytes.NewBuffer(s2)
  70. binary.Write(buf1, binary.BigEndian, v1)
  71. binary.Write(buf2, binary.BigEndian, v2)
  72. cells := []*hrpc.Cell{
  73. {
  74. Qualifier: []byte("play"),
  75. Value: buf1.Bytes(),
  76. Family: []byte("f"),
  77. },
  78. {
  79. Qualifier: []byte("play_last_w"),
  80. Value: buf1.Bytes(),
  81. Family: []byte("f"),
  82. },
  83. {
  84. Qualifier: []byte("fans_inc"),
  85. Value: buf2.Bytes(),
  86. Family: []byte("f"),
  87. },
  88. {
  89. Qualifier: []byte("rank0"),
  90. Value: buf1.Bytes(),
  91. Family: []byte("r"),
  92. },
  93. }
  94. return cells
  95. }