Skip to content

Commit

Permalink
[test] Added full-suite tests for databases with / in the name.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Feb 22, 2012
1 parent 270b7de commit 88f6e8c
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 149 deletions.
25 changes: 19 additions & 6 deletions test/connection-test.js
Expand Up @@ -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()": {
Expand Down
279 changes: 142 additions & 137 deletions test/database-test.js
Expand Up @@ -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);
24 changes: 23 additions & 1 deletion test/fixtures/databases.json
Expand Up @@ -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"
}
]
}

0 comments on commit 88f6e8c

Please sign in to comment.