From 88f6e8c21eac27206e1b0d1985cec038bb35dda4 Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 21 Feb 2012 22:49:10 -0500 Subject: [PATCH] [test] Added full-suite tests for databases with `/` in the name. --- test/connection-test.js | 25 +++- test/database-test.js | 279 ++++++++++++++++++----------------- test/fixtures/databases.json | 24 ++- test/helpers/seed.js | 11 +- 4 files changed, 190 insertions(+), 149 deletions(-) diff --git a/test/connection-test.js b/test/connection-test.js index 7b24dc4..ddd3756 100644 --- a/test/connection-test.js +++ b/test/connection-test.js @@ -141,13 +141,26 @@ vows.describe('cradle/connection').addBatch({ return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}); }, "create()": { - topic: function (c) { - c.database('badgers').create(this.callback); + "with no / in the name": { + topic: function (c) { + c.database('badgers').create(this.callback); + }, + "returns a 201": status(201), + "creates a database": { + topic: function (res, c) { c.database('badgers').exists(this.callback) }, + "it exists": function (res) { assert.ok(res) } + } }, - "returns a 201": status(201), - "creates a database": { - topic: function (res, c) { c.database('badgers').exists(this.callback) }, - "it exists": function (res) { assert.ok(res) } + "with a / in the name": { + topic: function (c) { + c.database('madeup/ewoks').create(this.callback); + }, + "returns a 201": status(201), + "creates a database": { + topic: function (res, c) { c.database('madeup/ewoks').exists(this.callback) }, + "it exists": function (res) { assert.ok(res) } + } + } }, "destroy()": { diff --git a/test/database-test.js b/test/database-test.js index 3165f24..262793c 100644 --- a/test/database-test.js +++ b/test/database-test.js @@ -12,169 +12,174 @@ function status(code) { }; } -var cradle = require('../lib/cradle'); +function shouldQueryCouch(name) { + return { + topic: function (c) { return c.database(name) }, -vows.describe('cradle/database').addBatch({ - "Connection": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}); + "info()": { + topic: function (db) { + db.info(this.callback); + }, + "returns a 200": status(200), + "returns database info": function (info) { + assert.equal(info['db_name'], name); + } }, - "database()": { - topic: function (c) { return c.database('pigs') }, - - "info()": { + "fetching a document by id (GET)": { + topic: function (db) { db.get('mike', this.callback) }, + "returns a 200": status(200), + "returns the document": function (res) { + assert.equal(res.id, 'mike'); + }, + "when not found": { + topic: function (_, db) { db.get('tyler', this.callback) }, + "returns a 404": status(404), + "returns the error": function (err, res) { + assert.isObject(err); + assert.isObject(err.headers); + assert.isUndefined(res); + }, + } + }, + "head()": { + topic: function (db) { db.head('mike', this.callback) }, + "returns the headers": function (res) { + assert.match(res.etag, /^"\d-[a-z0-9]+"$/); + } + }, + "save()": { + "with an id & doc": { topic: function (db) { - db.info(this.callback); + db.save('joe', {gender: 'male'}, this.callback); }, - "returns a 200": status(200), - "returns database info": function (info) { - assert.equal(info['db_name'], 'pigs'); + "creates a new document (201)": status(201), + "returns the revision": function (res) { + assert.ok(res.rev); } }, - "fetching a document by id (GET)": { - topic: function (db) { db.get('mike', this.callback) }, - "returns a 200": status(200), - "returns the document": function (res) { - assert.equal(res.id, 'mike'); + "with a doc containing non-ASCII characters": { + topic: function (db) { + db.save('john', {umlauts: 'äöü'}, this.callback); }, - "when not found": { - topic: function (_, db) { db.get('tyler', this.callback) }, - "returns a 404": status(404), - "returns the error": function (err, res) { - assert.isObject(err); - assert.isObject(err.headers); - assert.isUndefined(res); - }, - } - }, - "head()": { - topic: function (db) { db.head('mike', this.callback) }, - "returns the headers": function (res) { - assert.match(res.etag, /^"\d-[a-z0-9]+"$/); - } + "creates a new document (201)": status(201) }, - "save()": { - "with an id & doc": { - topic: function (db) { - db.save('joe', {gender: 'male'}, this.callback); - }, - "creates a new document (201)": status(201), - "returns the revision": function (res) { - assert.ok(res.rev); - } - }, - "with a doc containing non-ASCII characters": { - topic: function (db) { - db.save('john', {umlauts: 'äöü'}, this.callback); - }, - "creates a new document (201)": status(201) - }, - "with a large doc": { - topic: function (db) { - var text = (function (s) { - for (var i = 0; i < 18; i++) { s += s } - return s; - })('blah'); + "with a large doc": { + topic: function (db) { + var text = (function (s) { + for (var i = 0; i < 18; i++) { s += s } + return s; + })('blah'); - db.save('large-bob', { - gender: 'male', - speech: text - }, this.callback); - }, - "creates a new document (201)": status(201) + db.save('large-bob', { + gender: 'male', + speech: text + }, this.callback); }, - "with a '_design' id": { - topic: function (db) { - db.save('_design/horses', { - all: { - map: function (doc) { - if (doc.speed == 72) emit(null, doc); - } - } - }, this.callback); - }, - "creates a doc (201)": status(201), - "returns the revision": function (res) { - assert.ok(res.rev); - }, - "creates a design doc": { - topic: function (res, db) { - db.view('horses/all', this.callback); - }, - "which can be queried": status(200) - } - }, - "without an id (POST)": {}, + "creates a new document (201)": status(201) }, - "calling save() with an array": { + "with a '_design' id": { topic: function (db) { - db.save([{_id: 'tom'}, {_id: 'flint'}], this.callback); + db.save('_design/horses', { + all: { + map: function (doc) { + if (doc.speed == 72) emit(null, doc); + } + } + }, this.callback); }, - "returns an array of document ids and revs": function (res) { - assert.equal(res[0].id, 'tom'); - assert.equal(res[1].id, 'flint'); + "creates a doc (201)": status(201), + "returns the revision": function (res) { + assert.ok(res.rev); }, - "should bulk insert the documents": { + "creates a design doc": { topic: function (res, db) { - var promise = new(events.EventEmitter); - db.get('tom', function (e, tom) { - db.get('flint', function (e, flint) { - promise.emit('success', tom, flint); - }); - }); - return promise; + db.view('horses/all', this.callback); }, - "which can then be retrieved": function (e, tom, flint) { - assert.ok(tom._id); - assert.ok(flint._id); - } + "which can be queried": status(200) } }, - "getting all documents": { - topic: function (db) { - db.all(this.callback); - }, - "returns a 200": status(200), - "returns a list of all docs": function (res) { - assert.isArray(res); - assert.isNumber(res.total_rows); - assert.isNumber(res.offset); - assert.isArray(res.rows); - }, - "which can be iterated upon": function (res) { - assert.isFunction(res.forEach); - } + "without an id (POST)": {}, + }, + "calling save() with an array": { + topic: function (db) { + db.save([{_id: 'tom'}, {_id: 'flint'}], this.callback); }, - "updating a document (PUT)": { - topic: function (db) { - var promise = new(events.EventEmitter); - db.get('mike', function (err, doc) { - db.save('mike', doc.rev, - {color: doc.color, age: 13}, function (err, res) { - if (! err) promise.emit('success', res, db); - else promise.emit('error', res); - }); - }); - return promise; - }, - "returns a 201": status(201), - "returns the revision": function (res) { - assert.ok(res.rev); - assert.match(res.rev, /^2/); - }, + "returns an array of document ids and revs": function (res) { + assert.equal(res[0].id, 'tom'); + assert.equal(res[1].id, 'flint'); }, - "deleting a document (DELETE)": { - topic: function (db) { + "should bulk insert the documents": { + topic: function (res, db) { var promise = new(events.EventEmitter); - db.get('billy', function (e, res) { - db.remove('billy', res.rev, function (e, res) { - promise.emit('success', res); + db.get('tom', function (e, tom) { + db.get('flint', function (e, flint) { + promise.emit('success', tom, flint); }); }); return promise; }, - "returns a 200": status(200) + "which can then be retrieved": function (e, tom, flint) { + assert.ok(tom._id); + assert.ok(flint._id); + } + } + }, + "getting all documents": { + topic: function (db) { + db.all(this.callback); + }, + "returns a 200": status(200), + "returns a list of all docs": function (res) { + assert.isArray(res); + assert.isNumber(res.total_rows); + assert.isNumber(res.offset); + assert.isArray(res.rows); + }, + "which can be iterated upon": function (res) { + assert.isFunction(res.forEach); } + }, + "updating a document (PUT)": { + topic: function (db) { + var promise = new(events.EventEmitter); + db.get('mike', function (err, doc) { + db.save('mike', doc.rev, + {color: doc.color, age: 13}, function (err, res) { + if (! err) promise.emit('success', res, db); + else promise.emit('error', res); + }); + }); + return promise; + }, + "returns a 201": status(201), + "returns the revision": function (res) { + assert.ok(res.rev); + assert.match(res.rev, /^2/); + }, + }, + "deleting a document (DELETE)": { + topic: function (db) { + var promise = new(events.EventEmitter); + db.get('deleteme', function (e, res) { + db.remove('deleteme', res.rev, function (e, res) { + promise.emit('success', res); + }); + }); + return promise; + }, + "returns a 200": status(200) } } +} + +var cradle = require('../lib/cradle'); + +vows.describe('cradle/database').addBatch({ + "Connection": { + topic: function () { + return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}); + }, + "database() with no /": shouldQueryCouch('pigs'), + "database() with /": shouldQueryCouch('animals/snorlax') + } }).export(module); diff --git a/test/fixtures/databases.json b/test/fixtures/databases.json index ab10b68..257d2f2 100644 --- a/test/fixtures/databases.json +++ b/test/fixtures/databases.json @@ -19,7 +19,29 @@ { "_id": "bill", "color": "blue" + }, + { + "_id": "deleteme" } ], - "badgers": null + "badgers": null, + "animals/snorlax": [ + { + "_id": "_design/pigs", + "views": { + "all": { "map": "function (doc) { if (doc.color) emit(doc._id, doc) }" } + } + }, + { + "_id": "mike", + "color": "pink" + }, + { + "_id": "bill", + "color": "blue" + }, + { + "_id": "deleteme" + } + ] } \ No newline at end of file diff --git a/test/helpers/seed.js b/test/helpers/seed.js index 032b830..be6fa63 100644 --- a/test/helpers/seed.js +++ b/test/helpers/seed.js @@ -11,19 +11,20 @@ var seed = exports; seed.createDatabase = function (name, callback) { request({ method: 'PUT', - url: 'http://127.0.0.1:5984/' + name + url: 'http://127.0.0.1:5984/' + encodeURIComponent(name) }, callback); }; seed.deleteDatabase = function (name, callback) { request({ method: 'DELETE', - url: 'http://127.0.0.1:5984/' + name + url: 'http://127.0.0.1:5984/' + encodeURIComponent(name) }, callback); }; seed.seedDatabase = function (name, callback) { - seed.deleteDatabase(name, function (err) { + console.log('Seeding ' + name); + seed.deleteDatabase(name, function (err, res, body) { if (!databases[name]) { return callback(err); } @@ -31,7 +32,7 @@ seed.seedDatabase = function (name, callback) { function putDoc (doc, next) { request({ method: 'PUT', - url: 'http://127.0.0.1:5984/' + name + '/' + doc._id, + url: 'http://127.0.0.1:5984/' + encodeURIComponent(name) + '/' + doc._id, body: JSON.stringify(doc) }, next); } @@ -56,7 +57,7 @@ seed.requireSeed = function () { }; if (!module.parent) { - async.forEach(Object.keys(databases), seed.seedDatabase, function (err) { + async.forEachSeries(Object.keys(databases), seed.seedDatabase, function (err) { return err ? console.log('Error seeding database: ' + err.message) : console.log('Database seed completed.');