@@ -17,78 +17,80 @@ const BlockService = require('../src')
17
17
module . exports = ( repo ) => {
18
18
describe ( 'block-service' , ( ) => {
19
19
let bs
20
+ let testBlocks
20
21
21
- before ( ( ) => {
22
+ before ( ( done ) => {
22
23
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
+ } )
23
42
} )
24
43
25
- describe ( 'offline ' , ( ) => {
44
+ describe ( 'fetch only from local Repo ' , ( ) => {
26
45
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 ]
31
47
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 )
41
56
} )
42
57
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 ]
45
60
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 ( )
53
65
} )
54
66
} )
55
67
56
68
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 ) => {
64
74
expect ( err ) . to . not . exist ( )
65
- bs . putMany ( blocks , done )
75
+ expect ( blocks ) . to . eql ( testBlocks )
76
+ done ( )
66
77
} )
67
78
} )
68
79
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
+ } )
88
89
} )
89
90
90
91
it ( 'delete a block' , ( done ) => {
91
92
const data = Buffer . from ( 'Will not live that much' )
93
+
92
94
multihashing ( data , 'sha2-256' , ( err , hash ) => {
93
95
expect ( err ) . to . not . exist ( )
94
96
const b = new Block ( data , new CID ( hash ) )
@@ -140,7 +142,7 @@ module.exports = (repo) => {
140
142
} )
141
143
} )
142
144
143
- describe ( 'has exchange' , ( ) => {
145
+ describe ( 'fetch through Bitswap ( has exchange) ' , ( ) => {
144
146
beforeEach ( ( ) => {
145
147
bs = new BlockService ( repo )
146
148
} )
0 commit comments