views.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { ApolloServer } from 'apollo-server-express'
  2. import { Mongoose } from 'mongoose'
  3. import { Aggregates } from '../src/types'
  4. import { ApolloServerTestClient } from 'apollo-server-testing/dist/createTestClient'
  5. import { buildAggregates, connectMongoose, createServer } from '../src/server'
  6. import { createTestClient } from 'apollo-server-testing'
  7. import {
  8. ADD_VIDEO_VIEW,
  9. AddVideoView,
  10. AddVideoViewArgs,
  11. GET_CHANNEL_VIEWS,
  12. GET_MOST_VIEWED_CHANNELS,
  13. GET_MOST_VIEWED_CHANNELS_ALL_TIME,
  14. GET_VIDEO_VIEWS,
  15. GET_MOST_VIEWED_VIDEOS,
  16. GET_MOST_VIEWED_VIDEOS_ALL_TIME,
  17. GET_MOST_VIEWED_CATEGORIES,
  18. GET_MOST_VIEWED_CATEGORIES_ALL_TIME,
  19. GetChannelViews,
  20. GetChannelViewsArgs,
  21. GetVideoViews,
  22. GetVideoViewsArgs,
  23. GetMostViewedVideosArgs,
  24. GetMostViewedVideosAllTimeArgs,
  25. GetMostViewedChannelsArgs,
  26. GetMostViewedChannelsAllTimeArgs,
  27. GetMostViewedVideos,
  28. GetMostViewedVideosAllTime,
  29. GetMostViewedChannels,
  30. GetMostViewedChannelsAllTime,
  31. GetMostViewedCategoriesArgs,
  32. GetMostViewedCategoriesAllTimeArgs,
  33. GetMostViewedCategories,
  34. GetMostViewedCategoriesAllTime,
  35. } from './queries/views'
  36. import { EntityViewsInfo } from '../src/entities/EntityViewsInfo'
  37. import { VideoEventsBucketModel } from '../src/models/VideoEvent'
  38. import { TEST_BUCKET_SIZE } from './setup'
  39. const FIRST_VIDEO_ID = '12'
  40. const SECOND_VIDEO_ID = '13'
  41. const FIRST_CHANNEL_ID = '22'
  42. const SECOND_CHANNEL_ID = '23'
  43. const FIRST_CATEGORY_ID = '32'
  44. describe('Video and channel views resolver', () => {
  45. let server: ApolloServer
  46. let mongoose: Mongoose
  47. let aggregates: Aggregates
  48. let query: ApolloServerTestClient['query']
  49. let mutate: ApolloServerTestClient['mutate']
  50. beforeEach(async () => {
  51. mongoose = await connectMongoose(process.env.MONGO_URL!)
  52. aggregates = await buildAggregates()
  53. server = await createServer(mongoose, aggregates)
  54. const testClient = createTestClient(server)
  55. query = testClient.query
  56. mutate = testClient.mutate
  57. })
  58. afterEach(async () => {
  59. await server.stop()
  60. await VideoEventsBucketModel.deleteMany({})
  61. await mongoose.disconnect()
  62. })
  63. const addVideoView = async (videoId: string, channelId: string, categoryId?: string) => {
  64. const addVideoViewResponse = await mutate<AddVideoView, AddVideoViewArgs>({
  65. mutation: ADD_VIDEO_VIEW,
  66. variables: { videoId, channelId, categoryId },
  67. })
  68. expect(addVideoViewResponse.errors).toBeUndefined()
  69. return addVideoViewResponse.data?.addVideoView
  70. }
  71. const getVideoViews = async (videoId: string) => {
  72. const videoViewsResponse = await query<GetVideoViews, GetVideoViewsArgs>({
  73. query: GET_VIDEO_VIEWS,
  74. variables: { videoId },
  75. })
  76. expect(videoViewsResponse.errors).toBeUndefined()
  77. return videoViewsResponse.data?.videoViews
  78. }
  79. const getMostViewedVideos = async (timePeriodDays: number) => {
  80. const mostViewedVideosResponse = await query<GetMostViewedVideos, GetMostViewedVideosArgs>({
  81. query: GET_MOST_VIEWED_VIDEOS,
  82. variables: { timePeriodDays },
  83. })
  84. expect(mostViewedVideosResponse.errors).toBeUndefined()
  85. return mostViewedVideosResponse.data?.mostViewedVideos
  86. }
  87. const getMostViewedVideosAllTime = async (limit: number) => {
  88. const mostViewedVideosAllTimeResponse = await query<GetMostViewedVideosAllTime, GetMostViewedVideosAllTimeArgs>({
  89. query: GET_MOST_VIEWED_VIDEOS_ALL_TIME,
  90. variables: { limit },
  91. })
  92. expect(mostViewedVideosAllTimeResponse.errors).toBeUndefined()
  93. return mostViewedVideosAllTimeResponse.data?.mostViewedVideosAllTime
  94. }
  95. const getChannelViews = async (channelId: string) => {
  96. const channelViewsResponse = await query<GetChannelViews, GetChannelViewsArgs>({
  97. query: GET_CHANNEL_VIEWS,
  98. variables: { channelId },
  99. })
  100. expect(channelViewsResponse.errors).toBeUndefined()
  101. return channelViewsResponse.data?.channelViews
  102. }
  103. const getMostViewedChannels = async (timePeriodDays: number) => {
  104. const mostViewedChannelsResponse = await query<GetMostViewedChannels, GetMostViewedChannelsArgs>({
  105. query: GET_MOST_VIEWED_CHANNELS,
  106. variables: { timePeriodDays },
  107. })
  108. expect(mostViewedChannelsResponse.errors).toBeUndefined()
  109. return mostViewedChannelsResponse.data?.mostViewedChannels
  110. }
  111. const getMostViewedChannelsAllTime = async (limit: number) => {
  112. const mostViewedChannelsAllTimeResponse = await query<
  113. GetMostViewedChannelsAllTime,
  114. GetMostViewedChannelsAllTimeArgs
  115. >({
  116. query: GET_MOST_VIEWED_CHANNELS_ALL_TIME,
  117. variables: { limit },
  118. })
  119. expect(mostViewedChannelsAllTimeResponse.errors).toBeUndefined()
  120. return mostViewedChannelsAllTimeResponse.data?.mostViewedChannelsAllTime
  121. }
  122. const getMostViewedCategories = async (timePeriodDays: number) => {
  123. const mostViewedCategoriesResponse = await query<GetMostViewedCategories, GetMostViewedCategoriesArgs>({
  124. query: GET_MOST_VIEWED_CATEGORIES,
  125. variables: { timePeriodDays },
  126. })
  127. expect(mostViewedCategoriesResponse.errors).toBeUndefined()
  128. return mostViewedCategoriesResponse.data?.mostViewedCategories
  129. }
  130. const getMostViewedCategoriesAllTime = async (limit: number) => {
  131. const mostViewedCategoriesAllTimeResponse = await query<
  132. GetMostViewedCategoriesAllTime,
  133. GetMostViewedCategoriesAllTimeArgs
  134. >({
  135. query: GET_MOST_VIEWED_CATEGORIES_ALL_TIME,
  136. variables: { limit },
  137. })
  138. expect(mostViewedCategoriesAllTimeResponse.errors).toBeUndefined()
  139. return mostViewedCategoriesAllTimeResponse.data?.mostViewedCategoriesAllTime
  140. }
  141. it('should return null for unknown video, channel and category views', async () => {
  142. const videoViews = await getVideoViews(FIRST_VIDEO_ID)
  143. const mostViewedVideos = await getMostViewedVideos(30)
  144. const mostViewedVideosAllTime = await getMostViewedVideosAllTime(10)
  145. const channelViews = await getChannelViews(FIRST_CHANNEL_ID)
  146. const mostViewedChannels = await getMostViewedChannels(30)
  147. const mostViewedChannelsAllTime = await getMostViewedChannelsAllTime(10)
  148. const mostViewedCategories = await getMostViewedCategories(30)
  149. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  150. expect(videoViews).toBeNull()
  151. expect(mostViewedVideos).toHaveLength(0)
  152. expect(mostViewedVideosAllTime).toHaveLength(0)
  153. expect(channelViews).toBeNull()
  154. expect(mostViewedChannels).toHaveLength(0)
  155. expect(mostViewedChannelsAllTime).toHaveLength(0)
  156. expect(mostViewedCategories).toHaveLength(0)
  157. expect(mostViewedCategoriesAllTime).toHaveLength(0)
  158. })
  159. it('should properly save video and channel views', async () => {
  160. const expectedVideoViews: EntityViewsInfo = {
  161. id: FIRST_VIDEO_ID,
  162. views: 1,
  163. }
  164. const expectedChannelViews: EntityViewsInfo = {
  165. id: FIRST_CHANNEL_ID,
  166. views: 1,
  167. }
  168. const expectedCategoryViews: EntityViewsInfo = {
  169. id: FIRST_CATEGORY_ID,
  170. views: 1,
  171. }
  172. const checkViews = async () => {
  173. const videoViews = await getVideoViews(FIRST_VIDEO_ID)
  174. const mostViewedVideos = await getMostViewedVideos(30)
  175. const mostViewedVideosAllTime = await getMostViewedVideosAllTime(10)
  176. const channelViews = await getChannelViews(FIRST_CHANNEL_ID)
  177. const mostViewedChannels = await getMostViewedChannels(30)
  178. const mostViewedChannelsAllTime = await getMostViewedChannelsAllTime(10)
  179. const mostViewedCategories = await getMostViewedCategories(30)
  180. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  181. expect(videoViews).toEqual(expectedVideoViews)
  182. expect(mostViewedVideos).toEqual([expectedVideoViews])
  183. expect(mostViewedVideosAllTime).toEqual([expectedVideoViews])
  184. expect(channelViews).toEqual(expectedChannelViews)
  185. expect(mostViewedChannels).toEqual([expectedChannelViews])
  186. expect(mostViewedChannelsAllTime).toEqual([expectedChannelViews])
  187. expect(mostViewedCategories).toEqual([expectedCategoryViews])
  188. expect(mostViewedCategoriesAllTime).toEqual([expectedCategoryViews])
  189. }
  190. let addVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  191. expect(addVideoViewData).toEqual(expectedVideoViews)
  192. await checkViews()
  193. expectedVideoViews.views++
  194. expectedChannelViews.views++
  195. expectedCategoryViews.views++
  196. addVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  197. expect(addVideoViewData).toEqual(expectedVideoViews)
  198. await checkViews()
  199. })
  200. it('should distinct views of separate videos', async () => {
  201. const expectedFirstVideoViews: EntityViewsInfo = {
  202. id: FIRST_VIDEO_ID,
  203. views: 1,
  204. }
  205. const expectedSecondVideoViews: EntityViewsInfo = {
  206. id: SECOND_VIDEO_ID,
  207. views: 1,
  208. }
  209. const addFirstVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  210. const addSecondVideoViewData = await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID)
  211. expect(addFirstVideoViewData).toEqual(expectedFirstVideoViews)
  212. expect(addSecondVideoViewData).toEqual(expectedSecondVideoViews)
  213. expectedFirstVideoViews.views++
  214. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  215. const firstVideoViews = await getVideoViews(FIRST_VIDEO_ID)
  216. const secondVideoViews = await getVideoViews(SECOND_VIDEO_ID)
  217. const mostViewedVideos = await getMostViewedVideos(30)
  218. const mostViewedVideosAllTime = await getMostViewedVideosAllTime(10)
  219. expect(firstVideoViews).toEqual(expectedFirstVideoViews)
  220. expect(secondVideoViews).toEqual(expectedSecondVideoViews)
  221. expect(mostViewedVideos).toEqual([expectedFirstVideoViews, expectedSecondVideoViews])
  222. expect(mostViewedVideosAllTime).toEqual([expectedFirstVideoViews, expectedSecondVideoViews])
  223. })
  224. it('should distinct views of separate channels', async () => {
  225. const expectedFirstChanelViews: EntityViewsInfo = {
  226. id: FIRST_CHANNEL_ID,
  227. views: 1,
  228. }
  229. const expectedSecondChannelViews: EntityViewsInfo = {
  230. id: SECOND_CHANNEL_ID,
  231. views: 1,
  232. }
  233. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  234. await addVideoView(SECOND_VIDEO_ID, SECOND_CHANNEL_ID)
  235. const firstChannelViews = await getChannelViews(FIRST_CHANNEL_ID)
  236. const secondChannelViews = await getChannelViews(SECOND_CHANNEL_ID)
  237. const mostViewedChannels = await getMostViewedChannels(30)
  238. const mostViewedChannelsAllTime = await getMostViewedChannelsAllTime(10)
  239. expect(firstChannelViews).toEqual(expectedFirstChanelViews)
  240. expect(secondChannelViews).toEqual(expectedSecondChannelViews)
  241. expect(mostViewedChannels).toEqual([expectedFirstChanelViews, expectedSecondChannelViews])
  242. expect(mostViewedChannelsAllTime).toEqual([expectedFirstChanelViews, expectedSecondChannelViews])
  243. })
  244. it('should properly aggregate views of a channel', async () => {
  245. const expectedChannelViews: EntityViewsInfo = {
  246. id: FIRST_CHANNEL_ID,
  247. views: 2,
  248. }
  249. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  250. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID)
  251. const channelViews = await getChannelViews(FIRST_CHANNEL_ID)
  252. const mostViewedChannels = await getMostViewedChannels(30)
  253. const mostViewedChannelsAllTime = await getMostViewedChannelsAllTime(10)
  254. expect(channelViews).toEqual(expectedChannelViews)
  255. expect(mostViewedChannels).toEqual([expectedChannelViews])
  256. expect(mostViewedChannelsAllTime).toEqual([expectedChannelViews])
  257. })
  258. it('should properly aggregate views of a category', async () => {
  259. const expectedChannelViews: EntityViewsInfo = {
  260. id: FIRST_CATEGORY_ID,
  261. views: 2,
  262. }
  263. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  264. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  265. const mostViewedCategories = await getMostViewedCategories(30)
  266. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  267. expect(mostViewedCategories).toEqual([expectedChannelViews])
  268. expect(mostViewedCategoriesAllTime).toEqual([expectedChannelViews])
  269. })
  270. it('should properly rebuild the aggregate', async () => {
  271. const expectedFirstVideoViews: EntityViewsInfo = {
  272. id: FIRST_VIDEO_ID,
  273. views: 3,
  274. }
  275. const expectedSecondVideoViews: EntityViewsInfo = {
  276. id: SECOND_VIDEO_ID,
  277. views: 4,
  278. }
  279. const expectedChannelViews: EntityViewsInfo = {
  280. id: FIRST_CHANNEL_ID,
  281. views: 7,
  282. }
  283. const expectedCategoryViews: EntityViewsInfo = {
  284. id: FIRST_CATEGORY_ID,
  285. views: 7,
  286. }
  287. const checkViews = async () => {
  288. const firstVideoViews = await getVideoViews(FIRST_VIDEO_ID)
  289. const secondVideoViews = await getVideoViews(SECOND_VIDEO_ID)
  290. const channelViews = await getChannelViews(FIRST_CHANNEL_ID)
  291. const mostViewedVideos = await getMostViewedVideos(30)
  292. const mostViewedVideosAllTime = await getMostViewedVideosAllTime(10)
  293. const mostViewedChannels = await getMostViewedChannels(30)
  294. const mostViewedChannelsAllTime = await getMostViewedChannelsAllTime(10)
  295. const mostViewedCategories = await getMostViewedCategories(30)
  296. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  297. expect(firstVideoViews).toEqual(expectedFirstVideoViews)
  298. expect(secondVideoViews).toEqual(expectedSecondVideoViews)
  299. expect(mostViewedVideos).toEqual([expectedSecondVideoViews, expectedFirstVideoViews])
  300. expect(mostViewedVideosAllTime).toEqual([expectedSecondVideoViews, expectedFirstVideoViews])
  301. expect(channelViews).toEqual(expectedChannelViews)
  302. expect(mostViewedChannels).toEqual([expectedChannelViews])
  303. expect(mostViewedChannelsAllTime).toEqual([expectedChannelViews])
  304. expect(mostViewedCategories).toEqual([expectedCategoryViews])
  305. expect(mostViewedCategoriesAllTime).toEqual([expectedCategoryViews])
  306. }
  307. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  308. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  309. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  310. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  311. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  312. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  313. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  314. await checkViews()
  315. await server.stop()
  316. aggregates = await buildAggregates()
  317. server = await createServer(mongoose, aggregates)
  318. const testClient = createTestClient(server)
  319. query = testClient.query
  320. mutate = testClient.mutate
  321. await checkViews()
  322. })
  323. it('should properly handle saving events across buckets', async () => {
  324. const eventsCount = TEST_BUCKET_SIZE * 2 + 1
  325. const expectedVideoViews: EntityViewsInfo = {
  326. id: FIRST_VIDEO_ID,
  327. views: eventsCount,
  328. }
  329. for (let i = 0; i < eventsCount; i++) {
  330. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  331. }
  332. const videoViews = await getVideoViews(FIRST_VIDEO_ID)
  333. const mostViewedVideos = await getMostViewedVideos(30)
  334. const mostViewedVideosAllTime = await getMostViewedVideosAllTime(10)
  335. expect(videoViews).toEqual(expectedVideoViews)
  336. expect(mostViewedVideos).toEqual([expectedVideoViews])
  337. expect(mostViewedVideosAllTime).toEqual([expectedVideoViews])
  338. })
  339. })