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

Commit a985305

Browse files
committed
test: refactor offline tests
1 parent 25aade7 commit a985305

File tree

1 file changed

+51
-72
lines changed

1 file changed

+51
-72
lines changed

test/block-service-test.js

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,101 +17,80 @@ const BlockService = require('../src')
1717
module.exports = (repo) => {
1818
describe('block-service', () => {
1919
let bs
20+
let testBlocks
2021

21-
before(() => {
22+
before((done) => {
2223
bs = new BlockService(repo)
24+
25+
const data = [
26+
Buffer.from('1'),
27+
Buffer.from('2'),
28+
Buffer.from('3'),
29+
Buffer.from('A random data block')
30+
]
31+
32+
map(data, (d, cb) => {
33+
multihashing(d, 'sha2-256', (err, hash) => {
34+
expect(err).to.not.exist()
35+
cb(null, new Block(d, new CID(hash)))
36+
})
37+
}, (err, blocks) => {
38+
expect(err).to.not.exist()
39+
testBlocks = blocks
40+
done()
41+
})
2342
})
2443

25-
describe('offline', () => {
44+
describe('fetch only from local Repo', () => {
2645
it('store and get a block', (done) => {
27-
const data = Buffer.from('A random data block')
28-
multihashing(data, 'sha2-256', (err, hash) => {
29-
expect(err).to.not.exist()
30-
const b = new Block(data, new CID(hash))
46+
const b = testBlocks[3]
3147

32-
waterfall([
33-
(cb) => bs.put(b, cb),
34-
(cb) => bs.get(b.cid, cb),
35-
(res, cb) => {
36-
expect(res).to.be.eql(b)
37-
cb()
38-
}
39-
], done)
40-
})
48+
waterfall([
49+
(cb) => bs.put(b, cb),
50+
(cb) => bs.get(b.cid, cb),
51+
(res, cb) => {
52+
expect(res).to.eql(b)
53+
cb()
54+
}
55+
], done)
4156
})
4257

43-
it('get a non existent block', (done) => {
44-
const data = Buffer.from('Not stored')
58+
it('get a non stored yet block', (done) => {
59+
const b = testBlocks[2]
4560

46-
multihashing(data, 'sha2-256', (err, hash) => {
47-
expect(err).to.not.exist()
48-
bs.get(new CID(hash), (err, block) => {
49-
expect(err).to.exist()
50-
expect(block).to.not.exist()
51-
done()
52-
})
61+
bs.get(b.cid, (err, block) => {
62+
expect(err).to.exist()
63+
expect(block).to.not.exist()
64+
done()
5365
})
5466
})
5567

5668
it('store many blocks', (done) => {
57-
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
69+
bs.putMany(testBlocks, done)
70+
})
5871

59-
map(data, (d, cb) => {
60-
multihashing(d, 'sha2-256', (err, hash) => {
61-
expect(err).to.not.exist()
62-
cb(null, new Block(d, new CID(hash)))
63-
})
64-
}, (err, blocks) => {
72+
it('get many blocks through .get', (done) => {
73+
map(testBlocks, (b, cb) => bs.get(b.cid, cb), (err, blocks) => {
6574
expect(err).to.not.exist()
66-
bs.putMany(blocks, done)
75+
expect(blocks).to.eql(testBlocks)
76+
done()
6777
})
6878
})
6979

70-
it('get many blocks through get', (done) => {
71-
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
72-
73-
waterfall([
74-
(cb) => map(data, (d, cb) => {
75-
multihashing(d, 'sha2-256', (err, hash) => {
76-
expect(err).to.not.exist()
77-
cb(null, new Block(d, new CID(hash)))
78-
})
79-
}, cb),
80-
(blocks, cb) => map(
81-
blocks,
82-
(b, cb) => bs.get(b.cid, cb),
83-
(err, res) => {
84-
expect(err).to.not.exist()
85-
expect(res).to.be.eql(blocks)
86-
cb()
87-
}
88-
)
89-
], done)
90-
})
91-
92-
it('get many blocks through getMany', (done) => {
93-
const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')]
94-
95-
waterfall([
96-
(cb) => map(data, (d, cb) => {
97-
multihashing(d, 'sha2-256', (err, hash) => {
98-
expect(err).to.not.exist()
99-
cb(null, new Block(d, new CID(hash)))
100-
})
101-
}, cb),
102-
(blocks, cb) => map(blocks, (b, cb) => cb(null, b.cid), (err, cids) => {
80+
it('get many blocks through .getMany', (done) => {
81+
map(testBlocks, (b, cb) => cb(null, b.cid), (err, cids) => {
82+
expect(err).to.not.exist()
83+
bs.getMany(cids, (err, _blocks) => {
10384
expect(err).to.not.exist()
104-
bs.getMany(cids, (err, _blocks) => {
105-
expect(err).to.not.exist()
106-
expect(blocks).to.eql(blocks)
107-
cb()
108-
})
85+
expect(testBlocks).to.eql(testBlocks)
86+
done()
10987
})
110-
], done)
88+
})
11189
})
11290

11391
it('delete a block', (done) => {
11492
const data = Buffer.from('Will not live that much')
93+
11594
multihashing(data, 'sha2-256', (err, hash) => {
11695
expect(err).to.not.exist()
11796
const b = new Block(data, new CID(hash))
@@ -163,7 +142,7 @@ module.exports = (repo) => {
163142
})
164143
})
165144

166-
describe('has exchange', () => {
145+
describe('fetch through Bitswap (has exchange)', () => {
167146
beforeEach(() => {
168147
bs = new BlockService(repo)
169148
})

0 commit comments

Comments
 (0)