소스 검색

Make tests green

sbkwgh 8 년 전
부모
커밋
f545ec0a15
3개의 변경된 파일18개의 추가작업 그리고 21개의 파일을 삭제
  1. 4 1
      models/thread.js
  2. 5 11
      routes/thread.js
  3. 9 9
      test/thread_post.js

+ 4 - 1
models/thread.js

@@ -17,7 +17,7 @@ module.exports = (sequelize, DataTypes) => {
 				Thread.belongsTo(models.Category)
 				Thread.hasMany(models.Post)
 			},
-			includeOptions () {
+			includeOptions (start, limit) {
 				let models = sequelize.models
 
 				return [
@@ -25,6 +25,9 @@ module.exports = (sequelize, DataTypes) => {
 					models.Category,
 					{ 
 						model: models.Post, 
+						where: { createdAt: { $lt: start } },
+						limit: limit,
+						order: [['createdAt', 'DESC']],
 						include: [
 							{ model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] }, 
 							{

+ 5 - 11
routes/thread.js

@@ -6,25 +6,18 @@ let { User, Thread, Category } = require('../models')
 
 router.get('/:thread_id', async (req, res) => {
 	try {
+		let start = req.query.start || new Date()
 		let limit = +req.query.limit || 10
-		let whereObj = { id: req.params.thread_id }
 
-		if(req.query.start) {
-			whereObj.createdAt = { $lt: req.query.start }
-		}
-
-		let thread = await Thread.findAll({
-			where: whereObj,
-			limit: limit,
-			order: [['createdAt', 'DESC']],
-			include: Thread.includeOptions()
+		let thread = await Thread.findById(req.params.thread_id, {
+			include: Thread.includeOptions(start, limit)
 		})
 
 		if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
 
 		let meta = { limit: limit, next: null }
 
-		if(thread.Posts.length) {
+		if(thread.Posts && thread.Posts.length) {
 			let lastPost = thread.Posts.slice(-1)[0]
 			meta.next = lastPost.createdAt
 		}
@@ -33,6 +26,7 @@ router.get('/:thread_id', async (req, res) => {
 			meta: meta,
 			thread: thread.toJSON()
 		})
+		
 	} catch (e) {
 		if(e.name === 'invalidParameter') {
 			res.status(400)

+ 9 - 9
test/thread_post.js

@@ -345,18 +345,18 @@ describe('Thread and post', () => {
 
 			res.should.have.status(200)
 			res.should.be.json
-			res.body.should.have.property('name', 'thread')
-			res.body.should.have.deep.property('Category.name', 'category_name')
-			res.body.should.have.deep.property('User.username', 'username')
-			res.body.should.have.property('Posts')
+			res.body.thread.should.have.property('name', 'thread')
+			res.body.thread.should.have.deep.property('Category.name', 'category_name')
+			res.body.thread.should.have.deep.property('User.username', 'username')
+			res.body.thread.should.have.property('Posts')
 			
-			res.body.Posts.should.have.property('length', 2)
+			res.body.thread.Posts.should.have.property('length', 2)
 
-			res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
-			res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
+			res.body.thread.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
+			res.body.thread.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
 			
-			res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
-			res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
+			res.body.thread.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
+			res.body.thread.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
 		})
 		it('should return an error if :id is invalid', async () => {
 			try {