1
1
/*
2
- * Copyright © 2024 Hexastack. All rights reserved.
2
+ * Copyright © 2025 Hexastack. All rights reserved.
3
3
*
4
4
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
5
5
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
6
6
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
7
7
*/
8
8
9
- import fs from 'fs' ;
10
-
11
9
import { NotFoundException } from '@nestjs/common/exceptions' ;
12
10
import { EventEmitter2 } from '@nestjs/event-emitter' ;
13
11
import { MongooseModule } from '@nestjs/mongoose' ;
14
12
import { Test , TestingModule } from '@nestjs/testing' ;
15
13
16
- import { AttachmentRepository } from '@/attachment/repositories/attachment.repository' ;
17
- import {
18
- Attachment ,
19
- AttachmentModel ,
20
- } from '@/attachment/schemas/attachment.schema' ;
21
- import { AttachmentService } from '@/attachment/services/attachment.service' ;
22
14
import { LoggerService } from '@/logger/logger.service' ;
23
15
import { NOT_FOUND_ID } from '@/utils/constants/mock' ;
24
16
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto' ;
@@ -48,10 +40,8 @@ describe('ContentController', () => {
48
40
let contentController : ContentController ;
49
41
let contentService : ContentService ;
50
42
let contentTypeService : ContentTypeService ;
51
- let attachmentService : AttachmentService ;
52
43
let contentType : ContentType | null ;
53
44
let content : Content | null ;
54
- let attachment : Attachment | null ;
55
45
let updatedContent ;
56
46
let pageQuery : PageQueryDto < Content > ;
57
47
@@ -60,34 +50,24 @@ describe('ContentController', () => {
60
50
controllers : [ ContentController ] ,
61
51
imports : [
62
52
rootMongooseTestModule ( installContentFixtures ) ,
63
- MongooseModule . forFeature ( [
64
- ContentTypeModel ,
65
- ContentModel ,
66
- AttachmentModel ,
67
- ] ) ,
53
+ MongooseModule . forFeature ( [ ContentTypeModel , ContentModel ] ) ,
68
54
] ,
69
55
providers : [
70
56
LoggerService ,
71
57
ContentTypeService ,
72
58
ContentService ,
73
59
ContentRepository ,
74
- AttachmentService ,
75
60
ContentTypeRepository ,
76
- AttachmentRepository ,
77
61
EventEmitter2 ,
78
62
] ,
79
63
} ) . compile ( ) ;
80
64
contentController = module . get < ContentController > ( ContentController ) ;
81
65
contentService = module . get < ContentService > ( ContentService ) ;
82
- attachmentService = module . get < AttachmentService > ( AttachmentService ) ;
83
66
contentTypeService = module . get < ContentTypeService > ( ContentTypeService ) ;
84
67
contentType = await contentTypeService . findOne ( { name : 'Product' } ) ;
85
68
content = await contentService . findOne ( {
86
69
title : 'Jean' ,
87
70
} ) ;
88
- attachment = await attachmentService . findOne ( {
89
- name : 'store1.jpg' ,
90
- } ) ;
91
71
92
72
pageQuery = getPageQuery < Content > ( {
93
73
limit : 1 ,
@@ -243,91 +223,76 @@ describe('ContentController', () => {
243
223
} ) ;
244
224
245
225
describe ( 'import' , ( ) => {
246
- it ( 'should import content from a CSV file' , async ( ) => {
247
- const mockCsvData : string = `other,title,status, image
248
- should not appear,store 3,true,image.jpg` ;
249
-
250
- const mockCsvContentDto : ContentCreateDto = {
251
- entity : '0 ' ,
252
- title : 'store 3 ' ,
253
- status : true ,
254
- dynamicFields : {
255
- image : 'image.jpg ' ,
256
- } ,
257
- } ;
258
- jest . spyOn ( contentService , 'createMany' ) ;
259
- jest . spyOn ( fs , 'existsSync' ) . mockReturnValueOnce ( true ) ;
260
- jest . spyOn ( fs , 'readFileSync' ) . mockReturnValueOnce ( mockCsvData ) ;
226
+ const mockCsvData : string = `other,title,status,image
227
+ should not appear,store 3,true, image.jpg` ;
228
+
229
+ const file : Express . Multer . File = {
230
+ buffer : Buffer . from ( mockCsvData , 'utf-8' ) ,
231
+ originalname : 'test.csv ' ,
232
+ mimetype : 'text/csv ' ,
233
+ size : mockCsvData . length ,
234
+ fieldname : 'file' ,
235
+ encoding : '7bit ' ,
236
+ stream : null ,
237
+ destination : '' ,
238
+ filename : '' ,
239
+ path : '' ,
240
+ } as unknown as Express . Multer . File ;
261
241
262
- const contentType = await contentTypeService . findOne ( {
242
+ it ( 'should import content from a CSV file' , async ( ) => {
243
+ const mockContentType = {
244
+ id : '0' ,
263
245
name : 'Store' ,
264
- } ) ;
265
-
266
- const result = await contentController . import ( {
267
- idFileToImport : attachment ! . id ,
268
- idTargetContentType : contentType ! . id ,
269
- } ) ;
270
- expect ( contentService . createMany ) . toHaveBeenCalledWith ( [
271
- { ...mockCsvContentDto , entity : contentType ! . id } ,
246
+ } as unknown as ContentType ;
247
+ jest
248
+ . spyOn ( contentTypeService , 'findOne' )
249
+ . mockResolvedValueOnce ( mockContentType ) ;
250
+ jest . spyOn ( contentService , 'parseAndSaveDataset' ) . mockResolvedValueOnce ( [
251
+ {
252
+ entity : mockContentType . id ,
253
+ title : 'store 3' ,
254
+ status : true ,
255
+ dynamicFields : {
256
+ image : 'image.jpg' ,
257
+ } ,
258
+ id : '' ,
259
+ createdAt : null as unknown as Date ,
260
+ updatedAt : null as unknown as Date ,
261
+ } ,
272
262
] ) ;
273
263
274
- expect ( result ) . toEqualPayload (
275
- [
276
- {
277
- ...mockCsvContentDto ,
278
- entity : contentType ! . id ,
279
- } ,
280
- ] ,
281
- [ ...IGNORED_TEST_FIELDS , 'rag' ] ,
264
+ const result = await contentController . import ( file , mockContentType . id ) ;
265
+ expect ( contentService . parseAndSaveDataset ) . toHaveBeenCalledWith (
266
+ mockCsvData ,
267
+ mockContentType . id ,
268
+ mockContentType ,
282
269
) ;
270
+ expect ( result ) . toEqual ( [
271
+ {
272
+ entity : mockContentType . id ,
273
+ title : 'store 3' ,
274
+ status : true ,
275
+ dynamicFields : {
276
+ image : 'image.jpg' ,
277
+ } ,
278
+ id : '' ,
279
+ createdAt : null as unknown as Date ,
280
+ updatedAt : null as unknown as Date ,
281
+ } ,
282
+ ] ) ;
283
283
} ) ;
284
284
285
285
it ( 'should throw NotFoundException if content type is not found' , async ( ) => {
286
+ jest . spyOn ( contentTypeService , 'findOne' ) . mockResolvedValueOnce ( null ) ;
286
287
await expect (
287
- contentController . import ( {
288
- idFileToImport : attachment ! . id ,
289
- idTargetContentType : NOT_FOUND_ID ,
290
- } ) ,
288
+ contentController . import ( file , 'INVALID_ID' ) ,
291
289
) . rejects . toThrow ( new NotFoundException ( 'Content type is not found' ) ) ;
292
290
} ) ;
293
291
294
- it ( 'should throw NotFoundException if file is not found in attachment database' , async ( ) => {
295
- const contentType = await contentTypeService . findOne ( {
296
- name : 'Product' ,
297
- } ) ;
298
- jest . spyOn ( contentTypeService , 'findOne' ) ;
299
- await expect (
300
- contentController . import ( {
301
- idFileToImport : NOT_FOUND_ID ,
302
- idTargetContentType : contentType ! . id . toString ( ) ,
303
- } ) ,
304
- ) . rejects . toThrow ( new NotFoundException ( 'File does not exist' ) ) ;
305
- } ) ;
306
-
307
- it ( 'should throw NotFoundException if file does not exist in the given path ' , async ( ) => {
308
- jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( false ) ;
309
- await expect (
310
- contentController . import ( {
311
- idFileToImport : attachment ! . id ,
312
- idTargetContentType : contentType ! . id ,
313
- } ) ,
314
- ) . rejects . toThrow ( new NotFoundException ( 'File does not exist' ) ) ;
292
+ it ( 'should throw NotFoundException if idTargetContentType is missing' , async ( ) => {
293
+ await expect ( contentController . import ( file , '' ) ) . rejects . toThrow (
294
+ new NotFoundException ( 'Missing parameter' ) ,
295
+ ) ;
315
296
} ) ;
316
-
317
- it . each ( [
318
- [ 'file param and content type params are missing' , '' , '' ] ,
319
- [ 'content type param is missing' , '' , NOT_FOUND_ID ] ,
320
- [ 'file param is missing' , NOT_FOUND_ID , '' ] ,
321
- ] ) (
322
- 'should throw NotFoundException if %s' ,
323
- async ( _message , fileToImport , targetContentType ) => {
324
- await expect (
325
- contentController . import ( {
326
- idFileToImport : fileToImport ,
327
- idTargetContentType : targetContentType ,
328
- } ) ,
329
- ) . rejects . toThrow ( new NotFoundException ( 'Missing params' ) ) ;
330
- } ,
331
- ) ;
332
297
} ) ;
333
298
} ) ;
0 commit comments