test: For a non-existent document.

This commit is contained in:
Hayden Young 2023-02-07 21:17:24 +00:00 committed by haad
parent 2af2f7d8fb
commit 2d425f7323
2 changed files with 49 additions and 36 deletions

View File

@ -24,7 +24,7 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
* @returns {string} The hash of the new oplog entry.
*/
const del = async (key) => {
if (!get(key)) { throw new Error(`No entry with key '${key}' in the database`) }
if (!await get(key)) { throw new Error(`No document with key '${key}' in the database`) }
return addOperation({ op: 'DEL', key, value: null })
}

View File

@ -104,38 +104,51 @@ Object.keys(testAPIs).forEach((IPFS) => {
describe('using database', () => {
it('gets a document', async () => {
const key = 'hello world 1'
const key = 'hello world 1'
const expected = { _id: key, msg: 'writing 1 to db1' }
const expected = { _id: key, msg: 'writing 1 to db1' }
await db1.put(expected)
await db1.put(expected)
const doc = await db1.get(key)
deepStrictEqual(doc, expected)
const doc = await db1.get(key)
deepStrictEqual(doc, expected)
})
it('deletes a document', async () => {
const key = 'hello world 1'
const key = 'hello world 1'
await db1.put({ _id: key, msg: 'writing 1 to db1' })
await db1.del(key)
await db1.put({ _id: key, msg: 'writing 1 to db1' })
await db1.del(key)
const doc = await db1.get(key)
strictEqual(doc, undefined)
const doc = await db1.get(key)
strictEqual(doc, undefined)
})
it('queries a document', async () => {
const expected = { _id: 'hello world 1', msg: 'writing new 1 to db1', views: 10 }
it('throws an error when deleting a non-existent document', async () => {
const key = 'i do not exist'
let err
await db1.put({ _id: 'hello world 1', msg: 'writing 1 to db1', views: 10 })
await db1.put({ _id: 'hello world 2', msg: 'writing 2 to db1', views: 5 })
await db1.put({ _id: 'hello world 3', msg: 'writing 3 to db1', views: 12 })
await db1.del('hello world 3')
await db1.put(expected)
try {
await db1.del(key)
} catch (e) {
err = e
}
const findFn = (doc) => doc.views > 5
strictEqual(err.message, `No document with key \'${key}\' in the database`)
})
deepStrictEqual(await db1.query(findFn), [expected])
it('queries for a document', async () => {
const expected = { _id: 'hello world 1', msg: 'writing new 1 to db1', views: 10 }
await db1.put({ _id: 'hello world 1', msg: 'writing 1 to db1', views: 10 })
await db1.put({ _id: 'hello world 2', msg: 'writing 2 to db1', views: 5 })
await db1.put({ _id: 'hello world 3', msg: 'writing 3 to db1', views: 12 })
await db1.del('hello world 3')
await db1.put(expected)
const findFn = (doc) => doc.views > 5
deepStrictEqual(await db1.query(findFn), [expected])
})
})
})