test: Reduce DB operations during data store tests (#19697)

This commit is contained in:
Tomi Turtiainen
2025-09-18 14:33:48 +03:00
committed by GitHub
parent 83b2a5772e
commit d239a7fda6
2 changed files with 14 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import { DataSource as Connection } from '@n8n/typeorm';
import { randomString } from 'n8n-workflow';
export const testDbPrefix = 'n8n_test_';
let isInitialized = false;
/**
* Generate options for a bootstrap DB connection, to create and drop test databases.
@@ -27,6 +28,8 @@ export const getBootstrapDBOptions = (dbType: 'postgresdb' | 'mysqldb'): DataSou
* Initialize one test DB per suite run, with bootstrap connection if needed.
*/
export async function init() {
if (isInitialized) return;
const globalConfig = Container.get(GlobalConfig);
const dbType = globalConfig.database.type;
const testDbName = `${testDbPrefix}${randomString(6, 10).toLowerCase()}_${Date.now()}`;
@@ -52,6 +55,8 @@ export async function init() {
await dbConnection.migrate();
await Container.get(AuthRolesService).init();
isInitialized = true;
}
export function isReady() {
@@ -66,6 +71,7 @@ export async function terminate() {
const dbConnection = Container.get(DbConnection);
await dbConnection.close();
dbConnection.connectionState.connected = false;
isInitialized = false;
}
type EntityName =

View File

@@ -42,12 +42,7 @@ let dataStoreColumnRepository: DataStoreColumnRepository;
let dataStoreRowsRepository: DataStoreRowsRepository;
beforeAll(async () => {
await testDb.init();
mockDataStoreSizeValidator();
});
beforeEach(async () => {
await testDb.truncate(['DataTable', 'DataTableColumn', 'Project', 'ProjectRelation']);
projectRepository = Container.get(ProjectRepository);
dataStoreRepository = Container.get(DataStoreRepository);
@@ -66,11 +61,19 @@ beforeEach(async () => {
memberProject = await getPersonalProject(member);
});
beforeEach(async () => {
await testDb.truncate(['DataTable', 'DataTableColumn']);
});
afterAll(async () => {
await testDb.terminate();
});
describe('POST /projects/:projectId/data-tables', () => {
beforeEach(async () => {
await testDb.truncate(['DataTable', 'DataTableColumn']);
});
test('should not create data store when project does not exist', async () => {
const payload = {
name: 'Test Data Store',