Permalink
Browse files

[tests] doc/atomic

  • Loading branch information...
1 parent fda2992 commit 1d66cde09d533c5c8db0a99f4d60377213673a38 @dscape committed May 13, 2012
Showing with 49 additions and 91 deletions.
  1. +1 −1 package.json
  2. +13 −16 tests/doc/atomic.js
  3. +0 −73 tests/doc/update_with_handler_doc.js
  4. +28 −0 tests/fixtures/doc/atomic.json
  5. +7 −1 tests/helpers.js
View
@@ -24,7 +24,7 @@
, "devDependencies" :
{ "async" : "0.1.x"
, "specify" : "0.4.x"
- , "nock" : "0.10.x"
+ , "nock" : "0.13.x"
}
, "scripts" :
{ "test" : "NOCK=on ls -tr tests/*/*.js | xargs -I{} node {}" }
View
@@ -5,19 +5,20 @@ var specify = require("specify")
, nock = helpers.nock
;
-var mock = nock(helpers.couch, "doc/handler")
- , db = nano.use("doc_handler")
+var mock = nock(helpers.couch, "doc/atomic")
+ , db = nano.use("doc_atomic")
, rev
;
-specify("doc_handler:setup", timeout, function (assert) {
- nano.db.create("doc_handler", function (err) {
+specify("doc_atomic:setup", timeout, function (assert) {
+ nano.db.create("doc_atomic", function (err) {
assert.equal(err, undefined, "Failed to create database");
db.insert(
{ "updates":
- { "in-place": function (doc, req) {
- var body = toJSON(req.body);
- return [doc, req.body];
+ { "inplace": function (doc, req) {
+ var body = JSON.parse(req.body);
+ doc[body.field] = body.value;
+ return [doc, JSON.stringify(doc)];
}
}
}, "_design/update", function (error, response) {
@@ -31,20 +32,16 @@ specify("doc_handler:setup", timeout, function (assert) {
});
});
-specify("doc_handler:test", timeout, function (assert) {
- db.updateWithHandler("update", "in-place", "foobar",
+specify("doc_atomic:test", timeout, function (assert) {
+ db.atomic("update", "inplace", "foobar",
{field: "foo", value: "bar"}, function (error, response) {
assert.equal(error, undefined, "Failed to update");
- db.get("foobar", function (error, foobar) {
- assert.equal(error, undefined, "Failed to update");
- assert.equal(foobar._id, "foobar", "My id is foobar");
- assert.equal(foobar.foo, "bar", "Update worked");
- });
+ assert.equal(response.foo, "bar", "Update worked");
});
});
-specify("doc_handler:teardown", timeout, function (assert) {
- nano.db.destroy("doc_handler", function (err) {
+specify("doc_atomic:teardown", timeout, function (assert) {
+ nano.db.destroy("doc_atomic", function (err) {
assert.equal(err, undefined, "Failed to destroy database");
assert.ok(mock.isDone(), "Some mocks didn't run");
});
@@ -1,73 +0,0 @@
-var ensure = require('ensure')
- , nock = require('nock')
- , cfg = require('../../cfg/tests.js')
- , nano = require('../../nano')(cfg)
- , db_name = require('../utils').db_name("doc_up")
- , tests = exports
- , couch
- ;
-
- couch = nock(cfg.url)
- .put('/' + db_name('a'))
- .reply(201, "{\"ok\":true}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a'),
- date: 'Fri, 02 Dec 2011 19:38:03 GMT',
- 'content-type': 'application/json',
- 'content-length': '12',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/foo', {"foo": "bar"})
- .reply(201, "{\"ok\": true,\"id\": \"foo\",\"rev\": \"1-4c6114c65e295552ab1019e2b046b10e\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/foo',
- etag: '"1-4c6114c65e295552ab1019e2b046b10e"',
- date: 'Fri, 02 Dec 2011 19:38:04 GMT',
- 'content-type': 'application/json',
- 'content-length': '66',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/foo',
- { "_rev": "1-4c6114c65e295552ab1019e2b046b10e"
- , "foo": "baz"
- })
- .reply(201, "{\"ok\": true,\"id\": \"foo\",\"rev\": \"2-cfcd6781f13994bde69a1c3320bfdadb\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/foo',
- etag: '"2-cfcd6781f13994bde69a1c3320bfdadb"',
- date: 'Fri, 02 Dec 2011 19:38:04 GMT',
- 'content-type': 'application/json',
- 'content-length': '66',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/_design/my_design_doc/_update/in-place/foo {"field":"bar","value":"foo"}')
- .reply(201, "[{\"id\": \"foo\", \"key\": \"foo\", \"value\": { \"foo\": \"baz\", \"bar\": \"foo\"} }, \"set bar to foo\"]",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/foo',
- etag: '"2-cfcd6781f13994bde69a1c3320bfdadb"',
- date: 'Fri, 02 Dec 2011 19:38:04 GMT',
- 'content-type': 'application/json',
- 'content-length': '66',
- 'cache-control': 'must-revalidate' }
- );
-
-function db(i) { return nano.use(db_name(i)); }
-
-tests.update_with_handler_doc = function (callback) {
- nano.db.create(db_name('a'), function () {
- db('a').insert({foo: "bar"}, "foo", function (_,b) {
- db('a').insert({"_rev": b.rev, foo: "baz"}, "foo", function(_,b) {
- db('a').updateWithHandler('my_design_doc','in-place', 'foo', {field: 'bar', value: 'foo'}, callback);
- });
- });
- });
-};
-
-
-tests.update_with_handler_doc_ok = function (e,b) {
- this.t.notOk(e, 'I got err free status');
- this.t.equal(b[0].id, "foo", 'My filename is foo');
- this.t.ok(b[0].key, 'I am now ok');
- this.t.ok(b[0].value, 'I got value');
-
- this.t.ok(couch.isDone(), 'Nock is done');
-};
-
-ensure(__filename,tests,module,process.argv[2]);
@@ -0,0 +1,28 @@
+[
+ { "method" : "put"
+ , "path" : "/doc_atomic"
+ , "status" : 201
+ , "response" : "{ \"ok\": true }"
+ }
+, { "method" : "put"
+ , "path" : "/doc_atomic/_design/update"
+ , "body" : "*"
+ , "status" : 201
+ , "response" : "{\"ok\":true,\"id\":\"_design/update\",\"rev\":\"1-14e6bc\"}"
+ }
+, { "method" : "put"
+ , "status" : 201
+ , "path" : "/doc_atomic/foobar"
+ , "body" : "{\"foo\":\"baz\"}"
+ , "response" : "{\"ok\":true,\"id\":\"foobar\",\"rev\":\"1-611488\"}"
+ }
+, { "method" : "put"
+ , "path" : "/doc_atomic/_design/update/_update/inplace/foobar"
+ , "body" : "{\"field\":\"foo\",\"value\":\"bar\"}"
+ , "response" : "{\"foo\": \"bar\"}"
+ }
+, { "method" : "delete"
+ , "path" : "/doc_atomic"
+ , "response" : "{ \"ok\": true }"
+ }
+]
View
@@ -65,7 +65,13 @@ helpers.nock = function helpersNock(url, fixture) {
if(typeof headers === "string" && endsWith(headers, '.json')) {
headers = helpers.loadFixture(path.join(fixture, headers));
}
- nock(url)[method](path, body).reply(status, response, headers);
+ if(body==="*") {
+ nock(url).filteringRequestBody(function(path) {
+ return "*";
+ })[method](path, "*").reply(status, response, headers);
+ } else {
+ nock(url)[method](path, body).reply(status, response, headers);
+ }
});
nock(url).log(console.log);
return nock(url);

0 comments on commit 1d66cde

Please sign in to comment.