Browse Source

QR Code: add basic unit tests

pull/4142/head
Daniel Spaude 2 years ago
parent
commit
f9cf48f624
No known key found for this signature in database
GPG Key ID: 654A3D1FA4F35FFE
  1. 116
      packages/nocodb/tests/unit/factory/column.ts
  2. 2
      packages/nocodb/tests/unit/rest/index.test.ts
  3. 96
      packages/nocodb/tests/unit/rest/tests/columnTypeSpecific.test.ts

116
packages/nocodb/tests/unit/factory/column.ts

@ -9,35 +9,37 @@ import Project from '../../../src/lib/models/Project';
import View from '../../../src/lib/models/View';
import { isSqlite } from '../init/db';
const defaultColumns = function(context) {
const defaultColumns = function (context) {
return [
{
column_name: 'id',
title: 'Id',
uidt: 'ID',
},
{
column_name: 'title',
title: 'Title',
uidt: 'SingleLineText',
},
{
cdf: 'CURRENT_TIMESTAMP',
column_name: 'created_at',
title: 'CreatedAt',
dtxp: '',
dtxs: '',
uidt: 'DateTime',
},
{
cdf: isSqlite(context) ? 'CURRENT_TIMESTAMP': 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP',
column_name: 'updated_at',
title: 'UpdatedAt',
dtxp: '',
dtxs: '',
uidt: 'DateTime',
},
]
{
column_name: 'id',
title: 'Id',
uidt: 'ID',
},
{
column_name: 'title',
title: 'Title',
uidt: 'SingleLineText',
},
{
cdf: 'CURRENT_TIMESTAMP',
column_name: 'created_at',
title: 'CreatedAt',
dtxp: '',
dtxs: '',
uidt: 'DateTime',
},
{
cdf: isSqlite(context)
? 'CURRENT_TIMESTAMP'
: 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP',
column_name: 'updated_at',
title: 'UpdatedAt',
dtxp: '',
dtxs: '',
uidt: 'DateTime',
},
];
};
const createColumn = async (context, table, columnAttr) => {
@ -152,6 +154,38 @@ const createLookupColumn = async (
return lookupColumn;
};
const createQrCodeColumn = async (
context,
{
title,
table,
referencedQrValueTableColumnTitle,
}: {
title: string;
table: Model;
referencedQrValueTableColumnTitle: string;
}
) => {
const referencedQrValueTableColumnId = await table
.getColumns()
.then(
(cols) =>
cols.find(
(column) => column.title == referencedQrValueTableColumnTitle
)['id']
);
const qrCodeColumn = await createColumn(context, table, {
title: title,
uidt: UITypes.QrCode,
// TODO: check - table_name needed?
table_name: table.table_name,
column_name: title,
fk_qr_value_column_id: referencedQrValueTableColumnId,
});
return qrCodeColumn;
};
const createLtarColumn = async (
context,
{
@ -178,26 +212,30 @@ const createLtarColumn = async (
return ltarColumn;
};
const updateViewColumn = async (context, {view, column, attr}: {column: Column, view: View, attr: any}) => {
const updateViewColumn = async (
context,
{ view, column, attr }: { column: Column; view: View; attr: any }
) => {
const res = await request(context.app)
.patch(`/api/v1/db/meta/views/${view.id}/columns/${column.id}`)
.set('xc-auth', context.token)
.send({
...attr,
});
.patch(`/api/v1/db/meta/views/${view.id}/columns/${column.id}`)
.set('xc-auth', context.token)
.send({
...attr,
});
const updatedColumn: FormViewColumn | GridViewColumn | GalleryViewColumn = (await view.getColumns()).find(
(column) => column.id === column.id
)!;
const updatedColumn: FormViewColumn | GridViewColumn | GalleryViewColumn = (
await view.getColumns()
).find((column) => column.id === column.id)!;
return updatedColumn;
}
};
export {
defaultColumns,
createColumn,
createQrCodeColumn,
createRollupColumn,
createLookupColumn,
createLtarColumn,
updateViewColumn
updateViewColumn,
};

2
packages/nocodb/tests/unit/rest/index.test.ts

@ -1,6 +1,7 @@
import 'mocha';
import authTests from './tests/auth.test';
import projectTests from './tests/project.test';
import columnTypeSpecificTests from './tests/columnTypeSpecific.test';
import tableTests from './tests/table.test';
import tableRowTests from './tests/tableRow.test';
import viewRowTests from './tests/viewRow.test';
@ -11,6 +12,7 @@ function restTests() {
tableTests();
tableRowTests();
viewRowTests();
columnTypeSpecificTests();
}
export default function () {

96
packages/nocodb/tests/unit/rest/tests/columnTypeSpecific.test.ts

@ -0,0 +1,96 @@
import 'mocha';
import init from '../../init';
import { createProject, createSakilaProject } from '../../factory/project';
import request from 'supertest';
import { ColumnType, UITypes } from 'nocodb-sdk';
import { createQrCodeColumn, createColumn } from '../../factory/column';
import { createTable, getTable } from '../../factory/table';
import Model from '../../../../src/lib/models/Model';
import Project from '../../../../src/lib/models/Project';
import { expect } from 'chai';
import Column from '../../../../src/lib/models/Column';
import { table } from 'console';
import { title } from 'process';
function columnTypeSpecificTests() {
let context;
let project: Project;
let sakilaProject: Project;
let customerTable: Model;
let qrValueReferenceColumn: Column;
const qrValueReferenceColumnTitle = 'Qr Value Column';
const qrCodeReferenceColumnTitle = 'Qr Code Column';
beforeEach(async function () {
context = await init();
sakilaProject = await createSakilaProject(context);
project = await createProject(context);
customerTable = await getTable({
project: sakilaProject,
name: 'customer',
});
qrValueReferenceColumn = await createColumn(context, customerTable, {
title: qrValueReferenceColumnTitle,
uidt: UITypes.SingleLineText,
table_name: customerTable.table_name,
column_name: title,
});
});
describe('Qr Code Column', () => {
describe('adding a QR code column which references the column ', async () => {
beforeEach(async function () {
await createQrCodeColumn(context, {
title: qrCodeReferenceColumnTitle,
table: customerTable,
referencedQrValueTableColumnTitle: qrValueReferenceColumnTitle,
});
});
it('delivers the same cell values as the referenced column', async () => {
const resp = await request(context.app)
.get(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}`)
.set('xc-auth', context.token)
.expect(200);
expect(resp.body.list[0][qrValueReferenceColumnTitle]).to.eql(
resp.body.list[0][qrCodeReferenceColumnTitle]
);
expect(resp.body.list.map((row) => row[qrValueReferenceColumnTitle])).to.eql(
resp.body.list.map((row) => row[qrCodeReferenceColumnTitle])
);
});
it('gets deleted if the referenced column gets deleted', async () => {
// delete referenced value column
const columnsBeforeReferencedColumnDeleted =
await customerTable.getColumns();
expect(
columnsBeforeReferencedColumnDeleted.some(
(col) => col['title'] === qrCodeReferenceColumnTitle
)
).to.eq(true);
const response = await request(context.app)
.delete(`/api/v1/db/meta/columns/${qrValueReferenceColumn.id}`)
.set('xc-auth', context.token)
.send({});
const columnsAfterReferencedColumnDeleted =
await customerTable.getColumns();
expect(
columnsAfterReferencedColumnDeleted.some(
(col) => col['title'] === qrCodeReferenceColumnTitle
)
).to.eq(false);
});
});
});
}
export default function () {
describe('Column types specific behavior', columnTypeSpecificTests);
}
Loading…
Cancel
Save