From 9fa075ea69f908a7946f8b6279c32875a570ec9f Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Fri, 2 Sep 2022 15:08:37 +0530 Subject: [PATCH] refactor/Added unit test for exists table row api and improved table dropping while starting tests --- .../__tests__/unit/rest/init/cleanupMeta.ts | 10 +++--- .../__tests__/unit/rest/tests/factory/row.ts | 14 ++++++-- .../unit/rest/tests/tableRow.test.ts | 33 ++++++++++++++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts b/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts index 38c504c0ec..c46d2118d7 100644 --- a/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts +++ b/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts @@ -24,11 +24,11 @@ const dropTablesAllNonExternalProjects = async (knexClient) => { }) ); - await Promise.all( - userCreatedTableNames.map(async (tableName) => { - await knexClient.raw(`DROP TABLE ${tableName}`); - }) - ); + await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0'); + for (const tableName of userCreatedTableNames) { + await knexClient.raw(`DROP TABLE ${tableName}`); + } + await knexClient.raw('SET FOREIGN_KEY_CHECKS = 1'); }; const cleanupMetaTables = async (knexClient) => { diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts b/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts index 654a0ec0d0..8370830c29 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts @@ -1,4 +1,3 @@ -import console from 'console'; import { ColumnType, UITypes } from 'nocodb-sdk'; import request from 'supertest'; import Column from '../../../../../lib/models/Column'; @@ -32,6 +31,17 @@ const getRow = async (context, project, table, id) => { return response.body; }; +const getOneRow = async ( + context, + { project, table }: { project: Project; table: Model } +) => { + const response = await request(context.app) + .get(`/api/v1/db/data/noco/${project.id}/${table.id}/find-one`) + .set('xc-auth', context.token); + + return response.body; +}; + const createRow = async ( context, { @@ -102,4 +112,4 @@ const createRelation = async ( .set('xc-auth', context.token); }; -export { createRow, getRow, createRelation }; +export { createRow, getRow, createRelation, getOneRow }; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts index d5b4994923..b1271bb141 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts @@ -11,7 +11,7 @@ import { createRollupColumn, } from './factory/column'; import { createTable } from './factory/table'; -import { createRelation, createRow, getRow } from './factory/row'; +import { createRelation, createRow, getOneRow, getRow } from './factory/row'; const isColumnsCorrectInResponse = (row, columns: ColumnType[]) => { const responseColumnsListStr = Object.keys(row).sort().join(','); @@ -1232,6 +1232,37 @@ function tableTest() { throw new Error('Should give ltar foreign key error'); } }); + + it('Exist should be true table row when it exists', async function () { + const row = await getOneRow(context, { + project: sakilaProject, + table: customerTable, + }); + + const response = await request(context.app) + .get( + `/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${row['CustomerId']}/exist` + ) + .set('xc-auth', context.token) + .expect(200); + + if (!response.body) { + throw new Error('Should exist'); + } + }); + + it('Exist should be false table row when it does not exists', async function () { + const response = await request(context.app) + .get( + `/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/invalid-id/exist` + ) + .set('xc-auth', context.token) + .expect(200); + + if (response.body) { + throw new Error('Should not exist'); + } + }); } export default function () {