Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: use correct property when checking if resolver exists
Browse files Browse the repository at this point in the history
The right property is `.codec` and not `.format`.

Adds a test for the same and also runs the custom format tests again.
  • Loading branch information
achingbrain authored and vmx committed Oct 29, 2020
1 parent 1a115e7 commit b412899
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class IPLDResolver {
*/
addFormat (format) {
const codec = format.codec
if (this.resolvers[format.format]) {

if (this.resolvers[format.codec]) {
const codecName = multicodec.print[codec]
throw new Error(`Resolver already exists for codec "${codecName}"`)
}
Expand Down
112 changes: 60 additions & 52 deletions test/format-support.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,80 @@ const inMemory = require('ipld-in-memory')

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

module.exports = (repo) => {
describe('IPLD format support', () => {
let data, cid
describe('IPLD format support', () => {
let data, cid

before(async () => {
const resolver = await inMemory(IPLDResolver)
before(async () => {
const resolver = await inMemory(IPLDResolver)

data = { now: Date.now() }
data = { now: Date.now() }

cid = await resolver.put(data, multicodec.DAG_CBOR)
})

describe('Dynamic format loading', () => {
it('should fail to dynamically load format', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: []
})
cid = await resolver.put(data, multicodec.DAG_CBOR)
})

const result = resolver.resolve(cid, '')
await expect(result.next()).to.be.rejectedWith(
'No resolver found for codec "dag-cbor"')
describe('Dynamic format loading', () => {
it('should fail to dynamically load format', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: []
})

it('should fail to dynamically load format via loadFormat option', async () => {
const errMsg = 'BOOM' + Date.now()
const resolver = await inMemory(IPLDResolver, {
formats: [],
loadFormat (codec) {
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
throw new Error(errMsg)
}
})
const result = resolver.resolve(cid, '')
await expect(result.next()).to.be.rejectedWith(
'No resolver found for codec "dag-cbor"')
})

const result = resolver.resolve(cid, '')
await expect(result.next()).to.be.rejectedWith(errMsg)
it('should fail to dynamically load format via loadFormat option', async () => {
const errMsg = 'BOOM' + Date.now()
const resolver = await inMemory(IPLDResolver, {
formats: [],
loadFormat (codec) {
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
throw new Error(errMsg)
}
})

it('should dynamically load missing format', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: [],
loadFormat (codec) {
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
return dagCBOR
const result = resolver.resolve(cid, '')
await expect(result.next()).to.be.rejectedWith(errMsg)
})

it('should dynamically load missing format', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: [],
loadFormat (codec) {
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
})
return dagCBOR
}
})

const result = resolver.resolve(cid, '')
const node = await result.first()
expect(node.value).to.eql(data)
cid = await resolver.put(data, multicodec.DAG_CBOR)

await expect(resolver.get(cid)).to.eventually.eql(data)
})

it('should not dynamically load format added statically', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: [dagCBOR],
loadFormat (codec) {
throw new Error(`unexpected load format ${codec}`)
}
})

it('should not dynamically load format added statically', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: [dagCBOR],
loadFormat (codec) {
throw new Error(`unexpected load format ${codec}`)
}
})
cid = await resolver.put(data, multicodec.DAG_CBOR)

await expect(resolver.get(cid)).to.eventually.eql(data)
})

const result = resolver.resolve(cid, '')
await result.next()
it('should throw if adding format twice', async () => {
const resolver = await inMemory(IPLDResolver, {
formats: []
})
resolver.addFormat(dagCBOR)

expect(() => resolver.addFormat(dagCBOR)).to.throw(/Resolver already exists/)
})
})
}
})

0 comments on commit b412899

Please sign in to comment.