From 488f61bf8bf81fc0de9245cf2c6fe7b6ceed5c06 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 16 Aug 2016 17:16:24 +0100 Subject: [PATCH] fix(block): tests --- API/block/README.md | 2 +- package.json | 2 +- src/block.js | 121 +++++++++++++++++++++----------------------- 3 files changed, 61 insertions(+), 64 deletions(-) diff --git a/API/block/README.md b/API/block/README.md index e5064b0f..bdb3f1d8 100644 --- a/API/block/README.md +++ b/API/block/README.md @@ -40,7 +40,7 @@ Where `block` can be: - `Buffer` - the raw bytes of the Block - [`Block`][block] instance -`callback` has the signature `function (err) {}`, where `err` is an error if the operation was not successful. +`callback` has the signature `function (err, block) {}`, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block. If no `callback` is passed, a promise is returned. diff --git a/package.json b/package.json index 1c7c9006..d07b7625 100644 --- a/package.json +++ b/package.json @@ -48,4 +48,4 @@ "greenkeeperio-bot ", "nginnever " ] -} \ No newline at end of file +} diff --git a/src/block.js b/src/block.js index c81684af..39bb682c 100644 --- a/src/block.js +++ b/src/block.js @@ -1,87 +1,84 @@ /* eslint-env mocha */ -/* globals apiClients */ +/* eslint max-nested-callbacks: ["error", 8] */ + 'use strict' const expect = require('chai').expect module.exports = (common) => { - describe.only('.block', () => { - const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' - const blorb = Buffer('blorb') - - it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb], (err) => { - console.log(err) - expect(err).to.be.an.instanceof(Error) - }) - }) + describe('.block', () => { + let ipfs - it('block.put', (done) => { - apiClients.a.block.put(blorb, (err, res) => { - expect(err).to.not.exist - expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') - done() - }) - }) + before(function (done) { + // CI takes longer to instantiate the daemon, + // so we need to increase the timeout for the + // before step + this.timeout(20 * 1000) - it('block.get', (done) => { - apiClients.a.block.get(blorbKey, (err, res) => { + common.setup((err, factory) => { expect(err).to.not.exist - - let buf = '' - res - .on('data', function (data) { buf += data }) - .on('end', function () { - expect(buf).to.be.equal('blorb') - done() - }) + factory.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) }) }) - it('block.stat', (done) => { - apiClients.a.block.stat(blorbKey, (err, res) => { - expect(err).to.not.exist - expect(res).to.have.property('Key') - expect(res).to.have.property('Size') - done() - }) + after((done) => { + common.teardown(done) }) - describe('promise', () => { - it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb]) - .catch((err) => { - expect(err).to.be.an.instanceof(Error) - }) + describe('callback API', () => { + it('.put', (done) => { + const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + const blob = Buffer('blorb') + + ipfs.block.put(blob, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.a.property('Key', expectedHash) + done() + }) }) - it('block.put', () => { - return apiClients.a.block.put(blorb) - .then((res) => { - expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') - }) + it('.put error with array of blocks', () => { + const blob = Buffer('blorb') + + ipfs.block.put([blob, blob], (err) => { + expect(err).to.be.an.instanceof(Error) + }) }) it('block.get', (done) => { - return apiClients.a.block.get(blorbKey) - .then((res) => { - let buf = '' - res - .on('data', function (data) { buf += data }) - .on('end', function () { - expect(buf).to.be.equal('blorb') - done() - }) - }) + const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + + ipfs.block.get(hash, (err, res) => { + expect(err).to.not.exist + + // TODO review this + let buf = '' + res + .on('data', function (data) { buf += data }) + .on('end', function () { + expect(buf).to.be.equal('blorb') + done() + }) + }) }) - it('block.stat', () => { - return apiClients.a.block.stat(blorbKey) - .then((res) => { - expect(res).to.have.property('Key') - expect(res).to.have.property('Size') - }) + it('block.stat', (done) => { + const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' + + ipfs.block.stat(hash, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.property('Key') + expect(res).to.have.property('Size') + done() + }) }) }) + + describe('promise API', () => { + }) }) }