mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
test: For a non-existent document.
This commit is contained in:
parent
2af2f7d8fb
commit
2d425f7323
@ -11,9 +11,9 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
|
|||||||
*/
|
*/
|
||||||
const put = async (doc) => {
|
const put = async (doc) => {
|
||||||
const key = doc[indexBy]
|
const key = doc[indexBy]
|
||||||
|
|
||||||
if (!key) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
|
if (!key) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
|
||||||
|
|
||||||
return addOperation({ op: 'PUT', key, value: doc })
|
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.
|
* @returns {string} The hash of the new oplog entry.
|
||||||
*/
|
*/
|
||||||
const del = async (key) => {
|
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 })
|
return addOperation({ op: 'DEL', key, value: null })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +38,11 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
|
|||||||
const get = async (key) => {
|
const get = async (key) => {
|
||||||
for await (const doc of iterator()) {
|
for await (const doc of iterator()) {
|
||||||
if (key === doc[indexBy]) {
|
if (key === doc[indexBy]) {
|
||||||
return doc
|
return doc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries the document store for documents matching mapper filters.
|
* 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 query = async (findFn) => {
|
||||||
const results = []
|
const results = []
|
||||||
|
|
||||||
for await (const doc of iterator()) {
|
for await (const doc of iterator()) {
|
||||||
if (findFn(doc)) {
|
if (findFn(doc)) {
|
||||||
results.push(doc)
|
results.push(doc)
|
||||||
|
@ -57,12 +57,12 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
// Create an identity for each peers
|
// Create an identity for each peers
|
||||||
testIdentity1 = await createIdentity({ id: 'userA', keystore, signingKeystore })
|
testIdentity1 = await createIdentity({ id: 'userA', keystore, signingKeystore })
|
||||||
testIdentity2 = await createIdentity({ id: 'userB', keystore, signingKeystore })
|
testIdentity2 = await createIdentity({ id: 'userB', keystore, signingKeystore })
|
||||||
|
|
||||||
const accessController = {
|
const accessController = {
|
||||||
canAppend: (entry) => entry.identity.id === testIdentity1.id
|
canAppend: (entry) => entry.identity.id === testIdentity1.id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
db1 = await Documents({ OpLog: Log, Database, ipfs: ipfs1, identity: testIdentity1, databaseId, accessController })
|
db1 = await Documents({ OpLog: Log, Database, ipfs: ipfs1, identity: testIdentity1, databaseId, accessController })
|
||||||
})
|
})
|
||||||
@ -104,38 +104,51 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
|
|
||||||
describe('using database', () => {
|
describe('using database', () => {
|
||||||
it('gets a document', async () => {
|
it('gets a document', async () => {
|
||||||
const key = 'hello world 1'
|
const key = 'hello world 1'
|
||||||
|
|
||||||
const expected = { _id: key, msg: 'writing 1 to db1' }
|
|
||||||
|
|
||||||
await db1.put(expected)
|
const expected = { _id: key, msg: 'writing 1 to db1' }
|
||||||
|
|
||||||
const doc = await db1.get(key)
|
await db1.put(expected)
|
||||||
deepStrictEqual(doc, expected)
|
|
||||||
|
const doc = await db1.get(key)
|
||||||
|
deepStrictEqual(doc, expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('deletes a document', async () => {
|
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.put({ _id: key, msg: 'writing 1 to db1' })
|
||||||
await db1.del(key)
|
await db1.del(key)
|
||||||
|
|
||||||
const doc = await db1.get(key)
|
const doc = await db1.get(key)
|
||||||
strictEqual(doc, undefined)
|
strictEqual(doc, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('queries a document', async () => {
|
it('throws an error when deleting a non-existent document', async () => {
|
||||||
const expected = { _id: 'hello world 1', msg: 'writing new 1 to db1', views: 10 }
|
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 })
|
try {
|
||||||
await db1.put({ _id: 'hello world 3', msg: 'writing 3 to db1', views: 12 })
|
await db1.del(key)
|
||||||
await db1.del('hello world 3')
|
} catch (e) {
|
||||||
await db1.put(expected)
|
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])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user