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

Commit 11a75d1

Browse files
committed
use object tests from interface-ipfs-core
1 parent e8f5b64 commit 11a75d1

File tree

3 files changed

+73
-236
lines changed

3 files changed

+73
-236
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"detect-node": "^2.0.3",
1010
"flatmap": "0.0.3",
1111
"glob": "^7.0.3",
12+
"ipfs-merkle-dag": "^0.5.1",
1213
"multiaddr": "^1.3.0",
1314
"multipart-stream": "^2.0.1",
1415
"ndjson": "^1.4.3",
@@ -93,4 +94,4 @@
9394
}
9495
}
9596
}
96-
}
97+
}

src/api/object.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
'use strict'
22

33
const argCommand = require('../cmd-helpers').argCommand
4+
const DAGNode = require('ipfs-merkle-dag').DAGNode
45

56
module.exports = (send) => {
67
return {
78
get: argCommand(send, 'object/get'),
8-
put (file, encoding, cb) {
9-
if (typeof encoding === 'function') {
10-
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
9+
put (obj, options, callback) {
10+
if (typeof options === 'function') {
11+
callback = options
12+
options = {}
1113
}
12-
return send('object/put', encoding, null, file, cb)
14+
if (!options) {
15+
options = {}
16+
}
17+
18+
let tmpObj = {
19+
Data: null,
20+
Links: []
21+
}
22+
23+
if (Buffer.isBuffer(obj)) {
24+
if (!options.enc) {
25+
tmpObj = { Data: obj.toString(), Links: [] }
26+
} else {
27+
tmpObj = JSON.parse(obj.toString())
28+
}
29+
} else if (obj.multihash) {
30+
tmpObj = {
31+
Data: obj.data.toString(),
32+
Links: obj.links.map((l) => { return l.toJSON() })
33+
}
34+
} else if (typeof obj === 'object') {
35+
tmpObj.Data = obj.Data.toString()
36+
} else {
37+
return callback(new Error('obj not recognized'))
38+
}
39+
40+
const buf = new Buffer(JSON.stringify(tmpObj))
41+
const enc = options.enc || 'json'
42+
43+
return send('object/put', enc, null, buf, (err, result) => {
44+
if (err) {
45+
return callback(err)
46+
}
47+
48+
if (Buffer.isBuffer(obj)) {
49+
if (!options.enc) {
50+
obj = { Data: obj, Links: [] }
51+
} else {
52+
obj = JSON.parse(obj.toString())
53+
}
54+
}
55+
let node
56+
if (obj.multihash) {
57+
node = obj
58+
console.log('->', node)
59+
} else {
60+
node = new DAGNode(obj.Data, obj.Links)
61+
}
62+
63+
if (node.toJSON().Hash !== result.Hash) {
64+
return callback(new Error('Stored object was different from constructed object'))
65+
}
66+
67+
callback(null, node)
68+
})
1369
},
1470
data: argCommand(send, 'object/data'),
1571
links: argCommand(send, 'object/links'),

test/api/object.spec.js

Lines changed: 11 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -1,237 +1,17 @@
11
/* eslint-env mocha */
22
/* globals apiClients */
3-
'use strict'
4-
5-
const expect = require('chai').expect
6-
7-
describe('.object', () => {
8-
const testObject = Buffer(JSON.stringify({Data: 'testdata', Links: []}))
9-
const testObjectHash = 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD'
10-
const testPatchObject = Buffer(JSON.stringify({Data: 'new test data'}))
11-
const testPatchObjectHash = 'QmWJDtdQWQSajQPx1UVAGWKaSGrHVWdjnrNhbooHP7LuF2'
12-
13-
it('object.put', (done) => {
14-
apiClients.a.object.put(testObject, 'json', (err, res) => {
15-
expect(err).to.not.exist
16-
expect(res).to.have.a.property('Hash', testObjectHash)
17-
expect(res.Links).to.be.empty
18-
done()
19-
})
20-
})
21-
22-
it('object.get', (done) => {
23-
apiClients.a.object.get(testObjectHash, (err, res) => {
24-
expect(err).to.not.exist
25-
expect(res).to.have.a.property('Data', 'testdata')
26-
expect(res.Links).to.be.empty
27-
done()
28-
})
29-
})
30-
31-
it('object.data', (done) => {
32-
apiClients.a.object.data(testObjectHash, (err, res) => {
33-
expect(err).to.not.exist
34-
35-
let buf = ''
36-
res
37-
.on('error', (err) => {
38-
expect(err).to.not.exist
39-
})
40-
.on('data', (data) => {
41-
buf += data
42-
})
43-
.on('end', () => {
44-
expect(buf).to.equal('testdata')
45-
done()
46-
})
47-
})
48-
})
49-
50-
it('object.stat', (done) => {
51-
apiClients.a.object.stat(testObjectHash, (err, res) => {
52-
expect(err).to.not.exist
53-
expect(res).to.be.eql({
54-
Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
55-
NumLinks: 0,
56-
BlockSize: 10,
57-
LinksSize: 2,
58-
DataSize: 8,
59-
CumulativeSize: 10
60-
})
61-
done()
62-
})
63-
})
64-
65-
it('object.links', (done) => {
66-
apiClients.a.object.links(testObjectHash, (err, res) => {
67-
expect(err).to.not.exist
683

69-
expect(res).to.be.eql({
70-
Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD'
71-
})
72-
done()
73-
})
74-
})
75-
76-
describe('object.patch', () => {
77-
before((done) => {
78-
apiClients.a.object.put(testPatchObject, 'json', (err, res) => {
79-
expect(err).to.not.exist
80-
done()
81-
})
82-
})
83-
84-
it('.addLink', (done) => {
85-
apiClients.a.object.patch
86-
.addLink(testObjectHash, 'next', testPatchObjectHash, (err, res) => {
87-
expect(err).to.not.exist
88-
expect(res).to.be.eql({
89-
Hash: 'QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd'
90-
})
91-
apiClients.a.object.get(res.Hash, (err, res) => {
92-
expect(err).to.not.exist
93-
expect(res).to.be.eql({
94-
Data: 'testdata',
95-
Links: [{
96-
Name: 'next',
97-
Hash: 'QmWJDtdQWQSajQPx1UVAGWKaSGrHVWdjnrNhbooHP7LuF2',
98-
Size: 15
99-
}]
100-
})
101-
done()
102-
})
103-
})
104-
})
105-
106-
it('.rmLink', (done) => {
107-
apiClients.a.object.patch
108-
.rmLink('QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd', 'next', (err, res) => {
109-
expect(err).to.not.exist
110-
expect(res).to.be.eql({
111-
Hash: testObjectHash
112-
})
113-
apiClients.a.object.get(res.Hash, (err, res) => {
114-
expect(err).to.not.exist
115-
expect(res).to.be.eql({
116-
Data: 'testdata',
117-
Links: []
118-
})
119-
done()
120-
})
121-
})
122-
})
123-
124-
it('.appendData', (done) => {
125-
apiClients.a.object.patch
126-
.appendData(testObjectHash, new Buffer(' hello'), (err, res) => {
127-
expect(err).to.not.exist
128-
expect(res).to.be.eql({
129-
Hash: 'Qmcjhr2QztQxCAoEf8tJPTGTVkTsUrTQ36JurH14DNYNsc'
130-
})
131-
apiClients.a.object.get(res.Hash, (err, res) => {
132-
expect(err).to.not.exist
133-
expect(res).to.be.eql({
134-
Data: 'testdata hello',
135-
Links: []
136-
})
137-
done()
138-
})
139-
})
140-
})
141-
it('.setData', (done) => {
142-
apiClients.a.object.patch
143-
.setData(testObjectHash, new Buffer('hello world'), (err, res) => {
144-
expect(err).to.not.exist
145-
expect(res).to.be.eql({
146-
Hash: 'QmU1Sq1B7RPQD2XcQNLB58qJUyJffVJqihcxmmN1STPMxf'
147-
})
148-
apiClients.a.object.get(res.Hash, (err, res) => {
149-
expect(err).to.not.exist
150-
expect(res).to.be.eql({
151-
Data: 'hello world',
152-
Links: []
153-
})
154-
done()
155-
})
156-
})
157-
})
158-
})
159-
160-
it('object.new', (done) => {
161-
apiClients.a.object.new('unixfs-dir', (err, res) => {
162-
expect(err).to.not.exist
163-
expect(res).to.deep.equal({
164-
Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
165-
})
166-
done()
167-
})
168-
})
169-
170-
describe('promise', () => {
171-
it('object.put', () => {
172-
return apiClients.a.object.put(testObject, 'json')
173-
.then((res) => {
174-
expect(res).to.have.a.property('Hash', testObjectHash)
175-
expect(res.Links).to.be.empty
176-
})
177-
})
178-
179-
it('object.get', () => {
180-
return apiClients.a.object.get(testObjectHash)
181-
.then((res) => {
182-
expect(res).to.have.a.property('Data', 'testdata')
183-
expect(res.Links).to.be.empty
184-
})
185-
})
186-
187-
it('object.data', (done) => {
188-
return apiClients.a.object.data(testObjectHash)
189-
.then((res) => {
190-
let buf = ''
191-
res
192-
.on('error', (err) => {
193-
throw err
194-
})
195-
.on('data', (data) => {
196-
buf += data
197-
})
198-
.on('end', () => {
199-
expect(buf).to.equal('testdata')
200-
done()
201-
})
202-
})
203-
})
4+
'use strict'
2045

205-
it('object.stat', () => {
206-
return apiClients.a.object.stat(testObjectHash)
207-
.then((res) => {
208-
expect(res).to.be.eql({
209-
Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
210-
NumLinks: 0,
211-
BlockSize: 10,
212-
LinksSize: 2,
213-
DataSize: 8,
214-
CumulativeSize: 10
215-
})
216-
})
217-
})
6+
const test = require('interface-ipfs-core')
2187

219-
it('object.links', () => {
220-
return apiClients.a.object.links(testObjectHash)
221-
.then((res) => {
222-
expect(res).to.be.eql({
223-
Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD'
224-
})
225-
})
226-
})
8+
const common = {
9+
setup: function (cb) {
10+
cb(null, apiClients.a)
11+
},
12+
teardown: function (cb) {
13+
cb()
14+
}
15+
}
22716

228-
it('object.new', () => {
229-
return apiClients.a.object.new('unixfs-dir')
230-
.then((res) => {
231-
expect(res).to.deep.equal({
232-
Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
233-
})
234-
})
235-
})
236-
})
237-
})
17+
test.object(common)

0 commit comments

Comments
 (0)