Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: add test and fix constructor
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
jacobheun authored and daviddias committed May 29, 2018
1 parent ba6e47a commit 396f657
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ node_modules
lib
dist
test/test-repo/datastore
init-default
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
"pull-stream": "^3.6.1"
},
"devDependencies": {
"aegir": "^12.1.3",
"async": "^2.5.0",
"aegir": "^13.1.0",
"async": "^2.6.0",
"chai": "^4.1.2",
"cids": "~0.5.2",
"cids": "~0.5.3",
"dirty-chai": "^2.0.1",
"flow-bin": "^0.58.0",
"memdown": "^1.4.1",
Expand Down
25 changes: 15 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ class LevelDatastore {
/* :: db: levelup */

constructor (path /* : string */, opts /* : ?LevelOptions */) {
// Default to leveldown db
const database = opts && opts.db ? opts.db : require('leveldown')
delete opts.db
let database

if (opts && opts.db) {
database = opts.db
delete opts.db
} else {
// Default to leveldown db
database = require('leveldown')
}

this.db = levelup(
database(
path,
Object.assign({}, opts, {
compression: false, // same default as go
valueEncoding: 'binary'
})
), (err) => {
database(path),
Object.assign({}, opts, {
compression: false, // same default as go
valueEncoding: 'binary'
}),
(err) => {
// Prevent an uncaught exception error on duplicate locks
if (err) {
throw err
Expand Down
38 changes: 38 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,49 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const memdown = require('memdown')
const LevelDown = require('leveldown')

const LevelStore = require('../src')

describe('LevelDatastore', () => {
describe('initialization', () => {
it('should default to a leveldown database', (done) => {
const levelStore = new LevelStore('init-default')

levelStore.open((err) => {
expect(err).to.not.exist()
expect(levelStore.db.db instanceof LevelDown).to.equal(true)
expect(levelStore.db.options).to.include({
createIfMissing: true,
errorIfExists: false
})
done()
})
})

it('should be able to override the database', (done) => {
const levelStore = new LevelStore('init-default', {
db: memdown,
createIfMissing: true,
errorIfExists: true
})

levelStore.open((err) => {
expect(err).to.not.exist()
expect(levelStore.db.db instanceof memdown).to.equal(true)
expect(levelStore.db.options).to.include({
createIfMissing: true,
errorIfExists: true
})
done()
})
})
})

describe('interface-datastore (memdown)', () => {
require('interface-datastore/src/tests')({
setup (callback) {
Expand Down

0 comments on commit 396f657

Please sign in to comment.