Skip to content

Commit

Permalink
Updated for node.js v0.1.0 (incompatible API changes). Also forgot to…
Browse files Browse the repository at this point in the history
… checkin a test case.
  • Loading branch information
sixtus committed Jul 1, 2009
1 parent 38ab66e commit fdf6acc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
10 changes: 5 additions & 5 deletions module/node-couch.js
Expand Up @@ -25,7 +25,7 @@ function _interact(verb, path, successStatus, options, port, host) {
options = options || {};
var request;

var client = new node.http.Client(port, host);
var client = node.http.createClient(port, host);
var requestPath = path + encodeOptions(options);
node.debug("COUCHING " + requestPath + " -> " + verb);

Expand All @@ -43,11 +43,11 @@ function _interact(verb, path, successStatus, options, port, host) {
var responseBody = "";
response.setBodyEncoding("utf8");

response.onBody = function(chunk) {
response.addListener("body", function(chunk) {
responseBody += chunk;
};
});

response.onBodyComplete = function() {
response.addListener("complete", function() {
responseBody = JSON.parse(responseBody);
if (response.statusCode === successStatus) {
if (options.success) {
Expand All @@ -56,7 +56,7 @@ function _interact(verb, path, successStatus, options, port, host) {
} else if (options.error) {
options.error(responseBody);
}
};
});
});
}

Expand Down
80 changes: 80 additions & 0 deletions test/mjsunit/test-doc-create-update-delete.js
@@ -0,0 +1,80 @@
include("mjsunit.js");
include("../../module/node-couch.js");

function unwantedError(result) {
throw("Unwanted error" + JSON.stringify(result));
}

var db;
var doc;
var id;
var rev;

function onLoad () {
CouchDB.generateUUIDs({
count : 1,
success : withUUIDs,
error : unwantedError
});
}

function withUUIDs(uuids) {
db = CouchDB.db("test" + uuids[0]);
db.create({
success : withDB,
error : unwantedError
});
}

function withDB() {
doc = {};
db.saveDoc(doc, {
success : afterSave,
error : unwantedError
});
}

function afterSave(returnVal) {
assertEquals(doc, returnVal, "should return the doc");
assertEquals(typeof doc._id, "string");

id = doc._id;
rev = doc._rev;
doc.foo = "bar";
db.saveDoc(doc, {
success : afterUpdate,
error : unwantedError
});
}

function afterUpdate(returnVal) {
assertEquals(doc, returnVal, "should return the doc");
assertEquals(typeof doc._id, "string");
assertFalse(doc._rev === rev, "rev did not update");
assertEquals(id, doc._id, "doc id changed");

db.removeDoc(doc, {
success : afterRemove,
error : unwantedError
});
}

function afterRemove(returnVal) {
assertEquals(doc, returnVal, "did not return the doc");

assertTrue(doc._rev === undefined, "did not remove the rev");
assertEquals(id, doc._id, "changed the id");

db.drop({
success : afterDrop,
error : unwantedError
});
}

function afterDrop() {
db = "success";
}

function onExit() {
assertEquals("success", db, "Please check the chain, last callback was never reached");
}

0 comments on commit fdf6acc

Please sign in to comment.