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

Commit

Permalink
Merge pull request #64 from ipfs/feat/update-block-api
Browse files Browse the repository at this point in the history
Make block API spec and tests coherent.
  • Loading branch information
daviddias authored Aug 24, 2016
2 parents dad7f81 + 235c02f commit a6a4d56
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions API/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ If no `callback` is passed, a promise is returned.

```JavaScript
{
Key: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
Size: 10
key: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
size: 10
}
```

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"chai": "^3.5.0",
"concat-stream": "^1.5.1",
"detect-node": "^2.0.3",
"ipfs-block": "^0.3.0",
"ipfs-merkle-dag": "^0.6.2",
"multihashes": "^0.2.2",
"readable-stream": "1.1.13",
"run-series": "^1.1.4"
},
Expand All @@ -48,4 +50,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>"
]
}
}
43 changes: 27 additions & 16 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
'use strict'

const expect = require('chai').expect
const Block = require('ipfs-block')
const multihash = require('multihashes')

module.exports = (common) => {
describe('.block', () => {
Expand All @@ -30,13 +32,26 @@ module.exports = (common) => {
})

describe('callback API', () => {
it('.put', (done) => {
it('.put a buffer', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = Buffer('blorb')

ipfs.block.put(blob, (err, res) => {
ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist
expect(res).to.have.a.property('Key', expectedHash)
expect(block.key).to.eql(multihash.fromB58String(expectedHash))
expect(block).to.have.a.property('data', blob)
done()
})
})

it('.put a block', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = new Block(new Buffer('blorb'))

ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist
expect(block.key).to.eql(multihash.fromB58String(expectedHash))
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
})
Expand All @@ -52,30 +67,26 @@ module.exports = (common) => {
it('block.get', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.get(hash, (err, res) => {
ipfs.block.get(hash, (err, block) => {
expect(err).to.not.exist

// TODO review this
let buf = ''
res
.on('data', function (data) { buf += data })
.on('end', function () {
expect(buf).to.be.equal('blorb')
done()
})
expect(block.key).to.eql(multihash.fromB58String(hash))
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
})

it('block.stat', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.stat(hash, (err, res) => {
ipfs.block.stat(hash, (err, stats) => {
expect(err).to.not.exist
expect(res).to.have.property('Key')
expect(res).to.have.property('Size')
expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
done()
})
})

it.skip('block.rm', (done) => {}) // TODO once block.rm is shipped in go-ipfs
})

describe('promise API', () => {
Expand Down

0 comments on commit a6a4d56

Please sign in to comment.