Files
outline/server/commands/teamPermanentDeleter.test.ts
Apoorv Mishra 0c51bfb899 perf: reduce memory usage upon running server tests (#3949)
* perf: reduce memory usage upon running server tests

* perf: plug leaks in server/routes

* perf: plug leaks in server/scripts

* perf: plug leaks in server/policies

* perf: plug leaks in server/models

* perf: plug leaks in server/middlewares

* perf: plug leaks in server/commands

* fix: missing await on db.flush

* perf: plug leaks in server/queues

* chore: remove unused legacy funcs

* fix: await on db.flush

* perf: await on GC to run in between tests

* fix: remove db refs

* fix: revert embeds

* perf: plug leaks in shared/i18n
2022-08-11 21:39:17 +05:30

114 lines
2.7 KiB
TypeScript

import { subDays } from "date-fns";
import { Attachment, User, Document, Collection, Team } from "@server/models";
import {
buildAttachment,
buildUser,
buildTeam,
buildDocument,
} from "@server/test/factories";
import { getTestDatabase } from "@server/test/support";
import teamPermanentDeleter from "./teamPermanentDeleter";
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("teamPermanentDeleter", () => {
it("should destroy related data", async () => {
const team = await buildTeam({
deletedAt: subDays(new Date(), 90),
});
const user = await buildUser({
teamId: team.id,
});
await buildDocument({
teamId: team.id,
userId: user.id,
});
await teamPermanentDeleter(team);
expect(await Team.count()).toEqual(0);
expect(await User.count()).toEqual(0);
expect(
await Document.unscoped().count({
paranoid: false,
})
).toEqual(0);
expect(
await Collection.unscoped().count({
paranoid: false,
})
).toEqual(0);
});
it("should not destroy unrelated data", async () => {
const team = await buildTeam({
deletedAt: subDays(new Date(), 90),
});
await buildUser();
await buildTeam();
await buildDocument();
await teamPermanentDeleter(team);
expect(await Team.count()).toEqual(4); // each build command creates a team
expect(await User.count()).toEqual(2);
expect(
await Document.unscoped().count({
paranoid: false,
})
).toEqual(1);
expect(
await Collection.unscoped().count({
paranoid: false,
})
).toEqual(1);
});
it("should destroy attachments", async () => {
const team = await buildTeam({
deletedAt: subDays(new Date(), 90),
});
const user = await buildUser({
teamId: team.id,
});
const document = await buildDocument({
teamId: team.id,
userId: user.id,
});
await buildAttachment({
teamId: document.teamId,
documentId: document.id,
});
await teamPermanentDeleter(team);
expect(await Team.count()).toEqual(0);
expect(await User.count()).toEqual(0);
expect(await Attachment.count()).toEqual(0);
expect(
await Document.unscoped().count({
paranoid: false,
})
).toEqual(0);
expect(
await Collection.unscoped().count({
paranoid: false,
})
).toEqual(0);
});
it("should error when trying to destroy undeleted team", async () => {
const team = await buildTeam();
let error;
try {
await teamPermanentDeleter(team);
} catch (err) {
error = err.message;
}
expect(error).toEqual(
`Cannot permanently delete ${team.id} team. Please delete it and try again.`
);
});
});