7
7
mockIORedisClient ,
8
8
mockIORedisCluster , MockType ,
9
9
} from 'src/__mocks__' ;
10
- import { MemoryStoredFile } from 'nestjs-form-data' ;
11
10
import { BulkActionSummary } from 'src/modules/bulk-actions/models/bulk-action-summary' ;
12
11
import { IBulkActionOverview } from 'src/modules/bulk-actions/interfaces/bulk-action-overview.interface' ;
13
12
import { BulkActionStatus , BulkActionType } from 'src/modules/bulk-actions/constants' ;
@@ -17,6 +16,7 @@ import * as fs from 'fs-extra';
17
16
import config from 'src/utils/config' ;
18
17
import { join } from 'path' ;
19
18
import { wrapHttpError } from 'src/common/utils' ;
19
+ import { Readable } from 'stream' ;
20
20
21
21
const PATH_CONFIG = config . get ( 'dir_path' ) ;
22
22
@@ -71,13 +71,7 @@ const mockEmptyImportResult: IBulkActionOverview = {
71
71
duration : 0 ,
72
72
} ;
73
73
74
- const mockUploadImportFileDto = {
75
- file : {
76
- originalname : 'filename' ,
77
- size : 1 ,
78
- buffer : Buffer . from ( 'SET foo bar' ) ,
79
- } as unknown as MemoryStoredFile ,
80
- } ;
74
+ const mockReadableStream = Readable . from ( Buffer . from ( 'SET foo bar' ) ) ;
81
75
82
76
const mockUploadImportFileByPathDto = {
83
77
path : '/some/path' ,
@@ -152,7 +146,7 @@ describe('BulkImportService', () => {
152
146
153
147
it ( 'should import data' , async ( ) => {
154
148
spy . mockResolvedValue ( mockSummary ) ;
155
- expect ( await service . import ( mockClientMetadata , mockUploadImportFileDto ) ) . toEqual ( {
149
+ expect ( await service . import ( mockClientMetadata , mockReadableStream ) ) . toEqual ( {
156
150
...mockImportResult ,
157
151
duration : jasmine . anything ( ) ,
158
152
} ) ;
@@ -168,21 +162,17 @@ describe('BulkImportService', () => {
168
162
succeed : 10_000 ,
169
163
failed : 0 ,
170
164
} ) ) ;
171
- expect ( await service . import ( mockClientMetadata , {
172
- file : {
173
- ...mockUploadImportFileDto . file ,
174
- buffer : generateNCommandsBuffer ( 100_000 ) ,
175
- } as unknown as MemoryStoredFile ,
176
- } ) ) . toEqual ( {
177
- ...mockImportResult ,
178
- summary : {
179
- processed : 100_000 ,
180
- succeed : 100_000 ,
181
- failed : 0 ,
182
- errors : [ ] ,
183
- } ,
184
- duration : jasmine . anything ( ) ,
185
- } ) ;
165
+ expect ( await service . import ( mockClientMetadata , Readable . from ( generateNCommandsBuffer ( 100_000 ) ) ) )
166
+ . toEqual ( {
167
+ ...mockImportResult ,
168
+ summary : {
169
+ processed : 100_000 ,
170
+ succeed : 100_000 ,
171
+ failed : 0 ,
172
+ errors : [ ] ,
173
+ } ,
174
+ duration : jasmine . anything ( ) ,
175
+ } ) ;
186
176
} ) ;
187
177
188
178
it ( 'should import data (10K) from file in batches 10K each' , async ( ) => {
@@ -191,21 +181,17 @@ describe('BulkImportService', () => {
191
181
succeed : 10_000 ,
192
182
failed : 0 ,
193
183
} ) ) ;
194
- expect ( await service . import ( mockClientMetadata , {
195
- file : {
196
- ...mockUploadImportFileDto . file ,
197
- buffer : generateNCommandsBuffer ( 10_000 ) ,
198
- } as unknown as MemoryStoredFile ,
199
- } ) ) . toEqual ( {
200
- ...mockImportResult ,
201
- summary : {
202
- processed : 10_000 ,
203
- succeed : 10_000 ,
204
- failed : 0 ,
205
- errors : [ ] ,
206
- } ,
207
- duration : jasmine . anything ( ) ,
208
- } ) ;
184
+ expect ( await service . import ( mockClientMetadata , Readable . from ( generateNCommandsBuffer ( 10_000 ) ) ) )
185
+ . toEqual ( {
186
+ ...mockImportResult ,
187
+ summary : {
188
+ processed : 10_000 ,
189
+ succeed : 10_000 ,
190
+ failed : 0 ,
191
+ errors : [ ] ,
192
+ } ,
193
+ duration : jasmine . anything ( ) ,
194
+ } ) ;
209
195
} ) ;
210
196
211
197
it ( 'should not import any data due to parse error' , async ( ) => {
@@ -214,12 +200,10 @@ describe('BulkImportService', () => {
214
200
succeed : 0 ,
215
201
failed : 0 ,
216
202
} ) ) ;
217
- expect ( await service . import ( mockClientMetadata , {
218
- file : {
219
- ...mockUploadImportFileDto . file ,
220
- buffer : Buffer . from ( '{"incorrectdata"}\n{"incorrectdata"}' ) ,
221
- } as unknown as MemoryStoredFile ,
222
- } ) ) . toEqual ( {
203
+ expect ( await service . import (
204
+ mockClientMetadata ,
205
+ Readable . from ( Buffer . from ( '{"incorrectdata"}\n{"incorrectdata"}' ) ) ,
206
+ ) ) . toEqual ( {
223
207
...mockImportResult ,
224
208
summary : {
225
209
processed : 2 ,
@@ -233,21 +217,19 @@ describe('BulkImportService', () => {
233
217
} ) ;
234
218
235
219
it ( 'should ignore blank lines' , async ( ) => {
236
- await service . import ( mockClientMetadata , {
237
- file : {
238
- ...mockUploadImportFileDto . file ,
239
- buffer : Buffer . from ( '\n SET foo bar \n \n SET foo bar \n ' ) ,
240
- } as unknown as MemoryStoredFile ,
241
- } )
242
- expect ( spy ) . toBeCalledWith ( mockIORedisClient , [ [ 'set' , [ 'foo' , 'bar' ] ] , [ 'set' , [ 'foo' , 'bar' ] ] ] )
220
+ await service . import (
221
+ mockClientMetadata ,
222
+ Readable . from ( Buffer . from ( '\n SET foo bar \n \n SET foo bar \n ' ) ) ,
223
+ ) ;
224
+ expect ( spy ) . toBeCalledWith ( mockIORedisClient , [ [ 'set' , [ 'foo' , 'bar' ] ] , [ 'set' , [ 'foo' , 'bar' ] ] ] ) ;
243
225
expect ( mockIORedisClient . disconnect ) . toHaveBeenCalled ( ) ;
244
226
} ) ;
245
227
246
228
it ( 'should throw an error in case of global error' , async ( ) => {
247
229
try {
248
230
databaseConnectionService . createClient . mockRejectedValueOnce ( new NotFoundException ( ) ) ;
249
231
250
- await service . import ( mockClientMetadata , mockUploadImportFileDto ) ;
232
+ await service . import ( mockClientMetadata , mockReadableStream ) ;
251
233
252
234
fail ( ) ;
253
235
} catch ( e ) {
@@ -275,15 +257,15 @@ describe('BulkImportService', () => {
275
257
276
258
await service . uploadFromTutorial ( mockClientMetadata , mockUploadImportFileByPathDto ) ;
277
259
278
- expect ( mockedFs . readFile ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , mockUploadImportFileByPathDto . path ) ) ;
260
+ expect ( mockedFs . createReadStream ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , mockUploadImportFileByPathDto . path ) ) ;
279
261
} ) ;
280
262
281
263
it ( 'should import file by path with static' , async ( ) => {
282
264
mockedFs . pathExists . mockImplementationOnce ( async ( ) => true ) ;
283
265
284
266
await service . uploadFromTutorial ( mockClientMetadata , { path : '/static/guides/_data.file' } ) ;
285
267
286
- expect ( mockedFs . readFile ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , '/guides/_data.file' ) ) ;
268
+ expect ( mockedFs . createReadStream ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , '/guides/_data.file' ) ) ;
287
269
} ) ;
288
270
289
271
it ( 'should normalize path before importing and not search for file outside home folder' , async ( ) => {
@@ -293,7 +275,7 @@ describe('BulkImportService', () => {
293
275
path : '/../../../danger' ,
294
276
} ) ;
295
277
296
- expect ( mockedFs . readFile ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , 'danger' ) ) ;
278
+ expect ( mockedFs . createReadStream ) . toHaveBeenCalledWith ( join ( PATH_CONFIG . homedir , 'danger' ) ) ;
297
279
} ) ;
298
280
299
281
it ( 'should normalize path before importing and throw an error when search for file outside home folder (relative)' , async ( ) => {
@@ -324,19 +306,5 @@ describe('BulkImportService', () => {
324
306
expect ( e . message ) . toEqual ( 'Data file was not found' ) ;
325
307
}
326
308
} ) ;
327
-
328
- it ( 'should throw BadRequest when file size is greater then 100MB' , async ( ) => {
329
- mockedFs . pathExists . mockImplementationOnce ( async ( ) => true ) ;
330
- mockedFs . stat . mockImplementationOnce ( async ( ) => ( { size : 100 * 1024 * 1024 + 1 } as fs . Stats ) ) ;
331
-
332
- try {
333
- await service . uploadFromTutorial ( mockClientMetadata , mockUploadImportFileByPathDto ) ;
334
-
335
- fail ( ) ;
336
- } catch ( e ) {
337
- expect ( e ) . toBeInstanceOf ( BadRequestException ) ;
338
- expect ( e . message ) . toEqual ( 'Maximum file size is 100MB' ) ;
339
- }
340
- } ) ;
341
309
} ) ;
342
310
} ) ;
0 commit comments