mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 09:36:44 +00:00
test: Reduce DB operations during data store tests (#19697)
This commit is contained in:
@@ -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 =
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user