Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Add promise/callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb committed Nov 8, 2016
1 parent 0f55017 commit f9be05d
Showing 1 changed file with 63 additions and 25 deletions.
88 changes: 63 additions & 25 deletions src/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
const expect = require('chai').expect

module.exports = (common) => {
describe.only('.dht', () => {
describe('.dht', () => {
let ipfs
let peers

before((done) => {
common.setup((err, factory) => {
Expand All @@ -21,42 +22,79 @@ module.exports = (common) => {
after((done) => {
common.teardown(done)
})
xdescribe('.findpeer', () => {})
describe('.get', (done) => {
it('errors when getting a non-existent key from the DHT', (done) => {
ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => {
expect(err).to.be.an.instanceof(Error)
done()

describe('callback API', () => {
describe('.get', (done) => {
it('errors when getting a non-existent key from the DHT', (done) => {
ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})
})
// belongs in put or integration
it('puts and gets a key value pair in the DHT', (done) => {
ipfs.dht.put('scope', 'interplanetary', (err, res) => {
expect(err).to.not.exist

expect(res).to.be.an('array')
describe('.findprovs', () => {
it('finds providers', (done) => {
ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
expect(err).to.not.exist

expect(res).to.be.an('array')
done()
})
})
})
})
describe('promise API', () => {
describe('.get', (done) => {
it('errors when getting a non-existent key from the DHT', (done) => {
ipfs.dht.get('non-existing', {timeout: '100ms'}).catch((err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})
})
describe('.findprovs', () => {
it('finds providers', (done) => {
ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP').then((res) => {
expect(res).to.be.an('array')
done()
}).catch(done)
})
})
})
// Tests below are tests that haven't been implemented yet or is not
// passing currently
xdescribe('.findpeer', () => {
it('finds other peers', (done) => {
peers.a.ipfs.dht.findpeer(peers.b.peerID, (err, foundPeer) => {
expect(err).to.be.empty
expect(foundPeer.peerID).to.be.equal(peers.b.peerID)
done()
})
})
it('fails to find other peer, if peer doesnt exists', (done) => {
peers.a.ipfs.dht.findpeer('ARandomPeerID', (err, foundPeer) => {
expect(err).to.be.instanceof(Error)
expect(foundPeer).to.be.equal(null)
done()
// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234
// apiClients.a.dht.get('scope', (err, value) => {
// expect(err).to.not.exist
// expect(value).to.be.equal('interplanetary')
// done()
// })
})
})
})
xdescribe('.put', () => {})
xdescribe('.query', () => {})
describe('.findprovs', () => {
it('finds providers', (done) => {
ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
xdescribe('.put & .get', () => {
it('puts and gets a key value pair in the DHT', (done) => {
peers.a.ipfs.dht.put('scope', 'interplanetary', (err, res) => {
expect(err).to.not.exist

expect(res).to.be.an('array')
done()

// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234
peers.b.ipfs.dht.get('scope', (err, value) => {
expect(err).to.not.exist
expect(value).to.be.equal('interplanetary')
done()
})
})
})
})
xdescribe('.query', () => {})
})
}

0 comments on commit f9be05d

Please sign in to comment.