Skip to content

Commit

Permalink
Added tests for MongoDB. Couple other fixes. Upgraded dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Feb 25, 2016
1 parent bf26e00 commit 3c52bf9
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 286 deletions.
2 changes: 1 addition & 1 deletion 1-hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"devDependencies": {
"jshint": "^2.9.1",
"mocha": "^2.4.5",
"supertest": "^1.1.0"
"supertest": "^1.2.0"
},
"engines": {
"node": ">=0.12.7"
Expand Down
6 changes: 3 additions & 3 deletions 2-structured-data/books/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function (model) {
*
* Retrieve a book.
*/
router.get('/:book(\\d+)', function get(req, res, next) {
router.get('/:book', function get(req, res, next) {
model.read(req.params.book, function (err, entity) {
if (err) { return next(err); }
res.json(entity);
Expand All @@ -67,7 +67,7 @@ module.exports = function (model) {
*
* Update a book.
*/
router.put('/:book(\\d+)', function update(req, res, next) {
router.put('/:book', function update(req, res, next) {
model.update(req.params.book, req.body, function (err, entity) {
if (err) { return next(err); }
res.json(entity);
Expand All @@ -79,7 +79,7 @@ module.exports = function (model) {
*
* Delete a book.
*/
router.delete('/:book(\\d+)', function _delete(req, res, next) {
router.delete('/:book', function _delete(req, res, next) {
model.delete(req.params.book, function (err) {
if (err) { return next(err); }
res.status(200).send('OK');
Expand Down
52 changes: 22 additions & 30 deletions 2-structured-data/books/model-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,54 @@
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;


module.exports = function(config) {
module.exports = function (config) {

var url = config.mongodb.url;
var collectionName = config.mongodb.collection;
var collection;


// [START translate]
function fromMongo(item) {
if (item.length) { item = item.pop(); }
if (Array.isArray(item) && item.length) {
item = item[0];
}
item.id = item._id;
delete item._id;
return item;
}


function toMongo(item) {
delete item.id;
return item;
}
// [END translate]


function getCollection(cb) {
if (collection) {
setImmediate(function() { cb(null, collection); });
setImmediate(function () { cb(null, collection); });
return;
}
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function (err, db) {
if (err) {
console.log(err);
return cb(err);
}
collection = db.collection(collectionName);
cb(null, collection);
});
}


// [START list]
function list(limit, token, cb) {
token = token ? parseInt(token, 10) : 0;
getCollection(function(err, collection) {
if (isNaN(token)) {
return cb(new Error('invalid token'));
}
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.find({})
.skip(token)
.limit(limit)
.toArray(function(err, results) {
.toArray(function (err, results) {
if (err) { return cb(err); }
var hasMore =
results.length === limit ? token + results.length : false;
Expand All @@ -74,12 +73,11 @@ module.exports = function(config) {
}
// [END list]


// [START create]
function create(data, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.insert(data, {w: 1}, function(err, result) {
collection.insert(data, {w: 1}, function (err, result) {
if (err) { return cb(err); }
var item = fromMongo(result.ops);
cb(null, item);
Expand All @@ -88,13 +86,12 @@ module.exports = function(config) {
}
// [END create]


function read(id, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.findOne({
_id: new ObjectID(id)
}, function(err, result) {
}, function (err, result) {
if (err) { return cb(err); }
if (!result) {
return cb({
Expand All @@ -107,18 +104,15 @@ module.exports = function(config) {
});
}


// [START update]
function update(id, data, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.update({
_id: new ObjectID(id)
}, {
'$set': toMongo(data)
},
{w: 1},
function(err) {
collection.update(
{ _id: new ObjectID(id) },
{ '$set': toMongo(data) },
{ w: 1 },
function (err) {
if (err) { return cb(err); }
return read(id, cb);
}
Expand All @@ -127,9 +121,8 @@ module.exports = function(config) {
}
// [END update]


function _delete(id, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.remove({
_id: new ObjectID(id)
Expand All @@ -144,5 +137,4 @@ module.exports = function(config) {
delete: _delete,
list: list
};

};
12 changes: 6 additions & 6 deletions 2-structured-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
],
"license": "Apache Version 2.0",
"dependencies": {
"body-parser": "^1.14.2",
"body-parser": "^1.15.0",
"express": "^4.13.4",
"gcloud": "^0.27.0",
"gcloud": "^0.28.0",
"jade": "^1.11.0",
"kerberos": "^0.0.18",
"lodash": "^4.2.1",
"mongodb": "^2.1.4",
"lodash": "^4.5.1",
"mongodb": "^2.1.7",
"mysql": "^2.10.2",
"prompt": "^0.2.14"
"prompt": "^1.0.0"
},
"devDependencies": {
"jshint": "^2.9.1",
"mocha": "^2.4.5",
"supertest": "^1.1.0"
"supertest": "^1.2.0"
},
"engines": {
"node": ">=0.12.7"
Expand Down
17 changes: 6 additions & 11 deletions 2-structured-data/test/crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,10 @@ describe('crud.js', function () {
});

it('should handle error', function (done) {
var expected = 'Bad Request';
if (process.version.indexOf('v0.10') !== -1) {
expected = 'Error during request.';
}
if (require('../config')().dataBackend === 'cloudsql') {
expected = 'ER_SP_UNDECLARED_VAR: Undeclared variable: NaN';
}
request(proxyquire('../app', stubs))
.get('/books')
.query({ pageToken: 'badrequest' })
.expect(500)
.expect(function (response) {
assert.ok(response.text.indexOf(expected) !== -1);
})
.end(done);
});

Expand All @@ -88,7 +78,12 @@ describe('crud.js', function () {
.expect(302)
.expect(function (response) {
var location = response.headers.location;
id = parseInt(location.replace('/books/', ''), 10);
var idPart = location.replace('/books/', '');
if (require('../config')().dataBackend !== 'mongodb') {
id = parseInt(idPart, 10);
} else {
id = idPart;
}
assert.ok(response.text.indexOf('Redirecting to /books/') !== -1);
})
.end(done);
Expand Down
6 changes: 3 additions & 3 deletions 3-binary-data/books/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function (model) {
*
* Retrieve a book.
*/
router.get('/:book(\\d+)', function get(req, res, next) {
router.get('/:book', function get(req, res, next) {
model.read(req.params.book, function (err, entity) {
if (err) { return next(err); }
res.json(entity);
Expand All @@ -67,7 +67,7 @@ module.exports = function (model) {
*
* Update a book.
*/
router.put('/:book(\\d+)', function update(req, res, next) {
router.put('/:book', function update(req, res, next) {
model.update(req.params.book, req.body, function (err, entity) {
if (err) { return next(err); }
res.json(entity);
Expand All @@ -79,7 +79,7 @@ module.exports = function (model) {
*
* Delete a book.
*/
router.delete('/:book(\\d+)', function _delete(req, res, next) {
router.delete('/:book', function _delete(req, res, next) {
model.delete(req.params.book, function (err) {
if (err) { return next(err); }
res.status(200).send('OK');
Expand Down
51 changes: 22 additions & 29 deletions 3-binary-data/books/model-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,32 @@
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;


module.exports = function(config) {
module.exports = function (config) {

var url = config.mongodb.url;
var collectionName = config.mongodb.collection;
var collection;


function fromMongo(item) {
if (item.length) { item = item.pop(); }
if (Array.isArray(item) && item.length) {
item = item[0];
}
item.id = item._id;
delete item._id;
return item;
}


function toMongo(item) {
delete item.id;
return item;
}


function getCollection(cb) {
if (collection) {
setImmediate(function() { cb(null, collection); });
setImmediate(function () { cb(null, collection); });
return;
}
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function (err, db) {
if (err) {
console.log(err);
return cb(err);
Expand All @@ -53,15 +51,17 @@ module.exports = function(config) {
});
}


function list(limit, token, cb) {
token = token ? parseInt(token, 10) : 0;
getCollection(function(err, collection) {
if (isNaN(token)) {
return cb(new Error('invalid token'));
}
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.find({})
.skip(token)
.limit(limit)
.toArray(function(err, results) {
.toArray(function (err, results) {
if (err) { return cb(err); }
var hasMore =
results.length === limit ? token + results.length : false;
Expand All @@ -70,25 +70,23 @@ module.exports = function(config) {
});
}


function create(data, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.insert(data, {w: 1}, function(err, result) {
collection.insert(data, {w: 1}, function (err, result) {
if (err) { return cb(err); }
var item = fromMongo(result.ops);
cb(null, item);
});
});
}


function read(id, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.findOne({
_id: new ObjectID(id)
}, function(err, result) {
}, function (err, result) {
if (err) { return cb(err); }
if (!result) {
return cb({
Expand All @@ -101,27 +99,23 @@ module.exports = function(config) {
});
}


function update(id, data, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.update({
_id: new ObjectID(id)
}, {
'$set': toMongo(data)
},
{w: 1},
function(err) {
collection.update(
{ _id: new ObjectID(id) },
{ '$set': toMongo(data) },
{ w: 1 },
function (err) {
if (err) { return cb(err); }
return read(id, cb);
}
);
});
}


function _delete(id, cb) {
getCollection(function(err, collection) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.remove({
_id: new ObjectID(id)
Expand All @@ -136,5 +130,4 @@ module.exports = function(config) {
delete: _delete,
list: list
};

};
Loading

0 comments on commit 3c52bf9

Please sign in to comment.