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

Commit 8487d78

Browse files
committed
feat(pin): tests
1 parent 59a45d0 commit 8487d78

File tree

4 files changed

+155
-11
lines changed

4 files changed

+155
-11
lines changed

API/pinning-api/README.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
pinning API
2-
===========
1+
Pin API
2+
=======
33

44
#### `add`
55

6-
> Pin an IPFS object to local storage
6+
> Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).
77
88
##### `Go` **WIP**
99

@@ -12,10 +12,17 @@ pinning API
1212
Where:
1313

1414
- `hash` is an IPFS multihash.
15-
- `options` is an object that can contain the following keys:
16-
- `recursive` - Recursevely pin the object linked.
15+
- `options` is an object that can contain the following keys
16+
- `recursive` - Recursively pin the object linked. Type: bool. Default: 'false'
17+
18+
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an array of objects that represent the files that were pinned. Example:
1719

18-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
20+
```JavaScript
21+
{
22+
hash: 'QmHash',
23+
path: 'some-path
24+
}
25+
```
1926
2027
If no `callback` is passed, a promise is returned.
2128
@@ -37,7 +44,7 @@ Where:
3744
3845
- `hash` is an IPFS multihash.
3946
- `options` is an object that can contain the following keys:
40-
- `type` - Return also the type of pin (direct, indirect or recursive)
47+
- 'type' - Return also the type of pin (direct, indirect or recursive)
4148
4249
`callback` must follow `function (err, pinset) {}` signature, where `err` is an error if the operation was not successful. `pinset` is an array of objects with keys `hash` and `type`.
4350
@@ -52,13 +59,16 @@ ipfs.pin.ls(function (err, pinset) {})
5259
5360
#### `rm`
5461
55-
> Remove an hash from the pinset
62+
> Remove a hash from the pinset
5663
5764
##### `Go` **WIP**
5865
59-
##### `JavaScript` - ipfs.pin.rm(hash, [callback])
66+
##### `JavaScript` - ipfs.pin.rm(hash, [options, callback])
6067
61-
Where `hash` is a multihash.
68+
Where:
69+
- `hash` is a multihash.
70+
- `options` is an object that can contain the following keys
71+
- `recursive` - Recursively unpin the object linked. Type: bool. Default: 'false'
6272
6373
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
6474

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
"greenkeeperio-bot <[email protected]>",
4848
"nginnever <[email protected]>"
4949
]
50-
}
50+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
exports.object = require('./object')
44
exports.files = require('./files')
55
exports.config = require('./config')
6+
exports.pin = require('./pin')

src/pin.js

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
const fs = require('fs')
8+
const path = require('path')
9+
10+
const testfile = fs.readFileSync(path.join(__dirname, './data/testfile.txt'))
11+
12+
module.exports = (common) => {
13+
describe('.pin', () => {
14+
let ipfs
15+
16+
before(function (done) {
17+
// CI is slow
18+
this.timeout(20 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist
22+
factory.spawnNode((err, node) => {
23+
expect(err).to.not.exist
24+
ipfs = node
25+
done()
26+
})
27+
})
28+
})
29+
30+
after((done) => {
31+
common.teardown(done)
32+
})
33+
34+
it('add file for testing', (done) => {
35+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
36+
37+
ipfs.files.add(testfile, (err, res) => {
38+
expect(err).to.not.exist
39+
40+
expect(res).to.have.length(1)
41+
expect(res[0].hash).to.equal(expectedMultihash)
42+
expect(res[0].path).to.equal(expectedMultihash)
43+
done()
44+
})
45+
})
46+
47+
describe('callback API', () => {
48+
it('.rm (1st, because ipfs.files.add pins automatically)', (done) => {
49+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
50+
51+
ipfs.pin.rm(hash, { recursive: true }, (err, res) => {
52+
expect(err).to.not.exist
53+
expect(res).to.exist
54+
ipfs.pin.ls({ type: 'direct' }, (err, res) => {
55+
expect(err).to.not.exist
56+
expect(res).to.exist
57+
expect(res.Keys).to.be.empty
58+
done()
59+
})
60+
})
61+
})
62+
63+
it('.add', (done) => {
64+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
65+
66+
ipfs.pin.add(hash, { recursive: false }, (err, res) => {
67+
expect(err).to.not.exist
68+
expect(res.Pins[0]).to.be.equal(hash)
69+
done()
70+
})
71+
})
72+
73+
it('.list', (done) => {
74+
ipfs.pin.ls((err, res) => {
75+
expect(err).to.not.exist
76+
expect(res).to.exist
77+
done()
78+
})
79+
})
80+
81+
it('.list for a specific hash', (done) => {
82+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
83+
84+
ipfs.pin.ls(hash, (err, res) => {
85+
expect(err).to.not.exist
86+
expect(res).to.exist
87+
done()
88+
})
89+
})
90+
})
91+
92+
describe('promise API', () => {
93+
it('.add', () => {
94+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
95+
96+
return ipfs.pin.add(hash, { recursive: false })
97+
.then((res) => {
98+
expect(res.Pins[0]).to.be.equal(hash)
99+
})
100+
})
101+
102+
it('.ls', () => {
103+
return ipfs.pin.ls()
104+
.then((res) => {
105+
expect(res).to.exist
106+
})
107+
})
108+
109+
it('.ls hash', () => {
110+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
111+
112+
return ipfs.pin.ls(hash)
113+
.then((res) => {
114+
expect(res).to.exist
115+
})
116+
})
117+
118+
it('.rm', () => {
119+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
120+
121+
return ipfs.pin.rm(hash, { recursive: false })
122+
.then((res) => {
123+
expect(res).to.exist
124+
return ipfs.pin.ls({ type: 'direct' })
125+
})
126+
.then((res) => {
127+
expect(res).to.exist
128+
expect(res.Keys).to.be.empty
129+
})
130+
})
131+
})
132+
})
133+
}

0 commit comments

Comments
 (0)