@@ -17,101 +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' ) ]
69
+ bs . putMany ( testBlocks , done )
70
+ } )
58
71
59
- map ( data , ( d , cb ) => {
60
- multihashing ( d , 'sha2-256' , ( err , hash ) => {
61
- expect ( err ) . to . not . exist ( )
62
- cb ( null , new Block ( d , new CID ( hash ) ) )
63
- } )
64
- } , ( err , blocks ) => {
72
+ it ( 'get many blocks through .get' , ( done ) => {
73
+ map ( testBlocks , ( b , cb ) => bs . get ( b . cid , cb ) , ( err , blocks ) => {
65
74
expect ( err ) . to . not . exist ( )
66
- bs . putMany ( blocks , done )
75
+ expect ( blocks ) . to . eql ( testBlocks )
76
+ done ( )
67
77
} )
68
78
} )
69
79
70
- it ( 'get many blocks through get' , ( done ) => {
71
- const data = [ Buffer . from ( '1' ) , Buffer . from ( '2' ) , Buffer . from ( '3' ) ]
72
-
73
- waterfall ( [
74
- ( cb ) => map ( data , ( d , cb ) => {
75
- multihashing ( d , 'sha2-256' , ( err , hash ) => {
76
- expect ( err ) . to . not . exist ( )
77
- cb ( null , new Block ( d , new CID ( hash ) ) )
78
- } )
79
- } , cb ) ,
80
- ( blocks , cb ) => map (
81
- blocks ,
82
- ( b , cb ) => bs . get ( b . cid , cb ) ,
83
- ( err , res ) => {
84
- expect ( err ) . to . not . exist ( )
85
- expect ( res ) . to . be . eql ( blocks )
86
- cb ( )
87
- }
88
- )
89
- ] , done )
90
- } )
91
-
92
- it ( 'get many blocks through getMany' , ( done ) => {
93
- const data = [ Buffer . from ( '1' ) , Buffer . from ( '2' ) , Buffer . from ( '3' ) ]
94
-
95
- waterfall ( [
96
- ( cb ) => map ( data , ( d , cb ) => {
97
- multihashing ( d , 'sha2-256' , ( err , hash ) => {
98
- expect ( err ) . to . not . exist ( )
99
- cb ( null , new Block ( d , new CID ( hash ) ) )
100
- } )
101
- } , cb ) ,
102
- ( blocks , cb ) => map ( blocks , ( b , cb ) => cb ( null , b . cid ) , ( err , cids ) => {
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 ) => {
103
84
expect ( err ) . to . not . exist ( )
104
- bs . getMany ( cids , ( err , _blocks ) => {
105
- expect ( err ) . to . not . exist ( )
106
- expect ( blocks ) . to . eql ( blocks )
107
- cb ( )
108
- } )
85
+ expect ( testBlocks ) . to . eql ( testBlocks )
86
+ done ( )
109
87
} )
110
- ] , done )
88
+ } )
111
89
} )
112
90
113
91
it ( 'delete a block' , ( done ) => {
114
92
const data = Buffer . from ( 'Will not live that much' )
93
+
115
94
multihashing ( data , 'sha2-256' , ( err , hash ) => {
116
95
expect ( err ) . to . not . exist ( )
117
96
const b = new Block ( data , new CID ( hash ) )
@@ -163,7 +142,7 @@ module.exports = (repo) => {
163
142
} )
164
143
} )
165
144
166
- describe ( 'has exchange' , ( ) => {
145
+ describe ( 'fetch through Bitswap ( has exchange) ' , ( ) => {
167
146
beforeEach ( ( ) => {
168
147
bs = new BlockService ( repo )
169
148
} )
0 commit comments