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

Commit

Permalink
feat: adds pull stream tests for files.add
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw authored and daviddias committed Apr 23, 2018
1 parent 940f049 commit d75986a
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion js/src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,26 @@ module.exports = (common) => {
})
})

it('a Readable Stream', (done) => {
it('adds from readable stream', (done) => {
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'

const rs = new Readable()
rs.push(Buffer.from('some data'))
rs.push(null)

ipfs.files.add(rs, (err, filesAdded) => {
expect(err).to.not.exist()

expect(filesAdded).to.be.length(1)
const file = filesAdded[0]
expect(file.path).to.equal(expectedCid)
expect(file.size).to.equal(17)
expect(file.hash).to.equal(expectedCid)
done()
})
})

it('adds from array of objects with readable stream content', (done) => {
const expectedCid = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'

const rs = new Readable()
Expand All @@ -177,6 +196,37 @@ module.exports = (common) => {
})
})

it('adds from pull stream (callback)', (done) => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'

ipfs.files.add(pull.values([Buffer.from('test')]), (err, res) => {
if (err) return done(err)
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
done()
})
})

it('adds from pull stream (promise)', () => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'

return ipfs.files.add(pull.values([Buffer.from('test')]))
.then((res) => {
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
})
})

it('adds from array of objects with pull stream content', () => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'

return ipfs.files.add([{ content: pull.values([Buffer.from('test')]) }])
.then((res) => {
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
})
})

it('add a nested directory as array of tupples', function (done) {
// TODO: https://github.com/ipfs/js-ipfs-api/issues/339
if (!isNode) { this.skip() }
Expand Down Expand Up @@ -367,6 +417,21 @@ module.exports = (common) => {
})
)
})

it('adds with object chunks and pull stream content', (done) => {
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'

pull(
pull.values([{ content: pull.values([Buffer.from('test')]) }]),
ipfs.files.addPullStream(),
pull.collect((err, res) => {
if (err) return done(err)
expect(res).to.have.length(1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
done()
})
)
})
})

describe('.cat', () => {
Expand Down

0 comments on commit d75986a

Please sign in to comment.