mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
* db test * associated list action * associated list action * fix belongs to many repository test * create action * update action * add update & destroy has one * get action * add action * set action * remove action * toggle action * chore: code import * add sort field mutex * change field mutex position * feat: handle sort field scope change * feat: sort actions * fix: add action * rename sort action to move action * more actions params * feat: repository destroy with filter and filterByPK * feat: hasmany repository destroy with filter and filterByPK * feat: belongsToMany repository destroy with filter and filterByPK * fix: actions tests lock error * feat: code cleanup Co-authored-by: chenos <chenlinxh@gmail.com>
118 lines
2.3 KiB
TypeScript
118 lines
2.3 KiB
TypeScript
import { mockDatabase } from '../index';
|
|
import { Collection } from '../../collection';
|
|
|
|
describe('destroy', () => {
|
|
let db;
|
|
let User: Collection;
|
|
let Post: Collection;
|
|
|
|
beforeEach(async () => {
|
|
db = mockDatabase();
|
|
User = db.collection({
|
|
name: 'users',
|
|
fields: [
|
|
{ type: 'string', name: 'name' },
|
|
{ type: 'hasMany', name: 'posts' },
|
|
],
|
|
});
|
|
|
|
Post = db.collection({
|
|
name: 'posts',
|
|
fields: [
|
|
{ type: 'string', name: 'title' },
|
|
{ type: 'string', name: 'status' },
|
|
{ type: 'belongsTo', name: 'user' },
|
|
],
|
|
});
|
|
await db.sync();
|
|
});
|
|
|
|
test('destroy with filter and filterByPk', async () => {
|
|
const p1 = await Post.repository.create({
|
|
values: {
|
|
name: 'u1',
|
|
status: 'published',
|
|
},
|
|
});
|
|
|
|
await Post.repository.destroy({
|
|
filterByPk: p1.get('id') as number,
|
|
filter: {
|
|
status: 'draft',
|
|
},
|
|
});
|
|
|
|
expect(await Post.repository.count()).toEqual(1);
|
|
});
|
|
|
|
test('destroy all', async () => {
|
|
await User.repository.create({
|
|
values: {
|
|
name: 'u1',
|
|
posts: [{ title: 'u1p1' }],
|
|
},
|
|
});
|
|
|
|
await User.repository.destroy();
|
|
expect(await User.repository.count()).toEqual(1);
|
|
await User.repository.destroy({ truncate: true });
|
|
expect(await User.repository.count()).toEqual(0);
|
|
});
|
|
|
|
test('destroy with filter', async () => {
|
|
await User.repository.createMany({
|
|
records: [
|
|
{
|
|
name: 'u1',
|
|
},
|
|
{
|
|
name: 'u3',
|
|
},
|
|
{
|
|
name: 'u2',
|
|
},
|
|
],
|
|
});
|
|
|
|
await User.repository.destroy({
|
|
filter: {
|
|
name: 'u1',
|
|
},
|
|
});
|
|
|
|
expect(
|
|
await User.repository.findOne({
|
|
filter: {
|
|
name: 'u1',
|
|
},
|
|
}),
|
|
).toBeNull();
|
|
expect(await User.repository.count()).toEqual(2);
|
|
});
|
|
|
|
test('destroy with filterByPK', async () => {
|
|
await User.repository.createMany({
|
|
records: [
|
|
{
|
|
name: 'u1',
|
|
},
|
|
{
|
|
name: 'u3',
|
|
},
|
|
{
|
|
name: 'u2',
|
|
},
|
|
],
|
|
});
|
|
|
|
const u2 = await User.repository.findOne({
|
|
filter: {
|
|
name: 'u2',
|
|
},
|
|
});
|
|
|
|
await User.repository.destroy(u2['id']);
|
|
expect(await User.repository.count()).toEqual(2);
|
|
});
|
|
});
|