hbase_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 gose 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.ShouldEqual, p1)
  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 gose 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.ShouldEqual, p1)
  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.PlayInc, convey.ShouldEqual, val1)
  62. convey.So(stat.Rank0, convey.ShouldEqual, val1)
  63. convey.So(stat.FansInc, convey.ShouldEqual, val2)
  64. })
  65. }
  66. func getMockCells(v1, v2 int32) []*hrpc.Cell {
  67. s1 := make([]byte, 0)
  68. buf1 := bytes.NewBuffer(s1)
  69. s2 := make([]byte, 0)
  70. buf2 := bytes.NewBuffer(s2)
  71. binary.Write(buf1, binary.BigEndian, v1)
  72. binary.Write(buf2, binary.BigEndian, v2)
  73. cells := []*hrpc.Cell{
  74. {
  75. Qualifier: []byte("play"),
  76. Value: buf1.Bytes(),
  77. Family: []byte("f"),
  78. },
  79. {
  80. Qualifier: []byte("play_last_w"),
  81. Value: buf1.Bytes(),
  82. Family: []byte("f"),
  83. },
  84. {
  85. Qualifier: []byte("play_inc"),
  86. Value: buf1.Bytes(),
  87. Family: []byte("f"),
  88. },
  89. {
  90. Qualifier: []byte("fans_inc"),
  91. Value: buf2.Bytes(),
  92. Family: []byte("f"),
  93. },
  94. {
  95. Qualifier: []byte("rank168"),
  96. Value: buf1.Bytes(),
  97. Family: []byte("r"),
  98. },
  99. {
  100. Qualifier: []byte("rank0"),
  101. Value: buf1.Bytes(),
  102. Family: []byte("r"),
  103. },
  104. {
  105. Qualifier: []byte("rank1"),
  106. Value: buf1.Bytes(),
  107. Family: []byte("r"),
  108. },
  109. {
  110. Qualifier: []byte("rank3"),
  111. Value: buf1.Bytes(),
  112. Family: []byte("r"),
  113. },
  114. {
  115. Qualifier: []byte("rank4"),
  116. Value: buf1.Bytes(),
  117. Family: []byte("r"),
  118. },
  119. }
  120. return cells
  121. }