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

Commit e4245db

Browse files
committed
feat: modularise block, add mocha and test suite utils
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent ad70c2c commit e4245db

File tree

7 files changed

+251
-145
lines changed

7 files changed

+251
-145
lines changed

js/src/block.js

Lines changed: 0 additions & 145 deletions
This file was deleted.

js/src/block/get.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
const multihash = require('multihashes')
11+
const CID = require('cids')
12+
const Buffer = require('safe-buffer').Buffer
13+
const { getDescribe } = require('../utils/mocha')
14+
15+
module.exports = (common, options) => {
16+
const describe = getDescribe(options)
17+
18+
describe('.block.get', function () {
19+
let ipfs
20+
21+
before(function (done) {
22+
// CI takes longer to instantiate the daemon, so we need to increase the
23+
// timeout for the before step
24+
this.timeout(60 * 1000)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
factory.spawnNode((err, node) => {
29+
expect(err).to.not.exist()
30+
ipfs = node
31+
done()
32+
})
33+
})
34+
})
35+
36+
after((done) => common.teardown(done))
37+
38+
it('should get by CID object', (done) => {
39+
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
40+
const cid = new CID(hash)
41+
42+
ipfs.block.get(cid, (err, block) => {
43+
expect(err).to.not.exist()
44+
expect(block.data).to.eql(new Buffer('blorb'))
45+
expect(block.cid.multihash).to.eql(cid.multihash)
46+
done()
47+
})
48+
})
49+
50+
it('should get by CID in string', (done) => {
51+
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
52+
53+
ipfs.block.get(hash, (err, block) => {
54+
expect(err).to.not.exist()
55+
expect(block.data).to.eql(new Buffer('blorb'))
56+
expect(block.cid.multihash).to.eql(multihash.fromB58String(hash))
57+
done()
58+
})
59+
})
60+
61+
// TODO it.skip('Promises support', (done) => {})
62+
})
63+
}

js/src/block/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
put: require('./put'),
6+
get: require('./get'),
7+
stat: require('./stat')
8+
}
9+
10+
module.exports = createSuite(tests)

js/src/block/put.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
const Block = require('ipfs-block')
11+
const multihash = require('multihashes')
12+
const CID = require('cids')
13+
const Buffer = require('safe-buffer').Buffer
14+
const { getDescribe } = require('../utils/mocha')
15+
16+
module.exports = (common, options) => {
17+
const describe = getDescribe(options)
18+
19+
describe('.block.put', () => {
20+
let ipfs
21+
22+
before(function (done) {
23+
// CI takes longer to instantiate the daemon, so we need to increase the
24+
// timeout for the before step
25+
this.timeout(60 * 1000)
26+
27+
common.setup((err, factory) => {
28+
expect(err).to.not.exist()
29+
factory.spawnNode((err, node) => {
30+
expect(err).to.not.exist()
31+
ipfs = node
32+
done()
33+
})
34+
})
35+
})
36+
37+
after((done) => common.teardown(done))
38+
39+
it('should put a buffer, using defaults', (done) => {
40+
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
41+
const blob = new Buffer('blorb')
42+
43+
ipfs.block.put(blob, (err, block) => {
44+
expect(err).to.not.exist()
45+
expect(block.data).to.be.eql(blob)
46+
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
47+
done()
48+
})
49+
})
50+
51+
it('should put a buffer, using CID', (done) => {
52+
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
53+
const cid = new CID(expectedHash)
54+
const blob = new Buffer('blorb')
55+
56+
ipfs.block.put(blob, { cid: cid }, (err, block) => {
57+
expect(err).to.not.exist()
58+
expect(block.data).to.be.eql(blob)
59+
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
60+
done()
61+
})
62+
})
63+
64+
it('should put a buffer, using options', (done) => {
65+
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
66+
const blob = new Buffer('blorb')
67+
68+
ipfs.block.put(blob, {
69+
format: 'dag-pb',
70+
mhtype: 'sha2-256',
71+
version: 0
72+
}, (err, block) => {
73+
expect(err).to.not.exist()
74+
expect(block.data).to.be.eql(blob)
75+
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
76+
done()
77+
})
78+
})
79+
80+
it('should put a Block instance', (done) => {
81+
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
82+
const cid = new CID(expectedHash)
83+
const b = new Block(new Buffer('blorb'), cid)
84+
85+
ipfs.block.put(b, (err, block) => {
86+
expect(err).to.not.exist()
87+
expect(block.data).to.eql(new Buffer('blorb'))
88+
expect(block.cid.multihash).to.eql(multihash.fromB58String(expectedHash))
89+
done()
90+
})
91+
})
92+
93+
it('should error with array of blocks', (done) => {
94+
const blob = Buffer('blorb')
95+
96+
ipfs.block.put([blob, blob], (err) => {
97+
expect(err).to.be.an.instanceof(Error)
98+
done()
99+
})
100+
})
101+
102+
// TODO it.skip('Promises support', (done) => {})
103+
})
104+
}

js/src/block/stat.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
const CID = require('cids')
11+
const { getDescribe } = require('../utils/mocha')
12+
13+
module.exports = (common, options) => {
14+
const describe = getDescribe(options)
15+
16+
describe('.block.stat', () => {
17+
let ipfs
18+
19+
before(function (done) {
20+
// CI takes longer to instantiate the daemon, so we need to increase the
21+
// timeout for the before step
22+
this.timeout(60 * 1000)
23+
24+
common.setup((err, factory) => {
25+
expect(err).to.not.exist()
26+
factory.spawnNode((err, node) => {
27+
expect(err).to.not.exist()
28+
ipfs = node
29+
done()
30+
})
31+
})
32+
})
33+
34+
after((done) => common.teardown(done))
35+
36+
it('should stat by CID', (done) => {
37+
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
38+
const cid = new CID(hash)
39+
40+
ipfs.block.stat(cid, (err, stats) => {
41+
expect(err).to.not.exist()
42+
expect(stats).to.have.property('key')
43+
expect(stats).to.have.property('size')
44+
done()
45+
})
46+
})
47+
48+
// TODO it.skip('Promises support', (done) => {})
49+
})
50+
}

js/src/utils/mocha.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-env mocha */
2+
3+
// Get a describe function that is optionally 'skipped' or 'onlyed'
4+
function getDescribe (config) {
5+
if (config && config.skip) return describe.skip
6+
if (config && config.only) return describe.only
7+
return describe
8+
}
9+
10+
module.exports.getDescribe = getDescribe

0 commit comments

Comments
 (0)