Permalink
Browse files

[copy] problem with overwrite semantics

* also nock does not support copy

this refers to #93
  • Loading branch information...
1 parent 9ee0f2d commit 91c034a7f3dc12fcba2e6c2e758a9f7452c36937 @dscape committed Jul 4, 2012
Showing with 28 additions and 16 deletions.
  1. +14 −8 nano.js
  2. 0 tests/att/.temp.bmp
  3. +14 −8 tests/doc/copy.js
View
22 nano.js
@@ -652,15 +652,21 @@ module.exports = exports = nano = function database_module(cfg) {
callback = opts;
opts = {};
}
- var params = { db: db_name, doc: doc_src, method: "COPY"
- , headers: {"Destination": doc_dest} };
- head_doc(doc_dest, function (e,b,h) {
- if (!e && opts.overwrite) {
- params.headers.Destination += "?rev=" +
- h.etag.substring(1, h.etag.length - 1);
- }
+ var params =
+ { db: db_name, doc: doc_src, method: "COPY"
+ , headers: { "Destination": doc_dest }
+ };
+ if(opts.overwrite) {
+ return head_doc(doc_dest, function (e,b,h) {
+ if (typeof h.etag === "string") {
+ params.headers.Destination += "?rev=" +
+ h.etag.substring(1, h.etag.length - 1);
+ }
+ return relax(params, callback);
+ });
+ } else {
return relax(params, callback);
- });
+ }
}
/*
View
0 tests/att/.temp.bmp
No changes.
View
22 tests/doc/copy.js
@@ -7,7 +7,6 @@ var specify = require('specify')
var mock = nock(helpers.couch, "doc/copy")
, db = nano.use("doc_copy")
- , rev
;
specify("doc_copy:setup", timeout, function (assert) {
@@ -17,27 +16,34 @@ specify("doc_copy:setup", timeout, function (assert) {
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");
- rev = foo.rev;
});
db.insert({"baz": "foo"}, "foo_dest", 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");
- rev = foo.rev;
});
});
});
-specify("doc_copy:test", timeout, function (assert) {
- db.copy("foo_src", "foo_dest", { overwrite: true }, function (error, response, headers) {
- assert.equal(error, undefined, "Should have copied and overwritten foo_src to foo_dest");
+specify("doc_copy:overwrite", timeout, function (assert) {
+ db.copy("foo_src", "foo_dest", { overwrite: true },
+ function (error, response, headers) {
+ assert.equal(error, undefined,
+ "Should have copied and overwritten foo_src to foo_dest");
assert.equal(headers["status-code"], 201, "Status code should be 201");
});
- db.copy("foo_src", "foo_dest", { overwrite: false }, function (error, response, headers) {
+});
+
+specify("doc_copy:no_overwrite", timeout, function (assert) {
+ db.copy("foo_src", "foo_dest", function (error, response, headers) {
assert.equal(error.error, "conflict", "Should have a document conflict.");
});
+});
+
+specify("doc_copy:new_doc", timeout, function (assert) {
db.copy("foo_src", "baz_dest", function (error, response, headers) {
- assert.equal(error, undefined, "Should have copied foo_src to new baz_dest document");
+ assert.equal(error, undefined,
+ "Should have copied foo_src to new baz_dest document");
assert.equal(headers["status-code"], 201, "Status code should be 201");
});
});

0 comments on commit 91c034a

Please sign in to comment.