Skip to content

Commit

Permalink
nudge: More bulkDocs tests, and tweak bulkDocs overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
AGBrown committed May 2, 2015
1 parent aba66fd commit 3732dce
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 125 deletions.
24 changes: 18 additions & 6 deletions pouchdb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,15 @@ declare module pouchdb {
_deleted?: boolean;
}
/** Options for `bulkDocs()` */
interface DocumentPouch<D extends NewDoc | MixedDoc> extends options.EmptyOptions {
interface DocumentPouch<D extends NewDoc | MixedDoc> {
/** The array of documents to update */
docs: D[]
docs: D[];
}
/** Options for `bulkDocs()` */
interface DocumentPouchAndOptions<D extends NewDoc | MixedDoc>
extends BulkDocsOptions {
/** The array of documents to update */
docs: D[];
}
/** The options type for `bulkDocs()` */
interface BulkDocsOptions extends options.EmptyOptions {
Expand Down Expand Up @@ -378,7 +384,7 @@ declare module pouchdb {
* Update/Delete each doc in an array of documents.
* @param options an options object with the documents to update/delete
*/
bulkDocs(folder: DocumentPouch<ExistingDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
bulkDocs(folder: DocumentPouchAndOptions<ExistingDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
/**
* Update/Delete each doc in an array of documents.
* @param options an options object with the documents to update/delete
Expand All @@ -402,7 +408,7 @@ declare module pouchdb {
* Create multiple documents.
* @param doc the doc
*/
bulkDocs(folder: DocumentPouch<NewDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
bulkDocs(folder: DocumentPouchAndOptions<NewDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
/**
* Create multiple documents.
* @param doc the doc
Expand All @@ -426,7 +432,7 @@ declare module pouchdb {
* Perform mixed Create/Update/Delete operations on multiple documents.
* @param options the doc
*/
bulkDocs(folder: DocumentPouch<MixedDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
bulkDocs(folder: DocumentPouchAndOptions<MixedDoc>, callback?: async.Callback<BulkDocsResponse[]>): void;
/**
* Perform mixed Create/Update/Delete operations on multiple documents.
* @param options the doc
Expand Down Expand Up @@ -1100,20 +1106,26 @@ declare module pouchdb {
*/
interface PouchDB extends promise.PouchDB, async.Thenable<promise.PouchDB> { }
}

/** A pouch error definition */
interface ErrorDefinition {
/** The error status number */
status: number;
/** The error message */
message: string;
/** The error message */
reason: string;
}
/** The collection of error definitions defined for PouchDB */
interface ErrorDefinitions {
/** Indicates that a document _id was set to a reserved id */
RESERVED_ID: ErrorDefinition;
/** Indicates that a document _id was set to a reserved id */
/** Missing JSON list of 'docs' */
MISSING_BULK_DOCS: ErrorDefinition;
/** A document was not found */
MISSING_DOC: ErrorDefinition;
}

/** Static-side interface for PouchDB */
export interface PouchDB {
/** */
Expand Down
241 changes: 122 additions & 119 deletions tests/integration/test.bulk_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ adapters.forEach(function (adapter) {
});
});

// todo: not sure how to handle res.length in this test
// todo: not sure how to handle res as an [] in this test
//it('Bulk with new_edits=false', (done) => {
// var db = new PouchDB(dbs.name, (e, v) => { });
// var docs = [{
Expand Down Expand Up @@ -577,147 +577,150 @@ adapters.forEach(function (adapter) {
});
});

//it('Deletion with new_edits=false, no history', function () {
it('Deletion with new_edits=false, no history', () => {

// var db = new PouchDB(dbs.name);
// var doc1 = {
// '_id': 'foo',
// '_rev': '1-x',
// '_revisions': {
// 'start': 1,
// 'ids': ['x']
// }
// };
// var doc2 = {
// '_deleted': true,
// '_id': 'foo',
// '_rev': '2-y'
// };
var db = new PouchDB(dbs.name);
var doc1 = {
'_id': 'foo',
'_rev': '1-x',
'_revisions': {
'start': 1,
'ids': ['x']
}
};
var doc2 = {
'_deleted': true,
'_id': 'foo',
'_rev': '2-y'
};

// return db.put(doc1, { new_edits: false }).then(function () {
// return db.put(doc2, { new_edits: false });
// }).then(function () {
// return db.allDocs({ keys: ['foo'] });
// }).then(function (res) {
// res.rows[0].value.rev.should.equal('1-x');
// should.equal(!!res.rows[0].value.deleted, false);
// });
//});
return db.put(doc1, { new_edits: false }).then(() => {
return db.put(doc2, { new_edits: false });
}).then(() => {
return db.allDocs({ keys: ['foo'] });
}).then((res) => {
expect(res.rows[0].value.rev).to.equal('1-x');
expect(!!res.rows[0].value.deleted).to.equal(false);
});
});

//it('Modification with new_edits=false, no history', function () {
it('Modification with new_edits=false, no history', () => {

// var db = new PouchDB(dbs.name);
// var doc1 = {
// '_id': 'foo',
// '_rev': '1-x',
// '_revisions': {
// 'start': 1,
// 'ids': ['x']
// }
// };
// var doc2 = {
// '_id': 'foo',
// '_rev': '2-y'
// };
var db = new PouchDB(dbs.name);
var doc1 = {
'_id': 'foo',
'_rev': '1-x',
'_revisions': {
'start': 1,
'ids': ['x']
}
};
var doc2 = {
'_id': 'foo',
'_rev': '2-y'
};

// return db.put(doc1, { new_edits: false }).then(function () {
// return db.put(doc2, { new_edits: false });
// }).then(function () {
// return db.allDocs({ keys: ['foo'] });
// }).then(function (res) {
// res.rows[0].value.rev.should.equal('2-y');
// });
//});
return db.put(doc1, { new_edits: false }).then(() => {
return db.put(doc2, { new_edits: false });
}).then(() => {
return db.allDocs({ keys: ['foo'] });
}).then((res) => {
expect(res.rows[0].value.rev).to.equal('2-y');
});
});

//it('Deletion with new_edits=false, no history, no revisions', function () {
it('Deletion with new_edits=false, no history, no revisions',() => {

// var db = new PouchDB(dbs.name);
// var doc = {
// '_deleted': true,
// '_id': 'foo',
// '_rev': '2-y'
// };

// return db.put(doc, { new_edits: false }).then(function () {
// return db.allDocs({ keys: ['foo'] });
// }).then(function (res) {
// res.rows[0].value.rev.should.equal('2-y');
// res.rows[0].value.deleted.should.equal(true);
// });
//});
var db = new PouchDB(dbs.name);
var doc = {
'_deleted': true,
'_id': 'foo',
'_rev': '2-y'
};

//it('Testing new_edits=false in req body', function (done) {
// var db = new PouchDB(dbs.name);
return db.put(doc, { new_edits: false }).then(() => {
return db.allDocs({ keys: ['foo'] });
}).then((res) => {
expect(res.rows[0].value.rev).to.equal('2-y');
expect(res.rows[0].value.deleted).to.equal(true);
});
});

// todo: not sure how to handle res as an [] in this test
//it('Testing new_edits=false in req body', (done) => {
// var db = new PouchDB(dbs.name, (e, v) => { });
// var docs = [{
// '_id': 'foo',
// '_rev': '2-x',
// '_revisions': {
// 'start': 2,
// 'ids': ['x', 'a']
// }
// }, {
// '_id': 'foo',
// '_rev': '2-x',
// '_revisions': {
// 'start': 2,
// 'ids': ['x', 'a']
// }
// }, {
// '_id': 'foo',
// '_rev': '2-y',
// '_revisions': {
// 'start': 2,
// 'ids': ['y', 'a']
// }
// }];
// db.bulkDocs({ docs: docs, new_edits: false }, function (err, res) {
// db.get('foo', { open_revs: 'all' }, function (err, res) {
// res.sort(function (a, b) {
// return a.ok._rev < b.ok._rev ? -1 :
// a.ok._rev > b.ok._rev ? 1 : 0;
// });
// res.length.should.equal(2);
// res[0].ok._rev.should.equal('2-x', 'doc1 ok');
// res[1].ok._rev.should.equal('2-y', 'doc2 ok');
// db.bulkDocs({ docs: docs, new_edits: false }, (err, res) => {
// db.get('foo', { open_revs: 'all' }, (err, res) => {
// // res.sort(function (a, b) {
// // return a.ok._rev < b.ok._rev ? -1 :
// // a.ok._rev > b.ok._rev ? 1 : 0;
// // });
// // res.length.should.equal(2);
// // res[0].ok._rev.should.equal('2-x', 'doc1 ok');
// // res[1].ok._rev.should.equal('2-y', 'doc2 ok');
// done();
// });
// });
//});

//it('656 regression in handling deleted docs', function (done) {
// var db = new PouchDB(dbs.name);
// db.bulkDocs({
// docs: [{
// _id: 'foo',
// _rev: '1-a',
// _deleted: true
// }]
// }, { new_edits: false }, function (err, res) {
// db.get('foo', function (err, res) {
// should.exist(err, 'deleted');
// err.status.should.equal(PouchDB.Errors.MISSING_DOC.status,
// 'correct error status returned');
// err.message.should.equal(PouchDB.Errors.MISSING_DOC.message,
// 'correct error message returned');
// // todo: does not work in pouchdb-server.
// // err.reason.should.equal('deleted',
// // 'correct error reason returned');
// done();
// });
// });
//});
it('656 regression in handling deleted docs', (done) => {
var db = new PouchDB(dbs.name, (e, v) => { });
db.bulkDocs({
docs: [{
_id: 'foo',
_rev: '1-a',
_deleted: true
}]
}, { new_edits: false }, (err, res) => {
db.get('foo', (err, res) => {
expect(err).to.exist('deleted');
// todo: `get` error is a `BulkDocsError`
// todo: `BulkDocsError` error has similar shape to `ErrorDefinition`
expect((<BulkDocsError>err).status).to.equal(PouchDB.Errors.MISSING_DOC.status,
'correct error status returned');
expect((<BulkDocsError>err).message).to.equal(PouchDB.Errors.MISSING_DOC.message,
'correct error message returned');
// todo: does not work in pouchdb-server.
// err.reason.should.equal('deleted',
// 'correct error reason returned');
done();
});
});
});

//it('Test quotes in doc ids', function (done) {
// var db = new PouchDB(dbs.name);
// var docs = [{ _id: '\'your_sql_injection_script_here\'' }];
// db.bulkDocs({ docs: docs }, function (err, res) {
// should.not.exist(err, 'got error: ' + JSON.stringify(err));
// db.get('foo', function (err, res) {
// should.exist(err, 'deleted');
// done();
// });
// });
//});
it('Test quotes in doc ids', (done) => {
var db = new PouchDB(dbs.name, (e, v) => { });
var docs = [{ _id: '\'your_sql_injection_script_here\'' }];
db.bulkDocs({ docs: docs }, (err, res) => {
expect(err).not.to.exist('got error: ' + JSON.stringify(err));
db.get('foo', (err, res) => {
expect(err).to.exist('deleted');
done();
});
});
});

//it('Bulk docs empty list', function (done) {
// var db = new PouchDB(dbs.name);
// db.bulkDocs({ docs: [] }, function (err, res) {
// done(err);
// });
//});
it('Bulk docs empty list', (done) => {
var db = new PouchDB(dbs.name, (e, v) => { });
db.bulkDocs({ docs: [] }, (err, res) => {
done(err);
});
});

//it('handles simultaneous writes', function (done) {
// var db1 = new PouchDB(dbs.name);
Expand Down

0 comments on commit 3732dce

Please sign in to comment.