From f9be05d0503a79e03fcda86215c5c10179447528 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 8 Nov 2016 17:36:00 +0100 Subject: [PATCH] Add promise/callback tests --- src/dht.js | 88 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/src/dht.js b/src/dht.js index 0953d517..23037b3c 100644 --- a/src/dht.js +++ b/src/dht.js @@ -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) => { @@ -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', () => {}) }) }