orbitdb/test/db/document-store.test.js
haad a063b3fb4a Refactor OrbitDB
Fix sync
Fix linter
Fix tests
Clean up
Set default references count to 0
Fix sync
Use address instead of databaseId
Sync protocol
Keep references to open databases in OrbitDB
Fix append benchmark
Initial version of heads exchange
Remove Feed
Fix KeyValuePersisted iterator
Refactor OrbitDBAddress a bit more
Add rest of the database types
Refactor OrbitDB addresses
Initial version for the full circle
Initial structure and tests for new OrbitDB
Make sure KeyStore is open when a Database is created
Re-organize OrbitDB
Use new databases and Log
More clean up
Add 'drop' event to Database
Clean up OrbitDB
Remove id from OrbitDB
Use new KeyStore and Identities
Remove storage from OrbitDB
Remove migrations from OrbitDB
Remove caches from OrbitDB
Remove pubsub from OrbitDB
2023-03-01 16:21:07 +02:00

264 lines
7.5 KiB
JavaScript

import { deepStrictEqual, strictEqual } from 'assert'
import rimraf from 'rimraf'
import { Log, Entry } from '../../src/oplog/index.js'
import { DocumentStore, Database } from '../../src/db/index.js'
import { IPFSBlockStorage, LevelStorage } from '../../src/storage/index.js'
import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import { createTestIdentities, cleanUpTestIdentities } from '../fixtures/orbit-db-identity-keys.js'
const { sync: rmrf } = rimraf
const OpLog = { Log, Entry, IPFSBlockStorage, LevelStorage }
Object.keys(testAPIs).forEach((IPFS) => {
describe('DocumentStore Database (' + IPFS + ')', function () {
this.timeout(config.timeout * 2)
let ipfsd
let ipfs
let keystore, signingKeyStore
let accessController
let identities1
let testIdentity1
let db
const databaseId = 'documentstore-AAA'
before(async () => {
// Start two IPFS instances
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
const [identities, testIdentities] = await createTestIdentities(ipfs)
identities1 = identities[0]
testIdentity1 = testIdentities[0]
rmrf(testIdentity1.id)
})
after(async () => {
await cleanUpTestIdentities([identities1])
if (ipfsd) {
await stopIpfs(ipfsd)
}
if (keystore) {
await keystore.close()
}
if (signingKeyStore) {
await signingKeyStore.close()
}
if (testIdentity1) {
rmrf(testIdentity1.id)
}
})
describe('Default index \'_id\'', () => {
beforeEach(async () => {
db = await DocumentStore({ OpLog, Database, ipfs, identity: testIdentity1, address: databaseId, accessController })
})
afterEach(async () => {
if (db) {
await db.drop()
await db.close()
}
})
it('creates a document store', async () => {
strictEqual(db.address.toString(), databaseId)
strictEqual(db.type, 'documentstore')
strictEqual(db.indexBy, '_id')
})
it('gets a document', async () => {
const key = 'hello world 1'
const expected = { _id: key, msg: 'writing 1 to db' }
await db.put(expected)
const doc = await db.get(key)
deepStrictEqual(doc, expected)
})
it('throws an error when putting a document with the wrong key', async () => {
let err
const key = 'hello world 1'
const expected = { wrong_key: key, msg: 'writing 1 to db' }
try {
await db.put(expected)
} catch (e) {
err = e
}
strictEqual(err.message, 'The provided document doesn\'t contain field \'_id\'')
})
it('throws an error when getting a document with the wrong key', async () => {
let err
const key = 'hello world 1'
const expected = { wrong_key: key, msg: 'writing 1 to db' }
try {
await db.put(expected)
} catch (e) {
err = e
}
strictEqual(err.message, 'The provided document doesn\'t contain field \'_id\'')
})
it('deletes a document', async () => {
const key = 'hello world 1'
await db.put({ _id: key, msg: 'writing 1 to db' })
await db.del(key)
const doc = await db.get(key)
strictEqual(doc, undefined)
})
it('throws an error when deleting a non-existent document', async () => {
const key = 'i do not exist'
let err
try {
await db.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 db', views: 10 }
await db.put({ _id: 'hello world 1', msg: 'writing 1 to db', views: 10 })
await db.put({ _id: 'hello world 2', msg: 'writing 2 to db', views: 5 })
await db.put({ _id: 'hello world 3', msg: 'writing 3 to db', views: 12 })
await db.del('hello world 3')
await db.put(expected)
const findFn = (doc) => doc.views > 5
deepStrictEqual(await db.query(findFn), [expected])
})
it('queries for a non-existent document', async () => {
await db.put({ _id: 'hello world 1', msg: 'writing 1 to db', views: 10 })
await db.del('hello world 1')
const findFn = (doc) => doc.views > 5
deepStrictEqual(await db.query(findFn), [])
})
})
describe('Custom index \'doc\'', () => {
beforeEach(async () => {
db = await DocumentStore({ OpLog, Database, ipfs, identity: testIdentity1, address: databaseId, accessController, indexBy: 'doc' })
})
afterEach(async () => {
if (db) {
await db.drop()
await db.close()
}
})
it('creates a document store', async () => {
strictEqual(db.address.toString(), databaseId)
strictEqual(db.type, 'documentstore')
strictEqual(db.indexBy, 'doc')
})
it('gets a document', async () => {
const key = 'hello world 1'
const expected = { doc: key, msg: 'writing 1 to db' }
await db.put(expected)
const doc = await db.get(key)
deepStrictEqual(doc, expected)
})
it('deletes a document', async () => {
const key = 'hello world 1'
await db.put({ doc: key, msg: 'writing 1 to db' })
await db.del(key)
const doc = await db.get(key)
strictEqual(doc, undefined)
})
it('throws an error when putting a document with the wrong key', async () => {
let err
const key = 'hello world 1'
const expected = { _id: key, msg: 'writing 1 to db' }
try {
await db.put(expected)
} catch (e) {
err = e
}
strictEqual(err.message, 'The provided document doesn\'t contain field \'doc\'')
})
it('throws an error when getting a document with the wrong key', async () => {
let err
const key = 'hello world 1'
const expected = { _id: key, msg: 'writing 1 to db' }
try {
await db.put(expected)
} catch (e) {
err = e
}
strictEqual(err.message, 'The provided document doesn\'t contain field \'doc\'')
})
it('throws an error when deleting a non-existent document', async () => {
const key = 'i do not exist'
let err
try {
await db.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 = { doc: 'hello world 1', msg: 'writing new 1 to db', views: 10 }
await db.put({ doc: 'hello world 1', msg: 'writing 1 to db', views: 10 })
await db.put({ doc: 'hello world 2', msg: 'writing 2 to db', views: 5 })
await db.put({ doc: 'hello world 3', msg: 'writing 3 to db', views: 12 })
await db.del('hello world 3')
await db.put(expected)
const findFn = (doc) => doc.views > 5
deepStrictEqual(await db.query(findFn), [expected])
})
it('queries for a non-existent document', async () => {
await db.put({ doc: 'hello world 1', msg: 'writing 1 to db', views: 10 })
await db.del('hello world 1')
const findFn = (doc) => doc.views > 5
deepStrictEqual(await db.query(findFn), [])
})
})
})
})