Skip to content

Commit f391ff7

Browse files
committed
✅ test(moderations): add comprehensive repository function tests
- Add test coverage for GetModeration, GetModerationWithDetails, GetModerationForEmail - Add test coverage for GetDuplicateModerations and GetModerationsList - Test success paths, error handling, and edge cases - Fix database transaction usage in GetModerationsList - Use proper beforeEach(empty_database) pattern for test isolation
1 parent b57177f commit f391ff7

File tree

7 files changed

+616
-15
lines changed

7 files changed

+616
-15
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
3+
import { create_troll_organization } from "@~/identite-proconnect.database/seed/troll";
4+
import {
5+
create_adora_pony_moderation,
6+
create_adora_pony_user,
7+
create_pink_diamond_user,
8+
create_unicorn_organization,
9+
} from "@~/identite-proconnect.database/seed/unicorn";
10+
import {
11+
empty_database,
12+
migrate,
13+
pg,
14+
} from "@~/identite-proconnect.database/testing";
15+
import { beforeAll, beforeEach, expect, setSystemTime, test } from "bun:test";
16+
import { GetDuplicateModerations } from "./GetDuplicateModerations";
17+
18+
//
19+
20+
beforeAll(migrate);
21+
beforeEach(empty_database);
22+
23+
beforeAll(() => {
24+
setSystemTime(new Date("2222-01-01T00:00:00.000Z"));
25+
});
26+
27+
//
28+
29+
test("get duplicate moderations for the same user and organization", async () => {
30+
const organization_id = await create_unicorn_organization(pg);
31+
const user_id = await create_adora_pony_user(pg);
32+
33+
// Create multiple moderations for the same user and organization
34+
const moderation_id_1 = await create_adora_pony_moderation(pg, {
35+
type: "type_a",
36+
});
37+
const moderation_id_2 = await create_adora_pony_moderation(pg, {
38+
type: "type_b",
39+
});
40+
const moderation_id_3 = await create_adora_pony_moderation(pg, {
41+
type: "type_c",
42+
});
43+
44+
const get_duplicate_moderations = GetDuplicateModerations(pg);
45+
const moderations = await get_duplicate_moderations({
46+
organization_id,
47+
user_id,
48+
});
49+
50+
expect(moderations).toHaveLength(3);
51+
expect(moderations.map((m) => m.id)).toEqual([
52+
moderation_id_1,
53+
moderation_id_2,
54+
moderation_id_3,
55+
]);
56+
57+
// Verify they are ordered by created_at (ascending)
58+
const created_dates = moderations.map((m) => m.created_at);
59+
expect(created_dates).toEqual([...created_dates].sort());
60+
});
61+
62+
test("returns empty array when no moderations found", async () => {
63+
const get_duplicate_moderations = GetDuplicateModerations(pg);
64+
const moderations = await get_duplicate_moderations({
65+
organization_id: 999999,
66+
user_id: 999999,
67+
});
68+
69+
expect(moderations).toEqual([]);
70+
});
71+
72+
test("filters by specific user and organization", async () => {
73+
const org1_id = await create_unicorn_organization(pg);
74+
await create_troll_organization(pg);
75+
const user1_id = await create_adora_pony_user(pg);
76+
await create_pink_diamond_user(pg);
77+
78+
await create_adora_pony_moderation(pg, { type: "for_user1_org1" });
79+
80+
const get_duplicate_moderations = GetDuplicateModerations(pg);
81+
82+
// Should only return moderations for specific user+org combination
83+
const moderations = await get_duplicate_moderations({
84+
organization_id: org1_id,
85+
user_id: user1_id,
86+
});
87+
88+
expect(moderations).toMatchInlineSnapshot(`
89+
[
90+
{
91+
"comment": null,
92+
"created_at": "2222-01-01 00:00:00+00",
93+
"id": 1,
94+
"moderated_at": null,
95+
"moderated_by": null,
96+
"organization_id": 1,
97+
"ticket_id": null,
98+
"type": "for_user1_org1",
99+
"user_id": 1,
100+
},
101+
]
102+
`);
103+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
3+
import {
4+
create_adora_pony_moderation,
5+
create_adora_pony_user,
6+
create_unicorn_organization,
7+
} from "@~/identite-proconnect.database/seed/unicorn";
8+
import {
9+
empty_database,
10+
migrate,
11+
pg,
12+
} from "@~/identite-proconnect.database/testing";
13+
import { beforeAll, beforeEach, expect, setSystemTime, test } from "bun:test";
14+
import { GetModeration } from "./GetModeration";
15+
16+
//
17+
18+
beforeAll(migrate);
19+
beforeEach(empty_database);
20+
21+
beforeAll(() => {
22+
setSystemTime(new Date("2222-01-01T00:00:00.000Z"));
23+
});
24+
25+
//
26+
27+
test("get a moderation with minimal fields", async () => {
28+
await create_unicorn_organization(pg);
29+
await create_adora_pony_user(pg);
30+
const moderation_id = await create_adora_pony_moderation(pg, { type: "" });
31+
32+
const get_moderation = GetModeration(pg);
33+
const moderation = await get_moderation(moderation_id);
34+
35+
expect(moderation).toMatchInlineSnapshot(`
36+
{
37+
"comment": null,
38+
"id": 1,
39+
"organization_id": 1,
40+
"ticket_id": null,
41+
"user": {
42+
"email": "adora.pony@unicorn.xyz",
43+
},
44+
"user_id": 1,
45+
}
46+
`);
47+
});
48+
49+
test("throws NotFoundError when moderation does not exist", async () => {
50+
const get_moderation = GetModeration(pg);
51+
52+
await expect(get_moderation(999999)).rejects.toThrow("Moderation not found.");
53+
});

sources/moderations/repository/src/GetModerationById.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import {
1010
migrate,
1111
pg,
1212
} from "@~/identite-proconnect.database/testing";
13-
import { beforeAll, expect, setSystemTime, test } from "bun:test";
13+
import { beforeAll, beforeEach, expect, setSystemTime, test } from "bun:test";
1414
import { GetModerationById } from "./GetModerationById";
1515

1616
//
1717

1818
beforeAll(migrate);
19-
beforeAll(empty_database);
19+
beforeEach(empty_database);
2020

2121
beforeAll(() => {
2222
setSystemTime(new Date("2222-01-01T00:00:00.000Z"));
2323
});
2424

2525
//
2626

27-
test("get a moderation ", async () => {
28-
const organization_id = await create_unicorn_organization(pg);
29-
const user_id = await create_adora_pony_user(pg);
27+
test("get a moderation", async () => {
28+
await create_unicorn_organization(pg);
29+
await create_adora_pony_user(pg);
3030
const moderation_id = await create_adora_pony_moderation(pg, { type: "" });
3131

3232
const get_moderation_by_id = GetModerationById({ pg });
@@ -40,11 +40,13 @@ test("get a moderation ", async () => {
4040
},
4141
});
4242

43-
expect(moderation).toEqual({
44-
created_at: "2222-01-01 00:00:00+00",
45-
id: moderation_id,
46-
moderated_at: null,
47-
organization_id,
48-
user_id,
49-
});
43+
expect(moderation).toMatchInlineSnapshot(`
44+
{
45+
"created_at": "2222-01-01 00:00:00+00",
46+
"id": 1,
47+
"moderated_at": null,
48+
"organization_id": 1,
49+
"user_id": 1,
50+
}
51+
`);
5052
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
3+
import {
4+
create_adora_pony_moderation,
5+
create_adora_pony_user,
6+
create_unicorn_organization,
7+
} from "@~/identite-proconnect.database/seed/unicorn";
8+
import {
9+
empty_database,
10+
migrate,
11+
pg,
12+
} from "@~/identite-proconnect.database/testing";
13+
import { beforeAll, beforeEach, expect, setSystemTime, test } from "bun:test";
14+
import { GetModerationForEmail } from "./GetModerationForEmail";
15+
16+
//
17+
18+
beforeAll(migrate);
19+
beforeEach(empty_database);
20+
21+
beforeAll(() => {
22+
setSystemTime(new Date("2222-01-01T00:00:00.000Z"));
23+
});
24+
25+
//
26+
27+
test("get a moderation with minimal email context data", async () => {
28+
await create_unicorn_organization(pg);
29+
await create_adora_pony_user(pg);
30+
const moderation_id = await create_adora_pony_moderation(pg, {
31+
type: "",
32+
ticket_id: "test-ticket-123",
33+
});
34+
35+
const get_moderation_for_email = GetModerationForEmail(pg);
36+
const moderation = await get_moderation_for_email(moderation_id);
37+
38+
expect(moderation).toMatchInlineSnapshot(`
39+
{
40+
"ticket_id": "test-ticket-123",
41+
"user": {
42+
"email": "adora.pony@unicorn.xyz",
43+
},
44+
}
45+
`);
46+
});
47+
48+
test("handles null ticket_id", async () => {
49+
await create_unicorn_organization(pg);
50+
await create_adora_pony_user(pg);
51+
const moderation_id = await create_adora_pony_moderation(pg, { type: "" });
52+
53+
const get_moderation_for_email = GetModerationForEmail(pg);
54+
const moderation = await get_moderation_for_email(moderation_id);
55+
56+
expect(moderation).toMatchInlineSnapshot(`
57+
{
58+
"ticket_id": null,
59+
"user": {
60+
"email": "adora.pony@unicorn.xyz",
61+
},
62+
}
63+
`);
64+
});
65+
66+
test("throws NotFoundError when moderation does not exist", async () => {
67+
const get_moderation_for_email = GetModerationForEmail(pg);
68+
69+
await expect(get_moderation_for_email(999999)).rejects.toThrow(
70+
"Moderation not found",
71+
);
72+
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
3+
import {
4+
create_adora_pony_moderation,
5+
create_adora_pony_user,
6+
create_unicorn_organization,
7+
} from "@~/identite-proconnect.database/seed/unicorn";
8+
import {
9+
empty_database,
10+
migrate,
11+
pg,
12+
} from "@~/identite-proconnect.database/testing";
13+
import { beforeAll, expect, setSystemTime, test } from "bun:test";
14+
import { GetModerationWithDetails } from "./GetModerationWithDetails";
15+
16+
//
17+
18+
beforeAll(migrate);
19+
beforeAll(empty_database);
20+
21+
beforeAll(() => {
22+
setSystemTime(new Date("2222-01-01T00:00:00.000Z"));
23+
});
24+
25+
//
26+
27+
test("get a moderation with full organization and user details", async () => {
28+
const organization_id = await create_unicorn_organization(pg);
29+
const user_id = await create_adora_pony_user(pg);
30+
const moderation_id = await create_adora_pony_moderation(pg, { type: "" });
31+
32+
const get_moderation_with_details = GetModerationWithDetails(pg);
33+
const moderation = await get_moderation_with_details(moderation_id);
34+
35+
expect(moderation).toMatchInlineSnapshot(`
36+
{
37+
"comment": null,
38+
"created_at": "2222-01-01 00:00:00+00",
39+
"id": 1,
40+
"moderated_at": null,
41+
"moderated_by": null,
42+
"organization": {
43+
"cached_activite_principale": null,
44+
"cached_adresse": null,
45+
"cached_code_postal": null,
46+
"cached_est_active": null,
47+
"cached_etat_administratif": null,
48+
"cached_libelle": "🦄 libelle",
49+
"cached_libelle_activite_principale": null,
50+
"cached_libelle_categorie_juridique": null,
51+
"cached_libelle_tranche_effectif": null,
52+
"cached_nom_complet": null,
53+
"cached_tranche_effectifs": null,
54+
"created_at": "1970-01-01T00:00:00+00:00",
55+
"id": 1,
56+
"siret": "🦄 siret",
57+
"updated_at": "1970-01-01T00:00:00+00:00",
58+
},
59+
"organization_id": 1,
60+
"ticket_id": null,
61+
"type": "",
62+
"user": {
63+
"created_at": "2222-01-01T00:00:00+00:00",
64+
"email": "adora.pony@unicorn.xyz",
65+
"family_name": "Pony",
66+
"given_name": "Adora",
67+
"id": 1,
68+
"job": null,
69+
"last_sign_in_at": null,
70+
"phone_number": null,
71+
"sign_in_count": 0,
72+
},
73+
"user_id": 1,
74+
}
75+
`);
76+
});
77+
78+
test("throws NotFoundError when moderation does not exist", async () => {
79+
const get_moderation_with_details = GetModerationWithDetails(pg);
80+
81+
await expect(get_moderation_with_details(999999)).rejects.toThrow(
82+
"Moderation not found.",
83+
);
84+
});

0 commit comments

Comments
 (0)