@@ -17,78 +17,80 @@ const BlockService = require('../src')
1717module . exports = ( repo ) => {
1818 describe ( 'block-service' , ( ) => {
1919 let bs
20+ let testBlocks
2021
21- before ( ( ) => {
22+ before ( ( done ) => {
2223 bs = new BlockService ( repo )
24+
25+ const data = [
26+ Buffer . from ( '1' ) ,
27+ Buffer . from ( '2' ) ,
28+ Buffer . from ( '3' ) ,
29+ Buffer . from ( 'A random data block' )
30+ ]
31+
32+ map ( data , ( d , cb ) => {
33+ multihashing ( d , 'sha2-256' , ( err , hash ) => {
34+ expect ( err ) . to . not . exist ( )
35+ cb ( null , new Block ( d , new CID ( hash ) ) )
36+ } )
37+ } , ( err , blocks ) => {
38+ expect ( err ) . to . not . exist ( )
39+ testBlocks = blocks
40+ done ( )
41+ } )
2342 } )
2443
25- describe ( 'offline ' , ( ) => {
44+ describe ( 'fetch only from local Repo ' , ( ) => {
2645 it ( 'store and get a block' , ( done ) => {
27- const data = Buffer . from ( 'A random data block' )
28- multihashing ( data , 'sha2-256' , ( err , hash ) => {
29- expect ( err ) . to . not . exist ( )
30- const b = new Block ( data , new CID ( hash ) )
46+ const b = testBlocks [ 3 ]
3147
32- waterfall ( [
33- ( cb ) => bs . put ( b , cb ) ,
34- ( cb ) => bs . get ( b . cid , cb ) ,
35- ( res , cb ) => {
36- expect ( res ) . to . be . eql ( b )
37- cb ( )
38- }
39- ] , done )
40- } )
48+ waterfall ( [
49+ ( cb ) => bs . put ( b , cb ) ,
50+ ( cb ) => bs . get ( b . cid , cb ) ,
51+ ( res , cb ) => {
52+ expect ( res ) . to . eql ( b )
53+ cb ( )
54+ }
55+ ] , done )
4156 } )
4257
43- it ( 'get a non existent block' , ( done ) => {
44- const data = Buffer . from ( 'Not stored' )
58+ it ( 'get a non stored yet block' , ( done ) => {
59+ const b = testBlocks [ 2 ]
4560
46- multihashing ( data , 'sha2-256' , ( err , hash ) => {
47- expect ( err ) . to . not . exist ( )
48- bs . get ( new CID ( hash ) , ( err , block ) => {
49- expect ( err ) . to . exist ( )
50- expect ( block ) . to . not . exist ( )
51- done ( )
52- } )
61+ bs . get ( b . cid , ( err , block ) => {
62+ expect ( err ) . to . exist ( )
63+ expect ( block ) . to . not . exist ( )
64+ done ( )
5365 } )
5466 } )
5567
5668 it ( 'store many blocks' , ( done ) => {
57- const data = [ Buffer . from ( '1' ) , Buffer . from ( '2' ) , Buffer . from ( '3' ) ]
58- map ( data , ( d , cb ) => {
59- multihashing ( d , 'sha2-256' , ( err , hash ) => {
60- expect ( err ) . to . not . exist ( )
61- cb ( null , new Block ( d , new CID ( hash ) ) )
62- } )
63- } , ( err , blocks ) => {
69+ bs . putMany ( testBlocks , done )
70+ } )
71+
72+ it ( 'get many blocks through .get' , ( done ) => {
73+ map ( testBlocks , ( b , cb ) => bs . get ( b . cid , cb ) , ( err , blocks ) => {
6474 expect ( err ) . to . not . exist ( )
65- bs . putMany ( blocks , done )
75+ expect ( blocks ) . to . eql ( testBlocks )
76+ done ( )
6677 } )
6778 } )
6879
69- it ( 'get many blocks' , ( done ) => {
70- const data = [ Buffer . from ( '1' ) , Buffer . from ( '2' ) , Buffer . from ( '3' ) ]
71- waterfall ( [
72- ( cb ) => map ( data , ( d , cb ) => {
73- multihashing ( d , 'sha2-256' , ( err , hash ) => {
74- expect ( err ) . to . not . exist ( )
75- cb ( null , new Block ( d , new CID ( hash ) ) )
76- } )
77- } , cb ) ,
78- ( blocks , cb ) => map (
79- blocks ,
80- ( b , cb ) => bs . get ( b . cid , cb ) ,
81- ( err , res ) => {
82- expect ( err ) . to . not . exist ( )
83- expect ( res ) . to . be . eql ( blocks )
84- cb ( )
85- }
86- )
87- ] , done )
80+ it ( 'get many blocks through .getMany' , ( done ) => {
81+ map ( testBlocks , ( b , cb ) => cb ( null , b . cid ) , ( err , cids ) => {
82+ expect ( err ) . to . not . exist ( )
83+ bs . getMany ( cids , ( err , _blocks ) => {
84+ expect ( err ) . to . not . exist ( )
85+ expect ( testBlocks ) . to . eql ( _blocks )
86+ done ( )
87+ } )
88+ } )
8889 } )
8990
9091 it ( 'delete a block' , ( done ) => {
9192 const data = Buffer . from ( 'Will not live that much' )
93+
9294 multihashing ( data , 'sha2-256' , ( err , hash ) => {
9395 expect ( err ) . to . not . exist ( )
9496 const b = new Block ( data , new CID ( hash ) )
@@ -140,7 +142,7 @@ module.exports = (repo) => {
140142 } )
141143 } )
142144
143- describe ( 'has exchange' , ( ) => {
145+ describe ( 'fetch through Bitswap ( has exchange) ' , ( ) => {
144146 beforeEach ( ( ) => {
145147 bs = new BlockService ( repo )
146148 } )
0 commit comments