wip: parameters validation, more tests

This commit is contained in:
Eugene Molodkin 2024-11-07 13:24:02 +01:00
parent 9849739d39
commit 2482903c7c
No known key found for this signature in database
3 changed files with 58 additions and 2 deletions

View file

@ -59,4 +59,7 @@ export class TestDefinition extends WithTimestamps {
*/ */
@ManyToOne('AnnotationTagEntity', 'test') @ManyToOne('AnnotationTagEntity', 'test')
annotationTag: AnnotationTagEntity; annotationTag: AnnotationTagEntity;
@RelationId((test: TestDefinition) => test.annotationTag)
annotationTagId: string;
} }

View file

@ -1,10 +1,12 @@
import { Service } from 'typedi'; import { Service } from 'typedi';
import type { TestDefinition } from '@/databases/entities/test-definition.ee'; import type { TestDefinition } from '@/databases/entities/test-definition.ee';
import { AnnotationTagRepository } from '@/databases/repositories/annotation-tag.repository.ee';
import { TestDefinitionRepository } from '@/databases/repositories/test-definition.repository.ee'; import { TestDefinitionRepository } from '@/databases/repositories/test-definition.repository.ee';
import { NotFoundError } from '@/errors/response-errors/not-found.error'; import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { validateEntity } from '@/generic-helpers'; import { validateEntity } from '@/generic-helpers';
import type { ListQuery } from '@/requests'; import type { ListQuery } from '@/requests';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
type TestDefinitionLike = Omit< type TestDefinitionLike = Omit<
Partial<TestDefinition>, Partial<TestDefinition>,
@ -17,7 +19,10 @@ type TestDefinitionLike = Omit<
@Service() @Service()
export class TestDefinitionsService { export class TestDefinitionsService {
constructor(private testRepository: TestDefinitionRepository) {} constructor(
private testRepository: TestDefinitionRepository,
private annotationTagRepository: AnnotationTagRepository,
) {}
private toEntityLike(attrs: { private toEntityLike(attrs: {
name?: string; name?: string;
@ -89,6 +94,20 @@ export class TestDefinitionsService {
delete attrs.workflowId; delete attrs.workflowId;
} }
// Check if the annotation tag exists
if (attrs.annotationTagId) {
const annotationTagExists = await this.annotationTagRepository.exists({
where: {
id: attrs.annotationTagId,
},
});
if (!annotationTagExists) {
throw new BadRequestError('Annotation tag not found');
}
}
// Update the test definition
const queryResult = await this.testRepository.update(id, this.toEntityLike(attrs)); const queryResult = await this.testRepository.update(id, this.toEntityLike(attrs));
if (queryResult.affected === 0) { if (queryResult.affected === 0) {

View file

@ -9,12 +9,15 @@ import { createWorkflow } from './../shared/db/workflows';
import * as testDb from './../shared/test-db'; import * as testDb from './../shared/test-db';
import type { SuperAgentTest } from './../shared/types'; import type { SuperAgentTest } from './../shared/types';
import * as utils from './../shared/utils/'; import * as utils from './../shared/utils/';
import { AnnotationTagEntity } from '@/databases/entities/annotation-tag-entity.ee';
import { createAnnotationTags } from '@test-integration/db/executions';
let authOwnerAgent: SuperAgentTest; let authOwnerAgent: SuperAgentTest;
let workflowUnderTest: WorkflowEntity; let workflowUnderTest: WorkflowEntity;
let evaluationWorkflow: WorkflowEntity; let evaluationWorkflow: WorkflowEntity;
let otherWorkflow: WorkflowEntity; let otherWorkflow: WorkflowEntity;
let ownerShell: User; let ownerShell: User;
let annotationTag: AnnotationTagEntity;
const testServer = utils.setupTestServer({ endpointGroups: ['evaluation'] }); const testServer = utils.setupTestServer({ endpointGroups: ['evaluation'] });
beforeAll(async () => { beforeAll(async () => {
@ -23,11 +26,12 @@ beforeAll(async () => {
}); });
beforeEach(async () => { beforeEach(async () => {
await testDb.truncate(['TestDefinition', 'Workflow']); await testDb.truncate(['TestDefinition', 'Workflow', 'AnnotationTag']);
workflowUnderTest = await createWorkflow({ name: 'workflow-under-test' }, ownerShell); workflowUnderTest = await createWorkflow({ name: 'workflow-under-test' }, ownerShell);
evaluationWorkflow = await createWorkflow({ name: 'evaluation-workflow' }, ownerShell); evaluationWorkflow = await createWorkflow({ name: 'evaluation-workflow' }, ownerShell);
otherWorkflow = await createWorkflow({ name: 'other-workflow' }); otherWorkflow = await createWorkflow({ name: 'other-workflow' });
annotationTag = (await createAnnotationTags(['test-tag']))[0];
}); });
describe('GET /evaluation/test-definitions', () => { describe('GET /evaluation/test-definitions', () => {
@ -244,4 +248,34 @@ describe('PATCH /evaluation/test-definitions/:id', () => {
expect(resp.body.data.name).toBe('updated-test'); expect(resp.body.data.name).toBe('updated-test');
expect(resp.body.data.workflowId).toBe(workflowUnderTest.id); expect(resp.body.data.workflowId).toBe(workflowUnderTest.id);
}); });
test('should update annotationTagId', async () => {
const newTest = Container.get(TestDefinitionRepository).create({
name: 'test',
workflow: { id: workflowUnderTest.id },
});
await Container.get(TestDefinitionRepository).save(newTest);
const resp = await authOwnerAgent.patch(`/evaluation/test-definitions/${newTest.id}`).send({
annotationTagId: annotationTag.id,
});
expect(resp.statusCode).toBe(200);
expect(resp.body.data.annotationTag.id).toBe(annotationTag.id);
});
test('should return error if annotationTagId is invalid', async () => {
const newTest = Container.get(TestDefinitionRepository).create({
name: 'test',
workflow: { id: workflowUnderTest.id },
});
await Container.get(TestDefinitionRepository).save(newTest);
const resp = await authOwnerAgent.patch(`/evaluation/test-definitions/${newTest.id}`).send({
annotationTagId: 123,
});
expect(resp.statusCode).toBe(400);
expect(resp.body.message).toBe('Annotation tag not found');
});
}); });