Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:cloudhead/cradle
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Mar 5, 2012
2 parents d85b6f0 + ae85a88 commit 5d40aef
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 137 deletions.
24 changes: 9 additions & 15 deletions test/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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+/);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) }
Expand All @@ -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) }
Expand All @@ -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);
Expand All @@ -176,4 +170,4 @@ vows.describe('cradle/connection').addBatch({
}
}
}
}).export(module);
}).export(module);
84 changes: 30 additions & 54 deletions test/database-attachment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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": {
Expand All @@ -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/);
Expand All @@ -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/);
Expand All @@ -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": {
Expand All @@ -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-/);
Expand All @@ -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');
},
Expand All @@ -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',
Expand All @@ -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');
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -340,5 +316,5 @@ vows.describe('cradle/database/attachments').addBatch({
}
}
}
}
}).export(module);
})
).export(module);
27 changes: 9 additions & 18 deletions test/database-cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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/);
},
Expand All @@ -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/);
},
Expand Down Expand Up @@ -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/);
},
Expand Down Expand Up @@ -128,5 +119,5 @@ vows.describe('cradle/database/cache').addBatch({
}
}
}
}
}).export(module);
})
).export(module);
Loading

0 comments on commit 5d40aef

Please sign in to comment.