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

@ -11,9 +11,9 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
*/
const put = async (doc) => {
const key = doc[indexBy]
if (!key) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
return addOperation({ op: 'PUT', key, value: doc })
}
@ -24,8 +24,8 @@ 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 })
}
@ -38,11 +38,11 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
const get = async (key) => {
for await (const doc of iterator()) {
if (key === doc[indexBy]) {
return doc
return doc
}
}
}
/**
* Queries the document store for documents matching mapper filters.
*
@ -51,7 +51,7 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
*/
const query = async (findFn) => {
const results = []
for await (const doc of iterator()) {
if (findFn(doc)) {
results.push(doc)

View File

@ -57,12 +57,12 @@ Object.keys(testAPIs).forEach((IPFS) => {
// Create an identity for each peers
testIdentity1 = await createIdentity({ id: 'userA', keystore, signingKeystore })
testIdentity2 = await createIdentity({ id: 'userB', keystore, signingKeystore })
const accessController = {
canAppend: (entry) => entry.identity.id === testIdentity1.id
}
})
beforeEach(async () => {
db1 = await Documents({ OpLog: Log, Database, ipfs: ipfs1, identity: testIdentity1, databaseId, accessController })
})
@ -104,38 +104,51 @@ Object.keys(testAPIs).forEach((IPFS) => {
describe('using database', () => {
it('gets a document', async () => {
const key = 'hello world 1'
const expected = { _id: key, msg: 'writing 1 to db1' }
const key = 'hello world 1'
await db1.put(expected)
const doc = await db1.get(key)
deepStrictEqual(doc, expected)
const expected = { _id: key, msg: 'writing 1 to db1' }
await db1.put(expected)
const doc = await db1.get(key)
deepStrictEqual(doc, expected)
})
it('deletes a document', async () => {
const key = 'hello world 1'
await db1.put({ _id: key, msg: 'writing 1 to db1' })
await db1.del(key)
const doc = await db1.get(key)
strictEqual(doc, undefined)
const key = 'hello world 1'
await db1.put({ _id: key, msg: 'writing 1 to db1' })
await db1.del(key)
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 }
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])
it('throws an error when deleting a non-existent document', async () => {
const key = 'i do not exist'
let err
try {
await db1.del(key)
} catch (e) {
err = e
}
strictEqual(err.message, `No document with key \'${key}\' in the database`)
})
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])
})
})
})