orbitdb/test/kvstore.test.js
haad 42885b20a4 Write permissions for databases
Use latest store modules from npm
Update README
Update docs
Update examples
Update benchmarks
Update dependencies
Add Getting Started guide
Add new a screenshot
Add a new live demo
Add persistency tests for snapshot saving/loading and events
Add network stress tests (but skip them by default as they're very heavy and lengthy)
Add browser benchmarks
Add log() alias for eventlog() database
Add possibility to create database if it doesn't exist yet
Add support for orbitdb addresses
Add a test for starting replication when peers connect
Add debug build
Use IPFS nodeID as default user id
Use ipfs-pubsub-room
Handle closing of databases properly
Handle cache errors
Clean up tests, re-organize code files
Clean up code style
Support for CLI
Remove obsolete scripts
2017-11-28 09:10:51 +01:00

124 lines
2.9 KiB
JavaScript

'use strict'
const assert = require('assert')
const rmrf = require('rimraf')
const OrbitDB = require('../src/OrbitDB')
const config = require('./utils/config')
const startIpfs = require('./utils/start-ipfs')
const dbPath = './orbitdb/tests/kvstore'
const ipfsPath = './orbitdb/tests/kvstore/ipfs'
describe('orbit-db - Key-Value Store', function() {
this.timeout(config.timeout)
let ipfs, orbitdb1, orbitdb2, db
before(async () => {
config.daemon1.repo = ipfsPath
rmrf.sync(config.daemon1.repo)
rmrf.sync(dbPath)
ipfs = await startIpfs(config.daemon1)
orbitdb1 = new OrbitDB(ipfs, dbPath + '/1')
orbitdb2 = new OrbitDB(ipfs, dbPath + '/2')
})
after(async () => {
if(orbitdb1)
orbitdb1.stop()
if(orbitdb2)
orbitdb2.stop()
if (ipfs)
await ipfs.stop()
})
beforeEach(async () => {
db = await orbitdb1.kvstore(config.dbname, { path: dbPath })
})
afterEach(async () => {
await db.drop()
})
it('creates and opens a database', async () => {
db = await orbitdb1.keyvalue('first kv database')
db = await orbitdb1.keyvalue('first kv database')
})
it('put', async () => {
await db.put('key1', 'hello1')
const value = db.get('key1')
assert.equal(value, 'hello1')
})
it('get', async () => {
await db.put('key1', 'hello2')
const value = db.get('key1')
assert.equal(value, 'hello2')
})
it('put updates a value', async () => {
await db.put('key1', 'hello3')
await db.put('key1', 'hello4')
const value = db.get('key1')
assert.equal(value, 'hello4')
})
it('set is an alias for put', async () => {
await db.set('key1', 'hello5')
const value = db.get('key1')
assert.equal(value, 'hello5')
})
it('put/get - multiple keys', async () => {
await db.put('key1', 'hello1')
await db.put('key2', 'hello2')
await db.put('key3', 'hello3')
const v1 = db.get('key1')
const v2 = db.get('key2')
const v3 = db.get('key3')
assert.equal(v1, 'hello1')
assert.equal(v2, 'hello2')
assert.equal(v3, 'hello3')
})
it('deletes a key', async () => {
await db.put('key1', 'hello!')
await db.del('key1')
const value = db.get('key1')
assert.equal(value, null)
})
it('deletes a key after multiple updates', async () => {
await db.put('key1', 'hello1')
await db.put('key1', 'hello2')
await db.put('key1', 'hello3')
await db.del('key1')
const value = db.get('key1')
assert.equal(value, null)
})
it('get - integer value', async () => {
const val = 123
await db.put('key1', val)
const v1 = db.get('key1')
assert.equal(v1, val)
})
it('get - object value', async () => {
const val = { one: 'first', two: 2 }
await db.put('key1', val)
const v1 = db.get('key1')
assert.deepEqual(v1, val)
})
it('get - array value', async () => {
const val = [1, 2, 3, 4, 5]
await db.put('key1', val)
const v1 = db.get('key1')
assert.deepEqual(v1, val)
})
})