views.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { ApolloServer } from 'apollo-server-express'
  2. import { Mongoose } from 'mongoose'
  3. import { Aggregates } from '../src/types'
  4. import { buildAggregates, connectMongoose, createServer } from '../src/server'
  5. import {
  6. ADD_VIDEO_VIEW,
  7. AddVideoView,
  8. AddVideoViewArgs,
  9. GET_MOST_VIEWED_CHANNELS_CONNECTION,
  10. GET_MOST_VIEWED_VIDEOS_CONNECTION,
  11. GET_MOST_VIEWED_CATEGORIES,
  12. GET_MOST_VIEWED_CATEGORIES_ALL_TIME,
  13. GetMostViewedVideosConnectionArgs,
  14. GetMostViewedChannelsConnectionArgs,
  15. GetMostViewedVideosConnection,
  16. GetMostViewedChannelsConnection,
  17. GetMostViewedCategoriesArgs,
  18. GetMostViewedCategoriesAllTimeArgs,
  19. GetMostViewedCategories,
  20. GetMostViewedCategoriesAllTime,
  21. } from './queries/views'
  22. import { EntityViewsInfo } from '../src/entities/EntityViewsInfo'
  23. import { VideoEventModel } from '../src/models/VideoEvent'
  24. import { createMutationFn, createQueryFn, MutationFn, QueryFn } from './helpers'
  25. const FIRST_VIDEO_ID = '7'
  26. const SECOND_VIDEO_ID = '8'
  27. const FIRST_CHANNEL_ID = '6'
  28. const SECOND_CHANNEL_ID = '7'
  29. const FIRST_CATEGORY_ID = '1'
  30. describe('Video and channel views resolver', () => {
  31. let server: ApolloServer
  32. let mongoose: Mongoose
  33. let aggregates: Aggregates
  34. let query: QueryFn
  35. let mutate: MutationFn
  36. beforeEach(async () => {
  37. mongoose = await connectMongoose(process.env.MONGO_URL!)
  38. aggregates = await buildAggregates()
  39. server = await createServer(mongoose, aggregates, process.env.ORION_QUERY_NODE_URL!)
  40. await server.start()
  41. query = createQueryFn(server)
  42. mutate = createMutationFn(server)
  43. })
  44. afterEach(async () => {
  45. await server.stop()
  46. await VideoEventModel.deleteMany({})
  47. await mongoose.disconnect()
  48. })
  49. const addVideoView = async (videoId: string, channelId: string, categoryId?: string) => {
  50. const addVideoViewResponse = await mutate<AddVideoView, AddVideoViewArgs>({
  51. mutation: ADD_VIDEO_VIEW,
  52. variables: { videoId, channelId, categoryId },
  53. })
  54. expect(addVideoViewResponse.errors).toBeUndefined()
  55. return addVideoViewResponse.data?.addVideoView
  56. }
  57. const getMostViewedVideos = async (periodDays: 7 | 30 | null) => {
  58. const mostViewedVideosResponse = await query<GetMostViewedVideosConnection, GetMostViewedVideosConnectionArgs>({
  59. query: GET_MOST_VIEWED_VIDEOS_CONNECTION,
  60. variables: { periodDays, limit: 10 },
  61. })
  62. expect(mostViewedVideosResponse.errors).toBeUndefined()
  63. return mostViewedVideosResponse.data?.mostViewedVideosConnection
  64. }
  65. const getMostViewedChannels = async (periodDays: 7 | 30 | null) => {
  66. const mostViewedChannelsResponse = await query<
  67. GetMostViewedChannelsConnection,
  68. GetMostViewedChannelsConnectionArgs
  69. >({
  70. query: GET_MOST_VIEWED_CHANNELS_CONNECTION,
  71. variables: { periodDays, limit: 10 },
  72. })
  73. expect(mostViewedChannelsResponse.errors).toBeUndefined()
  74. return mostViewedChannelsResponse.data?.mostViewedChannelsConnection
  75. }
  76. const getMostViewedCategories = async (timePeriodDays: number) => {
  77. const mostViewedCategoriesResponse = await query<GetMostViewedCategories, GetMostViewedCategoriesArgs>({
  78. query: GET_MOST_VIEWED_CATEGORIES,
  79. variables: { timePeriodDays },
  80. })
  81. expect(mostViewedCategoriesResponse.errors).toBeUndefined()
  82. return mostViewedCategoriesResponse.data?.mostViewedCategories
  83. }
  84. const getMostViewedCategoriesAllTime = async (limit: number) => {
  85. const mostViewedCategoriesAllTimeResponse = await query<
  86. GetMostViewedCategoriesAllTime,
  87. GetMostViewedCategoriesAllTimeArgs
  88. >({
  89. query: GET_MOST_VIEWED_CATEGORIES_ALL_TIME,
  90. variables: { limit },
  91. })
  92. expect(mostViewedCategoriesAllTimeResponse.errors).toBeUndefined()
  93. return mostViewedCategoriesAllTimeResponse.data?.mostViewedCategoriesAllTime
  94. }
  95. it('should return null for unknown video, channel and category views', async () => {
  96. const mostViewedVideos = await getMostViewedVideos(30)
  97. const mostViewedVideosAllTime = await getMostViewedVideos(null)
  98. const mostViewedChannels = await getMostViewedChannels(30)
  99. const mostViewedChannelsAllTime = await getMostViewedChannels(null)
  100. const mostViewedCategories = await getMostViewedCategories(30)
  101. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  102. expect(mostViewedVideos?.edges).toHaveLength(0)
  103. expect(mostViewedVideosAllTime?.edges).toHaveLength(0)
  104. expect(mostViewedChannels?.edges).toHaveLength(0)
  105. expect(mostViewedChannelsAllTime?.edges).toHaveLength(0)
  106. expect(mostViewedCategories).toHaveLength(0)
  107. expect(mostViewedCategoriesAllTime).toHaveLength(0)
  108. })
  109. it('should properly save video and channel views', async () => {
  110. const expectedVideoViews: EntityViewsInfo = {
  111. id: FIRST_VIDEO_ID,
  112. views: 1,
  113. }
  114. const expectedChannelViews: EntityViewsInfo = {
  115. id: FIRST_CHANNEL_ID,
  116. views: 1,
  117. }
  118. const expectedCategoryViews: EntityViewsInfo = {
  119. id: FIRST_CATEGORY_ID,
  120. views: 1,
  121. }
  122. const expectedMostViewedVideos = {
  123. edges: [expectedVideoViews].map((view) => ({ node: view })),
  124. }
  125. const expectedMostViewedChannels = {
  126. edges: [expectedChannelViews].map((view) => ({ node: view })),
  127. }
  128. const checkViews = async () => {
  129. const mostViewedVideos = await getMostViewedVideos(30)
  130. const mostViewedVideosAllTime = await getMostViewedVideos(null)
  131. const mostViewedChannels = await getMostViewedChannels(30)
  132. const mostViewedChannelsAllTime = await getMostViewedChannels(null)
  133. const mostViewedCategories = await getMostViewedCategories(30)
  134. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  135. expect(mostViewedVideos).toEqual(expectedMostViewedVideos)
  136. expect(mostViewedVideosAllTime).toEqual(expectedMostViewedVideos)
  137. expect(mostViewedChannels).toEqual(expectedMostViewedChannels)
  138. expect(mostViewedChannelsAllTime).toEqual(expectedMostViewedChannels)
  139. expect(mostViewedCategories).toEqual([expectedCategoryViews])
  140. expect(mostViewedCategoriesAllTime).toEqual([expectedCategoryViews])
  141. }
  142. let addVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  143. expect(addVideoViewData).toEqual(expectedVideoViews)
  144. await checkViews()
  145. expectedVideoViews.views++
  146. expectedChannelViews.views++
  147. expectedCategoryViews.views++
  148. addVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  149. expect(addVideoViewData).toEqual(expectedVideoViews)
  150. await checkViews()
  151. })
  152. it('should distinct views of separate videos', async () => {
  153. const expectedFirstVideoViews: EntityViewsInfo = {
  154. id: FIRST_VIDEO_ID,
  155. views: 1,
  156. }
  157. const expectedSecondVideoViews: EntityViewsInfo = {
  158. id: SECOND_VIDEO_ID,
  159. views: 1,
  160. }
  161. const expectedMostViewedVideos = {
  162. edges: [expectedFirstVideoViews, expectedSecondVideoViews].map((view) => ({ node: view })),
  163. }
  164. const addFirstVideoViewData = await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  165. const addSecondVideoViewData = await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID)
  166. expect(addFirstVideoViewData).toEqual(expectedFirstVideoViews)
  167. expect(addSecondVideoViewData).toEqual(expectedSecondVideoViews)
  168. expectedFirstVideoViews.views++
  169. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  170. const mostViewedVideos = await getMostViewedVideos(30)
  171. const mostViewedVideosAllTime = await getMostViewedVideos(null)
  172. expect(mostViewedVideos).toEqual(expectedMostViewedVideos)
  173. expect(mostViewedVideosAllTime).toEqual(expectedMostViewedVideos)
  174. })
  175. it('should distinct views of separate channels', async () => {
  176. const expectedFirstChanelViews: EntityViewsInfo = {
  177. id: FIRST_CHANNEL_ID,
  178. views: 1,
  179. }
  180. const expectedSecondChannelViews: EntityViewsInfo = {
  181. id: SECOND_CHANNEL_ID,
  182. views: 1,
  183. }
  184. const expectedMostViewedChannels = {
  185. edges: [expectedFirstChanelViews, expectedSecondChannelViews].map((view) => ({ node: view })),
  186. }
  187. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  188. await addVideoView(SECOND_VIDEO_ID, SECOND_CHANNEL_ID)
  189. const mostViewedChannels = await getMostViewedChannels(30)
  190. const mostViewedChannelsAllTime = await getMostViewedChannels(null)
  191. expect(mostViewedChannels).toEqual(expectedMostViewedChannels)
  192. expect(mostViewedChannelsAllTime).toEqual(expectedMostViewedChannels)
  193. })
  194. it('should properly aggregate views of a channel', async () => {
  195. const expectedChannelViews: EntityViewsInfo = {
  196. id: FIRST_CHANNEL_ID,
  197. views: 2,
  198. }
  199. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID)
  200. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID)
  201. const expectedMostViewedChannels = {
  202. edges: [expectedChannelViews].map((view) => ({ node: view })),
  203. }
  204. const mostViewedChannels = await getMostViewedChannels(30)
  205. const mostViewedChannelsAllTime = await getMostViewedChannels(null)
  206. expect(mostViewedChannels).toEqual(expectedMostViewedChannels)
  207. expect(mostViewedChannelsAllTime).toEqual(expectedMostViewedChannels)
  208. })
  209. it('should properly aggregate views of a category', async () => {
  210. const expectedChannelViews: EntityViewsInfo = {
  211. id: FIRST_CATEGORY_ID,
  212. views: 2,
  213. }
  214. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  215. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  216. const mostViewedCategories = await getMostViewedCategories(30)
  217. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  218. expect(mostViewedCategories).toEqual([expectedChannelViews])
  219. expect(mostViewedCategoriesAllTime).toEqual([expectedChannelViews])
  220. })
  221. it('should properly rebuild the aggregate', async () => {
  222. const expectedFirstVideoViews: EntityViewsInfo = {
  223. id: FIRST_VIDEO_ID,
  224. views: 3,
  225. }
  226. const expectedSecondVideoViews: EntityViewsInfo = {
  227. id: SECOND_VIDEO_ID,
  228. views: 4,
  229. }
  230. const expectedChannelViews: EntityViewsInfo = {
  231. id: FIRST_CHANNEL_ID,
  232. views: 7,
  233. }
  234. const expectedCategoryViews: EntityViewsInfo = {
  235. id: FIRST_CATEGORY_ID,
  236. views: 7,
  237. }
  238. const expectedMostViewedVideos = {
  239. edges: [expectedFirstVideoViews, expectedSecondVideoViews].map((view) => ({ node: view })),
  240. }
  241. const expectedMostViewedChannels = {
  242. edges: [expectedChannelViews].map((view) => ({ node: view })),
  243. }
  244. const checkViews = async () => {
  245. const mostViewedVideos = await getMostViewedVideos(30)
  246. const mostViewedVideosAllTime = await getMostViewedVideos(null)
  247. const mostViewedChannels = await getMostViewedChannels(30)
  248. const mostViewedChannelsAllTime = await getMostViewedChannels(null)
  249. const mostViewedCategories = await getMostViewedCategories(30)
  250. const mostViewedCategoriesAllTime = await getMostViewedCategoriesAllTime(10)
  251. expect(mostViewedVideos).toEqual(expectedMostViewedVideos)
  252. expect(mostViewedVideosAllTime).toEqual(expectedMostViewedVideos)
  253. expect(mostViewedChannels).toEqual(expectedMostViewedChannels)
  254. expect(mostViewedChannelsAllTime).toEqual(expectedMostViewedChannels)
  255. expect(mostViewedCategories).toEqual([expectedCategoryViews])
  256. expect(mostViewedCategoriesAllTime).toEqual([expectedCategoryViews])
  257. }
  258. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  259. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  260. await addVideoView(FIRST_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  261. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  262. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  263. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  264. await addVideoView(SECOND_VIDEO_ID, FIRST_CHANNEL_ID, FIRST_CATEGORY_ID)
  265. await checkViews()
  266. await server.stop()
  267. aggregates = await buildAggregates()
  268. server = await createServer(mongoose, aggregates, process.env.ORION_QUERY_NODE_URL!)
  269. query = createQueryFn(server)
  270. mutate = createMutationFn(server)
  271. await checkViews()
  272. })
  273. })