Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 0c55fdc

Browse files
committed
Merge pull request #9 from noffle/coverage
100% (line) coverage.
2 parents a907f86 + 58f1191 commit 0c55fdc

File tree

4 files changed

+135
-23
lines changed

4 files changed

+135
-23
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,27 @@ Asynchronously adds an array of block instances to the underlying repo.
149149
#### bs.getBlock(multihash, callback(err, block))
150150

151151
Asynchronously returns the block whose content multihash matches `multihash`.
152+
Returns an error (`err.code === 'ENOENT'`) if the block does not exist.
153+
154+
If the block could not be found, expect `err.code` to be `'ENOENT'`.
152155

153156
#### bs.getBlocks(multihashes, callback(err, blocks))
154157

155158
Asynchronously returns the blocks whose content multihashes match the array
156159
`multihashes`.
157160

161+
`blocks` is an object that maps each `multihash` to an object of the form
162+
163+
```js
164+
{
165+
err: Error
166+
block: Block
167+
}
168+
```
169+
170+
Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block
171+
=== null` if a block did not exist.
172+
158173
*Does not guarantee atomicity.*
159174

160175
#### bs.deleteBlock(multihash, callback(err))

src/block-service.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ function BlockService (ipfsRepo, exchange) {
2626
}
2727

2828
this.getBlock = (multihash, extension, callback) => {
29-
if (!multihash) {
30-
return callback(new Error('Invalid multihash'))
31-
}
32-
3329
if (typeof extension === 'function') {
3430
callback = extension
3531
extension = undefined
3632
}
3733

34+
if (!multihash) {
35+
return callback(new Error('Invalid multihash'))
36+
}
37+
3838
ipfsRepo.datastore.createReadStream(multihash, extension)
3939
.pipe(bl((err, data) => {
4040
if (err) { return callback(err) }
@@ -43,50 +43,53 @@ function BlockService (ipfsRepo, exchange) {
4343
}
4444

4545
this.getBlocks = (multihashes, extension, callback) => {
46-
if (!Array.isArray(multihashes)) {
47-
return callback(new Error('Invalid batch of multihashes'))
48-
}
49-
5046
if (typeof extension === 'function') {
5147
callback = extension
5248
extension = undefined
5349
}
5450

55-
const blocks = []
51+
if (!Array.isArray(multihashes)) {
52+
return callback(new Error('Invalid batch of multihashes'))
53+
}
54+
55+
var results = {}
5656

5757
async.each(multihashes, (multihash, next) => {
5858
this.getBlock(multihash, extension, (err, block) => {
59-
if (err) { return next(err) }
60-
blocks.push(block)
59+
results[multihash] = {
60+
err: err,
61+
block: block
62+
}
63+
next()
6164
})
6265
}, (err) => {
63-
callback(err, blocks)
66+
callback(err, results)
6467
})
6568
}
6669

6770
this.deleteBlock = (multihash, extension, callback) => {
68-
if (!multihash) {
69-
return callback(new Error('Invalid multihash'))
70-
}
71-
7271
if (typeof extension === 'function') {
7372
callback = extension
7473
extension = undefined
7574
}
7675

76+
if (!multihash) {
77+
return callback(new Error('Invalid multihash'))
78+
}
79+
7780
ipfsRepo.datastore.remove(multihash, extension, callback)
7881
}
7982

8083
this.deleteBlocks = (multihashes, extension, callback) => {
81-
if (!Array.isArray(multihashes)) {
82-
return callback('Invalid batch of multihashes')
83-
}
84-
8584
if (typeof extension === 'function') {
8685
callback = extension
8786
extension = undefined
8887
}
8988

89+
if (!Array.isArray(multihashes)) {
90+
return callback(new Error('Invalid batch of multihashes'))
91+
}
92+
9093
async.each(multihashes, (multihash, next) => {
9194
this.deleteBlock(multihash, extension, next)
9295
}, (err) => {

test/block-service-test.js

+90-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = (repo) => {
1515
done()
1616
})
1717

18-
it('store a block', (done) => {
18+
it('store and get a block', (done) => {
1919
const b = new Block('A random data block')
2020
bs.addBlock(b, (err) => {
2121
expect(err).to.not.exist
@@ -28,7 +28,7 @@ module.exports = (repo) => {
2828
})
2929
})
3030

31-
it('store a block, with custom extension', (done) => {
31+
it('store and get a block, with custom extension', (done) => {
3232
const b = new Block('A random data block', 'ext')
3333
bs.addBlock(b, (err) => {
3434
expect(err).to.not.exist
@@ -60,6 +60,79 @@ module.exports = (repo) => {
6060
})
6161
})
6262

63+
it('addBlocks: bad invocation', (done) => {
64+
const b1 = new Block('1')
65+
66+
bs.addBlocks(b1, (err) => {
67+
expect(err).to.be.an('error')
68+
done()
69+
})
70+
})
71+
72+
it('getBlock: bad invocation', (done) => {
73+
bs.getBlock(null, (err) => {
74+
expect(err).to.be.an('error')
75+
done()
76+
})
77+
})
78+
79+
it('getBlocks: bad invocation', (done) => {
80+
bs.getBlocks(null, 'protobuf', (err) => {
81+
expect(err).to.be.an('error')
82+
done()
83+
})
84+
})
85+
86+
it('get many blocks', (done) => {
87+
const b1 = new Block('1')
88+
const b2 = new Block('2')
89+
const b3 = new Block('3')
90+
91+
bs.addBlocks([b1, b2, b3], (err) => {
92+
expect(err).to.not.exist
93+
94+
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
95+
expect(err).to.not.exist
96+
expect(Object.keys(blocks)).to.have.lengthOf(3)
97+
expect(blocks[b1.key]).to.exist
98+
expect(blocks[b1.key].err).to.not.exist
99+
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
100+
expect(blocks[b2.key]).to.exist
101+
expect(blocks[b2.key].err).to.not.exist
102+
expect(blocks[b2.key].block.data).to.deep.equal(b2.data)
103+
expect(blocks[b3.key]).to.exist
104+
expect(blocks[b3.key].err).to.not.exist
105+
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
106+
done()
107+
})
108+
})
109+
})
110+
111+
it('get many blocks: partial success', (done) => {
112+
const b1 = new Block('a1')
113+
const b2 = new Block('a2')
114+
const b3 = new Block('a3')
115+
116+
bs.addBlocks([b1, b3], (err) => {
117+
expect(err).to.not.exist
118+
119+
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
120+
expect(err).to.not.exist
121+
expect(Object.keys(blocks)).to.have.lengthOf(3)
122+
expect(blocks[b1.key]).to.exist
123+
expect(blocks[b1.key].err).to.not.exist
124+
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
125+
expect(blocks[b2.key]).to.exist
126+
expect(blocks[b2.key].err).to.exist
127+
expect(blocks[b2.key].block).to.not.exist
128+
expect(blocks[b3.key]).to.exist
129+
expect(blocks[b3.key].err).to.not.exist
130+
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
131+
done()
132+
})
133+
})
134+
})
135+
63136
it('delete a block', (done) => {
64137
const b = new Block('Will not live that much')
65138
bs.addBlock(b, (err) => {
@@ -74,6 +147,13 @@ module.exports = (repo) => {
74147
})
75148
})
76149

150+
it('deleteBlock: bad invocation', (done) => {
151+
bs.deleteBlock(null, (err) => {
152+
expect(err).to.be.an('error')
153+
done()
154+
})
155+
})
156+
77157
it('delete a block, with custom extension', (done) => {
78158
const b = new Block('Will not live that much', 'ext')
79159
bs.addBlock(b, (err) => {
@@ -101,10 +181,17 @@ module.exports = (repo) => {
101181
const b2 = new Block('2')
102182
const b3 = new Block('3')
103183

104-
bs.deleteBlocks([b1, b2, b3], (err) => {
184+
bs.deleteBlocks([b1, b2, b3], 'data', (err) => {
105185
expect(err).to.not.exist
106186
done()
107187
})
108188
})
189+
190+
it('deleteBlocks: bad invocation', (done) => {
191+
bs.deleteBlocks(null, (err) => {
192+
expect(err).to.be.an('error')
193+
done()
194+
})
195+
})
109196
})
110197
}

test/block.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ describe('block', () => {
1212
expect(b.extension).to.be.eql('data')
1313
})
1414

15+
it('create /wo new', () => {
16+
const b = Block('random-data')
17+
expect(b.key).to.exist
18+
expect(b.data).to.exist
19+
expect(b.extension).to.be.eql('data')
20+
})
21+
1522
it('fail to create an empty block', () => {
1623
expect(() => new Block()).to.throw()
1724
})

0 commit comments

Comments
 (0)