Navigation Menu

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

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed Aug 11, 2011
1 parent ecf687b commit 123f8a3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
13 changes: 7 additions & 6 deletions error.js
Expand Up @@ -10,17 +10,18 @@
*
* @return An error augmented an driver specific code
*/
function gen_err(error, code, http_code, type) {
if(typeof error === 'string') { error = new Error(error); }
function gen_err(reason,code,request,http_code,type) {
if(typeof reason === 'string') { error = new Error(reason); }
if(!type) {
type = http_code;
http_code = 500;
}
error.nano_code = code;
error.error = code;
error.http_code = http_code;
error.type = type;
error.namespace = type;
error.request = request;
return error;
}

exports.request_err = function (e,c,h) { return gen_err(e,c,h,"request"); };
exports.couch_err = function (e,c,h) { return gen_err(e,c,h,"couch"); };
exports.request_err = function (e,c,r,h) { return gen_err(e,c,r,h,"request"); };
exports.couch_err = function (e,c,r,h) { return gen_err(e,c,r,h,"couch"); };
33 changes: 28 additions & 5 deletions nano.js
Expand Up @@ -24,6 +24,9 @@ module.exports = exports = nano = function nano_module(cfg) {
* @return Execution of the code in your callback. Hopefully you are handling
*/
function request_db(name,method,callback) {
if(!callback) { // Then ignore callback
callback = function () { return; };
}
var req =
{ uri: cfg.database(name)
, method: method
Expand All @@ -33,13 +36,13 @@ module.exports = exports = nano = function nano_module(cfg) {
var status_code = h.statusCode
, parsed;
if(e) {
callback(error.request_err(e, "connect_db",status_code));
callback(error.request_err(e,"connect_db",req,status_code));
return;
}
parsed = JSON.parse(b);
if (status_code === 200 || status_code === 201) { callback(null,parsed); }
else { // Proxy the error
callback(error.couch_err(parsed.reason,parsed.error,status_code));
callback(error.couch_err(parsed.reason,parsed.error,req,status_code));
}
});
}
Expand All @@ -61,18 +64,38 @@ module.exports = exports = nano = function nano_module(cfg) {
* @see request_db
*/
function create_db(name, callback) {
request_db(name, "PUT", callback);
request_db(name,"PUT",callback);
}

/*
* Annihilates a CouchDB Database
*
* e.g. nano.db.delete(db_name);
*
* Even though this looks sync it is an async function
* and therefor order is not guaranteed
*
* @see request_db
*/
function destroy_db(name, callback) {
request_db(name, "DELETE", callback);
request_db(name,"DELETE",callback);
}

/*
* Gets information about a CouchDB Database
*
* e.g. nano.db.get(db_name, function(e,b) {
* console.log(b);
* });
*
* @see request_db
*/
function get_db(name, callback) {
request_db(name,"GET",callback);
}

public_functions = { db: { create: create_db
//, get: get_db
, get: get_db
, destroy: destroy_db
}
//, doc: { create: create_doc
Expand Down
38 changes: 7 additions & 31 deletions tests/db/create.js
@@ -1,17 +1,16 @@
var vows = require('/usr/lib/node_modules/vows/lib/vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)
, db_name = "foo";
, nano = require('../../nano')(cfg);

/*****************************************************************************
* create_db *
*****************************************************************************/
function create_db (callback) {
nano.db.destroy(db_name, function () {
nano.db.create(db_name, function (e,b) {
nano.db.destroy("cr1", function () {
nano.db.create("cr1", function (e,b) {
callback(e,b);
return;
nano.db.destroy("cr1");
});
});
}
Expand All @@ -21,35 +20,15 @@ function create_db_ok(e,b) {
assert.equal(b.ok, true);
}

/*****************************************************************************
* create_db_recursive_error *
*****************************************************************************/
function create_db_recursive_error(callback) {
nano.db.destroy(db_name, function () {
nano.db.create(db_name, function (e,b) {
if(e) {
callback(e);
}
else {
create_db_recursive_error(name,callback);
}
});
});
}

function create_db_recursive_error_ok(e,tried) {
assert.equal(e.nano_code,"file_exists");
}

/*****************************************************************************
* recursive_retries_create_db *
*****************************************************************************/
function recursive_retries_create_db(tried,callback) {
nano.db.destroy(db_name, function () {
nano.db.create(db_name, function (e,b) {
nano.db.destroy("cr2", function () {
nano.db.create("cr2", function (e,b) {
if(tried.tried === tried.max_retries) {
callback(true);
return;
nano.db.destroy("cr2");
}
else {
tried.tried += 1;
Expand All @@ -67,9 +46,6 @@ vows.describe('nano.db.create').addBatch({
"create_db": {
topic: function () { create_db(this.callback); }
, "=": create_db_ok },
"create_db_recursive_error": {
topic: function () { create_db_recursive_error(this.callback); }
, "=": create_db_recursive_error_ok },
"recursive_retries_create_db": {
topic: function () { recursive_retries_create_db({tried:0, max_retries:5},this.callback); }
, "=": recursive_retries_create_db_ok }
Expand Down
12 changes: 7 additions & 5 deletions tests/db/destroy.js
Expand Up @@ -3,10 +3,12 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg);

function destroyDb (name,callback) {
nano.db.destroy(name, function (e,b) {
callback(e,b);
return;
function destroyDb (callback) {
nano.db.create("de1", function () {
nano.db.destroy("de1", function (e,b) {
callback(e,b);
return;
});
});
}

Expand All @@ -17,7 +19,7 @@ function destroyDbWorked (e,b) {

vows.describe('nano.db.destroy').addBatch({
"nano.db.destroy(foo)": {
topic: function () { destroyDb("foo", this.callback); }
topic: function () { destroyDb(this.callback); }
, "=": destroyDbWorked
}
}).exportTo(module);

0 comments on commit 123f8a3

Please sign in to comment.