From 3f3f9e55f357b4741ee74546d3cb9be3251395ea Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Tue, 22 Nov 2022 11:36:21 +0530 Subject: [PATCH] fix(test): Temporary fix for pg sakila resetting issue --- packages/nocodb/src/lib/meta/api/testApis.ts | 1 + .../services/test/TestResetService/index.ts | 22 +++++++++++++------ tests/playwright/setup/db.ts | 9 ++++---- tests/playwright/setup/index.ts | 18 ++++++++++++++- tests/playwright/tests/metaSync.spec.ts | 2 +- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/nocodb/src/lib/meta/api/testApis.ts b/packages/nocodb/src/lib/meta/api/testApis.ts index 76fcfcd842..73cba3ceb4 100644 --- a/packages/nocodb/src/lib/meta/api/testApis.ts +++ b/packages/nocodb/src/lib/meta/api/testApis.ts @@ -6,6 +6,7 @@ export async function reset(req: Request, res) { parallelId: req.body.parallelId, dbType: req.body.dbType, isEmptyProject: req.body.isEmptyProject, + workerId: req.body.workerId }); res.json(await service.process()); diff --git a/packages/nocodb/src/lib/services/test/TestResetService/index.ts b/packages/nocodb/src/lib/services/test/TestResetService/index.ts index 59d7372565..ae9837ce3e 100644 --- a/packages/nocodb/src/lib/services/test/TestResetService/index.ts +++ b/packages/nocodb/src/lib/services/test/TestResetService/index.ts @@ -23,11 +23,13 @@ const loginRootUser = async () => { const projectTitleByType = { sqlite: 'sampleREST', mysql: 'externalREST', - pg: 'pgExtREST', + pg: 'pgExtREST' }; export class TestResetService { private readonly parallelId; + // todo: Hack to resolve issue with pg resetting + private readonly workerId; private readonly dbType; private readonly isEmptyProject: boolean; @@ -35,14 +37,17 @@ export class TestResetService { parallelId, dbType, isEmptyProject, + workerId }: { parallelId: string; dbType: string; isEmptyProject: boolean; + workerId: string; }) { this.parallelId = parallelId; this.dbType = dbType; this.isEmptyProject = isEmptyProject; + this.workerId = workerId; } async process() { @@ -68,6 +73,7 @@ export class TestResetService { token, dbType: this.dbType, parallelId: this.parallelId, + workerId: this.workerId }); try { @@ -90,10 +96,12 @@ export class TestResetService { token, dbType, parallelId, + workerId }: { token: string; dbType: string; parallelId: string; + workerId: string; }) { const title = `${projectTitleByType[dbType]}${parallelId}`; const project: Project | undefined = await Project.getByTitle(title); @@ -115,7 +123,7 @@ export class TestResetService { token, title, parallelId, - isEmptyProject: this.isEmptyProject, + isEmptyProject: this.isEmptyProject }); } else if (dbType == 'mysql') { await resetMysqlSakilaProject({ @@ -123,20 +131,20 @@ export class TestResetService { title, parallelId, oldProject: project, - isEmptyProject: this.isEmptyProject, + isEmptyProject: this.isEmptyProject }); } else if (dbType == 'pg') { await resetPgSakilaProject({ token, title, - parallelId, + parallelId: workerId, oldProject: project, - isEmptyProject: this.isEmptyProject, + isEmptyProject: this.isEmptyProject }); } return { - project: await Project.getByTitle(title), + project: await Project.getByTitle(title) }; } } @@ -169,7 +177,7 @@ const removeProjectUsersFromCache = async (project: Project) => { const projectUsers: ProjectUser[] = await ProjectUser.getUsersList({ project_id: project.id, limit: 1000, - offset: 0, + offset: 0 }); for (const projectUser of projectUsers) { diff --git a/tests/playwright/setup/db.ts b/tests/playwright/setup/db.ts index d42591f406..103af14789 100644 --- a/tests/playwright/setup/db.ts +++ b/tests/playwright/setup/db.ts @@ -11,17 +11,18 @@ const isSqlite = (context: NcContext) => context.dbType === 'sqlite'; const isPg = (context: NcContext) => context.dbType === 'pg'; -const pg_credentials = () => ({ +const pg_credentials = (context: NcContext) => ({ user: 'postgres', host: 'localhost', - database: `sakila_${process.env.TEST_PARALLEL_INDEX}`, + // todo: Hack to resolve issue with pg resetting + database: `sakila_${context.workerId}`, password: 'password', port: 5432, }); -const pgExec = async (query: string) => { +const pgExec = async (query: string, context: NcContext) => { // open pg client connection - const client = new Client(pg_credentials()); + const client = new Client(pg_credentials(context)); await client.connect(); await client.query(query); diff --git a/tests/playwright/setup/index.ts b/tests/playwright/setup/index.ts index a738d5c095..8d04b75464 100644 --- a/tests/playwright/setup/index.ts +++ b/tests/playwright/setup/index.ts @@ -1,10 +1,14 @@ import { Page, selectors } from '@playwright/test'; import axios from 'axios'; +const workerCount = {}; + export interface NcContext { project: any; token: string; dbType?: string; + // todo: Hack to resolve issue with pg resetting + workerId?: string; } selectors.setTestIdAttribute('data-testid'); @@ -13,11 +17,23 @@ const setup = async ({ page, isEmptyProject }: { page: Page; isEmptyProject?: bo let dbType = process.env.CI ? process.env.E2E_DB_TYPE : process.env.E2E_DEV_DB_TYPE; dbType = dbType || 'sqlite'; + let workerId; + // todo: Hack to resolve issue with pg resetting + if (dbType === 'pg') { + const workerIndex = process.env.TEST_PARALLEL_INDEX; + if (!workerCount[workerIndex]) { + workerCount[workerIndex] = 0; + } + workerCount[workerIndex]++; + workerId = String(Number(workerIndex) + Number(workerCount[workerIndex]) * 4); + } + // if (!process.env.CI) console.time(`setup ${process.env.TEST_PARALLEL_INDEX}`); let response; try { response = await axios.post(`http://localhost:8080/api/v1/meta/test/reset`, { parallelId: process.env.TEST_PARALLEL_INDEX, + workerId: workerId, dbType, isEmptyProject, }); @@ -59,7 +75,7 @@ const setup = async ({ page, isEmptyProject }: { page: Page; isEmptyProject?: bo await page.goto(`/#/nc/${project.id}/auth`, { waitUntil: 'networkidle' }); - return { project, token, dbType } as NcContext; + return { project, token, dbType, workerId } as NcContext; }; export default setup; diff --git a/tests/playwright/tests/metaSync.spec.ts b/tests/playwright/tests/metaSync.spec.ts index 086d2bcb10..bec73023bd 100644 --- a/tests/playwright/tests/metaSync.spec.ts +++ b/tests/playwright/tests/metaSync.spec.ts @@ -24,7 +24,7 @@ test.describe('Meta sync', () => { dbExec = mysqlExec; break; case 'pg': - dbExec = pgExec; + dbExec = query => pgExec(query, context); break; } });