service_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "time"
  11. "go-common/app/admin/main/push/conf"
  12. "go-common/app/admin/main/push/model"
  13. pushmdl "go-common/app/service/main/push/model"
  14. . "github.com/smartystreets/goconvey/convey"
  15. )
  16. var svr *Service
  17. func init() {
  18. dir, _ := filepath.Abs("../cmd/push-admin-test.toml")
  19. flag.Set("conf", dir)
  20. conf.Init()
  21. svr = New(conf.Conf)
  22. time.Sleep(time.Second)
  23. }
  24. func WithService(f func(s *Service)) func() {
  25. return func() {
  26. Reset(func() {})
  27. f(svr)
  28. }
  29. }
  30. func Test_Service(t *testing.T) {
  31. Convey("service test", t, WithService(func(s *Service) {
  32. s.Wait()
  33. s.Close()
  34. }))
  35. }
  36. func Test_AddTaskAll(t *testing.T) {
  37. Convey("get last report id", t, WithService(func(s *Service) {
  38. var maxID int64
  39. err := s.dao.DB.Raw("SELECT MAX(id) FROM push_reports").Row().Scan(&maxID)
  40. So(err, ShouldBeNil)
  41. t.Logf("maxid(%d)", maxID)
  42. }))
  43. Convey("get tokens", t, WithService(func(s *Service) {
  44. sqlStr := fmt.Sprintf("SELECT platform_id,device_token FROM push_reports WHERE id>%d and id<=%d and app_id=%d and dtime=0 and notify_switch=1", 1, 100, 1)
  45. rows, err := s.dao.DB.Raw(sqlStr).Rows()
  46. So(err, ShouldBeNil)
  47. var tokens []string
  48. for rows.Next() {
  49. var (
  50. platformID int
  51. token string
  52. )
  53. err = rows.Scan(&platformID, &token)
  54. So(err, ShouldBeNil)
  55. tokens = append(tokens, fmt.Sprintf("%d\t%s", platformID, token))
  56. }
  57. t.Logf("tokens(%d)", len(tokens))
  58. if len(tokens) > 0 {
  59. t.Logf("token one (%s)", tokens[0])
  60. }
  61. }))
  62. }
  63. func Test_CheckUploadMid(t *testing.T) {
  64. Convey("CheckUploadMid", t, WithService(func(s *Service) {
  65. data := []byte("1\n2\n3")
  66. err := s.CheckUploadMid(context.TODO(), data)
  67. So(err, ShouldBeNil)
  68. data = []byte("1\nabc\n3")
  69. err = s.CheckUploadMid(context.TODO(), data)
  70. So(err, ShouldNotBeNil)
  71. t.Logf("check mid error(%v)", err)
  72. }))
  73. }
  74. func Test_CheckUploadToken(t *testing.T) {
  75. Convey("CheckUploadToken", t, WithService(func(s *Service) {
  76. data := []byte("2 fdsahjfkdshaj\n3 hjkhjhjkhj")
  77. err := s.CheckUploadToken(context.TODO(), data)
  78. So(err, ShouldBeNil)
  79. data = []byte("2 fdsahjfkdshaj\n3 hjkhjhjkhj\n4\n")
  80. err = s.CheckUploadToken(context.TODO(), data)
  81. So(err, ShouldNotBeNil)
  82. t.Logf("check token error(%v)", err)
  83. }))
  84. }
  85. func Test_parseQuery(t *testing.T) {
  86. Convey("parse query", t, WithService(func(s *Service) {
  87. str := `{"age":1,"sex":1,"is_up":0,"is_formal_member":1,"user_active_day":0,"user_new_day":0,"user_silentDay":0,"area":"2","level":"2,3,4","platforms":"1,2,3","like":"1,2,3","channel":"huawei,xiaomi","vip_expire":[{"begin":"2018-07-01 00:00:00","end":"2018-07-27 00:00:00"}],"self_attention":null,"self_attention_type":0,"active":null,"ActivePeriod":0}`
  88. p := new(model.DPParams)
  89. err := json.Unmarshal([]byte(str), p)
  90. So(err, ShouldBeNil)
  91. p.Area = pushmdl.SplitInts(p.AreaStr)
  92. p.Level = pushmdl.SplitInts(p.LevelStr)
  93. p.Platforms = pushmdl.SplitInts(p.PlatformStr)
  94. p.Like = pushmdl.SplitInts(p.LikeStr)
  95. p.Channel = strings.Split(p.ChannelStr, ",")
  96. if p.VipExpireStr != "" {
  97. err = json.Unmarshal([]byte(p.VipExpireStr), &p.VipExpires)
  98. So(err, ShouldBeNil)
  99. }
  100. if p.AttentionStr != "" {
  101. err = json.Unmarshal([]byte(p.AttentionStr), &p.Attentions)
  102. So(err, ShouldBeNil)
  103. }
  104. if p.ActivePeriodStr != "" {
  105. err = json.Unmarshal([]byte(p.ActivePeriodStr), &p.ActivePeriods)
  106. So(err, ShouldBeNil)
  107. }
  108. sql := s.parseQuery(pushmdl.TaskTypeDataPlatformMid, p)
  109. t.Logf("sql(%s)", sql)
  110. }))
  111. }
  112. func TestCheckDpData(t *testing.T) {
  113. Convey("test check data platform data", t, WithService(func(s *Service) {
  114. err := s.CheckDpData(context.Background())
  115. So(err, ShouldBeNil)
  116. }))
  117. }