service_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "go-common/app/job/main/credit-timer/conf"
  10. "go-common/library/log"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. s *Service
  15. )
  16. func init() {
  17. dir, _ := filepath.Abs("../cmd/convey-test.toml")
  18. flag.Set("conf", dir)
  19. conf.Init()
  20. s = New(conf.Conf)
  21. time.Sleep(time.Second)
  22. }
  23. func WithService(f func(s *Service)) func() {
  24. return func() {
  25. f(s)
  26. }
  27. }
  28. // TestService_loadConf
  29. func Test_loadConf(t *testing.T) {
  30. Convey("should return err be nil", t, func() {
  31. s.loadConf(context.TODO())
  32. So(s.c.Judge.CaseEndVoteTotal, ShouldBeGreaterThanOrEqualTo, 0)
  33. })
  34. }
  35. // TestService_ComputePoint
  36. func Test_ComputePoint(t *testing.T) {
  37. Convey("should return err be nil", t, func() {
  38. r, err := s.ComputePoint(context.TODO(), 88889017)
  39. So(err, ShouldBeNil)
  40. So(r, ShouldNotBeNil)
  41. // So(r, ShouldResemble,model.KpiPoint{})
  42. })
  43. }
  44. func TestService_sort(t *testing.T) {
  45. var (
  46. res = []int64{1, 3, 4, 5, 5, 6, 7, 9, 9, 10, 11, 12}
  47. ps []int64
  48. )
  49. for _, k := range res {
  50. if len(ps) == 0 {
  51. ps = append(ps, k)
  52. continue
  53. }
  54. if ps[len(ps)-1] == k {
  55. continue
  56. }
  57. ps = append(ps, k)
  58. }
  59. t.Logf("%v", ps)
  60. for _, k := range res {
  61. for i, r := range ps {
  62. if r == k {
  63. t.Logf("%d,%d,%d", k, i+1, (i+1)*100/len(ps))
  64. break
  65. }
  66. }
  67. }
  68. }
  69. func TestService_point(t *testing.T) {
  70. var (
  71. point float64
  72. voteTotal int64
  73. voteRight int64
  74. //投准率
  75. vr float64
  76. //投准率系数
  77. vf float64
  78. )
  79. voteTotal = 12
  80. voteRight = 5
  81. vr = float64(voteRight) / float64(voteTotal)
  82. vf = float64(1.0)
  83. point = float64(voteTotal) * vr * vf
  84. t.Logf("%f", point)
  85. }
  86. func initConf(t *testing.T) {
  87. if err := conf.Init(); err != nil {
  88. t.Errorf("conf.Init() error(%v)", err)
  89. t.FailNow()
  90. }
  91. log.Init(conf.Conf.Xlog)
  92. defer log.Close()
  93. }
  94. func Test_Time(t *testing.T) {
  95. Convey("should return err be nil", t, func() {
  96. d := time.Now().AddDate(0, 0, 1)
  97. ts1 := time.Until(time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 1, 0, time.Local))
  98. ts2 := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 1, 0, time.Local).Sub(time.Now())
  99. t.Errorf("%#v %#v", ts1, ts2)
  100. So(ts1, ShouldEqual, ts2)
  101. })
  102. }
  103. func Test_FixKPI(t *testing.T) {
  104. Convey("should return err be nil", t, func() {
  105. res, err := s.FixKPI(context.TODO(), 2018, 2, 10, 7584862)
  106. fmt.Printf("res:%+v \n", res)
  107. So(err, ShouldBeNil)
  108. })
  109. }