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

Commit a37808a

Browse files
committed
test: adds test for exporting sharded directories
1 parent 0ae27e1 commit a37808a

File tree

3 files changed

+108
-7
lines changed

3 files changed

+108
-7
lines changed

Diff for: test/exporter-sharded.spec.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
const expect = chai.expect
7+
const IPLD = require('ipld')
8+
const UnixFS = require('ipfs-unixfs')
9+
const pull = require('pull-stream')
10+
const CID = require('cids')
11+
const waterfall = require('async/waterfall')
12+
const parallel = require('async/parallel')
13+
const randomBytes = require('./helpers/random-bytes')
14+
15+
const exporter = require('../src')
16+
const importer = require('ipfs-unixfs-importer')
17+
18+
const SHARD_SPLIT_THRESHOLD = 1000
19+
20+
describe('exporter sharded', () => {
21+
let ipld
22+
23+
before((done) => {
24+
IPLD.inMemory((err, resolver) => {
25+
expect(err).to.not.exist()
26+
27+
ipld = resolver
28+
29+
done()
30+
})
31+
})
32+
33+
it('exports a sharded directory', (done) => {
34+
const files = {}
35+
let directory
36+
37+
for (let i = 0; i < (SHARD_SPLIT_THRESHOLD + 1); i++) {
38+
files[`file-${Math.random()}.txt`] = {
39+
content: randomBytes(100)
40+
}
41+
}
42+
43+
waterfall([
44+
(cb) => pull(
45+
pull.values(
46+
Object.keys(files).map(path => ({
47+
path,
48+
content: files[path].content
49+
}))
50+
),
51+
importer(ipld, {
52+
wrap: true
53+
}),
54+
pull.collect(cb)
55+
),
56+
(imported, cb) => {
57+
directory = new CID(imported.pop().multihash)
58+
59+
// store the CIDs, we will validate them later
60+
imported.forEach(imported => {
61+
files[imported.path].cid = new CID(imported.multihash)
62+
})
63+
64+
ipld.get(directory, cb)
65+
},
66+
({ value, cid }, cb) => {
67+
const dir = UnixFS.unmarshal(value.data)
68+
69+
expect(dir.type).to.equal('hamt-sharded-directory')
70+
71+
pull(
72+
exporter(directory, ipld),
73+
pull.collect(cb)
74+
)
75+
},
76+
(exported, cb) => {
77+
const dir = exported.shift()
78+
79+
expect(dir.hash).to.deep.equal(directory.buffer)
80+
81+
parallel(
82+
exported.map(exported => (cb) => {
83+
pull(
84+
exported.content,
85+
pull.collect((err, bufs) => {
86+
if (err) {
87+
cb(err)
88+
}
89+
90+
// validate the CID
91+
expect(files[exported.name].cid.buffer).to.deep.equal(exported.hash)
92+
93+
// validate the exported file content
94+
expect(files[exported.name].content).to.deep.equal(bufs[0])
95+
96+
cb()
97+
})
98+
)
99+
}),
100+
cb
101+
)
102+
}
103+
], done)
104+
})
105+
})

Diff for: test/exporter.spec.js

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ const ONE_MEG = Math.pow(1024, 2)
3232
const bigFile = randomBytes(ONE_MEG * 1.2)
3333
const smallFile = randomBytes(200)
3434

35-
// loadFixture('test/fixtures/200Bytes.txt')
36-
3735
describe('exporter', () => {
3836
let ipld
3937

Diff for: test/node.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
describe('ipfs-unixfs-exporter', () => {
5-
// Exporter
6-
require('./exporter.spec')
7-
require('./exporter-subtree.spec')
8-
})
4+
require('./exporter.spec')
5+
require('./exporter-subtree.spec')
6+
require('./exporter-sharded.spec')

0 commit comments

Comments
 (0)