From aac7f6540bee4e1cf04a9785696a3c89345fddd2 Mon Sep 17 00:00:00 2001 From: Nicolas Pirotte <=> Date: Thu, 12 May 2016 11:31:05 +0200 Subject: [PATCH] Some refactoring --- .gitignore | 2 + .npmrc | 9 --- example.js | 6 +- package.json | 3 +- test/index.js | 172 +++++++++++++++++++++++++++++++++++++++++-------- test/rest.js | 148 ------------------------------------------ test/server.js | 26 ++++++++ 7 files changed, 177 insertions(+), 189 deletions(-) delete mode 100644 .npmrc delete mode 100644 test/rest.js create mode 100644 test/server.js diff --git a/.gitignore b/.gitignore index 93f1361..a7944ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules npm-debug.log + +.vscode \ No newline at end of file diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 12f9488..0000000 --- a/.npmrc +++ /dev/null @@ -1,9 +0,0 @@ -# registry = http://192.168.99.100:32768/content/repositories/npm-private/ - -# init.author.name = Jane Doe -# init.author.email = jane@example.com -# init.author.url = http://blog.example.com -# an email is required to publish npm packages -# email = jane@example.com -# always-auth = true -# _auth = YWRtaW46YWRtaW4xMjM= \ No newline at end of file diff --git a/example.js b/example.js index 0c85914..593d52b 100644 --- a/example.js +++ b/example.js @@ -1,13 +1,13 @@ const express = require('express') const expressRestResource = require('./index.js') -const nedb = require('nedb') +const Nedb = require('nedb') const PORT = 8080 const app = express() -const users = new nedb({ +const users = new Nedb({ timestampData: true -}) // eslint-disable-line +}) let count = 0 diff --git a/package.json b/package.json index 2dd6987..a337c23 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,6 @@ "mocha": "^2.4.5", "nedb": "^1.8.0", "supertest": "^1.2.0" - } + }, + "engines" : { "node" : ">=6.0" } } diff --git a/test/index.js b/test/index.js index 9f64248..7e9658e 100644 --- a/test/index.js +++ b/test/index.js @@ -1,32 +1,148 @@ -const express = require('express') -const expressRestResource = require('../index.js') -const nedb = require('nedb') - -const PORT = 8080 - -const app = express() -const users = new nedb({ - timestampData: true -}) // eslint-disable-line - -let count = 0 - -const config = { - db: users, - beforeInsert: function (data) { - data.count = ++count - return data - }, - beforeSend: function (data) { - data.id = data._id - return data - } +/* global describe it */ + +const request = require('supertest') +const app = require('./server.js') + +/* ==== Utilities ==== */ + +/** + * Populate the database with a given number of users + * @param {Function} cb - the callback to execute at the end. Pass the last created user as parametter. + * @param {number} wanted - the number of users to create. Default to 1. + */ +function createUsers (cb, wanted = 1) { + request(app) + .post('/api/person/') + .send({'name': 'Nicolas Pirotte'}) + .end((err, res) => { + if (err) throw err + + if (--wanted > 0) { + createUsers(cb, wanted) + } else { + cb(res.body) + } + }) } -app.use('/api/person', expressRestResource(config)) +describe('Routing', () => { + describe('List', () => { + it('Should respond an empty array', (done) => { + request(app) + .get('/api/person/') + .expect('Content-Type', /json/) + .expect(200) + .expect((res) => { + return res.body === [] + }) + .end(function (err, res) { + if (err) throw err + done() + }) + }) + }) + + describe('Post', () => { + it('Should add a person with id, createdOn and modifiedOn attributes', (done) => { + request(app) + .post('/api/person') + .send({'name': 'Nicolas Pirotte'}) + .expect(201) + .expect((res) => { + return res.body.name === 'Nicolas Pirotte' + }) + .expect((res) => { + return /[a-zA-Z0-9]+/.test(res.body.id) + }) + .expect((res) => { + const now = new Date() + return res.body.createdOn === now && res.body.modifiedOn === now + }) + .end(done) + }) + + it('Should record the created element in the database', (done) => { + const wantedNumberOfUsers = 2 + createUsers((user) => { + request(app) + .get('/api/person/') + .expect('Content-Type', /json/) + .expect((res) => { + return res.body.length === wantedNumberOfUsers + }) + .end(function (err, res) { + if (err) throw err + done() + }) + }, wantedNumberOfUsers) + }) + }) + + describe('Put', () => { + it('Should update a record', (done) => { + createUsers((user) => { + const id = user.id + request(app) + .put('/api/person/' + id) + .send({name: 'Benjamin'}) + .expect(204) + .end(function (err, res) { + if (err) throw err + + request(app) + .get('/api/person/') + .expect('Content-Type', /json/) + .expect((res) => { + return res.body[0].name === 'Benjamin' + }) + .expect((res) => { + return res.body[0].updatedOn === new Date() + }) + .end(function (err, res) { + if (err) throw err + done() + }) + }) + }) + }) + }) + + describe('Get', () => { + it('Should find a user by his id', (done) => { + createUsers((user) => { + const id = user.id + request(app) + .get('/api/person/' + id) + .expect(200) + .expect((res) => { + return res.body.d === id + }) + .end(done) + }, 2) + }) + }) -/* app.listen(PORT, () => { - console.log(`Backend server listening to port ${PORT}`) -}) */ + describe('Delete', () => { + it('Should delete a user by his id', (done) => { + createUsers((user) => { + const id = user.id + request(app) + .delete('/api/person/' + id) + .expect(204) + .end((err, res) => { + if (err) throw err -module.exports = app + request(app) + .get('/api/person') + .expect((res) => { + return res.body.length === 1 + }) + .end((err, res) => { + if (err) throw err + done() + }) + }) + }, 2) + }) + }) +}) diff --git a/test/rest.js b/test/rest.js deleted file mode 100644 index 4a7cfc6..0000000 --- a/test/rest.js +++ /dev/null @@ -1,148 +0,0 @@ -/* global describe it */ - -const request = require('supertest') -const app = require('./index.js') - -/* ==== Utilities ==== */ - -/** - * Populate the database with a given number of users - * @param {Function} cb - the callback to execute at the end. Pass the last created user as parametter. - * @param {number} wanted - the number of users to create. Default to 1. - */ -function createUsers (cb, wanted = 1) { - request(app) - .post('/api/person/') - .send({'name': 'Nicolas Pirotte'}) - .end((err, res) => { - if (err) throw err - - if (--wanted > 0) { - createUsers(cb, wanted) - } else { - cb(res.body) - } - }) -} - -describe('Routing', () => { - describe('List', () => { - it('Should respond an empty array', (done) => { - request(app) - .get('/api/person/') - .expect('Content-Type', /json/) - .expect(200) - .expect((res) => { - return res.body === [] - }) - .end(function (err, res) { - if (err) throw err - done() - }) - }) - }) - - describe('Post', () => { - it('Should add a person with id, createdOn and modifiedOn attributes', (done) => { - request(app) - .post('/api/person') - .send({'name': 'Nicolas Pirotte'}) - .expect(201) - .expect((res) => { - return res.body.name === 'Nicolas Pirotte' - }) - .expect((res) => { - return /[a-zA-Z0-9]+/.test(res.body.id) - }) - .expect((res) => { - const now = new Date() - return res.body.createdOn === now && res.body.modifiedOn === now - }) - .end(done) - }) - - it('Should record the created element in the database', (done) => { - const wantedNumberOfUsers = 2 - createUsers((user) => { - request(app) - .get('/api/person/') - .expect('Content-Type', /json/) - .expect((res) => { - return res.body.length === wantedNumberOfUsers - }) - .end(function (err, res) { - if (err) throw err - done() - }) - }, wantedNumberOfUsers) - }) - }) - - describe('Put', () => { - it('Should update a record', (done) => { - createUsers((user) => { - const id = user.id - request(app) - .put('/api/person/' + id) - .send({name: 'Benjamin'}) - .expect(204) - .end(function (err, res) { - if (err) throw err - - request(app) - .get('/api/person/') - .expect('Content-Type', /json/) - .expect((res) => { - return res.body[0].name === 'Benjamin' - }) - .expect((res) => { - return res.body[0].updatedOn === new Date() - }) - .end(function (err, res) { - if (err) throw err - done() - }) - }) - }) - }) - }) - - describe('Get', () => { - it('Should find a user by his id', (done) => { - createUsers((user) => { - const id = user.id - request(app) - .get('/api/person/' + id) - .expect(200) - .expect((res) => { - return res.body.d === id - }) - .end(done) - }, 2) - }) - }) - - describe('Delete', () => { - it('Should delete a user by his id', (done) => { - createUsers((user) => { - const id = user.id - request(app) - .delete('/api/person/' + id) - .expect(204) - .end((err, res) => { - if (err) throw err - - request(app) - .get('/api/person') - .expect((res) => { - return res.body.length === 1 - }) - .end((err, res) => { - if (err) throw err - done() - }) - }) - }, 2) - }) - }) -}) diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..bc6021d --- /dev/null +++ b/test/server.js @@ -0,0 +1,26 @@ +const express = require('express') +const expressRestResource = require('../index.js') +const Nedb = require('nedb') + +const app = express() +const users = new Nedb({ + timestampData: true +}) + +let count = 0 + +const config = { + db: users, + beforeInsert: function (data) { + data.count = ++count + return data + }, + beforeSend: function (data) { + data.id = data._id + return data + } +} + +app.use('/api/person', expressRestResource(config)) + +module.exports = app