-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from pouchdb/pouchdb-update
pouchdb-update
- Loading branch information
Showing
8 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |