Skip to content

Commit 58d6acb

Browse files
author
Benjamin Forster
committed
add more tests; fix bug with putting prefixes
1 parent 9d14f0f commit 58d6acb

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Graph (storage, key, opts) {
3636

3737
if (!opts) opts = {}
3838
this.db = hyperdb(storage, key, opts)
39-
this._prefixes = opts.prefixes || constants.DEFAULT_PREFIXES
39+
this._prefixes = Object.assign({}, opts.prefixes || constants.DEFAULT_PREFIXES)
4040
this._prefixes._ = opts.base || constants.DEFAULT_BASE
4141
this._indexes = opts.index === 'tri'
4242
? constants.HEXSTORE_INDEXES_REDUCED

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function put (db, data, callback) {
2121
function next (err) {
2222
if (err) return callback(err)
2323
var v = data[i++]
24-
db.put(v[0], v[1], (i < data.length - 1) ? next : callback)
24+
db.put(v[0], v[1], (i < data.length) ? next : callback)
2525
}
2626
}
2727

test/basic.spec.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const hyperdb = require('hyperdb')
77

88
const hypergraph = require('../index')
99
const constants = require('../lib/constants')
10+
const prefixes = require('../lib/prefixes')
1011

1112
function ramStore (filename) {
1213
// filename will be one of: data, bitfield, tree, signatures, key, secret_key
@@ -72,11 +73,44 @@ describe('hypergraph', function () {
7273
db = hypergraph(ramStore)
7374
db.on('ready', () => {
7475
const stream = db.db.createReadStream('@prefix/')
76+
var count = 0
7577
stream.on('data', (nodes) => {
76-
// add test
78+
const prefix = prefixes.fromKey(nodes[0].key)
79+
count++
80+
expect(nodes[0].value.toString()).to.eql(constants.DEFAULT_PREFIXES[prefix])
7781
})
7882
stream.on('error', done)
7983
stream.on('end', () => {
84+
expect(count).to.eql(Object.keys(constants.DEFAULT_PREFIXES).length)
85+
done()
86+
})
87+
})
88+
})
89+
it('includes only specified prefixes (options.prefixes)', (done) => {
90+
var customPrefixes = {
91+
schema: 'http://schema.org/',
92+
library: 'http://purl.org/library/',
93+
void: 'http://rdfs.org/ns/void#',
94+
dct: 'http://purl.org/dc/terms/',
95+
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
96+
madsrdf: 'http://www.loc.gov/mads/rdf/v1#',
97+
discovery: 'http://worldcat.org/vocab/discovery/',
98+
bgn: 'http://bibliograph.net/',
99+
pto: 'http://www.productontology.org/id/',
100+
dc: 'http://purl.org/dc/elements/1.1/'
101+
}
102+
db = hypergraph(ramStore, { prefixes: customPrefixes })
103+
db.on('ready', () => {
104+
const stream = db.db.createReadStream('@prefix/')
105+
var count = 0
106+
stream.on('data', (nodes) => {
107+
const prefix = prefixes.fromKey(nodes[0].key)
108+
count++
109+
expect(nodes[0].value.toString()).to.eql(customPrefixes[prefix])
110+
})
111+
stream.on('error', done)
112+
stream.on('end', () => {
113+
expect(count).to.eql(Object.keys(customPrefixes).length)
80114
done()
81115
})
82116
})

0 commit comments

Comments
 (0)