From 95e3c88a3d02632560ff227da9d5e1da22b1a142 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 1 Sep 2022 21:29:11 +0530 Subject: [PATCH] refactor/Added unit test to get table row and improved api validation --- .../__tests__/unit/rest/tests/factory/row.ts | 10 ++++++- .../unit/rest/tests/tableRow.test.ts | 29 +++++++++++++++++++ packages/nocodb/src/lib/models/Model.ts | 12 ++++---- 3 files changed, 45 insertions(+), 6 deletions(-) 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 8e51818610..63bfd62501 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts @@ -16,6 +16,14 @@ const rowValue = (column: ColumnType, index: number) => { } }; +const getRow = async (context, project, table, id) => { + const response = await request(context.app) + .get(`/api/v1/db/data/noco/${project.id}/${table.id}/${id}`) + .set('xc-auth', context.token); + + return response.body; +}; + const createRow = async ( context, project, @@ -36,4 +44,4 @@ const createRow = async ( return response.body; }; -export { createRow }; +export { createRow, getRow }; 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 c2314df37a..ef38161d57 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts @@ -9,6 +9,7 @@ import { createLookupColumn, createRollupColumn, } from './factory/column'; +import { createTable } from './factory/table'; const isColumnsCorrectInResponse = (response, columns: ColumnType[]) => { const responseColumnsListStr = Object.keys(response.body.list[0]) @@ -752,6 +753,34 @@ function tableTest() { // console.log(response.body); // }); + + it('Create table row', async function () { + const table = await createTable(context, project); + + const response = await request(context.app) + .post(`/api/v1/db/data/noco/${project.id}/${table.id}`) + .set('xc-auth', context.token) + .send({ + title: 'Test', + }) + .expect(200); + + const row = response.body; + if (row['Title'] !== 'Test') throw new Error('Wrong row title'); + }); + + it('Create table row with wrong table id', async function () { + const response = await request(context.app) + .post(`/api/v1/db/data/noco/${project.id}/wrong-table-id`) + .set('xc-auth', context.token) + .send({ + title: 'Test', + }) + .expect(404); + + if (response.body.msg !== 'Table not found') + throw new Error('Wrong error message'); + }); } export default function () { diff --git a/packages/nocodb/src/lib/models/Model.ts b/packages/nocodb/src/lib/models/Model.ts index 7104a44c38..a5b5c8fa92 100644 --- a/packages/nocodb/src/lib/models/Model.ts +++ b/packages/nocodb/src/lib/models/Model.ts @@ -628,11 +628,13 @@ export default class Model implements TableType { ], } ); - await NocoCache.set( - `${CacheScope.MODEL}:${project_id}:${aliasOrId}`, - model.id - ); - await NocoCache.set(`${CacheScope.MODEL}:${model.id}`, model); + if (model) { + await NocoCache.set( + `${CacheScope.MODEL}:${project_id}:${aliasOrId}`, + model.id + ); + await NocoCache.set(`${CacheScope.MODEL}:${model.id}`, model); + } return model && new Model(model); } return modelId && this.get(modelId);