Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Pirotte committed May 12, 2016
1 parent 6ab17a7 commit aac7f65
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 189 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log

.vscode
9 changes: 0 additions & 9 deletions .npmrc

This file was deleted.

6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"mocha": "^2.4.5",
"nedb": "^1.8.0",
"supertest": "^1.2.0"
}
},
"engines" : { "node" : ">=6.0" }
}
172 changes: 144 additions & 28 deletions test/index.js
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)
})
})
})
148 changes: 0 additions & 148 deletions test/rest.js

This file was deleted.

26 changes: 26 additions & 0 deletions test/server.js
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

0 comments on commit aac7f65

Please sign in to comment.