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

Commit 351ad26

Browse files
author
Alan Shaw
committed
refactor: object tests for new ipld format apis
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent ba6ea4a commit 351ad26

File tree

7 files changed

+123
-296
lines changed

7 files changed

+123
-296
lines changed

src/object/get.js

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const dagPB = require('ipld-dag-pb')
5-
const DAGNode = dagPB.DAGNode
4+
const { DAGNode } = require('ipld-dag-pb')
65
const series = require('async/series')
76
const hat = require('hat')
87
const { getDescribe, getIt, expect } = require('../utils/mocha')
@@ -67,15 +66,15 @@ module.exports = (createCommon, options) => {
6766

6867
// because js-ipfs-api can't infer if the
6968
// returned Data is Buffer or String
70-
if (typeof node2.data === 'string') {
71-
node2.data = Buffer.from(node2.data)
69+
if (typeof node2.Data === 'string') {
70+
node2.Data = Buffer.from(node2.Data)
7271
}
7372
cb()
7473
})
7574
},
7675
(cb) => {
77-
expect(node1.data).to.eql(node2.data)
78-
expect(node1.links).to.eql(node2.links)
76+
expect(node1.Data).to.deep.equal(node2.Data)
77+
expect(node1.Links).to.deep.equal(node2.Links)
7978
cb()
8079
}
8180
], done)
@@ -93,12 +92,12 @@ module.exports = (createCommon, options) => {
9392

9493
// because js-ipfs-api can't infer if the
9594
// returned Data is Buffer or String
96-
if (typeof node2.data === 'string') {
97-
node2.data = Buffer.from(node2.data)
95+
if (typeof node2.Data === 'string') {
96+
node2.Data = Buffer.from(node2.Data)
9897
}
9998

100-
expect(node1.data).to.deep.equal(node2.data)
101-
expect(node1.links).to.deep.equal(node2.links)
99+
expect(node1.Data).to.deep.equal(node2.Data)
100+
expect(node1.Links).to.deep.equal(node2.Links)
102101
})
103102

104103
it('should get object by multihash string', (done) => {
@@ -130,16 +129,16 @@ module.exports = (createCommon, options) => {
130129
expect(err).to.not.exist()
131130
// because js-ipfs-api can't infer if the
132131
// returned Data is Buffer or String
133-
if (typeof node.data === 'string') {
134-
node.data = Buffer.from(node.data)
132+
if (typeof node.Data === 'string') {
133+
node.Data = Buffer.from(node.Data)
135134
}
136135
node2 = node
137136
cb()
138137
})
139138
},
140139
(cb) => {
141-
expect(node1.data).to.eql(node2.data)
142-
expect(node1.links).to.eql(node2.links)
140+
expect(node1.Data).to.deep.equal(node2.Data)
141+
expect(node1.Links).to.deep.equal(node2.Links)
143142
cb()
144143
}
145144
], done)
@@ -157,12 +156,12 @@ module.exports = (createCommon, options) => {
157156

158157
// because js-ipfs-api can't infer if the
159158
// returned Data is Buffer or String
160-
if (typeof node2.data === 'string') {
161-
node2.data = Buffer.from(node2.data)
159+
if (typeof node2.Data === 'string') {
160+
node2.Data = Buffer.from(node2.Data)
162161
}
163162

164-
expect(node1.data).to.deep.equal(node2.data)
165-
expect(node1.links).to.deep.equal(node2.links)
163+
expect(node1.Data).to.deep.equal(node2.Data)
164+
expect(node1.Links).to.deep.equal(node2.Links)
166165
})
167166

168167
it('should get object with links by multihash string', (done) => {
@@ -174,31 +173,16 @@ module.exports = (createCommon, options) => {
174173

175174
series([
176175
(cb) => {
177-
DAGNode.create(Buffer.from('Some data 1'), (err, node) => {
178-
expect(err).to.not.exist()
179-
node1a = node
180-
181-
cb()
182-
})
183-
},
184-
(cb) => {
185-
DAGNode.create(Buffer.from('Some data 2'), (err, node) => {
186-
expect(err).to.not.exist()
187-
node2 = node
188-
189-
cb()
190-
})
176+
node1a = DAGNode.create(Buffer.from('Some data 1'))
177+
node2 = DAGNode.create(Buffer.from('Some data 2'))
178+
cb()
191179
},
192180
(cb) => {
193-
asDAGLink(node2, 'some-link', (err, link) => {
194-
expect(err).to.not.exist()
195-
196-
DAGNode.addLink(node1a, link, (err, node) => {
197-
expect(err).to.not.exist()
198-
node1b = node
199-
cb()
200-
})
201-
})
181+
asDAGLink(node2, 'some-link')
182+
.then(link => DAGNode.addLink(node1a, link))
183+
.then(node => { node1b = node })
184+
.then(cb)
185+
.catch(cb)
202186
},
203187
(cb) => {
204188
ipfs.object.put(node1b, (err, cid) => {
@@ -213,16 +197,16 @@ module.exports = (createCommon, options) => {
213197

214198
// because js-ipfs-api can't infer if the
215199
// returned Data is Buffer or String
216-
if (typeof node.data === 'string') {
217-
node.data = Buffer.from(node.data)
200+
if (typeof node.Data === 'string') {
201+
node.Data = Buffer.from(node.Data)
218202
}
219203

220204
node1c = node
221205
cb()
222206
})
223207
},
224208
(cb) => {
225-
expect(node1a.data).to.eql(node1c.data)
209+
expect(node1a.Data).to.eql(node1c.Data)
226210
cb()
227211
}
228212
], done)
@@ -257,15 +241,15 @@ module.exports = (createCommon, options) => {
257241
// because js-ipfs-api can't infer if the
258242
// returned Data is Buffer or String
259243
if (typeof node.data === 'string') {
260-
node.data = Buffer.from(node.data)
244+
node.Data = Buffer.from(node.Data)
261245
}
262246
node1b = node
263247
cb()
264248
})
265249
},
266250
(cb) => {
267-
expect(node1a.data).to.eql(node1b.data)
268-
expect(node1a.links).to.eql(node1b.links)
251+
expect(node1a.Data).to.eql(node1b.Data)
252+
expect(node1a.Links).to.eql(node1b.Links)
269253
cb()
270254
}
271255
], done)
@@ -299,16 +283,16 @@ module.exports = (createCommon, options) => {
299283
expect(err).to.not.exist()
300284
// because js-ipfs-api can't infer if the
301285
// returned Data is Buffer or String
302-
if (typeof node.data === 'string') {
303-
node.data = Buffer.from(node.data)
286+
if (typeof node.Data === 'string') {
287+
node.Data = Buffer.from(node.Data)
304288
}
305289
node1b = node
306290
cb()
307291
})
308292
},
309293
(cb) => {
310-
expect(node1a.data).to.eql(node1b.data)
311-
expect(node1a.links).to.eql(node1b.links)
294+
expect(node1a.Data).to.eql(node1b.Data)
295+
expect(node1a.Links).to.eql(node1b.Links)
312296
cb()
313297
}
314298
], done)
@@ -342,7 +326,7 @@ module.exports = (createCommon, options) => {
342326
return ipfs.object.get(result[0].hash)
343327
})
344328
.then((node) => {
345-
const meta = UnixFs.unmarshal(node.data)
329+
const meta = UnixFs.unmarshal(node.Data)
346330

347331
expect(meta.fileSize()).to.equal(data.length)
348332
})

src/object/links.js

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = (createCommon, options) => {
5050

5151
ipfs.object.links(cid, (err, links) => {
5252
expect(err).to.not.exist()
53-
expect(node.links).to.deep.equal(links)
53+
expect(node.Links).to.deep.equal(links)
5454
done()
5555
})
5656
})
@@ -67,54 +67,36 @@ module.exports = (createCommon, options) => {
6767
const node = await ipfs.object.get(cid)
6868
const links = await ipfs.object.links(cid)
6969

70-
expect(node.links).to.eql(links)
70+
expect(node.Links).to.eql(links)
7171
})
7272

7373
it('should get links by multihash', (done) => {
74-
let node1a
74+
const node1a = DAGNode.create(Buffer.from('Some data 1'))
7575
let node1b
7676
let node1bCid
77-
let node2
77+
const node2 = DAGNode.create(Buffer.from('Some data 2'))
7878

7979
series([
8080
(cb) => {
81-
DAGNode.create(Buffer.from('Some data 1'), (err, node) => {
82-
expect(err).to.not.exist()
83-
node1a = node
84-
85-
cb()
86-
})
87-
},
88-
(cb) => {
89-
DAGNode.create(Buffer.from('Some data 2'), (err, node) => {
90-
expect(err).to.not.exist()
91-
node2 = node
92-
cb()
93-
})
94-
},
95-
(cb) => {
96-
asDAGLink(node2, 'some-link', (err, link) => {
97-
expect(err).to.not.exist()
98-
99-
DAGNode.addLink(node1a, link, (err, node) => {
100-
expect(err).to.not.exist()
101-
node1b = node
102-
103-
dagPB.util.cid(node, (err, cid) => {
104-
expect(err).to.not.exist()
105-
node1bCid = cid
106-
cb()
107-
})
108-
})
109-
})
81+
asDAGLink(node2, 'some-link')
82+
.then(link => DAGNode.addLink(node1a, link))
83+
.then(node => { node1b = node })
84+
.then(() => dagPB.util.cid(dagPB.util.serialize(node1b)))
85+
.then(cid => { node1bCid = cid })
86+
.then(cb)
87+
.catch(cb)
11088
},
11189
(cb) => {
11290
ipfs.object.put(node1b, (cb))
11391
},
11492
(cb) => {
11593
ipfs.object.links(node1bCid, (err, links) => {
11694
expect(err).to.not.exist()
117-
expect(node1b.links[0].toJSON()).to.eql(links[0].toJSON())
95+
expect({
96+
cid: node1b.Links[0].Hash.toString(),
97+
name: node1b.Links[0].Name,
98+
size: node1b.Links[0].Tsize
99+
}).to.eql(links[0].toJSON())
118100
cb()
119101
})
120102
}
@@ -135,7 +117,7 @@ module.exports = (createCommon, options) => {
135117

136118
ipfs.object.links(cid.buffer, { enc: 'base58' }, (err, links) => {
137119
expect(err).to.not.exist()
138-
expect(node.links).to.deep.equal(links)
120+
expect(node.Links).to.deep.equal(links)
139121
done()
140122
})
141123
})
@@ -156,48 +138,11 @@ module.exports = (createCommon, options) => {
156138

157139
ipfs.object.links(cid.toBaseEncodedString(), { enc: 'base58' }, (err, links) => {
158140
expect(err).to.not.exist()
159-
expect(node.links).to.deep.equal(links)
141+
expect(node.Links).to.deep.equal(links)
160142
done()
161143
})
162144
})
163145
})
164146
})
165-
166-
it('should get links from CBOR object', (done) => {
167-
const hashes = []
168-
ipfs.add(Buffer.from('test data'), (err, res1) => {
169-
expect(err).to.not.exist()
170-
hashes.push(res1[0].hash)
171-
ipfs.add(Buffer.from('more test data'), (err, res2) => {
172-
hashes.push(res2[0].hash)
173-
expect(err).to.not.exist()
174-
const obj = {
175-
some: 'data',
176-
mylink: { '/': hashes[0] },
177-
myobj: {
178-
anotherLink: { '/': hashes[1] }
179-
}
180-
}
181-
ipfs.dag.put(obj, (err, cid) => {
182-
expect(err).to.not.exist()
183-
ipfs.object.links(cid, (err, links) => {
184-
expect(err).to.not.exist()
185-
expect(links.length).to.eql(2)
186-
187-
// TODO: js-ipfs succeeds but go returns empty strings for link name
188-
// const names = [links[0].name, links[1].name]
189-
// expect(names).includes('mylink')
190-
// expect(names).includes('myobj/anotherLink')
191-
192-
const cids = [links[0].cid.toString(), links[1].cid.toString()]
193-
expect(cids).includes(hashes[0])
194-
expect(cids).includes(hashes[1])
195-
196-
done()
197-
})
198-
})
199-
})
200-
})
201-
})
202147
})
203148
}

0 commit comments

Comments
 (0)