Browse Source

refactor/Added unit test for nested relation apis

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
25d48a0411
  1. 1
      packages/nocodb/src/lib/meta/api/dataApis/dataAliasNestedApis.ts
  2. 1
      packages/nocodb/tests/unit/rest/init/cleanupSakila.ts
  3. 157
      packages/nocodb/tests/unit/rest/tests/tableRow.test.ts

1
packages/nocodb/src/lib/meta/api/dataApis/dataAliasNestedApis.ts

@ -218,6 +218,7 @@ async function relationDataRemove(req, res) {
} }
//@ts-ignore //@ts-ignore
// todo: Give proper error message when reference row is already related and handle duplicate ref row id in hm
async function relationDataAdd(req, res) { async function relationDataAdd(req, res) {
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
if (!model) NcError.notFound('Table not found'); if (!model) NcError.notFound('Table not found');

1
packages/nocodb/tests/unit/rest/init/cleanupSakila.ts

@ -48,6 +48,7 @@ const cleanUpSakila = async (sakilaKnexClient) => {
const audits = sakilaProject && await Audit.projectAuditList(sakilaProject.id, {limit: 10}); const audits = sakilaProject && await Audit.projectAuditList(sakilaProject.id, {limit: 10});
if(audits?.length > 0 || global.touchedSakilaDb) { if(audits?.length > 0 || global.touchedSakilaDb) {
global.touchedSakilaDb = false;
return await resetAndSeedSakila(sakilaKnexClient); return await resetAndSeedSakila(sakilaKnexClient);
} }

157
packages/nocodb/tests/unit/rest/tests/tableRow.test.ts

@ -1498,21 +1498,6 @@ function tableTest() {
// } // }
// }) // })
it('Create list hm with invalid table id', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
)!;
const response = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/wrong-id/${rowId}/hm/${rentalListColumn.id}`)
.set('xc-auth', context.token)
.expect(404);
if(response.body['msg'] !== 'Table not found') {
throw new Error('Wrong error message');
}
})
it('Nested row list mm', async () => { it('Nested row list mm', async () => {
const rowId = 1; const rowId = 1;
const actorTable = await getTable({project: sakilaProject, name: 'actor'}); const actorTable = await getTable({project: sakilaProject, name: 'actor'});
@ -1571,6 +1556,148 @@ function tableTest() {
throw new Error('Wrong error message'); throw new Error('Wrong error message');
} }
}) })
it('Create hm relation with invalid table id', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
)!;
const refId = 1;
const response = await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/invalid-table-id/${rowId}/hm/${rentalListColumn.id}/${refId}`)
.set('xc-auth', context.token)
.expect(404);
global.touchedSakilaDb = true;
if(response.body['msg'] !== 'Table not found') {
throw new Error('Wrong error message');
}
})
it('Create list hm wrong column id', async () => {
const rowId = 1;
const refId = 1;
const response = await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/invalid-column/${refId}`)
.set('xc-auth', context.token)
.expect(404);
global.touchedSakilaDb = true;
if(response.body.msg !== "Column with id/name 'invalid-column' is not found") {
console.log(response.body)
throw new Error('Should error out');
}
})
// todo: mm create api does not error out in the case of existing ref row id
// it.only('Create list mm existing ref row id', async () => {
// const rowId = 1;
// const rentalListColumn = (await customerTable.getColumns()).find(
// (column) => column.title === 'Rental List'
// )!;
// const refId = 1;
// await request(context.app)
// .post(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`)
// .set('xc-auth', context.token)
// .expect(400)
// await request(context.app)
// .post(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`)
// .set('xc-auth', context.token)
// .expect(400)
// })
it('Create list hm', async () => {
const rowId = 1;
const rentalListColumn = (await customerTable.getColumns()).find(
(column) => column.title === 'Rental List'
)!;
const refId = 1;
const lisResponseBeforeUpdate = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`)
.set('xc-auth', context.token)
.expect(200);
await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}/${refId}`)
.set('xc-auth', context.token)
.expect(200);
global.touchedSakilaDb = true;
const lisResponseAfterUpdate = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}/${rowId}/hm/${rentalListColumn.id}`)
.set('xc-auth', context.token)
.expect(200);
if(lisResponseAfterUpdate.body.pageInfo.totalRows !== lisResponseBeforeUpdate.body.pageInfo.totalRows + 1) {
throw new Error('Wrong list length');
}
})
it('Create list mm wrong column id', async () => {
const rowId = 1;
const actorTable = await getTable({project: sakilaProject, name: 'actor'});
const refId = 1;
const response = await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/invalid-column/${refId}`)
.set('xc-auth', context.token)
.expect(404);
global.touchedSakilaDb = true;
if(response.body.msg !== "Column with id/name 'invalid-column' is not found") {
console.log(response.body)
throw new Error('Should error out');
}
})
it('Create list mm existing ref row id', async () => {
const rowId = 1;
const actorTable = await getTable({project: sakilaProject, name: 'actor'});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
)!;
const refId = 1;
await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`)
.set('xc-auth', context.token)
.expect(400);
global.touchedSakilaDb = true;
})
it('Create list mm', async () => {
const rowId = 1;
const actorTable = await getTable({project: sakilaProject, name: 'actor'});
const filmListColumn = (await actorTable.getColumns()).find(
(column) => column.title === 'Film List'
)!;
const refId = 2;
const lisResponseBeforeUpdate = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`)
.set('xc-auth', context.token)
.expect(200);
await request(context.app)
.post(`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}/${refId}`)
.set('xc-auth', context.token)
.expect(200);
global.touchedSakilaDb = true;
const lisResponseAfterUpdate = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/${actorTable.id}/${rowId}/mm/${filmListColumn.id}`)
.set('xc-auth', context.token)
.expect(200);
if(lisResponseAfterUpdate.body.pageInfo.totalRows !== lisResponseBeforeUpdate.body.pageInfo.totalRows + 1) {
throw new Error('Wrong list length');
}
})
} }
export default function () { export default function () {

Loading…
Cancel
Save