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

Commit 1c9d12a

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

File tree

4 files changed

+169
-15
lines changed

4 files changed

+169
-15
lines changed

API/pinning-api/README.md

+32-14
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

@@ -46,19 +53,27 @@ If no `callback` is passed, a promise is returned.
4653
Example:
4754

4855
```JavaScript
49-
ipfs.pin.ls(function (err, pinset) {})
56+
ipfs.pin.ls(function (err, pinset) {
57+
if (err) {
58+
throw err
59+
}
60+
// console.log(pinset)
61+
})
5062
```
5163

5264

5365
#### `rm`
5466

55-
> Remove an hash from the pinset
67+
> Remove a hash from the pinset
5668
5769
##### `Go` **WIP**
5870

59-
##### `JavaScript` - ipfs.pin.rm(hash, [callback])
71+
##### `JavaScript` - ipfs.pin.rm(hash, [options, callback])
6072

61-
Where `hash` is a multihash.
73+
Where:
74+
- `hash` is a multihash.
75+
- `options` is an object that can contain the following keys
76+
- `recursive` - Recursively unpin the object linked. Type: bool. Default: 'false'
6277

6378
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
6479

@@ -67,7 +82,10 @@ If no `callback` is passed, a promise is returned.
6782
Example:
6883

6984
```JavaScript
70-
ipfs.pin.rm(hash, function (err, pinset) {})
85+
ipfs.pin.rm(hash, function (err, pinset) {
86+
if (err) {
87+
throw err
88+
}
89+
// console.log(pinset) prints the hashes that were unpinned
90+
})
7191
```
72-
73-

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

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 takes longer to instantiate the daemon,
18+
// so we need to increase the timeout for the
19+
// before step
20+
this.timeout(20 * 1000)
21+
22+
common.setup((err, factory) => {
23+
expect(err).to.not.exist
24+
factory.spawnNode((err, node) => {
25+
expect(err).to.not.exist
26+
ipfs = node
27+
done()
28+
})
29+
})
30+
})
31+
32+
after((done) => {
33+
common.teardown(done)
34+
})
35+
36+
it('add file for testing', (done) => {
37+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
38+
39+
ipfs.files.add(testfile, (err, res) => {
40+
expect(err).to.not.exist
41+
42+
expect(res).to.have.length(1)
43+
expect(res[0].hash).to.equal(expectedMultihash)
44+
expect(res[0].path).to.equal(expectedMultihash)
45+
done()
46+
})
47+
})
48+
49+
describe('callback API', () => {
50+
it('.rm (1st, because ipfs.files.add pins automatically)', (done) => {
51+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
52+
53+
ipfs.pin.rm(hash, { recursive: true }, (err, res) => {
54+
expect(err).to.not.exist
55+
expect(res).to.exist
56+
ipfs.pin.ls({ type: 'direct' }, (err, res) => {
57+
expect(err).to.not.exist
58+
expect(res).to.exist
59+
expect(res.Keys).to.be.empty
60+
done()
61+
})
62+
})
63+
})
64+
65+
it('.add', (done) => {
66+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
67+
68+
ipfs.pin.add(hash, { recursive: false }, (err, res) => {
69+
expect(err).to.not.exist
70+
expect(res.Pins[0]).to.be.equal(hash)
71+
done()
72+
})
73+
})
74+
75+
it('.list', (done) => {
76+
ipfs.pin.ls((err, res) => {
77+
expect(err).to.not.exist
78+
expect(res).to.exist
79+
done()
80+
})
81+
})
82+
83+
it('.list for a specific hash', (done) => {
84+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
85+
86+
ipfs.pin.ls(hash, (err, res) => {
87+
expect(err).to.not.exist
88+
expect(res).to.exist
89+
done()
90+
})
91+
})
92+
})
93+
94+
describe('promise API', () => {
95+
it('.add', () => {
96+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
97+
98+
return ipfs.pin.add(hash, { recursive: false })
99+
.then((res) => {
100+
expect(res.Pins[0]).to.be.equal(hash)
101+
})
102+
})
103+
104+
it('.ls', () => {
105+
return ipfs.pin.ls()
106+
.then((res) => {
107+
expect(res).to.exist
108+
})
109+
})
110+
111+
it('.ls hash', () => {
112+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
113+
114+
return ipfs.pin.ls(hash)
115+
.then((res) => {
116+
expect(res).to.exist
117+
})
118+
})
119+
120+
it('.rm', () => {
121+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
122+
123+
return ipfs.pin.rm(hash, { recursive: false })
124+
.then((res) => {
125+
expect(res).to.exist
126+
return ipfs.pin.ls({ type: 'direct' })
127+
})
128+
.then((res) => {
129+
expect(res).to.exist
130+
expect(res.Keys).to.be.empty
131+
})
132+
})
133+
})
134+
})
135+
}

0 commit comments

Comments
 (0)