hierarchical tag tests

This commit is contained in:
Jorge Montejo 2025-10-04 12:53:31 +02:00
parent 09088678ab
commit 76374dc0fe

View file

@ -1,14 +1,11 @@
import { Kysely } from "kysely"; import { Kysely } from "kysely";
import { JobStatus } from "src/enum"; import { JobStatus } from "src/enum";
import { AccessRepository } from "src/repositories/access.repository"; import { AccessRepository } from "src/repositories/access.repository";
import { EventRepository } from "src/repositories/event.repository";
import { LoggingRepository } from "src/repositories/logging.repository"; import { LoggingRepository } from "src/repositories/logging.repository";
import { StorageRepository } from "src/repositories/storage.repository";
import { TagRepository } from "src/repositories/tag.repository"; import { TagRepository } from "src/repositories/tag.repository";
import { DB } from "src/schema"; import { DB } from "src/schema";
import { TagService } from "src/services/tag.service"; import { TagService } from "src/services/tag.service";
import { newMediumService } from "test/medium.factory"; import { newMediumService } from "test/medium.factory";
import { factory } from "test/small.factory";
import { getKyselyDB } from "test/utils"; import { getKyselyDB } from "test/utils";
let defaultDatabase: Kysely<DB>; let defaultDatabase: Kysely<DB>;
@ -25,10 +22,6 @@ beforeAll(async () => {
defaultDatabase = await getKyselyDB(); defaultDatabase = await getKyselyDB();
}); });
// single tag exists, connected to one asset, and is not deleted
// hierarchical tag exists, and the parent is connected to an asset, and the child is deleted
// hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted
describe(TagService.name, () => { describe(TagService.name, () => {
describe('deleteEmptyTags', () => { describe('deleteEmptyTags', () => {
it('single tag exists, not connected to any assets, and is deleted', async () => { it('single tag exists, not connected to any assets, and is deleted', async () => {
@ -56,5 +49,39 @@ describe(TagService.name, () => {
await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id })); await expect(tagRepo.getByValue(user.id, 'tag-1')).resolves.toEqual(expect.objectContaining({ id: tag.id }));
}) })
it('hierarchical tag exists, and the parent is connected to an asset, and the child is deleted', async () => {
const { sut, ctx } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id });
const { tag: parentTag } = await ctx.newTag({ userId: user.id, value: 'parent' });
const { tag: childrenTag } = await ctx.newTag({ userId: user.id, value: 'child', parentId: parentTag.id });
const tagRepo = ctx.get(TagRepository);
await ctx.newTagAsset({ tagIds: [parentTag.id], assetIds: [asset.id] });
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toBeUndefined();
})
it('hierarchical tag exists, and only the child is connected to an asset, and nothing is deleted', async () => {
const { sut, ctx } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id });
const { tag: parentTag } = await ctx.newTag({ userId: user.id, value: 'parent' });
const { tag: childrenTag } = await ctx.newTag({ userId: user.id, value: 'child', parentId: parentTag.id });
const tagRepo = ctx.get(TagRepository);
await ctx.newTagAsset({ tagIds: [childrenTag.id], assetIds: [asset.id] });
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
await expect(sut.handleTagCleanup()).resolves.toBe(JobStatus.Success);
await expect(tagRepo.getByValue(user.id, 'parent')).resolves.toEqual(expect.objectContaining({ id: parentTag.id }));
await expect(tagRepo.getByValue(user.id, 'child')).resolves.toEqual(expect.objectContaining({ id: childrenTag.id }));
})
}) })
}) })