From f6a90c41b5f118c902eeb387729291244f2125d5 Mon Sep 17 00:00:00 2001 From: minggas Date: Mon, 17 Jun 2019 23:39:10 -0300 Subject: [PATCH 1/6] fix: add book id variable --- tests/2_functional-tests.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index ea61dce..911f44b 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -10,6 +10,7 @@ var chaiHttp = require('chai-http'); var chai = require('chai'); var assert = chai.assert; var server = require('../server'); +var bookid = 0; chai.use(chaiHttp); @@ -47,6 +48,7 @@ suite('Functional Tests', function() { title: 'This is my first book!' }) .end(function(err, res) { + bookid = res.body._id; assert.equal(res.status, 200); assert.exists(res.body._id); assert.equal(res.body.title, 'This is my first book!'); From 08d983440dedca7c01da75280018e500399d7b4d Mon Sep 17 00:00:00 2001 From: minggas Date: Mon, 17 Jun 2019 23:43:00 -0300 Subject: [PATCH 2/6] fix: test GET array of books --- tests/2_functional-tests.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index 911f44b..ebc29e2 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -76,9 +76,19 @@ suite('Functional Tests', function() { suite('GET /api/books => array of books', function(){ test('Test GET /api/books', function(done){ - //done(); + chai.request(server) + .get('/api/books') + .end(function(err, res){ + assert.equal(res.status, 200); + assert.isArray(res.body, 'response should be an array'); + assert.property(res.body[0], 'commentcount', 'Books in array should contain commentcount'); + assert.property(res.body[0], 'title', 'Books in array should contain title'); + assert.property(res.body[0], '_id', 'Books in array should contain _id'); + assert.equal(res.body[0].title, 'This is my first book!') + assert.equal(res.body[0].commentcount, 0); + done(); + }); }); - }); @@ -86,8 +96,8 @@ suite('Functional Tests', function() { test('Test GET /api/books/[id] with id not in db', function(done){ //done(); - }); - + }); + test('Test GET /api/books/[id] with valid id in db', function(done){ //done(); }); @@ -103,6 +113,6 @@ suite('Functional Tests', function() { }); - }); + }); }); From d285c9cd05ce6eb872661544c813f0bc87405444 Mon Sep 17 00:00:00 2001 From: minggas Date: Mon, 17 Jun 2019 23:44:26 -0300 Subject: [PATCH 3/6] fix: GET to array of books --- routes/api.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/routes/api.js b/routes/api.js index f8b9d56..be35ec2 100644 --- a/routes/api.js +++ b/routes/api.js @@ -23,6 +23,16 @@ module.exports = function (app) { .get(function (req, res){ //response will be array of book objects //json res format: [{"_id": bookid, "title": book_title, "commentcount": num_of_comments },...] + + Book.find({},(err, doc) => { + if(err) console.log(err); + const result = doc.map(book => { + const {_id, title, comments} = book; + const commentcount = comments.length; + return {_id, title, commentcount} + }) + res.status(200).json(result); + }) }) .post(function (req, res){ From 9d472431e8500feef447220964d92e40ba0b6250 Mon Sep 17 00:00:00 2001 From: minggas Date: Tue, 18 Jun 2019 09:38:31 -0300 Subject: [PATCH 4/6] feat: test GET book with invalid id --- tests/2_functional-tests.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index ebc29e2..d1c816d 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -95,8 +95,14 @@ suite('Functional Tests', function() { suite('GET /api/books/[id] => book object with [id]', function(){ test('Test GET /api/books/[id] with id not in db', function(done){ - //done(); + chai.request(server) + .get('/api/books/B4Di6fjyh76f') + .end(function(err, res){ + assert.equal(res.status, 500); + assert.equal(res.text, 'no book exists') + done(); }); + }); test('Test GET /api/books/[id] with valid id in db', function(done){ //done(); From 9d5cd02a99aee7313dc737f838bedaaee2600c19 Mon Sep 17 00:00:00 2001 From: minggas Date: Tue, 18 Jun 2019 09:39:34 -0300 Subject: [PATCH 5/6] feat: test GET book with valid id --- tests/2_functional-tests.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index d1c816d..6a8d997 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -105,9 +105,19 @@ suite('Functional Tests', function() { }); test('Test GET /api/books/[id] with valid id in db', function(done){ - //done(); + chai.request(server) + .get(`/api/books/${bookid}`) + .end(function(err, res){ + assert.equal(res.status, 200); + assert.property(res.body, 'comments', 'Books in array should contain commentcount'); + assert.property(res.body, 'title', 'Books in array should contain title'); + assert.property(res.body, '_id', 'Books in array should contain _id'); + assert.equal(res.body.title, 'This is my first book!') + assert.isArray(res.body.comments); + assert.equal(res.body._id, bookid) + done(); }); - + }); }); From 43c20705d5f5838761ba3316118c0e5bbccd782c Mon Sep 17 00:00:00 2001 From: minggas Date: Tue, 18 Jun 2019 09:41:32 -0300 Subject: [PATCH 6/6] feat: add route GET book by id --- routes/api.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/routes/api.js b/routes/api.js index be35ec2..01f5622 100644 --- a/routes/api.js +++ b/routes/api.js @@ -60,6 +60,15 @@ module.exports = function (app) { .get(function (req, res){ var bookid = req.params.id; //json res format: {"_id": bookid, "title": book_title, "comments": [comment,comment,...]} + Book.findById(bookid, '_id title comments', (err, doc) => { + if(err) { + console.log(err); + }else if(!doc){ + res.status(500).send('no book exists'); + }else{ + res.status(200).json(doc); + } + }) }) .post(function(req, res){