From 5b3ebcf9c51ecbb70944e3080d38fd9bf40589c8 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Tue, 27 Sep 2016 11:01:26 +0200 Subject: [PATCH 1/5] refactor: update to new block api --- src/block.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/block.js b/src/block.js index 7512bb012..1543a475f 100644 --- a/src/block.js +++ b/src/block.js @@ -46,13 +46,14 @@ module.exports = (common) => { it('.put a block', (done) => { const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' - const blob = new Block(new Buffer('blorb')) - - ipfs.block.put(blob, (err, block) => { + Block.create(new Buffer('blorb'), (err, blob) => { expect(err).to.not.exist - expect(block.key).to.eql(multihash.fromB58String(expectedHash)) - expect(block.data).to.eql(new Buffer('blorb')) - done() + ipfs.block.put(blob, (err, block) => { + expect(err).to.not.exist + expect(block.key).to.eql(multihash.fromB58String(expectedHash)) + expect(block.data).to.eql(new Buffer('blorb')) + done() + }) }) }) From 3d03fd41bec77a358469b908bfcea1d1e7aa5917 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Wed, 28 Sep 2016 16:18:22 +0200 Subject: [PATCH 2/5] refactor object tests for async changes --- package.json | 6 +- src/object.js | 539 +++++++++++++++++++++++++++++--------------------- 2 files changed, 321 insertions(+), 224 deletions(-) diff --git a/package.json b/package.json index 69c877b1a..d139f21ae 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "interface-ipfs-core", "version": "0.15.0", "description": "A test suite and interface you can use to implement a IPFS core interface.", - "main": "lib/index.js", + "main": "src/index.js", "jsnext:main": "src/index.js", "scripts": { "test": "exit(0)", @@ -28,6 +28,7 @@ }, "homepage": "https://github.com/ipfs/interface-ipfs-core#readme", "dependencies": { + "async": "^2.0.1", "bl": "^1.1.2", "bs58": "^3.0.0", "chai": "^3.5.0", @@ -36,6 +37,7 @@ "ipfs-block": "^0.3.0", "ipfs-merkle-dag": "^0.7.0", "multihashes": "^0.2.2", + "promisify-es6": "^1.0.2", "readable-stream": "1.1.13", "run-series": "^1.1.4" }, @@ -51,4 +53,4 @@ "greenkeeperio-bot ", "nginnever " ] -} \ No newline at end of file +} diff --git a/src/object.js b/src/object.js index 39c71c4cd..db0958752 100644 --- a/src/object.js +++ b/src/object.js @@ -6,6 +6,36 @@ const expect = require('chai').expect const DAGNode = require('ipfs-merkle-dag').DAGNode const bs58 = require('bs58') +const parallel = require('async/parallel') +const waterfall = require('async/waterfall') +const promisify = require('promisify-es6') + +const checkMultihash = (n1, n2, cb) => { + parallel([ + (cb) => n1.multihash(cb), + (cb) => n2.multihash(cb) + ], (err, res) => { + if (err) { + return cb(err) + } + cb(null, res[0], res[1]) + }) +} +const sameMultihash = promisify((n1, n2, cb) => { + checkMultihash(n1, n2, (err, h1, h2) => { + expect(err).to.not.exist + expect(h2).to.be.eql(h2) + cb() + }) +}) + +const differentMultihash = promisify((n1, n2, cb) => { + checkMultihash(n1, n2, (err, h1, h2) => { + expect(err).to.not.exist + expect(h2).to.not.be.eql(h2) + cb() + }) +}) module.exports = (common) => { describe('.object', () => { @@ -29,14 +59,42 @@ module.exports = (common) => { common.teardown(done) }) + const putAndGet = promisify((putMethod, obj, getMethod, getParams, getOpts, assertions, done) => { + if (typeof getOpts === 'function') { + done = assertions + assertions = getOpts + getOpts = undefined + } + + waterfall([ + (cb) => ipfs.object[putMethod](obj, cb), + (node, cb) => waterfall([ + (cb) => getParams(node, cb), + (digest, cb) => { + if (getOpts) { + ipfs.object[getMethod](digest, getOpts, cb) + } else { + ipfs.object[getMethod](digest, cb) + } + } + ], (err, res) => { + expect(err).to.not.exist + assertions(node, res[1], cb) + }) + ], done) + }) + describe('callback API', () => { describe('.new', () => { it('no layout', (done) => { - ipfs.object.new((err, node) => { - expect(err).to.not.exist - expect(node.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + waterfall([ + (cb) => ipfs.object.new(cb), + (node, cb) => node.toJSON(cb), + (json, cb) => { + expect(json.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + cb() + } + ], done) }) }) @@ -47,14 +105,16 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(obj, (err, node) => { - expect(err).to.not.exist - const nodeJSON = node.toJSON() - expect(obj.Data).to.deep.equal(nodeJSON.Data) - expect(obj.Links).to.deep.equal(nodeJSON.Links) - expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') - done() - }) + waterfall([ + (cb) => ipfs.object.put(obj, cb), + (node, cb) => node.toJSON(cb), + (nodeJSON, cb) => { + expect(obj.Data).to.deep.equal(nodeJSON.Data) + expect(obj.Links).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + cb() + } + ], done) }) it('of json encoded buffer', (done) => { @@ -70,21 +130,23 @@ module.exports = (common) => { const buf = new Buffer(JSON.stringify(obj2)) - ipfs.object.put(buf, { enc: 'json' }, (err, node) => { - expect(err).to.not.exist - const nodeJSON = node.toJSON() + waterfall([ + (cb) => ipfs.object.put(buf, { enc: 'json' }, cb), + (node, cb) => node.toJSON((err, nodeJSON) => { + expect(err).to.not.exist - // because js-ipfs-api can't - // infer if the returned Data is Buffer or String - if (typeof node.data === 'string') { - node.data = new Buffer(node.data) - } + // because js-ipfs-api can't + // infer if the returned Data is Buffer or String + if (typeof node.data === 'string') { + node.data = new Buffer(node.data) + } - expect(obj.Data).to.deep.equal(node.data) - expect(obj.Links).to.deep.equal(nodeJSON.Links) - expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') - done() - }) + expect(obj.Data).to.deep.equal(node.data) + expect(obj.Links).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + cb() + }) + ], done) }) it('of protobuf encoded buffer', (done) => { @@ -95,32 +157,31 @@ module.exports = (common) => { expect(err).to.not.exist expect(dNode.data).to.deep.equal(node.data) expect(dNode.links).to.deep.equal(node.links) - expect(dNode.multihash()).to.deep.equal(node.multihash()) - done() + sameMultihash(dNode, node, done) }) }) it('of buffer treated as Data field', (done) => { const data = new Buffer('Some data') - ipfs.object.put(data, (err, node) => { - expect(err).to.not.exist - const nodeJSON = node.toJSON() - expect(data).to.deep.equal(nodeJSON.Data) - expect([]).to.deep.equal(nodeJSON.Links) - expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') - done() - }) + waterfall([ + (cb) => ipfs.object.put(data, cb), + (node, cb) => node.toJSON(cb), + (nodeJSON, cb) => { + expect(data).to.deep.equal(nodeJSON.Data) + expect([]).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + cb() + } + ], done) }) it('of DAGNode', (done) => { const dNode = new DAGNode(new Buffer('Some data')) - ipfs.object.put(dNode, (err, node) => { expect(err).to.not.exist expect(dNode.data).to.deep.equal(node.data) expect(dNode.links).to.deep.equal(node.links) - expect(dNode.multihash()).to.deep.equal(node.multihash()) - done() + sameMultihash(dNode, node, done) }) }) @@ -134,19 +195,20 @@ module.exports = (common) => { it('DAGNode with some DAGLinks', (done) => { const dNode1 = new DAGNode(new Buffer('Some data 1')) const dNode2 = new DAGNode(new Buffer('Some data 2')) - dNode1.addNodeLink('some-link', dNode2) - ipfs.object.put(dNode1, (err, node) => { - expect(err).to.not.exist - expect(dNode1.data).to.deep.equal(node.data) - expect( - dNode1.links.map((l) => l.toJSON()) - ).to.deep.equal( - node.links.map((l) => l.toJSON()) - ) - expect(dNode1.multihash()).to.deep.equal(node.multihash()) - done() - }) + waterfall([ + (cb) => dNode1.addNodeLink('some-link', dNode2, cb), + (cb) => ipfs.object.put(dNode1, cb), + (node, cb) => { + expect(dNode1.data).to.deep.equal(node.data) + expect( + dNode1.links.map((l) => l.toJSON()) + ).to.deep.equal( + node.links.map((l) => l.toJSON()) + ) + sameMultihash(dNode1, node, cb) + } + ], done) }) }) @@ -156,23 +218,22 @@ module.exports = (common) => { Data: new Buffer('get test object'), Links: [] } - - ipfs.object.put(testObj, (err, node1) => { - expect(err).to.not.exist - - ipfs.object.get(node1.multihash(), (err, node2) => { - expect(err).to.not.exist + putAndGet( + 'put', testObj, + 'get', (n, cb) => n.multihash(cb), + (node1, node2, cb) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof node2.data === 'string') { node2.data = new Buffer(node2.data) } - expect(node1.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) expect(node1.links).to.deep.equal(node2.links) - done() - }) - }) + sameMultihash(node1, node2, cb) + }, + done + ) }) it('with multihash (+ links)', (done) => { @@ -180,21 +241,21 @@ module.exports = (common) => { const dNode2 = new DAGNode(new Buffer('Some data 2')) dNode1.addNodeLink('some-link', dNode2) - ipfs.object.put(dNode1, (err, node1) => { - expect(err).to.not.exist - - ipfs.object.get(node1.multihash(), (err, node2) => { - expect(err).to.not.exist + putAndGet( + 'put', dNode1, + 'get', (n, cb) => n.multihash(cb), + (node1, node2, cb) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof node2.data === 'string') { node2.data = new Buffer(node2.data) } - expect(node1.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) - done() - }) - }) + sameMultihash(node1, node2, cb) + }, + done + ) }) it('with multihash base58 encoded', (done) => { @@ -203,22 +264,26 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node1) => { - expect(err).to.not.exist - - ipfs.object.get(bs58.encode(node1.multihash()), { enc: 'base58' }, (err, node2) => { + putAndGet( + 'put', testObj, + 'get', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest)) + }), + {enc: 'base58'}, + (node1, node2, cb) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof node2.data === 'string') { node2.data = new Buffer(node2.data) } - expect(node1.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) expect(node1.links).to.deep.equal(node2.links) - done() - }) - }) + sameMultihash(node1, node2, cb) + }, + done + ) }) it('with multihash base58 encoded toString', (done) => { @@ -226,23 +291,26 @@ module.exports = (common) => { Data: new Buffer('get test object'), Links: [] } - - ipfs.object.put(testObj, (err, node1) => { - expect(err).to.not.exist - - ipfs.object.get(bs58.encode(node1.multihash()).toString(), { enc: 'base58' }, (err, node2) => { + putAndGet( + 'put', testObj, + 'get', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest).toString()) + }), + {enc: 'base58'}, + (node1, node2, cb) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof node2.data === 'string') { node2.data = new Buffer(node2.data) } - expect(node1.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) expect(node1.links).to.deep.equal(node2.links) - done() - }) - }) + sameMultihash(node1, node2, cb) + }, + done + ) }) }) @@ -253,20 +321,21 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.data(node.multihash(), (err, data) => { - expect(err).to.not.exist + putAndGet( + 'put', testObj, + 'data', (n, cb) => n.multihash(cb), + (node, data, cb) => { // because js-ipfs-api can't infer // if the returned Data is Buffer or String if (typeof data === 'string') { data = new Buffer(data) } + expect(node.data).to.deep.equal(data) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded', (done) => { @@ -275,20 +344,24 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.data(bs58.encode(node.multihash()), { enc: 'base58' }, (err, data) => { + putAndGet( + 'put', testObj, + 'data', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest)) + }), + {enc: 'base58'}, + (node, data, cb) => { // because js-ipfs-api can't infer // if the returned Data is Buffer or String if (typeof data === 'string') { data = new Buffer(data) } expect(node.data).to.deep.equal(data) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded toString', (done) => { @@ -297,20 +370,24 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.data(bs58.encode(node.multihash()).toString(), { enc: 'base58' }, (err, data) => { + putAndGet( + 'put', testObj, + 'data', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest).toString()) + }), + {enc: 'base58'}, + (node, data, cb) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof data === 'string') { data = new Buffer(data) } expect(node.data).to.deep.equal(data) - done() - }) - }) + cb() + }, + done + ) }) }) @@ -321,15 +398,15 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.links(node.multihash(), (err, links) => { - expect(err).to.not.exist + putAndGet( + 'put', testObj, + 'links', (n, cb) => n.multihash(cb), + (node, links, cb) => { expect(node.links).to.deep.equal(links) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash (+ links)', (done) => { @@ -337,15 +414,15 @@ module.exports = (common) => { const dNode2 = new DAGNode(new Buffer('Some data 2')) dNode1.addNodeLink('some-link', dNode2) - ipfs.object.put(dNode1, (err, node) => { - expect(err).to.not.exist - - ipfs.object.links(node.multihash(), (err, links) => { - expect(err).to.not.exist + putAndGet( + 'put', dNode1, + 'links', (n, cb) => n.multihash(cb), + (node, links, cb) => { expect(node.links[0].toJSON()).to.deep.equal(links[0].toJSON()) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded', (done) => { @@ -354,15 +431,19 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.links(bs58.encode(node.multihash()), { enc: 'base58' }, (err, links) => { + putAndGet( + 'put', testObj, + 'links', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest)) + }), + {enc: 'base58'}, + (node, links, cb) => { expect(node.links).to.deep.equal(links) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded toString', (done) => { @@ -371,15 +452,19 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.links(bs58.encode(node.multihash()).toString(), { enc: 'base58' }, (err, links) => { + putAndGet( + 'put', testObj, + 'links', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest).toString()) + }), + {enc: 'base58'}, + (node, links, cb) => { expect(node.links).to.deep.equal(links) - done() - }) - }) + cb() + }, + done + ) }) }) @@ -390,11 +475,10 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.stat(node.multihash(), (err, stats) => { - expect(err).to.not.exist + putAndGet( + 'put', testObj, + 'stat', (n, cb) => n.multihash(cb), + (node, stats, cb) => { const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', NumLinks: 0, @@ -404,9 +488,10 @@ module.exports = (common) => { CumulativeSize: 17 } expect(expected).to.deep.equal(stats) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash (+ Links)', (done) => { @@ -414,11 +499,10 @@ module.exports = (common) => { const dNode2 = new DAGNode(new Buffer('Some data 2')) dNode1.addNodeLink('some-link', dNode2) - ipfs.object.put(dNode1, (err, node) => { - expect(err).to.not.exist - - ipfs.object.stat(node.multihash(), (err, stats) => { - expect(err).to.not.exist + putAndGet( + 'put', dNode1, + 'stat', (n, cb) => n.multihash(cb), + (node, stats, cb) => { const expected = { Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC', NumLinks: 1, @@ -428,9 +512,10 @@ module.exports = (common) => { CumulativeSize: 77 } expect(expected).to.deep.equal(stats) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded', (done) => { @@ -439,11 +524,14 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.stat(bs58.encode(node.multihash()), { enc: 'base58' }, (err, stats) => { + putAndGet( + 'put', testObj, + 'stat', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest)) + }), + {enc: 'base58'}, + (node, stats, cb) => { const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', NumLinks: 0, @@ -453,9 +541,10 @@ module.exports = (common) => { CumulativeSize: 17 } expect(expected).to.deep.equal(stats) - done() - }) - }) + cb() + }, + done + ) }) it('with multihash base58 encoded toString', (done) => { @@ -464,11 +553,14 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist - - ipfs.object.stat(bs58.encode(node.multihash()).toString(), { enc: 'base58' }, (err, stats) => { + putAndGet( + 'put', testObj, + 'stat', (n, cb) => n.multihash((err, digest) => { expect(err).to.not.exist + cb(null, bs58.encode(digest).toString()) + }), + {enc: 'base58'}, + (node, stats, cb) => { const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', NumLinks: 0, @@ -478,9 +570,10 @@ module.exports = (common) => { CumulativeSize: 17 } expect(expected).to.deep.equal(stats) - done() - }) - }) + cb() + }, + done + ) }) }) @@ -506,42 +599,42 @@ module.exports = (common) => { const dNode2 = new DAGNode(new Buffer('some other node')) // note: we need to put the linked obj, otherwise IPFS won't timeout // cause it needs the node to get its size - ipfs.object.put(dNode2, (err) => { - expect(err).to.not.exist - dNode1.addNodeLink('link-to-node', dNode2) - ipfs.object.patch.addLink(testNode.multihash(), dNode1.links[0], (err, node3) => { - expect(err).to.not.exist - expect(dNode1.multihash()).to.deep.equal(node3.multihash()) + waterfall([ + (cb) => ipfs.object.put(dNode2, cb), + (cb) => dNode1.addNodeLink('link-to-node', dNode2, cb), + (cb) => testNode.multihash(cb), + (digest, cb) => ipfs.object.patch.addLink(digest, dNode1.links[0], cb), + (node3, cb) => { testNodeWithLink = node3 testLink = dNode1.links[0] - done() - }) - }) + sameMultihash(dNode1, node3, cb) + } + ], done) }) it('.rmLink', (done) => { - ipfs.object.patch.rmLink(testNodeWithLink.multihash(), testLink, (err, node) => { - expect(err).to.not.exist - expect(node.multihash()).to.deep.equal(testNode.multihash()) - done() - }) + waterfall([ + (cb) => testNodeWithLink.multihash(cb), + (digest, cb) => ipfs.object.patch.rmLink(digest, testLink, cb), + (node, cb) => sameMultihash(node, testNode, cb) + ], done) }) it('.appendData', (done) => { - ipfs.object.patch.appendData(testNode.multihash(), new Buffer('append'), (err, node) => { - expect(err).to.not.exist - expect(node.multihash()).to.not.deep.equal(testNode.multihash()) - done() - }) + waterfall([ + (cb) => testNode.multihash(cb), + (digest, cb) => ipfs.object.patch.appendData(digest, new Buffer('append'), cb), + (node, cb) => differentMultihash(node, testNode, cb) + ], done) }) it('.setData', (done) => { - ipfs.object.patch.appendData(testNode.multihash(), new Buffer('set'), (err, node) => { - expect(err).to.not.exist - expect(node.multihash()).to.not.deep.equal(testNode.multihash()) - done() - }) + waterfall([ + (cb) => testNode.multihash(cb), + (digest, cb) => ipfs.object.patch.setData(digest, new Buffer('set'), cb), + (node, cb) => differentMultihash(node, testNode, cb) + ], done) }) }) }) @@ -549,8 +642,9 @@ module.exports = (common) => { describe('promise API', () => { it('object.new', () => { return ipfs.object.new() - .then((node) => { - expect(node.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + .then((node) => promisify(node.toJSON)()) + .then((json) => { + expect(json.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') }) }) @@ -561,8 +655,8 @@ module.exports = (common) => { } return ipfs.object.put(obj) - .then((node) => { - const nodeJSON = node.toJSON() + .then((node) => promisify(node.toJSON)()) + .then((nodeJSON) => { expect(obj.Data).to.deep.equal(nodeJSON.Data) expect(obj.Links).to.deep.equal(nodeJSON.Links) expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') @@ -577,16 +671,18 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node1) => { - return ipfs.object.get(node1.multihash()) + return promisify(node1.multihash)() + .then((digest) => ipfs.object.get(digest)) .then((node2) => { // because js-ipfs-api can't infer if the returned Data is Buffer // or String if (typeof node2.data === 'string') { node2.data = new Buffer(node2.data) } - expect(node1.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) expect(node1.links).to.deep.equal(node2.links) + return sameMultihash(node1, node2) }) }) }) @@ -599,7 +695,8 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node) => { - return ipfs.object.data(node.multihash()) + return promisify(node.multihash)() + .then((digest) => ipfs.object.data(digest)) .then((data) => { // because js-ipfs-api can't infer // if the returned Data is Buffer or String @@ -618,9 +715,8 @@ module.exports = (common) => { } return ipfs.object.put(testObj) - .then((node) => { - return ipfs.object.stat(node.multihash()) - }) + .then((node) => promisify(node.multihash)()) + .then((digest) => ipfs.object.stat(digest)) .then((stats) => { const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', @@ -642,10 +738,11 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node) => { - return ipfs.object.links(node.multihash()) - .then((links) => { - expect(node.links).to.deep.equal(links) - }) + return promisify(node.multihash)() + .then((digest) => ipfs.object.links(digest)) + .then((links) => { + expect(node.links).to.deep.equal(links) + }) }) }) @@ -653,6 +750,7 @@ module.exports = (common) => { let testNode let testNodeWithLink let testLink + let testNodeHash before(() => { const obj = { @@ -663,6 +761,9 @@ module.exports = (common) => { return ipfs.object.put(obj) .then((node) => { testNode = node + return promisify(testNode.multihash)() + }).then((digest) => { + testNodeHash = digest }) }) @@ -676,37 +777,31 @@ module.exports = (common) => { dNode1.addNodeLink('link-to-node', dNode2) return ipfs.object.patch - .addLink(testNode.multihash(), dNode1.links[0]) + .addLink(testNodeHash, dNode1.links[0]) .then((node3) => { - expect(dNode1.multihash()).to.deep.equal(node3.multihash()) testNodeWithLink = node3 testLink = dNode1.links[0] + return sameMultihash(dNode1, node3) }) }) }) it('.rmLink', () => { - return ipfs.object.patch - .rmLink(testNodeWithLink.multihash(), testLink) - .then((node) => { - expect(node.multihash()).to.deep.equal(testNode.multihash()) - }) + return promisify(testNodeWithLink.multihash)() + .then((digest) => ipfs.object.patch.rmLink(digest, testLink)) + .then((node) => sameMultihash(node, testNode)) }) it('.appendData', () => { return ipfs.object.patch - .appendData(testNode.multihash(), new Buffer('append')) - .then((node) => { - expect(node.multihash()).to.not.deep.equal(testNode.multihash()) - }) + .appendData(testNodeHash, new Buffer('append')) + .then((node) => sameMultihash(node, testNode)) }) it('.setData', () => { return ipfs.object.patch - .appendData(testNode.multihash(), new Buffer('set')) - .then((node) => { - expect(node.multihash()).to.not.deep.equal(testNode.multihash()) - }) + .appendData(testNodeHash, new Buffer('set')) + .then((node) => sameMultihash(node, testNode)) }) }) }) From eec1dcde67a7b0eba24152378fdc86685b892434 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 29 Sep 2016 09:31:30 +0200 Subject: [PATCH 3/5] fix object tests --- src/object.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/object.js b/src/object.js index db0958752..76bf32c6a 100644 --- a/src/object.js +++ b/src/object.js @@ -24,7 +24,7 @@ const checkMultihash = (n1, n2, cb) => { const sameMultihash = promisify((n1, n2, cb) => { checkMultihash(n1, n2, (err, h1, h2) => { expect(err).to.not.exist - expect(h2).to.be.eql(h2) + expect(h1).to.be.eql(h2) cb() }) }) @@ -32,7 +32,7 @@ const sameMultihash = promisify((n1, n2, cb) => { const differentMultihash = promisify((n1, n2, cb) => { checkMultihash(n1, n2, (err, h1, h2) => { expect(err).to.not.exist - expect(h2).to.not.be.eql(h2) + expect(h1).to.not.be.eql(h2) cb() }) }) @@ -79,7 +79,7 @@ module.exports = (common) => { } ], (err, res) => { expect(err).to.not.exist - assertions(node, res[1], cb) + assertions(node, res, cb) }) ], done) }) @@ -602,7 +602,7 @@ module.exports = (common) => { waterfall([ (cb) => ipfs.object.put(dNode2, cb), - (cb) => dNode1.addNodeLink('link-to-node', dNode2, cb), + (n, cb) => dNode1.addNodeLink('link-to-node', dNode2, cb), (cb) => testNode.multihash(cb), (digest, cb) => ipfs.object.patch.addLink(digest, dNode1.links[0], cb), (node3, cb) => { @@ -642,7 +642,7 @@ module.exports = (common) => { describe('promise API', () => { it('object.new', () => { return ipfs.object.new() - .then((node) => promisify(node.toJSON)()) + .then((node) => promisify(node.toJSON, {context: node})()) .then((json) => { expect(json.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') }) @@ -655,7 +655,7 @@ module.exports = (common) => { } return ipfs.object.put(obj) - .then((node) => promisify(node.toJSON)()) + .then((node) => promisify(node.toJSON, {context: node})()) .then((nodeJSON) => { expect(obj.Data).to.deep.equal(nodeJSON.Data) expect(obj.Links).to.deep.equal(nodeJSON.Links) @@ -671,7 +671,7 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node1) => { - return promisify(node1.multihash)() + return promisify(node1.multihash, {context: node1})() .then((digest) => ipfs.object.get(digest)) .then((node2) => { // because js-ipfs-api can't infer if the returned Data is Buffer @@ -695,7 +695,7 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node) => { - return promisify(node.multihash)() + return promisify(node.multihash, {context: node})() .then((digest) => ipfs.object.data(digest)) .then((data) => { // because js-ipfs-api can't infer @@ -715,7 +715,7 @@ module.exports = (common) => { } return ipfs.object.put(testObj) - .then((node) => promisify(node.multihash)()) + .then((node) => promisify(node.multihash, {context: node})()) .then((digest) => ipfs.object.stat(digest)) .then((stats) => { const expected = { @@ -738,7 +738,7 @@ module.exports = (common) => { return ipfs.object.put(testObj) .then((node) => { - return promisify(node.multihash)() + return promisify(node.multihash, {context: node})() .then((digest) => ipfs.object.links(digest)) .then((links) => { expect(node.links).to.deep.equal(links) @@ -761,7 +761,7 @@ module.exports = (common) => { return ipfs.object.put(obj) .then((node) => { testNode = node - return promisify(testNode.multihash)() + return promisify(testNode.multihash, {context: testNode})() }).then((digest) => { testNodeHash = digest }) @@ -787,7 +787,7 @@ module.exports = (common) => { }) it('.rmLink', () => { - return promisify(testNodeWithLink.multihash)() + return promisify(testNodeWithLink.multihash, {context: testNodeWithLink})() .then((digest) => ipfs.object.patch.rmLink(digest, testLink)) .then((node) => sameMultihash(node, testNode)) }) @@ -795,13 +795,13 @@ module.exports = (common) => { it('.appendData', () => { return ipfs.object.patch .appendData(testNodeHash, new Buffer('append')) - .then((node) => sameMultihash(node, testNode)) + .then((node) => differentMultihash(node, testNode)) }) it('.setData', () => { return ipfs.object.patch .appendData(testNodeHash, new Buffer('set')) - .then((node) => sameMultihash(node, testNode)) + .then((node) => differentMultihash(node, testNode)) }) }) }) From cc9d293d1b3bff07b2d3f67f129dbf89578e3f85 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 29 Sep 2016 10:22:15 +0200 Subject: [PATCH 4/5] some more fixes for object tests --- src/object.js | 96 +++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/src/object.js b/src/object.js index 76bf32c6a..227118233 100644 --- a/src/object.js +++ b/src/object.js @@ -239,23 +239,25 @@ module.exports = (common) => { it('with multihash (+ links)', (done) => { const dNode1 = new DAGNode(new Buffer('Some data 1')) const dNode2 = new DAGNode(new Buffer('Some data 2')) - dNode1.addNodeLink('some-link', dNode2) + dNode1.addNodeLink('some-link', dNode2, (err) => { + expect(err).to.not.exist - putAndGet( - 'put', dNode1, - 'get', (n, cb) => n.multihash(cb), - (node1, node2, cb) => { - // because js-ipfs-api can't infer if the returned Data is Buffer - // or String - if (typeof node2.data === 'string') { - node2.data = new Buffer(node2.data) - } + putAndGet( + 'put', dNode1, + 'get', (n, cb) => n.multihash(cb), + (node1, node2, cb) => { + // because js-ipfs-api can't infer if the returned Data is Buffer + // or String + if (typeof node2.data === 'string') { + node2.data = new Buffer(node2.data) + } - expect(node1.data).to.deep.equal(node2.data) - sameMultihash(node1, node2, cb) - }, - done - ) + expect(node1.data).to.deep.equal(node2.data) + sameMultihash(node1, node2, cb) + }, + done + ) + }) }) it('with multihash base58 encoded', (done) => { @@ -412,17 +414,19 @@ module.exports = (common) => { it('with multihash (+ links)', (done) => { const dNode1 = new DAGNode(new Buffer('Some data 1')) const dNode2 = new DAGNode(new Buffer('Some data 2')) - dNode1.addNodeLink('some-link', dNode2) + dNode1.addNodeLink('some-link', dNode2, (err) => { + expect(err).to.not.exist - putAndGet( - 'put', dNode1, - 'links', (n, cb) => n.multihash(cb), - (node, links, cb) => { - expect(node.links[0].toJSON()).to.deep.equal(links[0].toJSON()) - cb() - }, - done - ) + putAndGet( + 'put', dNode1, + 'links', (n, cb) => n.multihash(cb), + (node, links, cb) => { + expect(node.links[0].toJSON()).to.deep.equal(links[0].toJSON()) + cb() + }, + done + ) + }) }) it('with multihash base58 encoded', (done) => { @@ -497,25 +501,26 @@ module.exports = (common) => { it('with multihash (+ Links)', (done) => { const dNode1 = new DAGNode(new Buffer('Some data 1')) const dNode2 = new DAGNode(new Buffer('Some data 2')) - dNode1.addNodeLink('some-link', dNode2) - - putAndGet( - 'put', dNode1, - 'stat', (n, cb) => n.multihash(cb), - (node, stats, cb) => { - const expected = { - Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC', - NumLinks: 1, - BlockSize: 64, - LinksSize: 53, - DataSize: 11, - CumulativeSize: 77 - } - expect(expected).to.deep.equal(stats) - cb() - }, - done - ) + dNode1.addNodeLink('some-link', dNode2, (err) => { + expect(err).to.not.exist + putAndGet( + 'put', dNode1, + 'stat', (n, cb) => n.multihash(cb), + (node, stats, cb) => { + const expected = { + Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC', + NumLinks: 1, + BlockSize: 64, + LinksSize: 53, + DataSize: 11, + CumulativeSize: 77 + } + expect(expected).to.deep.equal(stats) + cb() + }, + done + ) + }) }) it('with multihash base58 encoded', (done) => { @@ -773,9 +778,8 @@ module.exports = (common) => { // note: we need to put the linked obj, otherwise IPFS won't timeout // cause it needs the node to get its size return ipfs.object.put(dNode2) + .then(() => promisify(dNode1.addNodeLink, {context: dNode1})('link-to-node', dNode2)) .then(() => { - dNode1.addNodeLink('link-to-node', dNode2) - return ipfs.object.patch .addLink(testNodeHash, dNode1.links[0]) .then((node3) => { From 329a94e14f0e5eb4b0c26f9f916c7d0dcfa5f8d2 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 29 Sep 2016 10:59:18 +0200 Subject: [PATCH 5/5] fix files error catching --- src/files.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/files.js b/src/files.js index af1ae188e..c844a8c38 100644 --- a/src/files.js +++ b/src/files.js @@ -436,10 +436,9 @@ module.exports = (common) => { }) }) - it('errors on invalid key', (done) => { + it('errors on invalid key', () => { const hash = 'somethingNotMultihash' - ipfs.files.get(hash) - .then((stream) => {}) + return ipfs.files.get(hash) .catch((err) => { expect(err).to.exist const errString = err.toString() @@ -449,7 +448,6 @@ module.exports = (common) => { if (errString === 'Error: Invalid Key') { expect(err.toString()).to.contain('Error: Invalid Key') } - done() }) }) })