Browse Source

feat(cypress): Added mysql sakila reseting

pull/3848/head
Muhammed Mustafa 2 years ago
parent
commit
abdb91f00d
  1. 44
      packages/nocodb/src/lib/services/test/TestResetService/createProjects.ts
  2. 5
      packages/nocodb/src/lib/services/test/TestResetService/index.ts
  3. 8
      packages/nocodb/src/lib/services/test/TestResetService/resetMeta.ts
  4. 16
      packages/nocodb/src/lib/services/test/TestResetService/resetMysqlSakila.ts
  5. 2
      packages/nocodb/src/lib/services/test/TestResetService/resetPgSakila.ts
  6. 26
      packages/nocodb/src/lib/utils/globals.ts

44
packages/nocodb/src/lib/services/test/TestResetService/createProjects.ts

@ -23,31 +23,31 @@ const extPgProject = {
external: true,
};
// const extMysqlProject = {
// title: 'externalREST',
// bases: [
// {
// type: 'mysql2',
// config: {
// client: 'mysql2',
// connection: {
// host: 'localhost',
// port: '5432',
// user: 'root',
// password: 'password',
// database: 'sakila',
// },
// },
// inflection_column: 'camelize',
// inflection_table: 'camelize',
// },
// ],
// external: true,
// };
const extMysqlProject = {
title: 'externalREST',
bases: [
{
type: 'mysql2',
config: {
client: 'mysql2',
connection: {
host: 'localhost',
port: '3306',
user: 'root',
password: 'password',
database: 'test_sakila',
},
},
inflection_column: 'camelize',
inflection_table: 'camelize',
},
],
external: true,
};
const createProjects = async (token) => {
return await Promise.all(
[extPgProject].map(async (projectAttr) => {
[extPgProject, extMysqlProject].map(async (projectAttr) => {
const response = await axios.post(
'http://localhost:8080/api/v1/db/meta/projects/',
projectAttr,

5
packages/nocodb/src/lib/services/test/TestResetService/index.ts

@ -7,6 +7,7 @@ import createProjects from './createProjects';
import { isPgSakilaToBeReset, resetPgSakila } from './resetPgSakila';
import createUser from './createUser';
import resetMeta from './resetMeta';
import { isMysqlSakilaToBeReset, resetMysqlSakila } from './resetMysqlSakila';
export class TestResetService {
private knex: Knex | null = null;
@ -23,6 +24,10 @@ export class TestResetService {
await resetPgSakila();
}
if (await isMysqlSakilaToBeReset()) {
await resetMysqlSakila();
}
await resetMeta(this.knex);
await NocoCache.destroy();

8
packages/nocodb/src/lib/services/test/TestResetService/resetMeta.ts

@ -1,15 +1,13 @@
import Model from '../../../models/Model';
import Project from '../../../models/Project';
import { orderedMetaTables } from '../../../utils/globals';
import { orderedMetaTables, sakilaTableNames } from '../../../utils/globals';
const disableForeignKeyChecks = async (knex) => {
await knex.raw('PRAGMA foreign_keys = OFF');
// await this.knex.raw(`SET FOREIGN_KEY_CHECKS = 0`);
};
const enableForeignKeyChecks = async (knex) => {
await knex.raw(`PRAGMA foreign_keys = ON;`);
// await this.knex.raw(`SET FOREIGN_KEY_CHECKS = 1`);
};
const dropTablesAllNonExternalProjects = async (knex) => {
@ -28,7 +26,9 @@ const dropTablesAllNonExternalProjects = async (knex) => {
base_id: base.id!,
});
models.forEach((model) => {
userCreatedTableNames.push(model.table_name);
if (!sakilaTableNames.includes(model.table_name)) {
userCreatedTableNames.push(model.table_name);
}
});
})
);

16
packages/nocodb/src/lib/services/test/TestResetService/resetMysqlSakila.ts

@ -10,10 +10,10 @@ const config = {
port: 3306,
user: 'root',
password: 'password',
database: 'sakila',
database: 'test_sakila',
multipleStatements: true,
dateStrings: true,
},
meta: { dbtype: '' },
pool: { min: 0, max: 5 },
};
const isMysqlSakilaToBeReset = async () => {
@ -22,18 +22,18 @@ const isMysqlSakilaToBeReset = async () => {
const audits =
sakilaProject && (await Audit.projectAuditList(sakilaProject.id, {}));
return audits.length > 0;
return audits?.length > 0;
};
const resetMysqlSakila = async () => {
const knexClient = knex(config);
try {
await knexClient.raw(`DROP DATABASE sakila`);
await knexClient.raw(`DROP DATABASE test_sakila`);
} catch (e) {
console.log('Error dropping db', e);
}
await knexClient.raw(`CREATE DATABASE sakila`);
await knexClient.raw(`CREATE DATABASE test_sakila`);
const testsDir = __dirname.replace(
'/src/lib/services/test/TestResetService',
@ -41,10 +41,10 @@ const resetMysqlSakila = async () => {
);
const schemaFile = fs
.readFileSync(`${testsDir}/mysql-sakila-db/01-mysql-sakila-schema.sql`)
.readFileSync(`${testsDir}/mysql-sakila-db/03-test-sakila-schema.sql`)
.toString();
const dataFile = fs
.readFileSync(`${testsDir}/mysql-sakila-db/02-mysql-sakila-insert-data.sql`)
.readFileSync(`${testsDir}/mysql-sakila-db/04-test-sakila-data.sql`)
.toString();
await knexClient.raw(schemaFile);
await knexClient.raw(dataFile);

2
packages/nocodb/src/lib/services/test/TestResetService/resetPgSakila.ts

@ -23,7 +23,7 @@ const isPgSakilaToBeReset = async () => {
const audits =
sakilaProject && (await Audit.projectAuditList(sakilaProject.id, {}));
return audits.length > 0;
return audits?.length > 0;
};
const resetPgSakila = async () => {

26
packages/nocodb/src/lib/utils/globals.ts

@ -74,6 +74,32 @@ export const orderedMetaTables = [
MetaTable.PROJECT,
];
export const sakilaTableNames = [
'actor',
'address',
'category',
'city',
'country',
'customer',
'film',
'film_actor',
'film_category',
'film_text',
'inventory',
'language',
'payment',
'rental',
'staff',
'store',
'actor_info',
'customer_list',
'film_list',
'nicer_but_slower_film_list',
'sales_by_film_category',
'sales_by_store',
'staff_list',
];
export enum CacheScope {
PROJECT = 'project',
BASE = 'base',

Loading…
Cancel
Save