Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Keep track of test case executions during test run (no-changelog) #12787

Merged
Merged
2 changes: 2 additions & 0 deletions packages/cli/src/databases/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Settings } from './settings';
import { SharedCredentials } from './shared-credentials';
import { SharedWorkflow } from './shared-workflow';
import { TagEntity } from './tag-entity';
import { TestCaseExecution } from './test-case-execution.ee';
import { TestDefinition } from './test-definition.ee';
import { TestMetric } from './test-metric.ee';
import { TestRun } from './test-run.ee';
Expand Down Expand Up @@ -64,4 +65,5 @@ export const entities = {
TestDefinition,
TestMetric,
TestRun,
TestCaseExecution,
};
68 changes: 68 additions & 0 deletions packages/cli/src/databases/entities/test-case-execution.ee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Column, Entity, ManyToOne, OneToOne } from '@n8n/typeorm';

import {
datetimeColumnType,
jsonColumnType,
WithStringId,
} from '@/databases/entities/abstract-entity';
import type { ExecutionEntity } from '@/databases/entities/execution-entity';
import { TestRun } from '@/databases/entities/test-run.ee';

export type TestCaseRunMetrics = Record<string, number | boolean>;

/**
* This entity represents the linking between the test runs and individual executions.
* It stores status, links to past, new and evaluation executions, and metrics produced by individual evaluation wf executions
* Entries in this table are meant to outlive the execution entities, which might be pruned over time.
* This allows us to keep track of the details of test runs' status and metrics even after the executions are deleted.
*/
@Entity({ name: 'test_case_execution' })
export class TestCaseExecution extends WithStringId {
@ManyToOne('TestRun')
testRun: TestRun;
burivuhster marked this conversation as resolved.
Show resolved Hide resolved

@ManyToOne('ExecutionEntity', {
onDelete: 'SET NULL',
nullable: true,
})
pastExecution: ExecutionEntity | null;
burivuhster marked this conversation as resolved.
Show resolved Hide resolved

@Column({ type: 'varchar', nullable: true })
pastExecutionId: string | null;

@OneToOne('ExecutionEntity', {
onDelete: 'SET NULL',
nullable: true,
})
execution: ExecutionEntity | null;

@Column({ type: 'varchar', nullable: true })
executionId: string | null;

@OneToOne('ExecutionEntity', {
onDelete: 'SET NULL',
nullable: true,
})
evaluationExecution: ExecutionEntity | null;

@Column({ type: 'varchar', nullable: true })
evaluationExecutionId: string | null;

@Column()
status: 'new' | 'running' | 'evaluation_running' | 'success' | 'error' | 'cancelled';

@Column({ type: datetimeColumnType, nullable: true })
runAt: Date | null;

@Column({ type: datetimeColumnType, nullable: true })
completedAt: Date | null;

@Column('varchar', { nullable: true })
errorCode: string | null;
burivuhster marked this conversation as resolved.
Show resolved Hide resolved

@Column(jsonColumnType, { nullable: true })
errorDetails: Record<string, unknown>;

@Column(jsonColumnType, { nullable: true })
metrics: TestCaseRunMetrics;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { MigrationContext, ReversibleMigration } from '@/databases/types';

const testCaseExecutionTableName = 'test_case_execution';

export class CreateTestCaseExecutionTable1736947513045 implements ReversibleMigration {
async up({ schemaBuilder: { createTable, column } }: MigrationContext) {
await createTable(testCaseExecutionTableName)
.withColumns(
column('id').varchar(36).primary.notNull,
column('testRunId').varchar(36).notNull,
column('pastExecutionId').int, // Might be null if execution was deleted after the test run
column('executionId').int, // Execution of the workflow under test. Might be null if execution was deleted after the test run
column('evaluationExecutionId').int, // Execution of the evaluation workflow. Might be null if execution was deleted after the test run, or if the test run was cancelled
burivuhster marked this conversation as resolved.
Show resolved Hide resolved
column('status').varchar().notNull,
burivuhster marked this conversation as resolved.
Show resolved Hide resolved
column('runAt').timestamp(),
column('completedAt').timestamp(),
column('errorCode').varchar(),
column('errorDetails').json,
column('metrics').json,
)
.withIndexOn('testRunId')
.withForeignKey('testRunId', {
tableName: 'test_run',
columnName: 'id',
onDelete: 'CASCADE',
})
.withForeignKey('pastExecutionId', {
tableName: 'execution_entity',
columnName: 'id',
onDelete: 'SET NULL',
})
.withForeignKey('executionId', {
tableName: 'execution_entity',
columnName: 'id',
onDelete: 'SET NULL',
})
.withForeignKey('evaluationExecutionId', {
tableName: 'execution_entity',
columnName: 'id',
onDelete: 'SET NULL',
}).withTimestamps;
}

async down({ schemaBuilder: { dropTable } }: MigrationContext) {
await dropTable(testCaseExecutionTableName);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/mysqldb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { CreateTestRun1732549866705 } from '../common/1732549866705-CreateTestRu
import { AddMockedNodesColumnToTestDefinition1733133775640 } from '../common/1733133775640-AddMockedNodesColumnToTestDefinition';
import { AddManagedColumnToCredentialsTable1734479635324 } from '../common/1734479635324-AddManagedColumnToCredentialsTable';
import { AddStatsColumnsToTestRun1736172058779 } from '../common/1736172058779-AddStatsColumnsToTestRun';
import { CreateTestCaseExecutionTable1736947513045 } from '../common/1736947513045-CreateTestCaseExecutionTable';

export const mysqlMigrations: Migration[] = [
InitialMigration1588157391238,
Expand Down Expand Up @@ -156,4 +157,5 @@ export const mysqlMigrations: Migration[] = [
AddManagedColumnToCredentialsTable1734479635324,
AddProjectIcons1729607673469,
AddStatsColumnsToTestRun1736172058779,
CreateTestCaseExecutionTable1736947513045,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { CreateTestRun1732549866705 } from '../common/1732549866705-CreateTestRu
import { AddMockedNodesColumnToTestDefinition1733133775640 } from '../common/1733133775640-AddMockedNodesColumnToTestDefinition';
import { AddManagedColumnToCredentialsTable1734479635324 } from '../common/1734479635324-AddManagedColumnToCredentialsTable';
import { AddStatsColumnsToTestRun1736172058779 } from '../common/1736172058779-AddStatsColumnsToTestRun';
import { CreateTestCaseExecutionTable1736947513045 } from '../common/1736947513045-CreateTestCaseExecutionTable';

export const postgresMigrations: Migration[] = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -156,4 +157,5 @@ export const postgresMigrations: Migration[] = [
AddManagedColumnToCredentialsTable1734479635324,
AddProjectIcons1729607673469,
AddStatsColumnsToTestRun1736172058779,
CreateTestCaseExecutionTable1736947513045,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { CreateTestRun1732549866705 } from '../common/1732549866705-CreateTestRu
import { AddMockedNodesColumnToTestDefinition1733133775640 } from '../common/1733133775640-AddMockedNodesColumnToTestDefinition';
import { AddManagedColumnToCredentialsTable1734479635324 } from '../common/1734479635324-AddManagedColumnToCredentialsTable';
import { AddStatsColumnsToTestRun1736172058779 } from '../common/1736172058779-AddStatsColumnsToTestRun';
import { CreateTestCaseExecutionTable1736947513045 } from '../common/1736947513045-CreateTestCaseExecutionTable';

const sqliteMigrations: Migration[] = [
InitialMigration1588102412422,
Expand Down Expand Up @@ -150,6 +151,7 @@ const sqliteMigrations: Migration[] = [
AddManagedColumnToCredentialsTable1734479635324,
AddProjectIcons1729607673469,
AddStatsColumnsToTestRun1736172058779,
CreateTestCaseExecutionTable1736947513045,
];

export { sqliteMigrations };
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Service } from '@n8n/di';
import type { EntityManager } from '@n8n/typeorm';
import { DataSource, In, Not, Repository } from '@n8n/typeorm';
import type { DeepPartial } from '@n8n/typeorm/common/DeepPartial';

import { TestCaseExecution } from '@/databases/entities/test-case-execution.ee';

@Service()
export class TestCaseExecutionRepository extends Repository<TestCaseExecution> {
constructor(dataSource: DataSource) {
super(TestCaseExecution, dataSource.manager);
}

async createBatch(testRunId: string, pastExecutionIds: string[]) {
burivuhster marked this conversation as resolved.
Show resolved Hide resolved
const mappings = this.create(
pastExecutionIds.map<DeepPartial<TestCaseExecution>>((id) => ({
testRun: {
id: testRunId,
},
pastExecution: {
id,
},
status: 'new',
})),
);

return await this.save(mappings);
}

async markAsRunning(testRunId: string, pastExecutionId: string, executionId: string) {
return await this.update(
{ testRun: { id: testRunId }, pastExecutionId },
{
status: 'running',
executionId,
runAt: new Date(),
},
);
}

async markAsEvaluationRunning(
testRunId: string,
pastExecutionId: string,
evaluationExecutionId: string,
) {
return await this.update(
{ testRun: { id: testRunId }, pastExecutionId },
{
status: 'evaluation_running',
evaluationExecutionId,
},
);
}

async markAsCompleted(
testRunId: string,
pastExecutionId: string,
metrics: Record<string, number>,
burivuhster marked this conversation as resolved.
Show resolved Hide resolved
trx?: EntityManager,
) {
trx = trx ?? this.manager;

return await trx.update(
TestCaseExecution,
{ testRun: { id: testRunId }, pastExecutionId },
{
status: 'success',
completedAt: new Date(),
metrics,
},
);
}

async markAllPendingAsCancelled(testRunId: string, trx?: EntityManager) {
trx = trx ?? this.manager;

return await trx.update(
TestCaseExecution,
{ testRun: { id: testRunId }, status: Not(In(['success', 'error', 'cancelled'])) },
burivuhster marked this conversation as resolved.
Show resolved Hide resolved
{
status: 'cancelled',
completedAt: new Date(),
},
);
}

async markAsFailed(testRunId: string, pastExecutionId: string, trx?: EntityManager) {
trx = trx ?? this.manager;

return await trx.update(
TestCaseExecution,
{ testRun: { id: testRunId }, pastExecutionId },
{
status: 'error',
completedAt: new Date(),
},
);
}
}
17 changes: 10 additions & 7 deletions packages/cli/src/databases/repositories/test-run.repository.ee.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Service } from '@n8n/di';
import type { FindManyOptions } from '@n8n/typeorm';
import type { EntityManager, FindManyOptions } from '@n8n/typeorm';
import { DataSource, Repository } from '@n8n/typeorm';

import type { AggregatedTestRunMetrics } from '@/databases/entities/test-run.ee';
Expand Down Expand Up @@ -35,16 +35,19 @@ export class TestRunRepository extends Repository<TestRun> {
return await this.update(id, { status: 'completed', completedAt: new Date(), metrics });
}

async markAsCancelled(id: string) {
return await this.update(id, { status: 'cancelled' });
async markAsCancelled(id: string, trx?: EntityManager) {
trx = trx ?? this.manager;
return await trx.update(TestRun, id, { status: 'cancelled' });
}

async incrementPassed(id: string) {
return await this.increment({ id }, 'passedCases', 1);
async incrementPassed(id: string, trx?: EntityManager) {
trx = trx ?? this.manager;
return await trx.increment(TestRun, { id }, 'passedCases', 1);
}

async incrementFailed(id: string) {
return await this.increment({ id }, 'failedCases', 1);
async incrementFailed(id: string, trx?: EntityManager) {
trx = trx ?? this.manager;
return await trx.increment(TestRun, { id }, 'failedCases', 1);
}

async getMany(testDefinitionId: string, options: ListQuery.Options) {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/evaluation.ee/test-definitions.types.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ export declare namespace TestRunsRequest {
type Delete = AuthenticatedRequest<RouteParams.TestId & RouteParams.TestRunId>;

type Cancel = AuthenticatedRequest<RouteParams.TestId & RouteParams.TestRunId>;

type GetCases = AuthenticatedRequest<RouteParams.TestId & RouteParams.TestRunId>;
}
Loading
Loading