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

Commit 5b0b144

Browse files
committed
feat: modularise object.data,get,links,put and stat
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 1d4d3e9 commit 5b0b144

File tree

6 files changed

+979
-2
lines changed

6 files changed

+979
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ tests.repo.gc(createCommon, { skip: true }) // pass an options object to skip th
107107

108108
// OR, at the subsystem level
109109

110-
tests.repo(createCommon, { skip: ['gc'] }) // skips ALL the repo.gc tests
110+
// skips ALL the repo.gc tests
111+
tests.repo(createCommon, { skip: ['gc'] })
112+
// skips ALL the object.patch.addLink tests
113+
tests.object(createCommon, { skip: ['patch.addLink'] })
111114
```
112115

113116
##### Skipping specific tests
@@ -127,7 +130,10 @@ tests.repo.gc(createCommon, { only: true }) // pass an options object to run onl
127130

128131
// OR, at the subsystem level
129132

130-
tests.repo(createCommon, { only: ['gc'] }) // runs only ALL the repo.gc tests
133+
// runs only ALL the repo.gc tests
134+
tests.repo(createCommon, { only: ['gc'] })
135+
// runs only ALL the object.patch.addLink tests
136+
tests.object(createCommon, { only: ['patch.addLink'] })
131137
```
132138

133139
##### Running only specific tests

js/src/object/data.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const bs58 = require('bs58')
9+
const hat = require('hat')
10+
const { getDescribe, getIt } = require('../utils/mocha')
11+
12+
const expect = chai.expect
13+
chai.use(dirtyChai)
14+
15+
module.exports = (createCommon, options) => {
16+
const describe = getDescribe(options)
17+
const it = getIt(options)
18+
const common = createCommon()
19+
20+
describe('.object.data', function () {
21+
this.timeout(80 * 1000)
22+
23+
let ipfs
24+
25+
before(function (done) {
26+
// CI takes longer to instantiate the daemon, so we need to increase the
27+
// timeout for the before step
28+
this.timeout(60 * 1000)
29+
30+
common.setup((err, factory) => {
31+
expect(err).to.not.exist()
32+
factory.spawnNode((err, node) => {
33+
expect(err).to.not.exist()
34+
ipfs = node
35+
done()
36+
})
37+
})
38+
})
39+
40+
after((done) => common.teardown(done))
41+
42+
it('should get data by multihash', (done) => {
43+
const testObj = {
44+
Data: Buffer.from(hat()),
45+
Links: []
46+
}
47+
48+
ipfs.object.put(testObj, (err, node) => {
49+
expect(err).to.not.exist()
50+
51+
ipfs.object.data(node.multihash, (err, data) => {
52+
expect(err).to.not.exist()
53+
54+
// because js-ipfs-api can't infer
55+
// if the returned Data is Buffer or String
56+
if (typeof data === 'string') {
57+
data = Buffer.from(data)
58+
}
59+
expect(node.data).to.eql(data)
60+
done()
61+
})
62+
})
63+
})
64+
65+
it('should get data by multihash (promised)', () => {
66+
const testObj = {
67+
Data: Buffer.from(hat()),
68+
Links: []
69+
}
70+
71+
return ipfs.object.put(testObj).then((node) => {
72+
return ipfs.object.data(node.multihash).then((data) => {
73+
// because js-ipfs-api can't infer
74+
// if the returned Data is Buffer or String
75+
if (typeof data === 'string') {
76+
data = Buffer.from(data)
77+
}
78+
expect(node.data).to.deep.equal(data)
79+
})
80+
})
81+
})
82+
83+
it('should get data by base58 encoded multihash', (done) => {
84+
const testObj = {
85+
Data: Buffer.from(hat()),
86+
Links: []
87+
}
88+
89+
ipfs.object.put(testObj, (err, node) => {
90+
expect(err).to.not.exist()
91+
92+
ipfs.object.data(bs58.encode(node.multihash), { enc: 'base58' }, (err, data) => {
93+
expect(err).to.not.exist()
94+
95+
// because js-ipfs-api can't infer
96+
// if the returned Data is Buffer or String
97+
if (typeof data === 'string') {
98+
data = Buffer.from(data)
99+
}
100+
expect(node.data).to.eql(data)
101+
done()
102+
})
103+
})
104+
})
105+
106+
it('should get data by base58 encoded multihash string', (done) => {
107+
const testObj = {
108+
Data: Buffer.from(hat()),
109+
Links: []
110+
}
111+
112+
ipfs.object.put(testObj, (err, node) => {
113+
expect(err).to.not.exist()
114+
115+
ipfs.object.data(bs58.encode(node.multihash).toString(), { enc: 'base58' }, (err, data) => {
116+
expect(err).to.not.exist()
117+
118+
// because js-ipfs-api can't infer if the
119+
// returned Data is Buffer or String
120+
if (typeof data === 'string') {
121+
data = Buffer.from(data)
122+
}
123+
expect(node.data).to.eql(data)
124+
done()
125+
})
126+
})
127+
})
128+
})
129+
}

0 commit comments

Comments
 (0)