From 3732dcef8f197efee9e7d89d83cb938ed5a7b863 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Sat, 2 May 2015 22:26:54 +0100 Subject: [PATCH] nudge: More bulkDocs tests, and tweak bulkDocs overloads --- pouchdb.d.ts | 24 ++- tests/integration/test.bulk_docs.ts | 241 ++++++++++++++-------------- 2 files changed, 140 insertions(+), 125 deletions(-) diff --git a/pouchdb.d.ts b/pouchdb.d.ts index 5694225..650439e 100644 --- a/pouchdb.d.ts +++ b/pouchdb.d.ts @@ -344,9 +344,15 @@ declare module pouchdb { _deleted?: boolean; } /** Options for `bulkDocs()` */ - interface DocumentPouch extends options.EmptyOptions { + interface DocumentPouch { /** The array of documents to update */ - docs: D[] + docs: D[]; + } + /** Options for `bulkDocs()` */ + interface DocumentPouchAndOptions + extends BulkDocsOptions { + /** The array of documents to update */ + docs: D[]; } /** The options type for `bulkDocs()` */ interface BulkDocsOptions extends options.EmptyOptions { @@ -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, callback?: async.Callback): void; + bulkDocs(folder: DocumentPouchAndOptions, callback?: async.Callback): void; /** * Update/Delete each doc in an array of documents. * @param options an options object with the documents to update/delete @@ -402,7 +408,7 @@ declare module pouchdb { * Create multiple documents. * @param doc the doc */ - bulkDocs(folder: DocumentPouch, callback?: async.Callback): void; + bulkDocs(folder: DocumentPouchAndOptions, callback?: async.Callback): void; /** * Create multiple documents. * @param doc the doc @@ -426,7 +432,7 @@ declare module pouchdb { * Perform mixed Create/Update/Delete operations on multiple documents. * @param options the doc */ - bulkDocs(folder: DocumentPouch, callback?: async.Callback): void; + bulkDocs(folder: DocumentPouchAndOptions, callback?: async.Callback): void; /** * Perform mixed Create/Update/Delete operations on multiple documents. * @param options the doc @@ -1100,20 +1106,26 @@ declare module pouchdb { */ interface PouchDB extends promise.PouchDB, async.Thenable { } } + /** 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 { /** */ diff --git a/tests/integration/test.bulk_docs.ts b/tests/integration/test.bulk_docs.ts index a43635d..325ff46 100644 --- a/tests/integration/test.bulk_docs.ts +++ b/tests/integration/test.bulk_docs.ts @@ -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 = [{ @@ -577,85 +577,86 @@ 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': { @@ -663,61 +664,63 @@ adapters.forEach(function (adapter) { // '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((err).status).to.equal(PouchDB.Errors.MISSING_DOC.status, + 'correct error status returned'); + expect((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);