This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkeytransform.spec.js
59 lines (53 loc) · 1.64 KB
/
keytransform.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import all from 'it-all'
import { Key } from 'interface-datastore/key'
import { MemoryDatastore } from '../src/memory.js'
import { KeyTransformDatastore } from '../src/keytransform.js'
describe('KeyTransformDatastore', () => {
it('basic', async () => {
const mStore = new MemoryDatastore()
const transform = {
/**
* @param {Key} key
*/
convert (key) {
return new Key('/abc').child(key)
},
/**
* @param {Key} key
*/
invert (key) {
const l = key.list()
if (l[0] !== 'abc') {
throw new Error('missing prefix, convert failed?')
}
return Key.withNamespaces(l.slice(1))
}
}
const kStore = new KeyTransformDatastore(mStore, transform)
const keys = [
'foo',
'foo/bar',
'foo/bar/baz',
'foo/barb',
'foo/bar/bazb',
'foo/bar/baz/barb'
].map((s) => new Key(s))
await Promise.all(keys.map((key) => kStore.put(key, key.uint8Array())))
const kResults = Promise.all(keys.map((key) => kStore.get(key)))
const mResults = Promise.all(keys.map((key) => mStore.get(new Key('abc').child(key))))
const results = await Promise.all([kResults, mResults])
expect(results[0]).to.eql(results[1])
const mRes = await all(mStore.query({}))
const kRes = await all(kStore.query({}))
expect(kRes).to.have.length(mRes.length)
mRes.forEach((a, i) => {
const kA = a.key
const kB = kRes[i].key
expect(transform.invert(kA)).to.eql(kB)
expect(kA).to.eql(transform.convert(kB))
})
await kStore.close()
})
})