Skip to content

Commit 9642e82

Browse files
MohamedAliBouhaoualamarrouchi
MohamedAliBouhaouala
authored andcommitted
feat: use mongo test db for unit tests
1 parent c718975 commit 9642e82

File tree

8 files changed

+71
-152
lines changed

8 files changed

+71
-152
lines changed

api/src/chat/services/block.service.spec.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
installBlockFixtures,
4747
} from '@/utils/test/fixtures/block';
4848
import { installContentFixtures } from '@/utils/test/fixtures/content';
49+
import { installNlpValueFixtures } from '@/utils/test/fixtures/nlpvalue';
4950
import {
5051
blockEmpty,
5152
blockGetStarted,
@@ -64,7 +65,6 @@ import {
6465
subscriberContextBlankInstance,
6566
} from '@/utils/test/mocks/conversation';
6667
import {
67-
mockNlpCacheMap,
6868
mockNlpEntitiesSetOne,
6969
nlpEntitiesGreeting,
7070
} from '@/utils/test/mocks/nlp';
@@ -102,6 +102,7 @@ describe('BlockService', () => {
102102
rootMongooseTestModule(async () => {
103103
await installContentFixtures();
104104
await installBlockFixtures();
105+
await installNlpValueFixtures();
105106
}),
106107
MongooseModule.forFeature([
107108
BlockModel,
@@ -357,10 +358,7 @@ describe('BlockService', () => {
357358

358359
describe('matchBestNLP', () => {
359360
it('should return the block with the highest NLP score', async () => {
360-
jest
361-
.spyOn(nlpEntityService, 'getNlpMap')
362-
.mockResolvedValue(mockNlpCacheMap);
363-
const blocks = [mockNlpBlock, blockGetStarted]; // You can add more blocks with different patterns and scores
361+
const blocks = [mockNlpBlock, blockGetStarted];
364362
const nlp = mockNlpEntitiesSetOne;
365363
// Spy on calculateBlockScore to check if it's called
366364
const calculateBlockScoreSpy = jest.spyOn(
@@ -379,10 +377,7 @@ describe('BlockService', () => {
379377
});
380378

381379
it('should return the block with the highest NLP score applying penalties', async () => {
382-
jest
383-
.spyOn(nlpEntityService, 'getNlpMap')
384-
.mockResolvedValue(mockNlpCacheMap);
385-
const blocks = [mockNlpBlock, mockModifiedNlpBlock]; // You can add more blocks with different patterns and scores
380+
const blocks = [mockNlpBlock, mockModifiedNlpBlock];
386381
const nlp = mockNlpEntitiesSetOne;
387382
// Spy on calculateBlockScore to check if it's called
388383
const calculateBlockScoreSpy = jest.spyOn(
@@ -401,9 +396,6 @@ describe('BlockService', () => {
401396
});
402397

403398
it('another case where it should return the block with the highest NLP score applying penalties', async () => {
404-
jest
405-
.spyOn(nlpEntityService, 'getNlpMap')
406-
.mockResolvedValue(mockNlpCacheMap);
407399
const blocks = [mockModifiedNlpBlockOne, mockModifiedNlpBlockTwo]; // You can add more blocks with different patterns and scores
408400
const nlp = mockNlpEntitiesSetOne;
409401
// Spy on calculateBlockScore to check if it's called
@@ -423,10 +415,7 @@ describe('BlockService', () => {
423415
});
424416

425417
it('should return undefined if no blocks match or the list is empty', async () => {
426-
jest
427-
.spyOn(nlpEntityService, 'getNlpMap')
428-
.mockResolvedValue(mockNlpCacheMap);
429-
const blocks: BlockFull[] = []; // Empty block array
418+
const blocks: BlockFull[] = [];
430419
const nlp = mockNlpEntitiesSetOne;
431420

432421
const bestBlock = await blockService.matchBestNLP(blocks, nlp);
@@ -438,9 +427,7 @@ describe('BlockService', () => {
438427

439428
describe('calculateBlockScore', () => {
440429
it('should calculate the correct NLP score for a block', async () => {
441-
jest
442-
.spyOn(nlpEntityService, 'getNlpMap')
443-
.mockResolvedValue(mockNlpCacheMap);
430+
const getNlpCacheMapSpy = jest.spyOn(nlpEntityService, 'getNlpMap');
444431
const score = await blockService.calculateBlockScore(
445432
mockNlpPatternsSetOne,
446433
mockNlpEntitiesSetOne,
@@ -449,16 +436,15 @@ describe('BlockService', () => {
449436
mockNlpPatternsSetTwo,
450437
mockNlpEntitiesSetOne,
451438
);
452-
439+
expect(getNlpCacheMapSpy).toHaveBeenCalledTimes(2);
440+
getNlpCacheMapSpy.mockRestore();
453441
expect(score).toBeGreaterThan(0);
454442
expect(score2).toBe(0);
455443
expect(score).toBeGreaterThan(score2);
456444
});
457445

458446
it('should calculate the correct NLP score for a block and apply penalties ', async () => {
459-
jest
460-
.spyOn(nlpEntityService, 'getNlpMap')
461-
.mockResolvedValue(mockNlpCacheMap);
447+
const getNlpCacheMapSpy = jest.spyOn(nlpEntityService, 'getNlpMap');
462448
const score = await blockService.calculateBlockScore(
463449
mockNlpPatternsSetOne,
464450
mockNlpEntitiesSetOne,
@@ -468,19 +454,25 @@ describe('BlockService', () => {
468454
mockNlpEntitiesSetOne,
469455
);
470456

457+
expect(getNlpCacheMapSpy).toHaveBeenCalledTimes(2);
458+
getNlpCacheMapSpy.mockRestore();
459+
471460
expect(score).toBeGreaterThan(0);
472461
expect(score2).toBeGreaterThan(0);
473462
expect(score).toBeGreaterThan(score2);
474463
});
475464

476465
it('should return 0 if no matching entities are found', async () => {
477-
jest.spyOn(nlpEntityService, 'getNlpMap').mockResolvedValue(new Map());
466+
const getNlpCacheMapSpy = jest.spyOn(nlpEntityService, 'getNlpMap');
467+
478468
const score = await blockService.calculateBlockScore(
479469
mockNlpPatternsSetTwo,
480470
mockNlpEntitiesSetOne,
481471
);
472+
expect(getNlpCacheMapSpy).toHaveBeenCalledTimes(1);
473+
getNlpCacheMapSpy.mockRestore();
482474

483-
expect(score).toBe(0); // No matching entity, so score should be 0
475+
expect(score).toBe(0); // No matching entity
484476
});
485477
});
486478

api/src/nlp/controllers/nlp-entity.controller.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ describe('NlpEntityController', () => {
201201
describe('findOne', () => {
202202
it('should find a nlp entity', async () => {
203203
const firstNameEntity = await nlpEntityService.findOne({
204-
name: 'first_name',
204+
name: 'firstname',
205205
});
206206
const result = await nlpEntityController.findOne(firstNameEntity!.id, []);
207207

208208
expect(result).toEqualPayload(
209-
nlpEntityFixtures.find(({ name }) => name === 'first_name')!,
209+
nlpEntityFixtures.find(({ name }) => name === 'firstname')!,
210210
);
211211
});
212212

213213
it('should find a nlp entity, and populate its values', async () => {
214214
const firstNameEntity = await nlpEntityService.findOne({
215-
name: 'first_name',
215+
name: 'firstname',
216216
});
217217
const firstNameValues = await nlpValueService.findOne({ value: 'jhon' });
218218
const firstNameWithValues: NlpEntityFull = {
@@ -242,7 +242,7 @@ describe('NlpEntityController', () => {
242242
describe('updateOne', () => {
243243
it('should update a nlp entity', async () => {
244244
const firstNameEntity = await nlpEntityService.findOne({
245-
name: 'first_name',
245+
name: 'firstname',
246246
});
247247
const updatedNlpEntity: NlpEntityCreateDto = {
248248
name: 'updated',

api/src/nlp/repositories/nlp-entity.repository.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('NlpEntityRepository', () => {
5151
NlpValueRepository,
5252
]);
5353
firstNameNlpEntity = await nlpEntityRepository.findOne({
54-
name: 'first_name',
54+
name: 'firstname',
5555
});
5656
});
5757

api/src/nlp/services/nlp-entity.service.spec.ts

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ import { buildTestingMocks } from '@/utils/test/utils';
2222
import { NlpEntityRepository } from '../repositories/nlp-entity.repository';
2323
import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository';
2424
import { NlpValueRepository } from '../repositories/nlp-value.repository';
25-
import {
26-
NlpEntity,
27-
NlpEntityFull,
28-
NlpEntityModel,
29-
} from '../schemas/nlp-entity.schema';
25+
import { NlpEntity, NlpEntityModel } from '../schemas/nlp-entity.schema';
3026
import { NlpSampleEntityModel } from '../schemas/nlp-sample-entity.schema';
3127
import { NlpValueModel } from '../schemas/nlp-value.schema';
3228

@@ -91,7 +87,7 @@ describe('nlpEntityService', () => {
9187
describe('findOneAndPopulate', () => {
9288
it('should return a nlp entity with populate', async () => {
9389
const firstNameNlpEntity = await nlpEntityRepository.findOne({
94-
name: 'first_name',
90+
name: 'firstname',
9591
});
9692
const result = await nlpEntityService.findOneAndPopulate(
9793
firstNameNlpEntity!.id,
@@ -112,7 +108,7 @@ describe('nlpEntityService', () => {
112108
it('should return all nlp entities with populate', async () => {
113109
const pageQuery = getPageQuery<NlpEntity>({ sort: ['name', 'desc'] });
114110
const firstNameNlpEntity = await nlpEntityRepository.findOne({
115-
name: 'first_name',
111+
name: 'firstname',
116112
});
117113
const result = await nlpEntityService.findPageAndPopulate(
118114
{ _id: firstNameNlpEntity!.id },
@@ -221,45 +217,37 @@ describe('nlpEntityService', () => {
221217
});
222218
describe('getNlpMap', () => {
223219
it('should return a NlpCacheMap with the correct structure', async () => {
224-
// Arrange
225-
const firstMockValues = {
226-
id: '1',
227-
weight: 1,
228-
};
229-
const firstMockEntity = {
230-
name: 'intent',
231-
...firstMockValues,
232-
values: [{ value: 'buy' }, { value: 'sell' }],
233-
} as unknown as Partial<NlpEntityFull>;
234-
const secondMockValues = {
235-
id: '2',
236-
weight: 5,
237-
};
238-
const secondMockEntity = {
239-
name: 'subject',
240-
...secondMockValues,
241-
values: [{ value: 'product' }],
242-
} as unknown as Partial<NlpEntityFull>;
243-
const mockEntities = [firstMockEntity, secondMockEntity];
244-
245-
// Mock findAndPopulate
246-
jest
247-
.spyOn(nlpEntityService, 'findAllAndPopulate')
248-
.mockResolvedValue(mockEntities as unknown as NlpEntityFull[]);
249-
250220
// Act
251221
const result = await nlpEntityService.getNlpMap();
252222

253223
expect(result).toBeInstanceOf(Map);
254-
expect(result.size).toBe(2);
255-
expect(result.get('intent')).toEqual({
256-
name: 'intent',
257-
...firstMockEntity,
258-
});
259-
expect(result.get('subject')).toEqual({
260-
name: 'subject',
261-
...secondMockEntity,
262-
});
224+
expect(result.get('firstname')).toEqual(
225+
expect.objectContaining({
226+
name: 'firstname',
227+
lookups: ['keywords'],
228+
doc: '',
229+
builtin: false,
230+
weight: 1,
231+
values: [
232+
expect.objectContaining({
233+
value: 'jhon',
234+
expressions: ['john', 'joohn', 'jhonny'],
235+
builtin: true,
236+
doc: '',
237+
}),
238+
],
239+
}),
240+
);
241+
expect(result.get('subject')).toEqual(
242+
expect.objectContaining({
243+
name: 'subject',
244+
lookups: ['trait'],
245+
doc: '',
246+
builtin: false,
247+
weight: 1,
248+
values: [],
249+
}),
250+
);
263251
});
264252
});
265253
});

api/src/nlp/services/nlp-value.service.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ describe('NlpValueService', () => {
6363
provide: CACHE_MANAGER,
6464
useValue: {
6565
del: jest.fn(),
66+
set: jest.fn(),
67+
get: jest.fn(),
6668
},
6769
},
6870
],
@@ -132,15 +134,15 @@ describe('NlpValueService', () => {
132134
'Hello do you see me',
133135
[
134136
{ entity: 'intent', value: 'greeting' },
135-
{ entity: 'first_name', value: 'jhon' },
137+
{ entity: 'firstname', value: 'jhon' },
136138
],
137139
storedEntities,
138140
);
139141
const intentEntity = await nlpEntityRepository.findOne({
140142
name: 'intent',
141143
});
142144
const firstNameEntity = await nlpEntityRepository.findOne({
143-
name: 'first_name',
145+
name: 'firstname',
144146
});
145147
const greetingValue = await nlpValueRepository.findOne({
146148
value: 'greeting',

api/src/utils/test/fixtures/nlpentity.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const nlpEntityFixtures: NlpEntityCreateDto[] = [
2020
weight: 1,
2121
},
2222
{
23-
name: 'first_name',
23+
name: 'firstname',
2424
lookups: ['keywords'],
2525
doc: '',
2626
builtin: false,
@@ -33,6 +33,13 @@ export const nlpEntityFixtures: NlpEntityCreateDto[] = [
3333
builtin: true,
3434
weight: 1,
3535
},
36+
{
37+
name: 'subject',
38+
lookups: ['trait'],
39+
doc: '',
40+
builtin: false,
41+
weight: 1,
42+
},
3643
];
3744

3845
export const installNlpEntityFixtures = async () => {

api/src/utils/test/fixtures/nlpvalue.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export const nlpValueFixtures: NlpValueCreateDto[] = [
4949
builtin: true,
5050
doc: '',
5151
},
52+
{
53+
entity: '0',
54+
value: 'affirmation',
55+
expressions: ['yes', 'oui', 'yeah'],
56+
builtin: false,
57+
doc: '',
58+
},
5259
];
5360

5461
export const installNlpValueFixtures = async () => {

0 commit comments

Comments
 (0)