mysql_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "go-common/app/service/main/push/model"
  9. xtime "go-common/library/time"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func Test_Task(t *testing.T) {
  13. Convey("add task", t, WithDao(func(d *Dao) {
  14. t := &model.Task{
  15. Job: 1378138219873,
  16. Type: model.TaskTypeBusiness,
  17. APPID: 1,
  18. BusinessID: 1,
  19. Platform: []int{1, 2, 3},
  20. Title: "test_tile",
  21. Summary: "test_summary",
  22. LinkType: model.LinkTypeBrowser,
  23. LinkValue: "https://www.bilibili.com",
  24. Build: map[int]*model.Build{2: {Build: 100, Condition: "gt"}},
  25. Sound: 1,
  26. Vibration: 1,
  27. PassThrough: 1,
  28. Progress: new(model.Progress),
  29. MidFile: "xxx.txt",
  30. PushTime: xtime.Time(1500000000),
  31. ExpireTime: xtime.Time(1600000000),
  32. Status: model.TaskStatusPrepared,
  33. }
  34. c := context.Background()
  35. taskID, err := d.AddTask(c, t)
  36. taskIDString := strconv.FormatInt(taskID, 10)
  37. So(err, ShouldBeNil)
  38. Convey("task info", func() {
  39. task, err := d.Task(c, taskIDString)
  40. So(err, ShouldBeNil)
  41. task.ID = ""
  42. So(task, ShouldResemble, t)
  43. })
  44. Convey("update task progress", func() {
  45. p := &model.Progress{TokenTotal: 100}
  46. err := d.UpdateTaskProgress(c, taskIDString, p)
  47. So(err, ShouldBeNil)
  48. task, err := d.Task(c, taskIDString)
  49. So(err, ShouldBeNil)
  50. So(task, ShouldNotBeEmpty)
  51. So(task.Progress.TokenTotal, ShouldEqual, 100)
  52. })
  53. Convey("update task status", func() {
  54. err := d.UpdateTaskStatus(c, taskIDString, model.TaskStatusDone)
  55. So(err, ShouldBeNil)
  56. task, err := d.Task(c, taskIDString)
  57. So(err, ShouldBeNil)
  58. So(task, ShouldNotBeEmpty)
  59. So(task.Status, ShouldEqual, model.TaskStatusDone)
  60. })
  61. Convey("tx tokens by platform", func() {
  62. tx, _ := d.BeginTx(context.Background())
  63. _, err := d.TxTaskByPlatform(tx, model.PlatformIPad)
  64. So(err, ShouldBeNil)
  65. err = tx.Commit()
  66. So(err, ShouldBeNil)
  67. })
  68. }))
  69. }
  70. func Test_Business(t *testing.T) {
  71. Convey("get businesses", t, WithDao(func(d *Dao) {
  72. res, err := d.Businesses(context.Background())
  73. So(err, ShouldBeNil)
  74. So(res, ShouldNotBeEmpty)
  75. fmt.Println(res[1])
  76. }))
  77. }
  78. func Test_Setting(t *testing.T) {
  79. Convey("setting", t, WithDao(func(d *Dao) {
  80. c := context.Background()
  81. mid := int64(910819)
  82. err := d.SetSetting(c, mid, model.Settings)
  83. So(err, ShouldBeNil)
  84. res, err := d.Setting(c, mid)
  85. So(err, ShouldBeNil)
  86. So(res, ShouldResemble, model.Settings)
  87. }))
  88. }
  89. func Test_Report(t *testing.T) {
  90. r := &model.Report{
  91. APPID: model.APPIDBBPhone,
  92. PlatformID: model.PlatformIPhone,
  93. Mid: 910819,
  94. Buvid: "b",
  95. DeviceToken: strconv.FormatInt(time.Now().UnixNano(), 10),
  96. Build: 2233,
  97. TimeZone: 8,
  98. NotifySwitch: model.SwitchOn,
  99. DeviceBrand: "OPPO",
  100. DeviceModel: "OPPO R9st",
  101. OSVersion: "6.0.1",
  102. }
  103. c := context.Background()
  104. Convey("report", t, WithDao(func(d *Dao) {
  105. id, err := d.AddReport(c, r)
  106. So(err, ShouldBeNil)
  107. r.ID = id
  108. rt, err := d.Report(c, r.DeviceToken)
  109. So(err, ShouldBeNil)
  110. So(rt, ShouldResemble, r)
  111. _, err = d.ReportsByMid(c, r.Mid)
  112. So(err, ShouldBeNil)
  113. _, err = d.ReportsByMids(c, []int64{r.Mid})
  114. So(err, ShouldBeNil)
  115. rows, err := d.DelReport(c, r.DeviceToken)
  116. So(err, ShouldBeNil)
  117. So(rows, ShouldBeGreaterThan, 0)
  118. res, err := d.ReportsByID(context.TODO(), []int64{1, 2, 3})
  119. So(err, ShouldBeNil)
  120. // t.Logf("ReportsByID res(%v)", res)
  121. So(len(res), ShouldBeGreaterThan, 0)
  122. res1, err := d.Reports(context.TODO(), []string{"742381013eb5fb21e003479d041369481ca861d41a9e489abe9d44c27dd43d74", "cidViiN2cwpUdlrQXXPJlyk47N69WDje3PA1+ISCGIA="})
  123. So(err, ShouldBeNil)
  124. t.Log(len(res1))
  125. }))
  126. }
  127. func Test_UpdateReport(t *testing.T) {
  128. Convey("update report", t, WithDao(func(d *Dao) {
  129. ctx := context.Background()
  130. r := &model.Report{
  131. APPID: model.APPIDBBPhone,
  132. PlatformID: model.PlatformIPhone,
  133. Mid: 910819,
  134. Buvid: "b",
  135. DeviceToken: "dt",
  136. Build: 2233,
  137. TimeZone: 8,
  138. NotifySwitch: model.SwitchOn,
  139. DeviceBrand: "OPPO",
  140. DeviceModel: "OPPO R9st",
  141. OSVersion: "6.0.1",
  142. }
  143. _, err := d.db.Exec(context.Background(), "delete from push_reports where token_hash=?", model.HashToken(r.DeviceToken))
  144. So(err, ShouldBeNil)
  145. id, err := d.AddReport(ctx, r)
  146. So(err, ShouldBeNil)
  147. So(id, ShouldBeGreaterThan, 0)
  148. r.ID = id
  149. rt, err := d.Report(ctx, r.DeviceToken)
  150. So(err, ShouldBeNil)
  151. So(rt, ShouldResemble, r)
  152. rt.APPID = 2
  153. rt.PlatformID = 3
  154. rt.NotifySwitch = model.SwitchOff
  155. rt.Mid = 123
  156. rt.Buvid = "buvidxxxx"
  157. rt.Build = 1000000
  158. rt.OSVersion = "x.x.x"
  159. err = d.UpdateReport(ctx, rt)
  160. So(err, ShouldBeNil)
  161. rt2, err := d.Report(ctx, r.DeviceToken)
  162. So(err, ShouldBeNil)
  163. So(rt2, ShouldResemble, rt)
  164. So(rt2, ShouldNotResemble, r)
  165. }))
  166. }
  167. func Test_Callback(t *testing.T) {
  168. Convey("add callback", t, WithDao(func(d *Dao) {
  169. cb := &model.Callback{
  170. Task: "task123",
  171. APP: model.APPIDBBPhone,
  172. Platform: model.PlatformXiaomi,
  173. Mid: 91221505,
  174. Pid: model.MobiAndroid,
  175. Token: "token",
  176. Buvid: "buvid",
  177. Click: 1,
  178. Extra: &model.CallbackExtra{
  179. Status: 2,
  180. },
  181. }
  182. err := d.AddCallback(context.TODO(), cb)
  183. So(err, ShouldBeNil)
  184. }))
  185. }
  186. func Test_ReportByID(t *testing.T) {
  187. Convey("report by id", t, WithDao(func(d *Dao) {
  188. r, err := d.ReportByID(context.TODO(), 1)
  189. So(err, ShouldBeNil)
  190. So(r, ShouldNotBeNil)
  191. t.Logf("reportByID res(%+v)", r)
  192. }))
  193. }