From 2d425f73238164539d31383fb4adf98df59d2e46 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 7 Feb 2023 21:17:24 +0000 Subject: [PATCH] test: For a non-existent document. --- src/documents.js | 14 ++++----- test/documents.spec.js | 71 +++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/documents.js b/src/documents.js index 0e9d2fd..063575f 100644 --- a/src/documents.js +++ b/src/documents.js @@ -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) diff --git a/test/documents.spec.js b/test/documents.spec.js index 9cf74b7..89af343 100644 --- a/test/documents.spec.js +++ b/test/documents.spec.js @@ -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]) }) }) })