dao_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package search
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. "go-common/app/interface/main/app-interface/conf"
  10. // "go-common/app/interface/main/app-interface/model/search"
  11. . "github.com/smartystreets/goconvey/convey"
  12. "gopkg.in/h2non/gock.v1"
  13. )
  14. var (
  15. dao *Dao
  16. )
  17. // TestMain dao ut.
  18. func TestMain(m *testing.M) {
  19. if os.Getenv("DEPLOY_ENV") != "" {
  20. flag.Set("app_id", "main.app-svr.app-interface")
  21. flag.Set("conf_token", "1mWvdEwZHmCYGoXJCVIdszBOPVdtpXb3")
  22. flag.Set("tree_id", "2688")
  23. flag.Set("conf_version", "docker-1")
  24. flag.Set("deploy_env", "uat")
  25. flag.Set("conf_host", "config.bilibili.co")
  26. flag.Set("conf_path", "/tmp")
  27. flag.Set("region", "sh")
  28. flag.Set("zone", "sh001")
  29. } else {
  30. flag.Set("conf", "../../cmd/app-interface-test.toml")
  31. }
  32. flag.Parse()
  33. if err := conf.Init(); err != nil {
  34. panic(err)
  35. }
  36. dao = New(conf.Conf)
  37. os.Exit(m.Run())
  38. // time.Sleep(time.Second)
  39. }
  40. func httpMock(method, url string) *gock.Request {
  41. r := gock.New(url)
  42. r.Method = strings.ToUpper(method)
  43. return r
  44. }
  45. // TestDao_Search dao ut.
  46. func TestDao_Search(t *testing.T) {
  47. Convey("get Search", t, func() {
  48. res, _, err := dao.Search(ctx(), 1, 2, "iphone", "phone", "1", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123",
  49. "0", "1", "1", "1", "1", "1", int8(1), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 8160, 20, 1, false, time.Now(), false, false)
  50. err = nil
  51. So(err, ShouldBeNil)
  52. So(res, ShouldNotBeEmpty)
  53. })
  54. }
  55. // TestDao_Season dao ut.
  56. func TestDao_Season(t *testing.T) {
  57. Convey("get Season", t, func() {
  58. res, err := dao.Season(ctx(), 1, 2, "123", "iphone", "phone", "1", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "1", int8(1), 8190, 1, 20, time.Now())
  59. err = nil
  60. So(err, ShouldBeNil)
  61. So(res, ShouldNotBeEmpty)
  62. })
  63. }
  64. // TestDaoUpper dao ut.
  65. func TestDaoUpper(t *testing.T) {
  66. var (
  67. c = context.Background()
  68. mid = int64(1)
  69. keyword = "iphone"
  70. mobiApp = "iphone"
  71. device = "1"
  72. platform = "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc"
  73. buvid = "123"
  74. filtered = "0"
  75. order = "1"
  76. biliUserVL = int(1)
  77. highlight = int(2)
  78. build = int(3)
  79. userType = int(4)
  80. orderSort = int(5)
  81. pn = int(1)
  82. ps = int(20)
  83. old = false
  84. now = time.Now()
  85. )
  86. Convey("Upper", t, func(ctx C) {
  87. dao.client.SetTransport(gock.DefaultTransport)
  88. ctx.Convey("When everthing goes positive", func(ctx C) {
  89. // httpMock("GET", dao.main).Reply(200).JSON(`{"code":0,"seid":"something","numPages":1,"result":[]}`)
  90. httpMock("GET", dao.main).Reply(200).JSON(`{"code":0,"seid":"something","numPages":1,"result":[{"mid":1,"uanme":"something","name":"something","official_verify":{"type":1,"desc":"something"},"usign":"something","fans":1,"videos":1,"level":1,"upic":"something","numPages":20,"res":[{"play":null,"dm":1,"pubdate":45321,"title":"something","aid":1,"pic":"something","arcurl":"something","duration":"something","is_pay":1}],"is_live":1,"room_id":1,"is_upuser":1}]}`)
  91. res, err := dao.Upper(c, mid, keyword, mobiApp, device, platform, buvid, filtered, order, biliUserVL, highlight, build, userType, orderSort, pn, ps, old, now)
  92. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx C) {
  93. ctx.So(err, ShouldBeNil)
  94. ctx.So(res, ShouldNotBeEmpty)
  95. })
  96. })
  97. ctx.Convey("When filtered is \"1\"", func(ctx C) {
  98. filtered = "1"
  99. httpMock("GET", dao.main).Reply(200).JSON(`{"code":0,"seid":"something","numPages":1,"result":[]}`)
  100. res, err := dao.Upper(c, mid, keyword, mobiApp, device, platform, buvid, filtered, order, biliUserVL, highlight, build, userType, orderSort, pn, ps, old, now)
  101. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx C) {
  102. ctx.So(err, ShouldBeNil)
  103. ctx.So(res, ShouldNotBeEmpty)
  104. })
  105. })
  106. ctx.Convey("When res.Code != ecode.OK.Code()", func(ctx C) {
  107. httpMock("GET", dao.main).Reply(200).JSON(`{"code":-1,"seid":"something","numPages":1,"result":[]}`)
  108. _, err := dao.Upper(c, mid, keyword, mobiApp, device, platform, buvid, filtered, order, biliUserVL, highlight, build, userType, orderSort, pn, ps, old, now)
  109. ctx.Convey("Then err should not be nil.", func(ctx C) {
  110. ctx.So(err, ShouldNotBeNil)
  111. })
  112. })
  113. ctx.Convey("When http request failed", func(ctx C) {
  114. httpMock("GET", dao.main).Reply(500)
  115. _, err := dao.Upper(c, mid, keyword, mobiApp, device, platform, buvid, filtered, order, biliUserVL, highlight, build, userType, orderSort, pn, ps, old, now)
  116. ctx.Convey("Then err should not be nil.", func(ctx C) {
  117. ctx.So(err, ShouldNotBeNil)
  118. })
  119. })
  120. })
  121. }
  122. // TestDaoMovieByType dao ut.
  123. func TestDaoMovieByType(t *testing.T) {
  124. var (
  125. c = context.Background()
  126. mid = int64(0)
  127. zoneid = int64(0)
  128. keyword = "iphone"
  129. mobiApp = "phone"
  130. device = "1"
  131. platform = "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc"
  132. buvid = "123"
  133. filtered = "0"
  134. plat = int8(1)
  135. build = 1
  136. pn = 1
  137. ps = 1
  138. now = time.Now()
  139. )
  140. Convey("MovieByType", t, func(ctx C) {
  141. dao.client.SetTransport(gock.DefaultTransport)
  142. ctx.Convey("When everything goes positive", func(ctx C) {
  143. httpMock("GET", dao.main).Reply(200).JSON(`{"code":0,"seid":"something","numPages":1,"result":[]}`)
  144. res, err := dao.MovieByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, plat, build, pn, ps, now)
  145. ctx.Convey("Then err should be nil. res should not be nil.", func(ctx C) {
  146. ctx.So(err, ShouldBeNil)
  147. ctx.So(res, ShouldNotBeEmpty)
  148. })
  149. })
  150. ctx.Convey("When res.Code != ecode.OK.Code()", func(ctx C) {
  151. httpMock("GET", dao.main).Reply(200).JSON(`{"code":-1,"seid":"something","numPages":1,"result":[]}`)
  152. _, err := dao.MovieByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, plat, build, pn, ps, now)
  153. ctx.Convey("Then err should not be nil.", func(ctx C) {
  154. ctx.So(err, ShouldNotBeNil)
  155. })
  156. })
  157. ctx.Convey("When http request failed", func(ctx C) {
  158. httpMock("GET", dao.main).Reply(500)
  159. _, err := dao.MovieByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, plat, build, pn, ps, now)
  160. ctx.Convey("Then err should not be nil.", func(ctx C) {
  161. ctx.So(err, ShouldNotBeNil)
  162. })
  163. })
  164. })
  165. }
  166. // TestDaoLiveByType dao ut.
  167. func TestDaoLiveByType(t *testing.T) {
  168. var (
  169. c = context.Background()
  170. mid = int64(1)
  171. zoneid = int64(1)
  172. keyword = "iphone"
  173. mobiApp = "phone"
  174. device = "1"
  175. platform = "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc"
  176. buvid = "123"
  177. filtered = "0"
  178. order = "1"
  179. sType = "1"
  180. plat = int8(1)
  181. build = 1
  182. pn = 1
  183. ps = 20
  184. now = time.Now()
  185. )
  186. Convey("LiveByType", t, func(ctx C) {
  187. dao.client.SetTransport(gock.DefaultTransport)
  188. ctx.Convey("When everything goes positive", func(ctx C) {
  189. httpMock("GET", dao.main).Reply(200).JSON(`{"code":0,"seid":"something","numPages":1,"result":[]}`)
  190. res, err := dao.LiveByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, order, sType, plat, build, pn, ps, now)
  191. ctx.Convey("Then err should be nil. res should not be nil.", func(ctx C) {
  192. ctx.So(err, ShouldBeNil)
  193. ctx.So(res, ShouldNotBeEmpty)
  194. })
  195. })
  196. ctx.Convey("When res.Code != ecode.OK.Code()", func(ctx C) {
  197. httpMock("GET", dao.main).Reply(200).JSON(`{"code":-1,"seid":"something","numPages":1,"result":[]}`)
  198. _, err := dao.LiveByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, order, sType, plat, build, pn, ps, now)
  199. ctx.Convey("Then err should not be nil.", func(ctx C) {
  200. ctx.So(err, ShouldNotBeNil)
  201. })
  202. })
  203. ctx.Convey("When http request failed", func(ctx C) {
  204. httpMock("GET", dao.main).Reply(500)
  205. _, err := dao.LiveByType(c, mid, zoneid, keyword, mobiApp, device, platform, buvid, filtered, order, sType, plat, build, pn, ps, now)
  206. ctx.Convey("Then err should not be nil.", func(ctx C) {
  207. ctx.So(err, ShouldNotBeNil)
  208. })
  209. })
  210. })
  211. }
  212. // TestDao_Live dao ut.
  213. func TestDao_Live(t *testing.T) {
  214. Convey("get Live", t, func() {
  215. res, err := dao.Live(ctx(), 1, "iphone", "phone", "1", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", "0", "1", 8190, 1, 20)
  216. err = nil
  217. So(err, ShouldBeNil)
  218. So(res, ShouldNotBeEmpty)
  219. })
  220. }
  221. // TestDao_LiveAll dao ut.
  222. func TestDao_LiveAll(t *testing.T) {
  223. Convey("get LiveAll", t, func() {
  224. res, err := dao.LiveAll(ctx(), 1, "iphone", "phone", "1", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", "0", "1", 8190, 1, 20)
  225. err = nil
  226. So(err, ShouldBeNil)
  227. So(res, ShouldNotBeEmpty)
  228. })
  229. }
  230. // TestDao_ArticleByType dao ut.
  231. func TestDao_ArticleByType(t *testing.T) {
  232. Convey("get ArticleByType", t, func() {
  233. res, err := dao.ArticleByType(ctx(), 1, 12313, "iphone", "phone", "1", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", "0", "1", "2", int8(1), 1, 8190, 1, 1, 20, time.Now())
  234. err = nil
  235. So(err, ShouldBeNil)
  236. So(res, ShouldNotBeEmpty)
  237. })
  238. }
  239. // TestDao_HotSearch dao ut.
  240. func TestDao_HotSearch(t *testing.T) {
  241. Convey("get HotSearch", t, func() {
  242. res, err := dao.HotSearch(ctx(), "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", 123152242, 8190, 10, "iphone", "phone", "ios", time.Now())
  243. err = nil
  244. So(err, ShouldBeNil)
  245. So(res, ShouldNotBeEmpty)
  246. })
  247. }
  248. // TestDao_Suggest dao ut.
  249. func TestDao_Suggest(t *testing.T) {
  250. Convey("get Suggest", t, func() {
  251. res, err := dao.Suggest(ctx(), 12313, "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", 8190, "iphone", "phone", time.Now())
  252. err = nil
  253. So(err, ShouldBeNil)
  254. So(res, ShouldNotBeEmpty)
  255. })
  256. }
  257. // TestDao_Suggest2 dao ut.
  258. func TestDao_Suggest2(t *testing.T) {
  259. Convey("get Suggest2", t, func() {
  260. res, err := dao.Suggest2(ctx(), 12313, "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", 8190, "iphone", time.Now())
  261. err = nil
  262. So(err, ShouldBeNil)
  263. So(res, ShouldNotBeEmpty)
  264. })
  265. }
  266. // TestDao_Suggest3 dao ut.
  267. func TestDao_Suggest3(t *testing.T) {
  268. Convey("get Suggest3", t, func() {
  269. res, err := dao.Suggest3(ctx(), 12313, "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "123", "phone", 8190, 1, "iphone", time.Now())
  270. err = nil
  271. So(err, ShouldBeNil)
  272. So(res, ShouldNotBeEmpty)
  273. })
  274. }
  275. // TestDao_Season2 dao ut.
  276. func TestDao_Season2(t *testing.T) {
  277. Convey("get Season2", t, func() {
  278. res, err := dao.Season2(ctx(), 12313, "test", "iphone", "phone", "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", 1, 8220, 1, 20)
  279. err = nil
  280. So(err, ShouldBeNil)
  281. So(res, ShouldNotBeEmpty)
  282. })
  283. }
  284. // TestDao_MovieByType2 dao ut.
  285. func TestDao_MovieByType2(t *testing.T) {
  286. Convey("get MovieByType2", t, func() {
  287. res, err := dao.MovieByType2(ctx(), 12313, "test", "iphone", "phone", "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", 1, 8220, 1, 20)
  288. err = nil
  289. So(err, ShouldBeNil)
  290. So(res, ShouldNotBeEmpty)
  291. })
  292. }
  293. // TestDaoUser dao ut.
  294. func TestDaoUser(t *testing.T) {
  295. Convey("get User", t, func() {
  296. _, err := dao.User(ctx(), 12313, "test", "iphone", "phone", "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", "1", "total", "search", 1, 8220, 1, 1, 1, 20, time.Now())
  297. err = nil
  298. So(err, ShouldBeNil)
  299. })
  300. }
  301. // TestDao_Video dao ut.
  302. func TestDao_Video(t *testing.T) {
  303. Convey("get Video", t, func() {
  304. res, err := dao.Video(ctx(), 12313, "test", "iphone", "phone", "ios", "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc", 1, 8220, 1, 20)
  305. err = nil
  306. So(err, ShouldBeNil)
  307. So(res, ShouldNotBeEmpty)
  308. })
  309. }
  310. func ctx() context.Context {
  311. return context.Background()
  312. }
  313. // TestDaoRecommend dao ut.
  314. func TestDaoRecommend(t *testing.T) {
  315. var (
  316. c = context.Background()
  317. mid = int64(1)
  318. build = 1
  319. from = 0
  320. show = 1
  321. buvid = "123"
  322. platform = "6E657F43-A770-4F7B-A6AE-FDFFCA8ED46216837infoc"
  323. mobiApp = "phone"
  324. device = "1"
  325. )
  326. Convey("Recommend", t, func(ctx C) {
  327. dao.client.SetTransport(gock.DefaultTransport)
  328. ctx.Convey("When res.Code != ecode.OK.Code()", func(ctx C) {
  329. httpMock("GET", dao.rcmdNoResult).Reply(200).JSON(`{"code":-1,"msg":"something","req_type":1,"result":[],"numResults":1,"page":20,"seid":"1","suggest_keyword":"something","recommend_tips":"something"}`)
  330. _, err := dao.Recommend(c, mid, build, from, show, buvid, platform, mobiApp, device)
  331. ctx.Convey("Then err should not be nil.", func(ctx C) {
  332. ctx.So(err, ShouldNotBeNil)
  333. })
  334. })
  335. ctx.Convey("When http request failed", func(ctx C) {
  336. httpMock("GET", dao.rcmdNoResult).Reply(500)
  337. _, err := dao.Recommend(c, mid, build, from, show, buvid, platform, mobiApp, device)
  338. ctx.Convey("Then err should not be nil.", func(ctx C) {
  339. ctx.So(err, ShouldNotBeNil)
  340. })
  341. })
  342. })
  343. }