Skip to content

Commit a1d3ffa

Browse files
committed
Merge pull request GoogleCloudPlatform#44 from GoogleCloudPlatform/mongo-tests
Added tests for MongoDB. Couple other fixes. Upgraded dependencies.
2 parents bf26e00 + 3c52bf9 commit a1d3ffa

File tree

27 files changed

+272
-286
lines changed

27 files changed

+272
-286
lines changed

1-hello-world/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"devDependencies": {
3535
"jshint": "^2.9.1",
3636
"mocha": "^2.4.5",
37-
"supertest": "^1.1.0"
37+
"supertest": "^1.2.0"
3838
},
3939
"engines": {
4040
"node": ">=0.12.7"

2-structured-data/books/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = function (model) {
5555
*
5656
* Retrieve a book.
5757
*/
58-
router.get('/:book(\\d+)', function get(req, res, next) {
58+
router.get('/:book', function get(req, res, next) {
5959
model.read(req.params.book, function (err, entity) {
6060
if (err) { return next(err); }
6161
res.json(entity);
@@ -67,7 +67,7 @@ module.exports = function (model) {
6767
*
6868
* Update a book.
6969
*/
70-
router.put('/:book(\\d+)', function update(req, res, next) {
70+
router.put('/:book', function update(req, res, next) {
7171
model.update(req.params.book, req.body, function (err, entity) {
7272
if (err) { return next(err); }
7373
res.json(entity);
@@ -79,7 +79,7 @@ module.exports = function (model) {
7979
*
8080
* Delete a book.
8181
*/
82-
router.delete('/:book(\\d+)', function _delete(req, res, next) {
82+
router.delete('/:book', function _delete(req, res, next) {
8383
model.delete(req.params.book, function (err) {
8484
if (err) { return next(err); }
8585
res.status(200).send('OK');

2-structured-data/books/model-mongodb.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,54 @@
1616
var MongoClient = require('mongodb').MongoClient;
1717
var ObjectID = require('mongodb').ObjectID;
1818

19-
20-
module.exports = function(config) {
19+
module.exports = function (config) {
2120

2221
var url = config.mongodb.url;
2322
var collectionName = config.mongodb.collection;
2423
var collection;
2524

26-
2725
// [START translate]
2826
function fromMongo(item) {
29-
if (item.length) { item = item.pop(); }
27+
if (Array.isArray(item) && item.length) {
28+
item = item[0];
29+
}
3030
item.id = item._id;
3131
delete item._id;
3232
return item;
3333
}
3434

35-
3635
function toMongo(item) {
3736
delete item.id;
3837
return item;
3938
}
4039
// [END translate]
4140

42-
4341
function getCollection(cb) {
4442
if (collection) {
45-
setImmediate(function() { cb(null, collection); });
43+
setImmediate(function () { cb(null, collection); });
4644
return;
4745
}
48-
MongoClient.connect(url, function(err, db) {
46+
MongoClient.connect(url, function (err, db) {
4947
if (err) {
50-
console.log(err);
5148
return cb(err);
5249
}
5350
collection = db.collection(collectionName);
5451
cb(null, collection);
5552
});
5653
}
5754

58-
5955
// [START list]
6056
function list(limit, token, cb) {
6157
token = token ? parseInt(token, 10) : 0;
62-
getCollection(function(err, collection) {
58+
if (isNaN(token)) {
59+
return cb(new Error('invalid token'));
60+
}
61+
getCollection(function (err, collection) {
6362
if (err) { return cb(err); }
6463
collection.find({})
6564
.skip(token)
6665
.limit(limit)
67-
.toArray(function(err, results) {
66+
.toArray(function (err, results) {
6867
if (err) { return cb(err); }
6968
var hasMore =
7069
results.length === limit ? token + results.length : false;
@@ -74,12 +73,11 @@ module.exports = function(config) {
7473
}
7574
// [END list]
7675

77-
7876
// [START create]
7977
function create(data, cb) {
80-
getCollection(function(err, collection) {
78+
getCollection(function (err, collection) {
8179
if (err) { return cb(err); }
82-
collection.insert(data, {w: 1}, function(err, result) {
80+
collection.insert(data, {w: 1}, function (err, result) {
8381
if (err) { return cb(err); }
8482
var item = fromMongo(result.ops);
8583
cb(null, item);
@@ -88,13 +86,12 @@ module.exports = function(config) {
8886
}
8987
// [END create]
9088

91-
9289
function read(id, cb) {
93-
getCollection(function(err, collection) {
90+
getCollection(function (err, collection) {
9491
if (err) { return cb(err); }
9592
collection.findOne({
9693
_id: new ObjectID(id)
97-
}, function(err, result) {
94+
}, function (err, result) {
9895
if (err) { return cb(err); }
9996
if (!result) {
10097
return cb({
@@ -107,18 +104,15 @@ module.exports = function(config) {
107104
});
108105
}
109106

110-
111107
// [START update]
112108
function update(id, data, cb) {
113-
getCollection(function(err, collection) {
109+
getCollection(function (err, collection) {
114110
if (err) { return cb(err); }
115-
collection.update({
116-
_id: new ObjectID(id)
117-
}, {
118-
'$set': toMongo(data)
119-
},
120-
{w: 1},
121-
function(err) {
111+
collection.update(
112+
{ _id: new ObjectID(id) },
113+
{ '$set': toMongo(data) },
114+
{ w: 1 },
115+
function (err) {
122116
if (err) { return cb(err); }
123117
return read(id, cb);
124118
}
@@ -127,9 +121,8 @@ module.exports = function(config) {
127121
}
128122
// [END update]
129123

130-
131124
function _delete(id, cb) {
132-
getCollection(function(err, collection) {
125+
getCollection(function (err, collection) {
133126
if (err) { return cb(err); }
134127
collection.remove({
135128
_id: new ObjectID(id)
@@ -144,5 +137,4 @@ module.exports = function(config) {
144137
delete: _delete,
145138
list: list
146139
};
147-
148140
};

2-structured-data/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@
3030
],
3131
"license": "Apache Version 2.0",
3232
"dependencies": {
33-
"body-parser": "^1.14.2",
33+
"body-parser": "^1.15.0",
3434
"express": "^4.13.4",
35-
"gcloud": "^0.27.0",
35+
"gcloud": "^0.28.0",
3636
"jade": "^1.11.0",
3737
"kerberos": "^0.0.18",
38-
"lodash": "^4.2.1",
39-
"mongodb": "^2.1.4",
38+
"lodash": "^4.5.1",
39+
"mongodb": "^2.1.7",
4040
"mysql": "^2.10.2",
41-
"prompt": "^0.2.14"
41+
"prompt": "^1.0.0"
4242
},
4343
"devDependencies": {
4444
"jshint": "^2.9.1",
4545
"mocha": "^2.4.5",
46-
"supertest": "^1.1.0"
46+
"supertest": "^1.2.0"
4747
},
4848
"engines": {
4949
"node": ">=0.12.7"

2-structured-data/test/crud.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,10 @@ describe('crud.js', function () {
4848
});
4949

5050
it('should handle error', function (done) {
51-
var expected = 'Bad Request';
52-
if (process.version.indexOf('v0.10') !== -1) {
53-
expected = 'Error during request.';
54-
}
55-
if (require('../config')().dataBackend === 'cloudsql') {
56-
expected = 'ER_SP_UNDECLARED_VAR: Undeclared variable: NaN';
57-
}
5851
request(proxyquire('../app', stubs))
5952
.get('/books')
6053
.query({ pageToken: 'badrequest' })
6154
.expect(500)
62-
.expect(function (response) {
63-
assert.ok(response.text.indexOf(expected) !== -1);
64-
})
6555
.end(done);
6656
});
6757

@@ -88,7 +78,12 @@ describe('crud.js', function () {
8878
.expect(302)
8979
.expect(function (response) {
9080
var location = response.headers.location;
91-
id = parseInt(location.replace('/books/', ''), 10);
81+
var idPart = location.replace('/books/', '');
82+
if (require('../config')().dataBackend !== 'mongodb') {
83+
id = parseInt(idPart, 10);
84+
} else {
85+
id = idPart;
86+
}
9287
assert.ok(response.text.indexOf('Redirecting to /books/') !== -1);
9388
})
9489
.end(done);

3-binary-data/books/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = function (model) {
5555
*
5656
* Retrieve a book.
5757
*/
58-
router.get('/:book(\\d+)', function get(req, res, next) {
58+
router.get('/:book', function get(req, res, next) {
5959
model.read(req.params.book, function (err, entity) {
6060
if (err) { return next(err); }
6161
res.json(entity);
@@ -67,7 +67,7 @@ module.exports = function (model) {
6767
*
6868
* Update a book.
6969
*/
70-
router.put('/:book(\\d+)', function update(req, res, next) {
70+
router.put('/:book', function update(req, res, next) {
7171
model.update(req.params.book, req.body, function (err, entity) {
7272
if (err) { return next(err); }
7373
res.json(entity);
@@ -79,7 +79,7 @@ module.exports = function (model) {
7979
*
8080
* Delete a book.
8181
*/
82-
router.delete('/:book(\\d+)', function _delete(req, res, next) {
82+
router.delete('/:book', function _delete(req, res, next) {
8383
model.delete(req.params.book, function (err) {
8484
if (err) { return next(err); }
8585
res.status(200).send('OK');

3-binary-data/books/model-mongodb.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,32 @@
1616
var MongoClient = require('mongodb').MongoClient;
1717
var ObjectID = require('mongodb').ObjectID;
1818

19-
20-
module.exports = function(config) {
19+
module.exports = function (config) {
2120

2221
var url = config.mongodb.url;
2322
var collectionName = config.mongodb.collection;
2423
var collection;
2524

26-
2725
function fromMongo(item) {
28-
if (item.length) { item = item.pop(); }
26+
if (Array.isArray(item) && item.length) {
27+
item = item[0];
28+
}
2929
item.id = item._id;
3030
delete item._id;
3131
return item;
3232
}
3333

34-
3534
function toMongo(item) {
3635
delete item.id;
3736
return item;
3837
}
3938

40-
4139
function getCollection(cb) {
4240
if (collection) {
43-
setImmediate(function() { cb(null, collection); });
41+
setImmediate(function () { cb(null, collection); });
4442
return;
4543
}
46-
MongoClient.connect(url, function(err, db) {
44+
MongoClient.connect(url, function (err, db) {
4745
if (err) {
4846
console.log(err);
4947
return cb(err);
@@ -53,15 +51,17 @@ module.exports = function(config) {
5351
});
5452
}
5553

56-
5754
function list(limit, token, cb) {
5855
token = token ? parseInt(token, 10) : 0;
59-
getCollection(function(err, collection) {
56+
if (isNaN(token)) {
57+
return cb(new Error('invalid token'));
58+
}
59+
getCollection(function (err, collection) {
6060
if (err) { return cb(err); }
6161
collection.find({})
6262
.skip(token)
6363
.limit(limit)
64-
.toArray(function(err, results) {
64+
.toArray(function (err, results) {
6565
if (err) { return cb(err); }
6666
var hasMore =
6767
results.length === limit ? token + results.length : false;
@@ -70,25 +70,23 @@ module.exports = function(config) {
7070
});
7171
}
7272

73-
7473
function create(data, cb) {
75-
getCollection(function(err, collection) {
74+
getCollection(function (err, collection) {
7675
if (err) { return cb(err); }
77-
collection.insert(data, {w: 1}, function(err, result) {
76+
collection.insert(data, {w: 1}, function (err, result) {
7877
if (err) { return cb(err); }
7978
var item = fromMongo(result.ops);
8079
cb(null, item);
8180
});
8281
});
8382
}
8483

85-
8684
function read(id, cb) {
87-
getCollection(function(err, collection) {
85+
getCollection(function (err, collection) {
8886
if (err) { return cb(err); }
8987
collection.findOne({
9088
_id: new ObjectID(id)
91-
}, function(err, result) {
89+
}, function (err, result) {
9290
if (err) { return cb(err); }
9391
if (!result) {
9492
return cb({
@@ -101,27 +99,23 @@ module.exports = function(config) {
10199
});
102100
}
103101

104-
105102
function update(id, data, cb) {
106-
getCollection(function(err, collection) {
103+
getCollection(function (err, collection) {
107104
if (err) { return cb(err); }
108-
collection.update({
109-
_id: new ObjectID(id)
110-
}, {
111-
'$set': toMongo(data)
112-
},
113-
{w: 1},
114-
function(err) {
105+
collection.update(
106+
{ _id: new ObjectID(id) },
107+
{ '$set': toMongo(data) },
108+
{ w: 1 },
109+
function (err) {
115110
if (err) { return cb(err); }
116111
return read(id, cb);
117112
}
118113
);
119114
});
120115
}
121116

122-
123117
function _delete(id, cb) {
124-
getCollection(function(err, collection) {
118+
getCollection(function (err, collection) {
125119
if (err) { return cb(err); }
126120
collection.remove({
127121
_id: new ObjectID(id)
@@ -136,5 +130,4 @@ module.exports = function(config) {
136130
delete: _delete,
137131
list: list
138132
};
139-
140133
};

0 commit comments

Comments
 (0)