fix(api): add postCreate unit tests

This commit is contained in:
yassinedorbozgithub 2025-06-17 08:20:51 +01:00
parent a810702ff6
commit bf37cd556a
2 changed files with 103 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import { SettingRepository } from '@/setting/repositories/setting.repository';
import { SettingModel } from '@/setting/schemas/setting.schema'; import { SettingModel } from '@/setting/schemas/setting.schema';
import { SettingSeeder } from '@/setting/seeds/setting.seed'; import { SettingSeeder } from '@/setting/seeds/setting.seed';
import { SettingService } from '@/setting/services/setting.service'; import { SettingService } from '@/setting/services/setting.service';
import { IGNORED_TEST_FIELDS } from '@/utils/test/constants';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity'; import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import { installNlpValueFixtures } from '@/utils/test/fixtures/nlpvalue'; import { installNlpValueFixtures } from '@/utils/test/fixtures/nlpvalue';
import { getPageQuery } from '@/utils/test/pagination'; import { getPageQuery } from '@/utils/test/pagination';
@ -173,4 +174,44 @@ describe('NlpEntityRepository', () => {
]); ]);
}); });
}); });
describe('postCreate', () => {
it('should not create and attached a foreign_id to the create nlp entity', async () => {
nlpEntityRepository.eventEmitter.once(
'hook:nlpEntity:postCreate',
async (...args) => {
await nlpService.handleEntityPostCreate(args[0]);
},
);
const result = await nlpEntityRepository.create({
name: 'test1',
});
const intentNlpEntity = await nlpEntityRepository.findOne(result.id);
expect(intentNlpEntity?.foreign_id).toBeDefined();
expect(intentNlpEntity).toEqualPayload(result, [
...IGNORED_TEST_FIELDS,
'foreign_id',
]);
});
it('should not create and attached a foreign_id to the create nlp entity with builtin true', async () => {
nlpEntityRepository.eventEmitter.once(
'hook:nlpEntity:postCreate',
async (...args) => {
await nlpService.handleEntityPostCreate(args[0]);
},
);
const result = await nlpEntityRepository.create({
name: 'test2',
builtin: true,
});
const intentNlpEntity = await nlpEntityRepository.findOne(result.id);
expect(intentNlpEntity?.foreign_id).toBeUndefined();
expect(intentNlpEntity).toEqualPayload(result);
});
});
}); });

View File

@ -18,6 +18,7 @@ import { SettingRepository } from '@/setting/repositories/setting.repository';
import { SettingModel } from '@/setting/schemas/setting.schema'; import { SettingModel } from '@/setting/schemas/setting.schema';
import { SettingSeeder } from '@/setting/seeds/setting.seed'; import { SettingSeeder } from '@/setting/seeds/setting.seed';
import { SettingService } from '@/setting/services/setting.service'; import { SettingService } from '@/setting/services/setting.service';
import { IGNORED_TEST_FIELDS } from '@/utils/test/constants';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity'; import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import { installNlpSampleEntityFixtures } from '@/utils/test/fixtures/nlpsampleentity'; import { installNlpSampleEntityFixtures } from '@/utils/test/fixtures/nlpsampleentity';
import { nlpValueFixtures } from '@/utils/test/fixtures/nlpvalue'; import { nlpValueFixtures } from '@/utils/test/fixtures/nlpvalue';
@ -53,6 +54,7 @@ describe('NlpValueRepository', () => {
let nlpSampleEntityRepository: NlpSampleEntityRepository; let nlpSampleEntityRepository: NlpSampleEntityRepository;
let nlpValues: NlpValue[]; let nlpValues: NlpValue[];
let nlpService: NlpService; let nlpService: NlpService;
let nlpEntityRepository: NlpEntityRepository;
beforeAll(async () => { beforeAll(async () => {
const { getMocks, module } = await buildTestingMocks({ const { getMocks, module } = await buildTestingMocks({
@ -102,12 +104,17 @@ describe('NlpValueRepository', () => {
], ],
}); });
[nlpValueRepository, nlpSampleEntityRepository, nlpService] = [
await getMocks([ nlpValueRepository,
NlpValueRepository, nlpSampleEntityRepository,
NlpSampleEntityRepository, nlpService,
NlpService, nlpEntityRepository,
]); ] = await getMocks([
NlpValueRepository,
NlpSampleEntityRepository,
NlpService,
NlpEntityRepository,
]);
nlpValues = await nlpValueRepository.findAll(); nlpValues = await nlpValueRepository.findAll();
const llmNluHelper = module.get(LlmNluHelper); const llmNluHelper = module.get(LlmNluHelper);
module.get(HelperService).register(llmNluHelper); module.get(HelperService).register(llmNluHelper);
@ -180,4 +187,53 @@ describe('NlpValueRepository', () => {
expect(sampleEntities.length).toEqual(0); expect(sampleEntities.length).toEqual(0);
}); });
}); });
describe('postCreate', () => {
it('should create and attached a foreign_id to the create nlp value', async () => {
nlpValueRepository.eventEmitter.once(
'hook:nlpValue:postCreate',
async (...args) => {
await nlpService.handleValuePostCreate(args[0]);
},
);
const createdNlpEntity = await nlpEntityRepository.create({
name: 'test1',
});
const result = await nlpValueRepository.create({
entity: createdNlpEntity.id,
value: 'test',
});
const intentNlpEntity = await nlpValueRepository.findOne(result.id);
expect(intentNlpEntity?.foreign_id).toBeDefined();
expect(intentNlpEntity).toEqualPayload(result, [
...IGNORED_TEST_FIELDS,
'foreign_id',
]);
});
it('should create and attached a foreign_id to the create nlp value with builtin true', async () => {
nlpValueRepository.eventEmitter.once(
'hook:nlpValue:postCreate',
async (...args) => {
await nlpService.handleValuePostCreate(args[0]);
},
);
const createdNlpEntity = await nlpEntityRepository.create({
name: 'nlpEntityTest2',
});
const result = await nlpValueRepository.create({
entity: createdNlpEntity.id,
value: 'nlpValueTest2',
builtin: true,
});
const intentNlpEntity = await nlpValueRepository.findOne(result.id);
expect(intentNlpEntity?.foreign_id).toBeUndefined();
expect(intentNlpEntity).toEqualPayload(result);
});
});
}); });