tachybase_todo/packages/database/src/__tests__/fields/string-field.test.ts
ChengLei Shao 99bfd75776
fix: test with database (#193)
* fix: ui schema test

* fix: sqlite array query

* fix: acl test

* fix: plugin-users test

* fix: database test with postgres

* fix: test with db.getTablePrefix

* fix: test with mysql database

* fix: test with sqlite database

* fix: test with  mysql

* fix: test order with mysql

* chore: test clean database

* chore: mockServer clean

* chore: app cleanDb

* chore: plugin-users cleanDb
2022-02-15 22:32:02 +08:00

78 lines
1.8 KiB
TypeScript

import { Database } from '../../database';
import { mockDatabase } from '../';
describe('string field', () => {
let db: Database;
beforeEach(async () => {
db = mockDatabase();
});
afterEach(async () => {
await db.close();
});
it('define', async () => {
const Test = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name' }],
});
await db.sync();
expect(Test.model.rawAttributes['name']).toBeDefined();
const model = await Test.model.create({
name: 'abc',
});
expect(model.toJSON()).toMatchObject({
name: 'abc',
});
});
it('set', async () => {
const Test = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name1' }],
});
await db.sync();
Test.addField('name2', { type: 'string' });
await db.sync({
alter: true,
});
expect(Test.model.rawAttributes['name1']).toBeDefined();
expect(Test.model.rawAttributes['name2']).toBeDefined();
const model = await Test.model.create({
name1: 'a1',
name2: 'a2',
});
expect(model.toJSON()).toMatchObject({
name1: 'a1',
name2: 'a2',
});
});
it('model hook', async () => {
const collection = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name' }],
});
await db.sync();
collection.model.beforeCreate((model) => {
const changed = model.changed();
for (const name of changed || []) {
model.set(name, `${model.get(name)}111`);
}
});
collection.addField('name2', { type: 'string' });
await db.sync({
alter: true,
});
const model = await collection.model.create({
name: 'n1',
name2: 'n2',
});
expect(model.toJSON()).toMatchObject({
name: 'n1111',
name2: 'n2111',
});
});
});