ChengLei Shao 0e0eb6432e
feat: provide the underscored option for the database (#1366)
* feat: underscored options

* feat: underscored using hook

* feat: database underscored options

* feat: underscored env

* fix: collectionExistsInDb

* fix: test

* fix: nocobase install

* fix: test

* fix: belongsTo association

* fix: test of underscored

* chore: console.log

* fix: list action test

* fix: dump test

* chore: snakeCase algo

* fix: underscored field create

* fix: underscored env

* fix(acl): custom appends merge strategy (#1416)

* Update index.md

* fix(plugin-workflow): use promise to request (#1426)

* Update index.md

* Update collection.md

* Update index.md

* Update index.md

* Update collection.md

* Update field.md

* Update repository.md

* Update has-one-repository.md

* Update has-many-repository.md

* Update belongs-to-many-repository.md

* Update index.md

* chore: translate 'Add tab' in page header (#1424)

* fix: test

* fix: workflow test

* fix: underscored with inherits

* fix: underscored test

* fix:  process.env.DB_UNDERSCORED

* fix: process.env.DB_UNDERSCORED === 'true'

* fix: test

* fix: pg test

* fix: underscored table name

* feat: tableName & fieldName conflict check

* fix: test

* fix: underscored index

* fix: update field unique index

* fix: sync default value

* fix: collection manager create field

* chore: field sync

* fix: pg test

* chore: test

* fix: test

* chore: default constraint name

* chore: syncUniqueIndex

* feat: field destory before check

* feat: field type check

* fix: test

* fix: test

* fix: improve

* fix: should destroy when fields refer to the same field

* fix: acl meta with underscored

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-02-13 21:38:47 +08:00

133 lines
3.3 KiB
TypeScript

import { MockServer } from '@nocobase/test';
import { createApp } from '..';
describe('field indexes', () => {
let app: MockServer;
let agent;
beforeEach(async () => {
app = await createApp();
agent = app.agent();
await agent.resource('collections').create({
values: {
name: 'test1',
},
});
});
afterEach(async () => {
await app.destroy();
});
it('create unique constraint after added duplicated records', async () => {
const tableName = 'test1';
// create an field with unique constraint
const field = await agent.resource('collections.fields', tableName).create({
values: {
name: 'title',
type: 'string',
},
});
// create a record
const response1 = await agent.resource(tableName).create({
values: { title: 't1' },
});
// create another same record
const response2 = await agent.resource(tableName).create({
values: { title: 't1' },
});
const response3 = await agent.resource('fields').update({
filterByTk: field.body.data.key,
values: {
unique: true,
},
});
console.log(response3.body);
expect(response3.status).toBe(400);
const response4 = await agent.resource(tableName).create({
values: { title: 't1' },
});
expect(response4.status).toBe(200);
expect(response4.body.data.title).toBe('t1');
});
it('field value cannot be duplicated with unique index', async () => {
const tableName = 'test1';
// create a field with unique constraint
const field = await agent.resource('collections.fields', tableName).create({
values: {
name: 'title',
type: 'string',
unique: true,
},
});
expect(field.status).toBe(200);
// create a record
const response1 = await agent.resource(tableName).create({
values: {
title: 't1',
},
});
expect(response1.status).toBe(200);
expect(response1.body.data.title).toBe('t1');
// create another record with the same value on unique field should fail
const response2 = await agent.resource(tableName).create({
values: {
title: 't1',
},
});
expect(response2.status).toBe(400);
// update field to remove unique constraint
await agent.resource('fields').update({
filterByTk: field.body.data.key,
values: {
unique: false,
},
});
// create another record with the same value on unique field should be ok
const response3 = await agent.resource(tableName).create({
values: {
title: 't1',
},
});
expect(response3.status).toBe(200);
expect(response3.body.data.title).toBe('t1');
// update field to add unique constraint should fail because of duplicated records
const response4 = await agent.resource('fields').update({
filterByTk: field.body.data.key,
values: {
unique: true,
},
});
expect(response4.status).toBe(400);
// remove a duplicated record
await agent.resource(tableName).destroy({
filterByTk: response3.body.data.id,
});
// update field to add unique constraint should be ok
const response6 = await agent.resource('fields').update({
filterByTk: field.body.data.key,
values: {
unique: true,
},
});
expect(response6.status).toBe(200);
});
});