var request = require('request'); var myStepDefinitionsWrapper = function () { var dbName = function(str){ return "deletion_conflicts_test_"+str; } var baseUrl = process.env['COUCH_DB'] || 'http://127.0.0.1:5984' this.Given(/^an empty CouchDB "([^"]*)"$/, function(arg1, callback) { request({ method: 'DELETE', uri: baseUrl+"/"+dbName(arg1), json: true }, function (error, response, json) { if (error) callback.fail(error) else { request({ method: 'PUT', uri: baseUrl+"/"+dbName(arg1), json: true }, function (error, response, json) { if (error) callback.fail(error) else if (json && !json.ok) callback.fail("unexpected response: "+ JSON.stringify(json)); else callback() }); } }); }); this.When(/^document "([^"]*)" with field "([^"]*)" set to "([^"]*)" is added to CouchDB "([^"]*)"$/, function(arg1, arg2, arg3, arg4, callback) { var data = { _id: arg1 } data[arg2] = arg3; request({ method: 'PUT', uri: baseUrl+"/"+dbName(arg4)+"/"+arg1, json: data }, function (error, response, json) { if (error) callback.fail(error) else if (json && !json.ok) callback.fail("unexpected response: "+ JSON.stringify(json)); else callback() }); }); this.When(/^CouchDB "([^"]*)" is replicated to CouchDB "([^"]*)"$/, function(arg1, arg2, callback) { // express the regexp above with the code you wish you had request({ method: 'POST', uri: baseUrl+"/_replicate", json: { source: dbName(arg1), target: dbName(arg2) } }, function (error, response, json) { if (error) callback.fail(error) else if (json && !json.ok) callback.fail("unexpected response: "+ JSON.stringify(json)); else callback() }); }); this.Then(/^CouchDB "([^"]*)" (?:should|does) have document "([^"]*)" with field "([^"]*)" set to "([^"]*)"$/, function(arg1, arg2, arg3, arg4, callback) { request({ method: 'GET', uri: baseUrl+"/"+dbName(arg1)+"/"+arg2, qs: { conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json) callback.fail("unexpected response: "+ JSON.stringify(response)); else if (json._id != arg2) callback.fail("invalid _id in doc: "+ JSON.stringify(json)); else if (json[arg3] != arg4) callback.fail("field "+arg3+" set to "+JSON.stringify(json[arg3])+". expected "+JSON.stringify(arg4)); else callback(); }); }); this.When(/^document "([^"]*)" gets deleted from CouchDB "([^"]*)"$/, function(arg1, arg2, callback) { request({ method: 'GET', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json) callback.fail("unexpected response: "+ JSON.stringify(response)); else if (json._id != arg1) callback.fail("invalid _id in doc: "+ JSON.stringify(json)); else if (json._conflicts) callback.fail("unexpected _conflicts in doc: "+ JSON.stringify(json)); else { request({ method: 'DELETE', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { rev: json._rev }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json || !json.ok) callback.fail("unexpected response: "+ JSON.stringify(response)); else callback(); }); } }); }); this.Then(/^CouchDB "([^"]*)" (?:should|does) have no document "([^"]*)"$/, function(arg1, arg2, callback) { request({ method: 'GET', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json || json.error != "not_found") callback.fail("unexpected response: "+ JSON.stringify(response)); else callback(); }); }); this.When(/^document "([^"]*)" in CouchDB "([^"]*)" is updated with field "([^"]*)" set to "([^"]*)"$/, function(arg1, arg2, arg3, arg4, callback) { request({ method: 'GET', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json) callback.fail("unexpected response: "+ JSON.stringify(response)); else if (json._id != arg1) callback.fail("invalid _id in doc: "+ JSON.stringify(json)); else if (json._conflicts) callback.fail("unexpected _conflicts in doc: "+ JSON.stringify(json)); else { var updatedJson = json; updatedJson[arg3] = arg4 request({ method: 'PUT', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, json: updatedJson }, function (error, response, json) { if (error) callback.fail(error) else if (!json || !json.ok) callback.fail("unexpected response: "+ JSON.stringify(response)); else callback(); }); } }); }); this.Then(/^document "([^"]*)" in CouchDB "([^"]*)" does indicate a deleted conflict$/, function(arg1, arg2, callback) { request({ method: 'GET', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { deleted_conflicts: "true", conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json) callback.fail("unexpected response: "+ JSON.stringify(response)); else if (json._id != arg1) callback.fail("invalid _id in doc: "+ JSON.stringify(json)); else if (!json._deleted_conflicts) callback.fail("expected _deleted_conflicts not found in doc: "+ JSON.stringify(json)); else callback(); }); }); this.Then(/^document "([^"]*)" in CouchDB "([^"]*)" (?:should|does)( NOT)? indicate a conflict/, function(arg1, arg2, arg3, callback) { not = !!arg3 request({ method: 'GET', uri: baseUrl+"/"+dbName(arg2)+"/"+arg1, qs: { conflicts: "true" }, json: true }, function (error, response, json) { if (error) callback.fail(error) else if (!json) callback.fail("unexpected response: "+ JSON.stringify(response)); else if (json._id != arg1) callback.fail("invalid _id in doc: "+ JSON.stringify(json)); else if (not && json._conflicts) callback.fail("expected NO _conflicts, but found in doc: "+ JSON.stringify(json)); else if (!not && !json._conflicts) callback.fail("expected _conflicts not found in doc: "+ JSON.stringify(json)); else callback(); }); }); } module.exports = myStepDefinitionsWrapper;