@@ -71,11 +71,11 @@ export class FilesManagerUnitTest {
71
71
unit . function ( this . _filesManager . exists ) ;
72
72
unit . stub ( this . _bucketManager , 'fileStat' ) . returns ( Observable . of ( { } ) ) ;
73
73
unit . stub ( this . _filesManager , 'findByFilename' ) . returns ( Observable . of ( { filename } ) ) ;
74
- const obs = this . _filesManager . exists ( filename ) ;
75
- obs . subscribe ( _ => {
76
- unit . bool ( _ ) . isTrue ( ) ;
77
- done ( ) ;
78
- } , err => done ( err ) ) ;
74
+ this . _filesManager . exists ( filename )
75
+ . subscribe ( _ => {
76
+ unit . bool ( _ ) . isTrue ( ) ;
77
+ done ( ) ;
78
+ } , err => done ( err ) ) ;
79
79
}
80
80
81
81
@test ( '- exists: no' )
@@ -84,34 +84,55 @@ export class FilesManagerUnitTest {
84
84
unit . function ( this . _filesManager . exists ) ;
85
85
unit . stub ( this . _bucketManager , 'fileStat' ) . returns ( Observable . of ( { } ) ) ;
86
86
unit . stub ( this . _filesManager , 'findByFilename' ) . returns ( Observable . of ( null ) ) ;
87
- const obs = this . _filesManager . exists ( filename ) ;
88
- obs . subscribe ( _ => {
89
- unit . bool ( _ ) . isFalse ( ) ;
90
- done ( ) ;
91
- } , err => done ( err ) ) ;
87
+ this . _filesManager . exists ( filename )
88
+ . subscribe ( _ => {
89
+ unit . bool ( _ ) . isFalse ( ) ;
90
+ done ( ) ;
91
+ } , err => done ( err ) ) ;
92
92
}
93
93
94
94
@test ( '- exists: no 2' )
95
95
existsNo2 ( done ) {
96
96
const filename = 'helloworld.txt' ;
97
97
unit . function ( this . _filesManager . exists ) ;
98
98
unit . stub ( this . _bucketManager , 'fileStat' ) . returns ( Observable . throw ( { code : 'NotFound' } ) ) ;
99
- const obs = this . _filesManager . exists ( filename ) ;
100
- obs . subscribe ( _ => {
101
- unit . bool ( _ ) . isFalse ( ) ;
102
- done ( ) ;
103
- } , err => done ( err ) ) ;
99
+ this . _filesManager . exists ( filename )
100
+ . subscribe ( _ => {
101
+ unit . bool ( _ ) . isFalse ( ) ;
102
+ done ( ) ;
103
+ } , err => done ( err ) ) ;
104
104
}
105
105
106
106
@test ( '- exists: filestat throw another error than "NotFound"' )
107
107
existsNo3 ( done ) {
108
108
const filename = 'helloworld.txt' ;
109
109
unit . function ( this . _filesManager . exists ) ;
110
110
unit . stub ( this . _bucketManager , 'fileStat' ) . returns ( Observable . throw ( { code : 'AnotherError' } ) ) ;
111
- const obs = this . _filesManager . exists ( filename ) ;
112
- obs . subscribe ( _ => {
113
- done ( new Error ( 'Should not be here!' ) ) ;
114
- } , err => done ( ) ) ;
111
+ this . _filesManager . exists ( filename )
112
+ . subscribe ( _ => {
113
+ done ( new Error ( 'Should not be here!' ) ) ;
114
+ } , err => done ( ) ) ;
115
+ }
116
+
117
+ @test ( '- when upload returns null' )
118
+ uploadReturnsNull ( done ) {
119
+ const input = Buffer . from ( 'helloworld' ) ;
120
+ const filename = 'helloworld.txt' ;
121
+ const contentType = 'text/plain' ;
122
+ const metadata = { lupulus : { stock : 99 , empty : 10 } } ;
123
+ unit . stub ( this . _bucketManager , 'createFile' ) . returns ( Observable . of ( {
124
+ size : input . length ,
125
+ contentType : contentType ,
126
+ etag : new Date ( ) . getTime ( ) ,
127
+ lastModified : new Date ( ) ,
128
+ } ) ) ;
129
+ this . _modelMock . findOneAndUpdate = unit . stub ( ) . returns ( Promise . resolve ( null ) ) ;
130
+ unit . function ( this . _filesManager . upload ) ;
131
+ this . _filesManager . upload ( input , filename , contentType , metadata )
132
+ . subscribe ( _ => {
133
+ unit . value ( _ ) . is ( null ) ;
134
+ done ( ) ;
135
+ } , err => done ( err ) ) ;
115
136
}
116
137
117
138
@test ( '- Upload' )
@@ -139,11 +160,11 @@ export class FilesManagerUnitTest {
139
160
toJSON : ( ) => file
140
161
} ) ) ;
141
162
unit . function ( this . _filesManager . upload ) ;
142
- const obs = this . _filesManager . upload ( input , filename , contentType , metadata ) ;
143
- obs . subscribe ( _ => {
144
- unit . object ( _ ) . is ( file ) ;
145
- done ( ) ;
146
- } , err => done ( err ) ) ;
163
+ this . _filesManager . upload ( input , filename , contentType , metadata )
164
+ . subscribe ( _ => {
165
+ unit . object ( _ ) . is ( file ) ;
166
+ done ( ) ;
167
+ } , err => done ( err ) ) ;
147
168
}
148
169
149
170
@test ( '- Create' )
@@ -162,11 +183,11 @@ export class FilesManagerUnitTest {
162
183
} ;
163
184
unit . stub ( this . _filesManager , 'exists' ) . returns ( Observable . of ( false ) ) ;
164
185
unit . stub ( this . _filesManager , 'upload' ) . returns ( Observable . of ( file ) ) ;
165
- const obs = this . _filesManager . create ( input , filename ) ;
166
- obs . subscribe ( _ => {
167
- unit . object ( _ ) . is ( file ) ;
168
- done ( ) ;
169
- } , err => done ( err ) ) ;
186
+ this . _filesManager . create ( input , filename )
187
+ . subscribe ( _ => {
188
+ unit . object ( _ ) . is ( file ) ;
189
+ done ( ) ;
190
+ } , err => done ( err ) ) ;
170
191
}
171
192
172
193
@test ( '- Create error: already exists' )
@@ -175,14 +196,37 @@ export class FilesManagerUnitTest {
175
196
const input = Buffer . from ( 'helloworld' ) ;
176
197
const filename = 'helloworld.txt' ;
177
198
unit . stub ( this . _filesManager , 'exists' ) . returns ( Observable . of ( true ) ) ;
178
- const obs = this . _filesManager . create ( input , filename ) ;
179
- obs . subscribe ( _ => done ( new Error ( 'Cannot be here' ) ) ,
180
- err => {
181
- unit . object ( err )
182
- . isInstanceOf ( Error )
183
- . hasProperty ( 'message' , 'File helloworld.txt already exists' ) ;
199
+ this . _filesManager . create ( input , filename )
200
+ . subscribe ( _ => done ( new Error ( 'Cannot be here' ) ) ,
201
+ err => {
202
+ unit . object ( err )
203
+ . isInstanceOf ( Error )
204
+ . hasProperty ( 'message' , 'File helloworld.txt already exists' ) ;
205
+ done ( ) ;
206
+ } ) ;
207
+ }
208
+
209
+ @test ( '- find returns null' )
210
+ findReturnsNull ( done ) {
211
+ const input = Buffer . from ( 'helloworld' ) ;
212
+ const filename = 'helloworld.txt' ;
213
+ const file = Object . assign ( Object . create ( this . _classMock ) , {
214
+ filename,
215
+ contentType : 'application/octet-stream' ,
216
+ metadata : { } ,
217
+ size : input . length ,
218
+ created_at : new Date ( ) ,
219
+ updated_at : new Date ( ) ,
220
+ md5 : new Date ( ) . getTime ( )
221
+ } ) ;
222
+
223
+ this . _modelMock . find . returns ( Promise . resolve ( [ null , null , file , null ] ) ) ;
224
+ unit . function ( this . _filesManager . find ) ;
225
+ this . _filesManager . find ( )
226
+ . subscribe ( _ => {
227
+ unit . array ( _ ) . is ( [ null , null , file , null ] ) ;
184
228
done ( ) ;
185
- } ) ;
229
+ } , err => done ( err ) ) ;
186
230
}
187
231
188
232
@test ( '- find' )
@@ -221,9 +265,21 @@ export class FilesManagerUnitTest {
221
265
} ) ;
222
266
this . _modelMock . find . returns ( Promise . resolve ( [ file ] ) ) ;
223
267
unit . function ( this . _filesManager . find ) ;
224
- const obs = this . _filesManager . find ( null , [ 'filename' , 'contentType' , 'size' , 'created_at' , 'updated_at' ] ) ;
225
- obs . subscribe ( _ => {
226
- unit . array ( _ ) . is ( [ file ] ) ;
268
+ this . _filesManager . find ( null , [ 'filename' , 'contentType' , 'size' , 'created_at' , 'updated_at' ] )
269
+ . subscribe ( _ => {
270
+ unit . array ( _ ) . is ( [ file ] ) ;
271
+ done ( ) ;
272
+ } , err => done ( err ) ) ;
273
+ }
274
+
275
+ @test ( '- findByFilename returns null' )
276
+ findByFilenameReturnsNull ( done ) {
277
+ const filename = 'helloworld.txt' ;
278
+ this . _modelMock . findOne . returns ( Promise . resolve ( null ) ) ;
279
+ unit . function ( this . _filesManager . findByFilename ) ;
280
+ this . _filesManager . findByFilename ( filename )
281
+ . subscribe ( _ => {
282
+ unit . value ( _ ) . is ( null ) ;
227
283
done ( ) ;
228
284
} , err => done ( err ) ) ;
229
285
}
@@ -243,11 +299,11 @@ export class FilesManagerUnitTest {
243
299
} ) ;
244
300
this . _modelMock . findOne . returns ( Promise . resolve ( file ) ) ;
245
301
unit . function ( this . _filesManager . findByFilename ) ;
246
- const obs = this . _filesManager . findByFilename ( filename ) ;
247
- obs . subscribe ( _ => {
248
- unit . object ( _ ) . is ( file ) ;
249
- done ( ) ;
250
- } , err => done ( err ) ) ;
302
+ this . _filesManager . findByFilename ( filename )
303
+ . subscribe ( _ => {
304
+ unit . object ( _ ) . is ( file ) ;
305
+ done ( ) ;
306
+ } , err => done ( err ) ) ;
251
307
}
252
308
253
309
@test ( '- findByFilename: with projection as an array' )
@@ -261,11 +317,11 @@ export class FilesManagerUnitTest {
261
317
} ) ;
262
318
this . _modelMock . findOne . returns ( Promise . resolve ( file ) ) ;
263
319
unit . function ( this . _filesManager . findByFilename ) ;
264
- const obs = this . _filesManager . findByFilename ( filename , [ 'filename' , 'contentType' , 'size' ] ) ;
265
- obs . subscribe ( _ => {
266
- unit . object ( _ ) . is ( file ) ;
267
- done ( ) ;
268
- } , err => done ( err ) ) ;
320
+ this . _filesManager . findByFilename ( filename , [ 'filename' , 'contentType' , 'size' ] )
321
+ . subscribe ( _ => {
322
+ unit . object ( _ ) . is ( file ) ;
323
+ done ( ) ;
324
+ } , err => done ( err ) ) ;
269
325
}
270
326
271
327
@test ( '- download' )
@@ -278,11 +334,11 @@ export class FilesManagerUnitTest {
278
334
} ) ;
279
335
const filename = 'helloworld.txt' ;
280
336
unit . stub ( this . _filesManager , 'findByFilename' ) . returns ( Observable . of ( { filename } ) ) ;
281
- const obs = this . _filesManager . download ( filename ) ;
282
- obs . subscribe ( _ => {
283
- unit . object ( _ ) . isInstanceOf ( Readable ) ;
284
- done ( ) ;
285
- } , err => done ( err ) ) ;
337
+ this . _filesManager . download ( filename )
338
+ . subscribe ( _ => {
339
+ unit . object ( _ ) . isInstanceOf ( Readable ) ;
340
+ done ( ) ;
341
+ } , err => done ( err ) ) ;
286
342
}
287
343
288
344
@test ( '- _prepareUpdateObject' )
@@ -330,12 +386,28 @@ export class FilesManagerUnitTest {
330
386
} , err => done ( err ) ) ;
331
387
}
332
388
389
+ @test ( '- updateByFilename returns null' )
390
+ updateByFilenameReturnsNull ( done ) {
391
+ const filename = 'helloworld.txt' ;
392
+
393
+ this . _modelMock . findOneAndUpdate . returns ( Promise . resolve ( null ) ) ;
394
+
395
+ unit . stub ( this . _filesManager , 'exists' ) . returns ( Observable . of ( true ) ) ;
396
+
397
+ this . _filesManager . updateByFilename ( filename , { meta1 : 'meta1' } )
398
+ . subscribe ( _ => {
399
+ unit . value ( _ ) . is ( null ) ;
400
+ unit . assert ( this . _modelMock . findOneAndUpdate . calledOnce ) ;
401
+ done ( ) ;
402
+ } , err => done ( err ) ) ;
403
+ }
404
+
333
405
@test ( '- updateByFilename: given file to update does not exist' )
334
406
updateByFilenameWithNonexistantFile ( done ) {
335
407
const filename = 'helloworld.txt' ;
336
408
unit . stub ( this . _filesManager , 'exists' ) . returns ( Observable . of ( false ) ) ;
337
- const obs = this . _filesManager . updateByFilename ( filename , { } ) ;
338
- obs . subscribe ( _ => done ( new Error ( 'Should fail' ) ) , err => done ( ) ) ;
409
+ this . _filesManager . updateByFilename ( filename , { } )
410
+ . subscribe ( _ => done ( new Error ( 'Should fail' ) ) , err => done ( ) ) ;
339
411
}
340
412
341
413
@test ( '- removeByFilename' )
0 commit comments