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

Commit

Permalink
[tests] doc insert
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed May 13, 2012
1 parent 1d66cde commit d1a9168
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
14 changes: 7 additions & 7 deletions nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ module.exports = exports = nano = function database_module(cfg) {
return error.request_err(ex3, 'qsstringify', {});
}
}
if(!callback) { // void callback, stream
try {
return request(req);
} catch (ex4) {
return error.request_err(ex4, 'streamthrow', {});
}
}
if(opts.body) {
if (Buffer.isBuffer(opts.body)) {
req.body = opts.body; // raw data
Expand All @@ -167,6 +160,13 @@ module.exports = exports = nano = function database_module(cfg) {
} // json data
}
log(req);
if(!callback) { // void callback, stream
try {
return request(req);
} catch (ex4) {
return error.request_err(ex4, 'streamthrow', {});
}
}
try {
var stream = request(req, function(e,h,b){
rh = (h && h.headers || {});
Expand Down
62 changes: 62 additions & 0 deletions tests/doc/insert2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var specify = require('specify')
, helpers = require('../helpers')
, timeout = helpers.timeout
, nano = helpers.nano
, nock = helpers.nock
;

var mock = nock(helpers.couch, "doc/insert")
, db = nano.use("doc_insert")
;

specify("doc_insert:setup", timeout, function (assert) {
nano.db.create("doc_insert", function (err) {
assert.equal(err, undefined, "Failed to create database");
});
});

specify("doc_insert:simple", timeout, function (assert) {
db.insert({"foo": "baz"}, "foobaz", function (error, foo) {
assert.equal(error, undefined, "Should have stored foo");
assert.equal(foo.ok, true, "Response should be ok");
assert.ok(foo.rev, "Response should have rev");
});
});

specify("doc_insert:functions", timeout, function (assert) {
db.insert({fn: function () { return true; },
fn2: "function () { return true; }"}, function (error, fns) {
assert.equal(error, undefined, "Should have stored foo");
assert.equal(fns.ok, true, "Response should be ok");
assert.ok(fns.rev, "Response should have rev");
db.get(fns.id, function (error, fns) {
assert.equal(fns.fn, fns.fn2, "fn matches fn2");
assert.equal(error, undefined, "Should get foo");
});
});
});

specify("doc_insert:streaming", timeout, function (assert) {
var buffer = ""
, foobar = db.insert({"foo": "bar"})
;

function runAssertions(error, foobar) {
assert.equal(error, undefined, "Should have stored foobar");
assert.ok(foobar.ok, "This is ok");
assert.ok(foobar.rev, "I GOT REVZ");
}

foobar.on('data', function(chunk) { buffer += chunk; });
foobar.on('end', function () { runAssertions(null, JSON.parse(buffer)); });
foobar.on('error', runAssertions);
});

specify("doc_insert:teardown", timeout, function (assert) {
nano.db.destroy("doc_insert", function (err) {
assert.equal(err, undefined, "Failed to destroy database");
assert.ok(mock.isDone(), "Some mocks didn't run");
});
});

specify.run(process.argv.slice(2));
36 changes: 36 additions & 0 deletions tests/fixtures/doc/insert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{ "method" : "put"
, "path" : "/doc_insert"
, "status" : 201
, "response" : "{ \"ok\": true }"
}
, { "method" : "put"
, "status" : 201
, "path" : "/doc_insert/foobaz"
, "body" : "{\"foo\":\"baz\"}"
, "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"1-611488\"}"
}
, { "method" : "post"
, "status" : 201
, "path" : "/doc_insert"
, "body" : "{\"fn\":\"function () { return true; }\",\"fn2\":\"function () { return true; }\"}"
, "response" : "{\"ok\":true,\"id\":\"123\",\"rev\":\"1-611488\"}"
}
, { "path" : "/doc_insert/123"
, "response" : "{\"fn\":\"function () { return true; }\",\"fn2\":\"function () { return true; }\",\"id\":\"123\",\"rev\":\"1-611488\"}"
}
, { "method" : "post"
, "status" : 201
, "path" : "/doc_insert"
, "body" : "{\"foo\":\"bar\"}"
, "response" : "{\"ok\":true,\"id\":\"234\",\"rev\":\"1-333231\"}"
}
, { "method" : "delete"
, "path" : "/doc_insert/foobaz?rev=1-611488"
, "response" : "{\"ok\":true}"
}
, { "method" : "delete"
, "path" : "/doc_insert"
, "response" : "{ \"ok\": true }"
}
]

0 comments on commit d1a9168

Please sign in to comment.