浏览代码

Add and implement test for top 3 posts route

sbkwgh 8 年之前
父节点
当前提交
43bdcc80ea
共有 2 个文件被更改,包括 123 次插入5 次删除
  1. 51 1
      routes/log.js
  2. 72 4
      test/log.js

+ 51 - 1
routes/log.js

@@ -61,10 +61,60 @@ router.post('/', async (req, res) => {
 
 			res.status(500)
 			res.json({
-				errors: Errors.unknown
+				errors: [Errors.unknown]
 			})
 		}
 	}
 })
 
+router.get('/top-threads', async (req, res) => {
+	try {
+		let logs = await Log.findAll({
+			where: {
+				createdAt: {
+					$gt: new Date(Date.now() - 1000*60*60*24)
+				},
+				route: 'thread'
+			},
+			include: [Thread]
+		})
+
+		//Sum each log for a thread
+		let pageViewsObj = logs.reduce((obj, log) => {
+			if(!obj[log.Thread.id]) {
+				obj[log.Thread.id] = { Thread: log.Thread, pageViews: 1 }
+			} else {
+				obj[log.Thread.id].pageViews++
+			}
+
+			return obj
+		}, {})
+
+		//Transform to array
+		let pageViewsArr = Object.keys(pageViewsObj).map(id => {
+			return pageViewsObj[id]
+		})
+
+		//Sort by number of page views descending
+		let sortedPageViewsArr = pageViewsArr.sort((a, b) => {
+			if(a.pageViews < b.pageViews) {
+				return 1
+			} else if (a.pageViews > b.pageViews) {
+				return -1
+			} else {
+				return 0
+			}
+		})
+
+		//Return top 3
+		res.json(sortedPageViewsArr.slice(0, 3))
+
+	} catch (e) {
+		res.status(500)
+		res.json({
+			errors: [Errors.unknown]
+		})
+	}
+})
+
 module.exports = router

+ 72 - 4
test/log.js

@@ -54,10 +54,25 @@ describe('Log', () => {
 					.set('content-type', 'application/json')
 					.send({ category: 'CATEGORY', name: 'thread' })
 
-					await admin
-						.post('/api/v1/post')
-						.set('content-type', 'application/json')
-						.send({ threadId: 1, content: 'post' })
+				await admin
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ category: 'CATEGORY', name: 'thread2' })
+
+				await admin
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ category: 'CATEGORY', name: 'thread3' })
+
+				await admin
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ category: 'CATEGORY', name: 'thread4' })
+
+				await admin
+					.post('/api/v1/post')
+					.set('content-type', 'application/json')
+					.send({ threadId: 1, content: 'post' })
 
 				return true
 			} catch (e) {
@@ -197,5 +212,58 @@ describe('Log', () => {
 
 	})
 
+	describe('GET /top-threads', () => {
+		before(async () => {
+			try {
+				let requests = []
+
+				for(let i = 0; i < 5; i++) {
+					requests.push(user
+						.post('/api/v1/log')
+						.set('content-type', 'application/json')
+						.send({ route: 'thread', resourceId: 1 }))
+				}
+
+				for(let i = 0; i < 3; i++) {
+					requests.push(user
+						.post('/api/v1/log')
+						.set('content-type', 'application/json')
+						.send({ route: 'thread', resourceId: 2 }))
+				}
+
+				for(let i = 0; i < 7; i++) {
+					requests.push(user
+						.post('/api/v1/log')
+						.set('content-type', 'application/json')
+						.send({ route: 'thread', resourceId: 3 }))
+				}
+
+				requests.push(user
+					.post('/api/v1/log')
+					.set('content-type', 'application/json')
+					.send({ route: 'thread', resourceId: 4 }))
+
+				await Promise.all(requests)
+
+				return true
+			} catch (e) {
+				return e
+			}
+		})
+
+		it('should return top 3 threads', async () => {
+			let res = await user.get('/api/v1/log/top-threads')
+
+			res.body[0].should.have.deep.property('Thread.name', 'thread3')
+			res.body[1].should.have.deep.property('Thread.name', 'thread')
+			res.body[2].should.have.deep.property('Thread.name', 'thread2')
+
+			res.body[0].should.have.property('pageViews', 7)
+			//6 because there was a previous log to the thread in previous test
+			res.body[1].should.have.property('pageViews', 6)
+			res.body[2].should.have.property('pageViews', 3)
+		})
+	})
+
 	after(() => sequelize.sync({ force: true }))
 })