tidb_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "go-common/app/service/main/thumbup/model"
  7. "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDaoBusinesses(t *testing.T) {
  10. var (
  11. c = context.TODO()
  12. )
  13. convey.Convey("Businesses", t, func(ctx convey.C) {
  14. res, err := d.Businesses(c)
  15. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  16. ctx.So(err, convey.ShouldBeNil)
  17. ctx.So(res, convey.ShouldNotBeNil)
  18. })
  19. })
  20. }
  21. func TestDaoLikeState(t *testing.T) {
  22. var (
  23. c = context.TODO()
  24. mid = int64(1)
  25. businessID = int64(1)
  26. originID = int64(1)
  27. messageID = int64(1)
  28. )
  29. convey.Convey("LikeState", t, func(ctx convey.C) {
  30. res, err := d.LikeState(c, mid, businessID, originID, messageID)
  31. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  32. ctx.So(err, convey.ShouldBeNil)
  33. ctx.So(res, convey.ShouldNotBeNil)
  34. })
  35. })
  36. }
  37. func TestDaoUserLikeCount(t *testing.T) {
  38. var (
  39. c = context.TODO()
  40. businessID = int64(1)
  41. mid = int64(1)
  42. typ = int8(1)
  43. )
  44. convey.Convey("UserLikeCount", t, func(ctx convey.C) {
  45. res, err := d.UserLikeCount(c, businessID, mid, typ)
  46. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  47. ctx.So(err, convey.ShouldBeNil)
  48. ctx.So(res, convey.ShouldNotBeNil)
  49. })
  50. })
  51. }
  52. func TestDaoRawItemLikeList(t *testing.T) {
  53. var (
  54. c = context.TODO()
  55. messageID = int64(1)
  56. businessID = int64(1)
  57. originID = int64(1)
  58. state = int8(1)
  59. start = int(1)
  60. end = int(1)
  61. )
  62. convey.Convey("RawItemLikeList", t, func(ctx convey.C) {
  63. _, err := d.RawItemLikeList(c, messageID, businessID, originID, state, start, end)
  64. ctx.Convey("Then err should be nil.res,all should not be nil.", func(ctx convey.C) {
  65. ctx.So(err, convey.ShouldBeNil)
  66. })
  67. })
  68. }
  69. func TestDaoRawUserLikeList(t *testing.T) {
  70. var (
  71. c = context.TODO()
  72. mid = int64(1)
  73. businessID = int64(1)
  74. state = int8(1)
  75. start = int(1)
  76. end = int(1)
  77. )
  78. convey.Convey("RawUserLikeList", t, func(ctx convey.C) {
  79. _, err := d.RawUserLikeList(c, mid, businessID, state, start, end)
  80. ctx.Convey("Then err should be nil.res,all should not be nil.", func(ctx convey.C) {
  81. ctx.So(err, convey.ShouldBeNil)
  82. // ctx.So(all, convey.ShouldNotBeNil)
  83. // ctx.So(res, convey.ShouldNotBeNil)
  84. })
  85. })
  86. }
  87. func TestDaoMessageStats(t *testing.T) {
  88. var (
  89. c = context.TODO()
  90. businessID = int64(1)
  91. ids = []int64{1}
  92. )
  93. convey.Convey("MessageStats", t, func(ctx convey.C) {
  94. res, err := d.MessageStats(c, businessID, ids)
  95. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  96. ctx.So(err, convey.ShouldBeNil)
  97. ctx.So(res, convey.ShouldBeEmpty)
  98. })
  99. })
  100. }
  101. func TestDaoOriginStats(t *testing.T) {
  102. var (
  103. c = context.TODO()
  104. businessID = int64(1)
  105. originID = int64(1)
  106. )
  107. convey.Convey("OriginStats", t, func(ctx convey.C) {
  108. res, err := d.OriginStats(c, businessID, originID)
  109. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  110. ctx.So(err, convey.ShouldBeNil)
  111. ctx.So(res, convey.ShouldNotBeNil)
  112. })
  113. })
  114. }
  115. func TestDaoStat(t *testing.T) {
  116. var (
  117. c = context.TODO()
  118. businessID = int64(1)
  119. originID = int64(1)
  120. messageID = int64(1)
  121. )
  122. convey.Convey("Stat", t, func(ctx convey.C) {
  123. res, err := d.Stat(c, businessID, originID, messageID)
  124. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  125. ctx.So(err, convey.ShouldBeNil)
  126. ctx.So(res, convey.ShouldNotBeNil)
  127. })
  128. })
  129. }
  130. func TestDaotidbRawStats(t *testing.T) {
  131. var (
  132. c = context.TODO()
  133. businessID = int64(1)
  134. originID = int64(1)
  135. messageID = int64(1)
  136. )
  137. convey.Convey("RawStats", t, func(ctx convey.C) {
  138. res, err := d.RawStats(c, businessID, originID, messageID)
  139. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  140. ctx.So(err, convey.ShouldBeNil)
  141. ctx.So(res, convey.ShouldNotBeNil)
  142. })
  143. })
  144. }
  145. func TestDaoUpdateCount(t *testing.T) {
  146. var (
  147. c = context.TODO()
  148. businessID = int64(1)
  149. originID = int64(1)
  150. messageID = int64(1)
  151. likeChange = int64(1)
  152. dislikeChange = int64(1)
  153. )
  154. convey.Convey("UpdateCount", t, func(ctx convey.C) {
  155. err := d.UpdateCount(c, businessID, originID, messageID, likeChange, dislikeChange)
  156. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  157. ctx.So(err, convey.ShouldBeNil)
  158. })
  159. })
  160. }
  161. // INSERT INTO `bilibili_likes`.`counts`(`id`, `mtime`, `ctime`, `business_id`, `origin_id`, `message_id`, `likes_count`, `dislikes_count`, `likes_change`, `dislikes_change`, `up_mid`) VALUES (0, '2018-11-14 17:25:40', '2018-11-03 12:06:27', 3, 0, 10099865, 100, 0, 0, 0, 8167601);
  162. func Test_updateCounts(t *testing.T) {
  163. convey.Convey("get data", t, func() {
  164. c := context.Background()
  165. messageID := int64(10099865)
  166. bid := int64(3)
  167. oid := int64(0)
  168. stat, err := d.Stat(c, bid, oid, messageID)
  169. convey.So(err, convey.ShouldBeNil)
  170. args := [][2]int64{
  171. {1, 1},
  172. {-1000, -1000},
  173. {1, 0},
  174. {0, 1},
  175. {-1, 0},
  176. {-1, -1},
  177. {0, -1},
  178. {0, 0},
  179. {100, -10000},
  180. {100, 20},
  181. {-10, 20},
  182. }
  183. var l, dd int64
  184. for _, x := range args {
  185. l = x[0]
  186. dd = x[1]
  187. convey.Convey(fmt.Sprintf("like %v dislike %v", l, dd), func() {
  188. err := d.UpdateCounts(c, bid, oid, messageID, l, dd, 0)
  189. convey.So(err, convey.ShouldBeNil)
  190. nstat, err := d.Stat(c, bid, oid, messageID)
  191. convey.So(err, convey.ShouldBeNil)
  192. likes := stat.Likes + l
  193. if likes < 0 {
  194. likes = 0
  195. }
  196. dislikes := stat.Dislikes + dd
  197. if dislikes < 0 {
  198. dislikes = 0
  199. }
  200. convey.So(nstat.Likes, convey.ShouldEqual, likes)
  201. convey.So(nstat.Dislikes, convey.ShouldEqual, dislikes)
  202. })
  203. }
  204. })
  205. }
  206. func TestDaoUpdateUpMids(t *testing.T) {
  207. var (
  208. c = context.TODO()
  209. businessID = int64(2)
  210. )
  211. data := []*model.UpMidsReq{
  212. {MessageID: 100, UpMid: 100},
  213. {MessageID: 200, UpMid: 200},
  214. {MessageID: 300, UpMid: 300},
  215. }
  216. convey.Convey("UpdateUpMids", t, func(ctx convey.C) {
  217. _, err := d.UpdateUpMids(c, businessID, data)
  218. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  219. ctx.So(err, convey.ShouldBeNil)
  220. })
  221. })
  222. }
  223. func TestDaoItemHasLike(t *testing.T) {
  224. var (
  225. c = context.TODO()
  226. businessID = int64(1)
  227. originID = int64(1)
  228. messageID = int64(1)
  229. mids = []int64{1, 2, 3}
  230. )
  231. convey.Convey("ItemHasLike", t, func(ctx convey.C) {
  232. res, err := d.ItemHasLike(c, businessID, originID, messageID, mids, model.StateLike)
  233. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  234. ctx.So(err, convey.ShouldBeNil)
  235. ctx.So(res, convey.ShouldNotBeEmpty)
  236. })
  237. })
  238. }