Test/database (#38)

* test: Database.

* test: Remove caching test. Caching is superseded by the various storage options.

* test: db2 replicates data in existing db1.

* test: Move event tests to generic database tests.

* test: Single instance cleanup.

* fix: Linting.

* Fix Sync race condition (#39)

* test: Fix key path import.

---------

Co-authored-by: Haad <haadcode@users.noreply.github.com>
This commit is contained in:
Hayden Young
2023-03-11 21:01:05 +08:00
committed by GitHub
parent bc816c7e2e
commit a40bc8bdcf
10 changed files with 391 additions and 99 deletions

99
test/database.test.js Normal file
View File

@@ -0,0 +1,99 @@
import { strictEqual, deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../src/index.js'
import config from './config.js'
import testKeysPath from './fixtures/test-keys-path.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
describe('Database', function () {
this.timeout(30000)
let ipfs
let keystore
let identities
let testIdentity
let db
const databaseId = 'documentstore-AAA'
const accessController = {
canAppend: async (entry) => {
const identity1 = await identities.getIdentity(entry.identity)
return identity1.id === testIdentity.id
}
}
before(async () => {
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })
identities = await Identities({ keystore })
testIdentity = await identities.createIdentity({ id: 'userA' })
})
after(async () => {
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
await keystore.close()
}
await rmrf(keysPath)
await rmrf('./ipfs1')
})
beforeEach(async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb1' })
})
afterEach(async () => {
await rmrf('./orbitdb1')
})
it('adds an operation', async () => {
const expected = 'zdpuAqQ9TJpMhPShuT315m2D9LUBkBPy8YX9zatjEynd2suZv'
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const actual = await db.addOperation(op)
deepStrictEqual(actual, expected)
await db.close()
})
describe('Events', () => {
it('emits \'close\' when the database is closed', async () => {
let closed = false
const onClose = () => {
closed = true
}
db.events.on('close', onClose)
await db.close()
strictEqual(closed, true)
})
it('emits \'drop\' when the database is dropped', async () => {
let dropped = false
const onDrop = () => {
dropped = true
}
db.events.on('drop', onDrop)
await db.drop()
strictEqual(dropped, true)
await db.close()
})
})
})