-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Pirotte
committed
May 12, 2016
1 parent
6ab17a7
commit aac7f65
Showing
7 changed files
with
177 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
npm-debug.log | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ | |
"mocha": "^2.4.5", | ||
"nedb": "^1.8.0", | ||
"supertest": "^1.2.0" | ||
} | ||
}, | ||
"engines" : { "node" : ">=6.0" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |