Skip to content

Commit 9d14f0f

Browse files
author
Benjamin Forster
committed
add tests - fix detection of new db
1 parent fe757a1 commit 9d14f0f

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const tripleAliasMap = {
1010
}
1111

1212
function isNewDatabase (db) {
13-
return db._writers.length > 0 || db._writers[0].feed.length > 0
13+
return !(db._writers.length > 1 || db._writers[0].feed.length > 0)
1414
}
1515

1616
function put (db, data, callback) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"istanbul": "^0.4.5",
2424
"mocha": "^5.0.0",
2525
"random-access-memory": "^2.4.0",
26-
"standard": "^10.0.3"
26+
"standard": "^10.0.3",
27+
"tmp": "0.0.33"
2728
},
2829
"scripts": {
2930
"test": "mocha test/**.spec.js",

test/basic.spec.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* eslint-env mocha */
22
const expect = require('chai').expect
33
const ram = require('random-access-memory')
4+
const tmp = require('tmp')
5+
const path = require('path')
6+
const hyperdb = require('hyperdb')
7+
48
const hypergraph = require('../index')
59
const constants = require('../lib/constants')
610

@@ -69,7 +73,7 @@ describe('hypergraph', function () {
6973
db.on('ready', () => {
7074
const stream = db.db.createReadStream('@prefix/')
7175
stream.on('data', (nodes) => {
72-
76+
// add test
7377
})
7478
stream.on('error', done)
7579
stream.on('end', () => {
@@ -79,7 +83,38 @@ describe('hypergraph', function () {
7983
})
8084
})
8185
context('when loading db that already exists', () => {
82-
it('does not add new metadata')
86+
it('does not add new metadata', (done) => {
87+
tmp.dir(function (err, dir, cleanupCallback) {
88+
if (err) return done(err)
89+
console.log('Dir: ', dir)
90+
const dbDir = path.join(dir, 'test.db')
91+
// create new hyperdb
92+
const hyper = hyperdb(dbDir)
93+
hyper.on('ready', () => {
94+
// as something so that its not an empty feed
95+
hyper.put('test', 'data', (err) => {
96+
if (err) return finish(err)
97+
openExistingDBAsGraphDB()
98+
})
99+
})
100+
hyper.on('error', finish)
101+
102+
function openExistingDBAsGraphDB () {
103+
db = hypergraph(dbDir)
104+
db.on('ready', () => {
105+
db.db.get('@version', (err, nodes) => {
106+
expect(nodes).to.eql(null)
107+
done(err)
108+
})
109+
})
110+
db.on('error', finish)
111+
}
112+
function finish (e) {
113+
cleanupCallback()
114+
done(e)
115+
}
116+
})
117+
})
83118
it('overrides options with metadata set in hyperdb (index)')
84119
it('overrides options with metadata set in hyperdb (name)')
85120
})

0 commit comments

Comments
 (0)