@@ -5,18 +5,40 @@ const expect = require('chai').expect
5
5
const Block = require ( 'ipfs-block' )
6
6
const mh = require ( 'multihashes' )
7
7
const pull = require ( 'pull-stream' )
8
- const parallel = require ( 'run-parallel' )
8
+ const parallel = require ( 'async/parallel' )
9
+ const waterfall = require ( 'async/waterfall' )
10
+ const map = require ( 'async/map' )
9
11
const _ = require ( 'lodash' )
10
12
11
13
module . exports = ( repo ) => {
12
14
describe ( 'blockstore' , ( ) => {
13
15
const helloKey = 'CIQLS/CIQLSTJHXGJU2PQIUUXFFV62PWV7VREE57RXUU4A52IIR55M4LX432I.data'
14
16
const helloIpldKey = 'CIQO2/CIQO2EUTF47PSTAHSL54KUTDS2AAN2DH4URM7H5KRATUGQFCM4OUIQI.ipld'
15
- const blockCollection = _ . range ( 100 ) . map ( ( i ) => new Block ( new Buffer ( `hello-${ i } -${ Math . random ( ) } ` ) ) )
17
+ let blockCollection
18
+ let b
19
+ let ipldBlock
16
20
17
21
describe ( '.putStream' , ( ) => {
22
+ before ( ( done ) => {
23
+ parallel ( [
24
+ ( cb ) => map ( _ . range ( 100 ) , ( i , cb ) => {
25
+ Block . create ( `hello-${ i } -${ Math . random ( ) } ` , cb )
26
+ } , cb ) ,
27
+ ( cb ) => Block . create ( 'hello world' , cb ) ,
28
+ ( cb ) => Block . create ( 'hello world 2' , 'ipld' , cb )
29
+ ] , ( err , results ) => {
30
+ if ( err ) {
31
+ return done ( err )
32
+ }
33
+
34
+ blockCollection = results [ 0 ]
35
+ b = results [ 1 ]
36
+ ipldBlock = results [ 2 ]
37
+ done ( )
38
+ } )
39
+ } )
40
+
18
41
it ( 'simple' , ( done ) => {
19
- const b = new Block ( 'hello world' )
20
42
pull (
21
43
pull . values ( [ b ] ) ,
22
44
repo . blockstore . putStream ( ) ,
@@ -29,8 +51,6 @@ module.exports = (repo) => {
29
51
} )
30
52
31
53
it ( 'multi write (locks)' , ( done ) => {
32
- const b = new Block ( 'hello world' )
33
-
34
54
let i = 0
35
55
const finish = ( err , meta ) => {
36
56
expect ( err ) . to . not . exist
@@ -68,9 +88,8 @@ module.exports = (repo) => {
68
88
} )
69
89
70
90
it ( 'custom extension' , function ( done ) {
71
- const b = new Block ( 'hello world 2' , 'ipld' )
72
91
pull (
73
- pull . values ( [ b ] ) ,
92
+ pull . values ( [ ipldBlock ] ) ,
74
93
repo . blockstore . putStream ( ) ,
75
94
pull . collect ( ( err , meta ) => {
76
95
expect ( err ) . to . not . exist
@@ -94,8 +113,6 @@ module.exports = (repo) => {
94
113
95
114
describe ( '.getStream' , ( ) => {
96
115
it ( 'simple' , ( done ) => {
97
- const b = new Block ( 'hello world' )
98
-
99
116
pull (
100
117
repo . blockstore . getStream ( b . key ) ,
101
118
pull . collect ( ( err , data ) => {
@@ -122,13 +139,11 @@ module.exports = (repo) => {
122
139
} )
123
140
124
141
it ( 'custom extension' , ( done ) => {
125
- const b = new Block ( 'hello world 2' , 'ipld' )
126
-
127
142
pull (
128
- repo . blockstore . getStream ( b . key , b . extension ) ,
143
+ repo . blockstore . getStream ( ipldBlock . key , ipldBlock . extension ) ,
129
144
pull . collect ( ( err , data ) => {
130
145
expect ( err ) . to . not . exist
131
- expect ( data [ 0 ] ) . to . be . eql ( b )
146
+ expect ( data [ 0 ] ) . to . be . eql ( ipldBlock )
132
147
133
148
done ( )
134
149
} )
@@ -148,8 +163,6 @@ module.exports = (repo) => {
148
163
149
164
describe ( '.has' , ( ) => {
150
165
it ( 'existing block' , ( done ) => {
151
- const b = new Block ( 'hello world' )
152
-
153
166
repo . blockstore . has ( b . key , ( err , exists ) => {
154
167
expect ( err ) . to . not . exist
155
168
expect ( exists ) . to . equal ( true )
@@ -158,8 +171,6 @@ module.exports = (repo) => {
158
171
} )
159
172
160
173
it ( 'with extension' , ( done ) => {
161
- const b = new Block ( 'hello world' )
162
-
163
174
repo . blockstore . has ( b . key , 'data' , ( err , exists ) => {
164
175
expect ( err ) . to . not . exist
165
176
expect ( exists ) . to . equal ( true )
@@ -168,9 +179,10 @@ module.exports = (repo) => {
168
179
} )
169
180
170
181
it ( 'non existent block' , ( done ) => {
171
- const b = new Block ( 'wooot' )
172
-
173
- repo . blockstore . has ( b . key , ( err , exists ) => {
182
+ waterfall ( [
183
+ ( cb ) => Block . create ( 'wooot' , cb ) ,
184
+ ( block , cb ) => repo . blockstore . has ( block . key , cb )
185
+ ] , ( err , exists ) => {
174
186
expect ( err ) . to . not . exist
175
187
expect ( exists ) . to . equal ( false )
176
188
done ( )
@@ -180,8 +192,6 @@ module.exports = (repo) => {
180
192
181
193
describe ( '.delete' , ( ) => {
182
194
it ( 'simple' , ( done ) => {
183
- const b = new Block ( 'hello world' )
184
-
185
195
repo . blockstore . delete ( b . key , ( err ) => {
186
196
expect ( err ) . to . not . exist
187
197
@@ -194,16 +204,17 @@ module.exports = (repo) => {
194
204
} )
195
205
196
206
it ( 'custom extension' , ( done ) => {
197
- const b = new Block ( 'hello world' , 'ipld' )
198
-
199
- repo . blockstore . delete ( b . key , b . extension , ( err ) => {
200
- expect ( err ) . to . not . exist
201
-
202
- repo . blockstore . has ( b . key , b . extension , ( err , exists ) => {
207
+ waterfall ( [
208
+ ( cb ) => Block . create ( 'hello world' , 'ipld' , cb ) ,
209
+ ( block , cb ) => repo . blockstore . delete ( block . key , block . extension , ( err ) => {
203
210
expect ( err ) . to . not . exist
204
- expect ( exists ) . to . equal ( false )
205
- done ( )
206
- } )
211
+ cb ( null , block )
212
+ } ) ,
213
+ ( block , cb ) => repo . blockstore . has ( block . key , block . extension , cb )
214
+ ] , ( err , exists ) => {
215
+ expect ( err ) . to . not . exist
216
+ expect ( exists ) . to . equal ( false )
217
+ done ( )
207
218
} )
208
219
} )
209
220
} )
0 commit comments