diff --git a/test/connection-test.js b/test/connection-test.js index ddd3756..2df5416 100644 --- a/test/connection-test.js +++ b/test/connection-test.js @@ -3,14 +3,8 @@ var path = require('path'), events = require('events'), http = require('http'), fs = require('fs'), - vows = require('vows'); - -function status(code) { - return function (e, res) { - assert.ok(res || e); - assert.equal((res || e).headers.status || (res || e).status, code); - }; -} + vows = require('vows'), + macros = require('./helpers/macros'); var cradle = require('../lib/cradle'); @@ -98,7 +92,7 @@ vows.describe('cradle/connection').addBatch({ "getting server info": { topic: function (c) { c.info(this.callback) }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns the version number": function (info) { assert.ok(info); assert.match(info.version, /\d+\.\d+\.\d+/); @@ -108,7 +102,7 @@ vows.describe('cradle/connection').addBatch({ "with count": { topic: function (c) { c.uuids(42, this.callback) }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns an array of UUIDs": function (uuids) { assert.isArray(uuids); assert.lengthOf(uuids, 42); @@ -117,7 +111,7 @@ vows.describe('cradle/connection').addBatch({ "without count": { topic: function (c) { c.uuids(this.callback) }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns an array of UUIDs": function (uuids) { assert.isArray(uuids); assert.lengthOf(uuids, 1); @@ -145,7 +139,7 @@ vows.describe('cradle/connection').addBatch({ topic: function (c) { c.database('badgers').create(this.callback); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "creates a database": { topic: function (res, c) { c.database('badgers').exists(this.callback) }, "it exists": function (res) { assert.ok(res) } @@ -155,7 +149,7 @@ vows.describe('cradle/connection').addBatch({ topic: function (c) { c.database('madeup/ewoks').create(this.callback); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "creates a database": { topic: function (res, c) { c.database('madeup/ewoks').exists(this.callback) }, "it exists": function (res) { assert.ok(res) } @@ -167,7 +161,7 @@ vows.describe('cradle/connection').addBatch({ topic: function (c) { c.database('rabbits').destroy(this.callback); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "destroys a database": { topic: function (res, c) { c.database('rabbits').exists(this.callback); @@ -176,4 +170,4 @@ vows.describe('cradle/connection').addBatch({ } } } -}).export(module); \ No newline at end of file +}).export(module); diff --git a/test/database-attachment-test.js b/test/database-attachment-test.js index 12d7aa2..e5b15d8 100644 --- a/test/database-attachment-test.js +++ b/test/database-attachment-test.js @@ -3,14 +3,8 @@ var path = require('path'), events = require('events'), http = require('http'), fs = require('fs'), - vows = require('vows'); - -function status(code) { - return function (e, res, body) { - assert.ok(res || e); - assert.equal((res || e).headers.status || (res || e).statusCode, code); - }; -} + vows = require('vows'), + macros = require('./helpers/macros'); function mixin(target) { var objs = Array.prototype.slice.call(arguments, 1); @@ -22,11 +16,8 @@ function mixin(target) { var cradle = require('../lib/cradle'); -vows.describe('cradle/database/attachments').addBatch({ - "Database with cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, { cache: true }).database('pigs'); - }, +vows.describe('cradle/database/attachments').addBatch( + macros.database({ cache: true }, { "saveAttachment()": { "updates the cache": { topic: function (db) { @@ -83,15 +74,12 @@ vows.describe('cradle/database/attachments').addBatch({ }, callback); }); }, - "and saves successfully": status(201) + "and saves successfully": macros.status(201) } } - } -}).addBatch({ - "Database with no cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, + }) +).addBatch( + macros.database({ "putting an attachment": { "to an existing document": { "with given data": { @@ -108,7 +96,7 @@ vows.describe('cradle/database/attachments').addBatch({ }, that.callback); }); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); assert.match(res.rev, /^2/); @@ -129,7 +117,7 @@ vows.describe('cradle/database/attachments').addBatch({ fs.createReadStream(__dirname + "/../README.md").pipe(stream); }); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); assert.match(res.rev, /^2/); @@ -152,7 +140,7 @@ vows.describe('cradle/database/attachments').addBatch({ }); }); }, - "returns a 409": status(409) + "returns a 409": macros.status(409) } }, "to a non-existing document": { @@ -163,7 +151,7 @@ vows.describe('cradle/database/attachments').addBatch({ body: 'Foo!' }, this.callback); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); assert.match(res.rev, /^1-/); @@ -187,7 +175,7 @@ vows.describe('cradle/database/attachments').addBatch({ db.getAttachment('attachment-getter', 'foo.txt', that.callback); }); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns the right mime-type in the header": function (err, res, body) { assert.equal(res.headers['content-type'], 'text/plain'); }, @@ -202,15 +190,12 @@ vows.describe('cradle/database/attachments').addBatch({ db.getAttachment('attachment-not-found', 'foo.txt', that.callback); }); }, - "returns a 404": status(404) + "returns a 404": macros.status(404) } } - } -}).addBatch({ - "Database with no cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, + }) +).addBatch( + macros.database({ "saving an attachment with ETag": { topic: function (db) { var id = 'attachment-incorrect-revision', @@ -227,25 +212,22 @@ vows.describe('cradle/database/attachments').addBatch({ }, that.callback); }); }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); assert.match(res.rev, /^3/); } } - } -}).addBatch({ - "Database with no cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, + }) +).addBatch( + macros.database({ "getting an attachment with .pipe()": { "when it exists": { topic: function (db) { var stream = db.getAttachment('piped-attachment', 'foo.txt', this.callback); stream.pipe(fs.createWriteStream(path.join(__dirname, 'fixtures', 'README.md'))); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns the right mime-type in the header": function (err, res, body) { assert.equal(res.headers['content-type'], 'text/plain'); }, @@ -274,12 +256,9 @@ vows.describe('cradle/database/attachments').addBatch({ } } } - } -}).addBatch({ - "Database with no cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, { cache: false }).database('pigs'); - }, + }) +).addBatch( + macros.database({ "removeAttachment()": { "when it exists": { topic: function (db) { @@ -307,12 +286,9 @@ vows.describe('cradle/database/attachments').addBatch({ } } } - } -}).addBatch({ - "Database with cache": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, { cache: true }).database('pigs'); - }, + }) +).addBatch( + macros.database({ cache: true }, { "removeAttachment()": { "when it exists": { topic: function (db) { @@ -340,5 +316,5 @@ vows.describe('cradle/database/attachments').addBatch({ } } } - } -}).export(module); \ No newline at end of file + }) +).export(module); diff --git a/test/database-cache-test.js b/test/database-cache-test.js index f150f6c..73e8f3d 100644 --- a/test/database-cache-test.js +++ b/test/database-cache-test.js @@ -3,22 +3,13 @@ var path = require('path'), events = require('events'), http = require('http'), fs = require('fs'), - vows = require('vows'); - -function status(code) { - return function (e, res) { - assert.ok(res || e); - assert.equal((res || e).headers.status || (res || e).status, code); - }; -} + vows = require('vows'), + macros = require('./helpers/macros'); var cradle = require('../lib/cradle'); -vows.describe('cradle/database/cache').addBatch({ - "A Cradle connection (cache)": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, { cache: true }).database('pigs'); - }, +vows.describe('cradle/database/cache').addBatch( + macros.database({ couch: true }, { "save()": { topic: function (db) { var promise = new(events.EventEmitter); @@ -47,7 +38,7 @@ vows.describe('cradle/database/cache').addBatch({ }); return promise; }, - "return a 201": status(201), + "return a 201": macros.status(201), "allow an overwrite": function (res) { assert.match(res.rev, /^2/); }, @@ -66,7 +57,7 @@ vows.describe('cradle/database/cache').addBatch({ }); return promise; }, - "return a 201": status(201), + "return a 201": macros.status(201), "allow an overwrite": function (res) { assert.match(res.rev, /^1/); }, @@ -95,7 +86,7 @@ vows.describe('cradle/database/cache').addBatch({ }); return promise; }, - "return a 201": status(201), + "return a 201": macros.status(201), "allow an overwrite": function (res) { assert.match(res.rev, /^2/); }, @@ -128,5 +119,5 @@ vows.describe('cradle/database/cache').addBatch({ } } } - } -}).export(module); \ No newline at end of file + }) +).export(module); diff --git a/test/database-test.js b/test/database-test.js index fe48786..64cf135 100644 --- a/test/database-test.js +++ b/test/database-test.js @@ -3,14 +3,8 @@ var path = require('path'), events = require('events'), http = require('http'), fs = require('fs'), - vows = require('vows'); - -function status(code) { - return function (e, res) { - assert.ok(res || e); - assert.equal((res || e).headers.status || (res || e).status, code); - }; -} + vows = require('vows'), + macros = require('./helpers/macros'); function shouldQueryCouch(name) { return { @@ -20,20 +14,20 @@ function shouldQueryCouch(name) { topic: function (db) { db.info(this.callback); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns database info": function (info) { assert.equal(info['db_name'], name); } }, "fetching a document by id (GET)": { topic: function (db) { db.get('mike', this.callback) }, - "returns a 200": status(200), + "returns a 200": macros.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 a 404": macros.status(404), "returns the error": function (err, res) { assert.isObject(err); assert.isObject(err.headers); @@ -52,7 +46,7 @@ function shouldQueryCouch(name) { topic: function (db) { db.save('joe', {gender: 'male'}, this.callback); }, - "creates a new document (201)": status(201), + "creates a new document (201)": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); } @@ -61,7 +55,7 @@ function shouldQueryCouch(name) { topic: function (db) { db.save('john', {umlauts: 'äöü'}, this.callback); }, - "creates a new document (201)": status(201) + "creates a new document (201)": macros.status(201) }, "with a large doc": { topic: function (db) { @@ -75,7 +69,7 @@ function shouldQueryCouch(name) { speech: text }, this.callback); }, - "creates a new document (201)": status(201) + "creates a new document (201)": macros.status(201) }, "with a '_design' id": { topic: function (db) { @@ -87,7 +81,7 @@ function shouldQueryCouch(name) { } }, this.callback); }, - "creates a doc (201)": status(201), + "creates a doc (201)": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); }, @@ -95,7 +89,7 @@ function shouldQueryCouch(name) { topic: function (res, db) { db.view('horses/all', this.callback); }, - "which can be queried": status(200) + "which can be queried": macros.status(200) } }, "without an id (POST)": {}, @@ -129,7 +123,7 @@ function shouldQueryCouch(name) { topic: function (db) { db.all(this.callback); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns a list of all docs": function (res) { assert.isArray(res); assert.isNumber(res.total_rows); @@ -144,7 +138,7 @@ function shouldQueryCouch(name) { topic: function (db) { db.all({ limit: 1 }, this.callback); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns a list of all docs": function (res) { assert.isArray(res); assert.isNumber(res.total_rows); @@ -160,7 +154,7 @@ function shouldQueryCouch(name) { topic: function (db) { db.all({ keys: ['mike'] }, this.callback); }, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns a list of all docs": function (res) { assert.isArray(res); assert.isNumber(res.total_rows); @@ -185,7 +179,7 @@ function shouldQueryCouch(name) { }); return promise; }, - "returns a 201": status(201), + "returns a 201": macros.status(201), "returns the revision": function (res) { assert.ok(res.rev); assert.match(res.rev, /^2/); @@ -201,7 +195,7 @@ function shouldQueryCouch(name) { }); return promise; }, - "returns a 200": status(200) + "returns a 200": macros.status(200) } } } diff --git a/test/database-view-test.js b/test/database-view-test.js index 7dfcd1d..072851d 100644 --- a/test/database-view-test.js +++ b/test/database-view-test.js @@ -3,19 +3,13 @@ var path = require('path'), events = require('events'), http = require('http'), fs = require('fs'), - vows = require('vows'); - -function status(code) { - return function (e, res) { - assert.ok(res || e); - assert.equal((res || e).headers.status || (res || e).status, code); - }; -} + vows = require('vows'), + macros = require('./helpers/macros'); function shouldQueryView(topic, rows, total) { return { topic: topic, - "returns a 200": status(200), + "returns a 200": macros.status(200), "returns view results": function (res) { assert.isArray(res.rows); assert.equal(res.rows.length, rows.length); @@ -35,11 +29,8 @@ function shouldQueryView(topic, rows, total) { var cradle = require('../lib/cradle'); -vows.describe('cradle/database/view').addBatch({ - "Database": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, +vows.describe('cradle/database/view').addBatch( + macros.database({ "querying a view": { "with no options": shouldQueryView( function (db) { @@ -93,18 +84,15 @@ vows.describe('cradle/database/view').addBatch({ topic: function (db) { db.viewCleanup(this.callback); }, - "returns a 202": status(202), + "returns a 202": macros.status(202), "no error is thrown and we get ok response": function (e, res) { assert.ok(!e); assert.ok(res && res.ok && res.ok === true); } } - } -}).addBatch({ - "Database": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, + }) +).addBatch( + macros.database({ "querying a temporary view": { "with a single key": shouldQueryView( function (db) { @@ -118,12 +106,9 @@ vows.describe('cradle/database/view').addBatch({ 3 ) } - } -}).addBatch({ - "Database": { - topic: function () { - return new(cradle.Connection)('127.0.0.1', 5984, {cache: false}).database('pigs'); - }, + }) +).addBatch( + macros.database({ "querying a temporary view": { "with a startKey and endKey": shouldQueryView( function (db) { @@ -137,5 +122,5 @@ vows.describe('cradle/database/view').addBatch({ 3 ) } - } -}).export(module); \ No newline at end of file + }) +).export(module); diff --git a/test/helpers/macros.js b/test/helpers/macros.js new file mode 100644 index 0000000..7cb156d --- /dev/null +++ b/test/helpers/macros.js @@ -0,0 +1,25 @@ +var assert = require('assert'), + cradle = require('../../'); + +var macros = exports; + +macros.database = function (options, tests) { + if (arguments.length === 1) { + tests = options; + options = { cache: false }; + } + + tests.topic = function () { + return new(cradle.Connection)('127.0.0.1', 5984, options).database('pigs'); + }; + return { + 'A `cradle.Connection`': tests + }; +}; + +macros.status = function (code) { + return function (e, res, body) { + assert.ok(res || e); + assert.equal((res || e).headers.status || (res || e).statusCode, code); + }; +};