Skip to content

Commit

Permalink
Merge pull request #246 from pouchdb/pouchdb-update
Browse files Browse the repository at this point in the history
pouchdb-update
  • Loading branch information
gr2m authored Jul 4, 2017
2 parents 86fd9e9 + ee02cea commit 31eaae7
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cookie-parser": "^1.4.3",
"corser": "~2.0.0",
"couchdb-calculate-session-id": "^1.1.0",
"couchdb-eval": "^1.0.0",
"couchdb-harness": "*",
"couchdb-log-parse": "^0.0.4",
"couchdb-objects": "^1.0.0",
Expand Down Expand Up @@ -58,6 +59,7 @@
"pouchdb-promise": "^6.1.2",
"pouchdb-replication": "^6.1.0",
"pouchdb-req-http-query": "^1.0.3",
"couchdb-resp-completer": "^1.0.0",
"pouchdb-rewrite": "^1.0.7",
"pouchdb-show": "^1.0.8",
"pouchdb-size": "^1.2.2",
Expand Down
9 changes: 9 additions & 0 deletions packages/node_modules/pouchdb-update/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions packages/node_modules/pouchdb-update/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/node_modules/pouchdb-update/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions tests/pouchdb-update/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const {setup, setupWithDoc, teardown, updateDocument, should, shouldThrowError} = require('./utils');

describe('Sync update tests', () => {
let db;

beforeEach(() => {
return setupWithDoc()

.then((result) => {
db = result.db;
return db.put(updateDocument);
});
});
afterEach(teardown);

it('args', () => {
return db.update('test/args/mytest', {query: {'a': 3}})

.then((response) => {
const [doc, req] = JSON.parse(response.body);
doc.test.should.be.ok;
req.id.should.equal('mytest');
req.raw_path.should.equal('/test/_design/test/_update/args/mytest?a=3');
});
});

it('args without doc', () => {
return db.update('test/args', {withValidation: true})

.then((response) => {
const [doc, req] = JSON.parse(response.body);
should.equal(doc, null);
req.should.not.have.property('withValidation');
});
});

it('missing function', () => {
shouldThrowError(() => {
return db.update('test/missing/mytest');
})

.then((error) => {
error.toString().should.be.ok;
error.name.should.equal('not_found');
error.message.should.equal('missing update function missing on design doc _design/test');
});
});

it('saving', () => {
db.update('test/save-adding-date', {body: JSON.stringify({
_id: 'test',
name: 'Today'
})})

.then((response) => {
response.body.should.equal('Hello World!');

return db.get('test');
})

.then((doc) => {
doc.updated.should.be.ok;
doc.name.should.equal('Today');
});
});
});

describe('Async update tests', () => {
let db;

beforeEach(() => {
db = setup();
return db.put(updateDocument);
});
afterEach(teardown);

it('exception', () => {
return db.update('test/exception')

.then(() => {
'db.update("test/exception") should not resolve'.should.equal('');
})

.catch((error) => {
error.status.should.equal(500);
error.name.should.equal('ReferenceError');
error.message.should.contain('abc');
});
});
});

describe('Async update with empty design doc', () => {
let db;

beforeEach(() => {
db = setup();
return db.put({_id: '_design/test'});
});
afterEach(teardown);

it('basic', () => {
return db.update('test/missing')

.then(() => {
'db.update("test/missing") should not resolve'.should.equal('');
})

.catch((error) => {
error.status.should.equal(404);
error.name.should.equal('not_found');
error.message.should.equal('missing update function missing on design doc _design/test');
});
});
});
21 changes: 21 additions & 0 deletions tests/pouchdb-update/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {setupHTTP, teardown, updateDocument, should} = require('./utils');

let db;

describe('http tests', () => {
beforeEach(() => {
db = setupHTTP();
return db.put(updateDocument);
});
afterEach(teardown);

it('update', () => {
return db.update('test/args/my-id')

.then((result) => {
const [doc, req] = JSON.parse(result.body);
should.not.exist(doc);
req.id.should.equal('my-id');
});
});
});
15 changes: 15 additions & 0 deletions tests/pouchdb-update/signatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const {setup, teardown} = require('./utils');

describe('signature tests', () => {
let db;
beforeEach(() => {
db = setup();
});
afterEach(teardown);

it('update', () => {
const promise = db.update('test/test/test', () => {});
promise.then.should.be.ok;
promise.catch.should.be.ok;
});
});
23 changes: 23 additions & 0 deletions tests/pouchdb-update/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const stuff = require('pouchdb-plugin-helper/testutils');
const Update = require('../../packages/node_modules/pouchdb-update');

stuff.PouchDB.plugin(Update);

stuff.updateDocument = {
_id: "_design/test",
updates: {
args: `function (doc, req) {
return [null, toJSON([doc, req])];
}`,
exception: `function (doc, req) {
return abc;
}`,
'save-adding-date': `function (oldDoc, req) {
var doc = JSON.parse(req.body);
doc.updated = new Date();
return [doc, "Hello World!"];
}`
}
};

module.exports = stuff;

0 comments on commit 31eaae7

Please sign in to comment.