Skip to content

Commit

Permalink
test: add namespaced datastore query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Feb 21, 2024
1 parent ca09950 commit f15cd79
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions packages/datastore-core/test/namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ describe('NamespaceDatastore', () => {
'abc',
''
]

const keys = [
'foo',
'foo/bar',
'foo/bar/baz',
'foo/barb',
'foo/bar/bazb',
'foo/bar/baz/barb'
].map((s) => new Key(s))

prefixes.forEach((prefix) => it(`basic '${prefix}'`, async () => {
const mStore = new MemoryDatastore()
const store = new NamespaceDatastore(mStore, new Key(prefix))

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(async key => { await store.put(key, uint8ArrayFromString(key.toString())) }))
const nResults = Promise.all(keys.map(async (key) => store.get(key)))
const mResults = Promise.all(keys.map(async (key) => mStore.get(new Key(prefix).child(key))))
Expand All @@ -45,6 +46,41 @@ describe('NamespaceDatastore', () => {
expect(results[0]).to.eql(results[1])
}))

const setupStores = async (keys: Key[]): Promise<NamespaceDatastore[]> => {
const prefixes = ['abc', '123', 'sub/prefix']
const mStore = new MemoryDatastore()

return Promise.all(prefixes.map(async prefix => {
const store = new NamespaceDatastore(mStore, new Key(prefix))

await Promise.all(keys.map(async key => { await store.put(key, uint8ArrayFromString(key.toString())) }))

return store
}))
}

it('queries keys under each prefix', async () => {
const stores = await setupStores(keys)

for (const store of stores) {
const nRes = await all(store.queryKeys({}))

expect(nRes).deep.equal(keys)
}
})

it('queries values under each prefix', async () => {
const stores = await setupStores(keys)

for (const store of stores) {
const nRes = await all(store.query({}))
const values = keys.map(key => uint8ArrayFromString(key.toString()))

expect(nRes.map(p => p.key)).deep.equal(keys)
expect(nRes.map(p => p.value)).deep.equal(values)
}
})

prefixes.forEach((prefix) => {
describe(`interface-datastore: '${prefix}'`, () => {
interfaceDatastoreTests({
Expand Down

0 comments on commit f15cd79

Please sign in to comment.