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

Commit 5da5966

Browse files
kenshyxdaviddias
authored andcommitted
testing(): add object.get tests for multihash string (#139)
* testing(): add object.get tests for multihash string
1 parent 0433312 commit 5da5966

File tree

1 file changed

+76
-88
lines changed

1 file changed

+76
-88
lines changed

src/object.js

+76-88
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ module.exports = (common) => {
230230
cb()
231231
})
232232
},
233+
(cb) => {
234+
// get object from ipfs multihash string
235+
ipfs.object.get(node1.toJSON().multihash, (err, node) => {
236+
expect(err).to.not.exist()
237+
expect(node).to.exist()
238+
cb()
239+
})
240+
},
233241
(cb) => {
234242
expect(node1.data).to.eql(node2.data)
235243
expect(node1.links).to.eql(node2.links)
@@ -756,39 +764,37 @@ module.exports = (common) => {
756764
})
757765

758766
describe('promise API', () => {
759-
it('object.new', (done) => {
760-
ipfs.object.new()
767+
it('object.new', () => {
768+
return ipfs.object.new()
761769
.then((node) => {
762770
const nodeJSON = node.toJSON()
763771
expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
764-
done()
765772
})
766773
})
767774

768-
it('object.put', (done) => {
775+
it('object.put', () => {
769776
const obj = {
770777
Data: new Buffer('Some data'),
771778
Links: []
772779
}
773780

774-
ipfs.object.put(obj)
781+
return ipfs.object.put(obj)
775782
.then((node) => {
776783
const nodeJSON = node.toJSON()
777784
expect(obj.Data).to.deep.equal(nodeJSON.data)
778785
expect(obj.Links).to.deep.equal(nodeJSON.links)
779786
expect(nodeJSON.multihash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK')
780-
done()
781787
})
782788
})
783789

784-
it('object.get', (done) => {
790+
it('object.get', () => {
785791
const testObj = {
786792
Data: new Buffer('get test object'),
787793
Links: []
788794
}
789795

790-
ipfs.object.put(testObj).then((node1) => {
791-
ipfs.object.get(node1.multihash).then((node2) => {
796+
return ipfs.object.put(testObj).then((node1) => {
797+
return ipfs.object.get(node1.multihash).then((node2) => {
792798
// because js-ipfs-api can't infer if the
793799
// returned Data is Buffer or String
794800
if (typeof node2.data === 'string') {
@@ -797,40 +803,44 @@ module.exports = (common) => {
797803

798804
expect(node1.data).to.deep.equal(node2.data)
799805
expect(node1.links).to.deep.equal(node2.links)
800-
done()
801806
})
802807
})
803808
})
804809

805-
it('object.data', (done) => {
810+
it('object.get multihash string', () => {
811+
return ipfs.object.get('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK').then((node) => {
812+
expect(node.data).to.exist()
813+
})
814+
})
815+
816+
it('object.data', () => {
806817
const testObj = {
807818
Data: new Buffer('get test object'),
808819
Links: []
809820
}
810821

811-
ipfs.object.put(testObj).then((node) => {
812-
ipfs.object.data(node.multihash).then((data) => {
822+
return ipfs.object.put(testObj).then((node) => {
823+
return ipfs.object.data(node.multihash).then((data) => {
813824
// because js-ipfs-api can't infer
814825
// if the returned Data is Buffer or String
815826
if (typeof data === 'string') {
816827
data = new Buffer(data)
817828
}
818829
expect(node.data).to.deep.equal(data)
819-
done()
820830
})
821831
})
822832
})
823833

824-
it('object.stat', (done) => {
834+
it('object.stat', () => {
825835
const testObj = {
826836
Data: new Buffer('get test object'),
827837
Links: []
828838
}
829839

830-
ipfs.object.put(testObj, (err, node) => {
840+
return ipfs.object.put(testObj, (err, node) => {
831841
expect(err).to.not.exist()
832842

833-
ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', {enc: 'base58'})
843+
return ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', {enc: 'base58'})
834844
.then((stats) => {
835845
const expected = {
836846
Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
@@ -841,24 +851,19 @@ module.exports = (common) => {
841851
CumulativeSize: 17
842852
}
843853
expect(expected).to.deep.equal(stats)
844-
done()
845854
})
846-
.catch((err) => {
847-
expect(err).to.not.exist()
848-
})
849855
})
850856
})
851857

852-
it('object.links', (done) => {
858+
it('object.links', () => {
853859
const testObj = {
854860
Data: new Buffer('get test object'),
855861
Links: []
856862
}
857863

858-
ipfs.object.put(testObj).then((node) => {
859-
ipfs.object.links(node.multihash).then((links) => {
864+
return ipfs.object.put(testObj).then((node) => {
865+
return ipfs.object.links(node.multihash).then((links) => {
860866
expect(node.links).to.eql(links)
861-
done()
862867
})
863868
})
864869
})
@@ -873,94 +878,77 @@ module.exports = (common) => {
873878
Links: []
874879
}
875880

876-
before((done) => {
877-
ipfs.object.put(obj, (err, node) => {
881+
before(() => {
882+
return ipfs.object.put(obj, (err, node) => {
878883
expect(err).to.not.exist()
879884
testNodeMultihash = node.multihash
880-
done()
881885
})
882886
})
883887

884-
it('.addLink', (done) => {
888+
it('.addLink', () => {
885889
let node1a
886890
let node1b
887891
let node2
888-
889-
series([
890-
(cb) => {
891-
DAGNode.create(obj.Data, obj.Links, (err, node) => {
892-
expect(err).to.not.exist()
893-
node1a = node
894-
cb()
895-
})
896-
},
897-
(cb) => {
898-
DAGNode.create(new Buffer('some other node'), (err, node) => {
899-
expect(err).to.not.exist()
900-
node2 = node
901-
cb()
892+
return new Promise((resolve, reject) => {
893+
DAGNode.create(obj.Data, obj.Links, function (err, node) {
894+
if (err) {
895+
return reject(err)
896+
}
897+
return resolve(node)
898+
})
899+
}).then((node) => {
900+
node1a = node
901+
return new Promise((resolve, reject) => {
902+
DAGNode.create(new Buffer('some other node'), function (err, node) {
903+
if (err) {
904+
return reject(err)
905+
}
906+
return resolve(node)
902907
})
903-
},
904-
(cb) => {
905-
// note: we need to put the linked obj, otherwise IPFS won't
906-
// timeout. Reason: it needs the node to get its size
907-
ipfs.object.put(node2, cb)
908-
},
909-
(cb) => {
910-
const link = node2.toJSON()
911-
link.name = 'link-to-node'
912-
DAGNode.addLink(node1a, link, (err, node) => {
913-
expect(err).to.not.exist()
914-
node1b = node
915-
cb()
908+
}).then((node1) => {
909+
node2 = node1
910+
return ipfs.object.put(node2)
911+
})
912+
}).then(() => {
913+
const link = node2.toJSON()
914+
link.name = 'link-to-node'
915+
return new Promise((resolve, reject) => {
916+
DAGNode.addLink(node1a, link, function (err, node) {
917+
if (err) {
918+
return reject(err)
919+
}
920+
return resolve(node)
916921
})
917-
},
918-
(cb) => {
919-
ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0])
920-
.then((node) => {
921-
expect(node1b.multihash).to.eql(node.multihash)
922-
testNodeWithLinkMultihash = node.multihash
923-
testLink = node1b.links[0]
924-
cb()
925-
})
926-
.catch((err) => {
927-
expect(err).to.not.exist()
928-
})
929-
}
930-
], done)
922+
}).then((node) => {
923+
node1b = node
924+
return ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0])
925+
})
926+
}).then((node) => {
927+
expect(node1b.multihash).to.eql(node.multihash)
928+
testNodeWithLinkMultihash = node.multihash
929+
testLink = node1b.links[0]
930+
})
931931
})
932932

933-
it('.rmLink', (done) => {
934-
ipfs.object.patch.rmLink(testNodeWithLinkMultihash, testLink)
933+
it('.rmLink', () => {
934+
return ipfs.object.patch.rmLink(testNodeWithLinkMultihash, testLink)
935935
.then((node) => {
936936
expect(node.multihash).to.not.deep.equal(testNodeWithLinkMultihash)
937-
done()
938-
})
939-
.catch((err) => {
940-
expect(err).to.not.exist()
941937
})
942938
})
943939

944-
it('.appendData', (done) => {
945-
ipfs.object.patch.appendData(testNodeMultihash, new Buffer('append'))
940+
it('.appendData', () => {
941+
return ipfs.object.patch.appendData(testNodeMultihash, new Buffer('append'))
946942
.then((node) => {
947943
expect(node.multihash).to.not.deep.equal(testNodeMultihash)
948-
done()
949-
})
950-
.catch((err) => {
951-
expect(err).to.not.exist()
952944
})
953945
})
954946

955-
it('.setData', (done) => {
956-
ipfs.object.patch.appendData(testNodeMultihash, new Buffer('set'))
947+
it('.setData', () => {
948+
return ipfs.object.patch.appendData(testNodeMultihash, new Buffer('set'))
957949
.then((node) => {
958950
expect(node.multihash).to.not.deep.equal(testNodeMultihash)
959-
done()
960951
})
961-
.catch((err) => {
962-
expect(err).to.not.exist()
963-
})
964952
})
965953
})
966954
})

0 commit comments

Comments
 (0)