Skip to content

Commit 0524829

Browse files
committed
test(facility): abstract raw db inserts into standard integration helpers
1 parent e3ea6e2 commit 0524829

2 files changed

Lines changed: 77 additions & 70 deletions

File tree

test/integration/facility.test.ts

Lines changed: 44 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ import { createFacility } from "@/modules/facility/service.js";
1717
import {
1818
createOrganizerTestSetup,
1919
createTestEventBody,
20+
createTestFacilityProvider,
21+
createTestFacilityType,
22+
createTestOrganization,
23+
createTestOrganizationType,
2024
createTestVenue,
25+
createTestVenueAllotment,
26+
createTestVenueType,
2127
getWorkflowForEvent,
2228
setupWorkflowTestEnvironment,
2329
} from "./integration-test-helpers.js";
@@ -29,43 +35,22 @@ describe("Facility Integration Tests", () => {
2935
let validVenueTypeId: number;
3036

3137
beforeAll(async () => {
32-
const [fType] = await db
33-
.insert(schema.facilityType)
34-
.values({ name: `TestFacilityType-${nanoid()}` })
35-
.returning();
36-
assert(fType);
38+
const fType = await createTestFacilityType();
3739
validFacilityTypeId = fType.id;
3840

39-
const [orgType] = await db
40-
.insert(schema.organizationType)
41-
.values({ name: `org-type-${nanoid()}` })
42-
.returning();
43-
assert(orgType);
44-
const [org] = await db
45-
.insert(schema.organization)
46-
.values({ name: `org-${nanoid()}`, organizationTypeId: orgType.id })
47-
.returning();
48-
assert(org);
41+
const orgType = await createTestOrganizationType();
42+
const org = await createTestOrganization({ organizationTypeId: orgType.id });
4943
validOrgId = org.id;
5044

51-
const [vType] = await db
52-
.insert(schema.venueType)
53-
.values({ name: `venue-type-${nanoid()}` })
54-
.returning();
55-
assert(vType);
45+
const vType = await createTestVenueType();
5646
validVenueTypeId = vType.id;
57-
assert(vType);
58-
const [venue] = await db
59-
.insert(schema.venue)
60-
.values({
61-
name: `venue-${nanoid()}`,
62-
venueTypeId: vType.id,
63-
accessLevel: "public",
64-
isAvailable: true,
65-
maxCapacity: 100,
66-
})
67-
.returning();
68-
assert(venue);
47+
const venue = await createTestVenue({
48+
venueTypeId: vType.id,
49+
name: `venue-${nanoid()}`,
50+
accessLevel: "public",
51+
isAvailable: true,
52+
maxCapacity: 100,
53+
});
6954
validVenueId = venue.id;
7055
});
7156

@@ -149,11 +134,7 @@ describe("Facility Integration Tests", () => {
149134
});
150135

151136
test("[Edge Case] facility becomes unfetchable via findFacilityById once its facilityType is soft-deleted", async () => {
152-
const [tempType] = await db
153-
.insert(schema.facilityType)
154-
.values({ name: `TempType-${nanoid()}` })
155-
.returning();
156-
assert(tempType);
137+
const tempType = await createTestFacilityType({ name: `TempType-${nanoid()}` });
157138
const facility = await createFacility({
158139
name: `TempFac-${nanoid()}`,
159140
typeId: tempType.id,
@@ -205,7 +186,7 @@ describe("Facility Integration Tests", () => {
205186
overlapPolicy: "shared",
206187
});
207188

208-
await db.insert(schema.facilityProvider).values({
189+
await createTestFacilityProvider({
209190
facilityId: facility.id,
210191
providerEntityType: "organization",
211192
providerEntityRefId: validOrgId,
@@ -228,7 +209,7 @@ describe("Facility Integration Tests", () => {
228209
overlapPolicy: "shared",
229210
});
230211

231-
await db.insert(schema.facilityProvider).values({
212+
await createTestFacilityProvider({
232213
facilityId: facility.id,
233214
providerEntityType: "venue",
234215
providerEntityRefId: validVenueId,
@@ -253,7 +234,7 @@ describe("Facility Integration Tests", () => {
253234
overlapPolicy: "shared",
254235
});
255236

256-
await db.insert(schema.facilityProvider).values({
237+
await createTestFacilityProvider({
257238
facilityId: facility.id,
258239
providerEntityType: "organization",
259240
providerEntityRefId: validOrgId,
@@ -277,13 +258,13 @@ describe("Facility Integration Tests", () => {
277258
overlapPolicy: "shared",
278259
});
279260

280-
await db.insert(schema.facilityProvider).values([
281-
{
261+
await Promise.all([
262+
createTestFacilityProvider({
282263
facilityId: facility.id,
283264
providerEntityType: "organization",
284265
providerEntityRefId: validOrgId,
285-
},
286-
{ facilityId: facility.id, providerEntityType: "venue", providerEntityRefId: validVenueId },
266+
}),
267+
createTestFacilityProvider({ facilityId: facility.id, providerEntityType: "venue", providerEntityRefId: validVenueId })
287268
]);
288269

289270
const fetched = await findFacilityById(facility.id);
@@ -413,15 +394,12 @@ describe("Facility Integration Tests", () => {
413394
const { event: eventB } = await createOrganizerTestSetup();
414395

415396
// Create a venue allotment on event B
416-
const [allotmentB] = await db
417-
.insert(schema.venueAllotment)
418-
.values({
419-
eventId: eventB.id,
420-
venueId: validVenueId,
421-
startsAt: new Date(Date.now() + 86400000).toISOString(),
422-
endsAt: new Date(Date.now() + 172800000).toISOString(),
423-
})
424-
.returning({ id: schema.venueAllotment.id });
397+
const allotmentB = await createTestVenueAllotment({
398+
eventId: eventB.id,
399+
venueId: validVenueId,
400+
startsAt: new Date(Date.now() + 86400000).toISOString(),
401+
endsAt: new Date(Date.now() + 172800000).toISOString(),
402+
});
425403

426404
const facility = await createFacility({
427405
name: `CrossAllot-${nanoid()}`,
@@ -448,15 +426,12 @@ describe("Facility Integration Tests", () => {
448426
name: `DiffVen-${nanoid()}`,
449427
});
450428

451-
const [allotment] = await db
452-
.insert(schema.venueAllotment)
453-
.values({
454-
eventId: event.id,
455-
venueId: validVenueId,
456-
startsAt: new Date(Date.now() + 86400000).toISOString(),
457-
endsAt: new Date(Date.now() + 172800000).toISOString(),
458-
})
459-
.returning({ id: schema.venueAllotment.id });
429+
const allotment = await createTestVenueAllotment({
430+
eventId: event.id,
431+
venueId: validVenueId,
432+
startsAt: new Date(Date.now() + 86400000).toISOString(),
433+
endsAt: new Date(Date.now() + 172800000).toISOString(),
434+
});
460435

461436
const facRaw = await createFacility({
462437
name: `ProviderMismatch-${nanoid()}`,
@@ -466,7 +441,7 @@ describe("Facility Integration Tests", () => {
466441
overlapPolicy: "shared",
467442
});
468443

469-
await db.insert(schema.facilityProvider).values({
444+
await createTestFacilityProvider({
470445
facilityId: facRaw.id,
471446
providerEntityType: "venue",
472447
providerEntityRefId: diffVenue.id,
@@ -518,13 +493,12 @@ describe("Facility Integration Tests", () => {
518493
overlapPolicy: "shared",
519494
});
520495

521-
const [testProviderOrg] = await db
522-
.insert(schema.organization)
523-
.values({ name: `FacProviderOrg-${nanoid()}`, organizationTypeId: setup.eventOrg.organizationTypeId })
524-
.returning();
525-
assert(testProviderOrg);
496+
const testProviderOrg = await createTestOrganization({
497+
name: `FacProviderOrg-${nanoid()}`,
498+
organizationTypeId: setup.eventOrg.organizationTypeId
499+
});
526500

527-
await db.insert(schema.facilityProvider).values({
501+
await createTestFacilityProvider({
528502
facilityId: facility.id,
529503
providerEntityType: "organization",
530504
providerEntityRefId: testProviderOrg.id,
@@ -577,7 +551,7 @@ describe("Facility Integration Tests", () => {
577551
workflowParticipationPolicy: "exclude",
578552
overlapPolicy: "shared",
579553
});
580-
await db.insert(schema.facilityProvider).values({
554+
await createTestFacilityProvider({
581555
facilityId: facility.id,
582556
providerEntityType: "organization",
583557
providerEntityRefId: validOrgId,

test/integration/integration-test-helpers.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,36 @@ export async function assignRolePermission(data: { roleId: number; permissionId:
940940
if (!rp) throw new Error("Failed to assign role permission");
941941
return rp;
942942
}
943+
944+
export async function createTestFacilityType(data?: Partial<typeof schema.facilityType.$inferInsert>) {
945+
const [type] = await db
946+
.insert(schema.facilityType)
947+
.values({
948+
name: data?.name ?? `TestFacilityType-${nanoid()}`,
949+
...data,
950+
})
951+
.returning();
952+
953+
if (!type) throw new Error("Failed to create facility type in test helper");
954+
return type;
955+
}
956+
957+
export async function createTestFacilityProvider(data: typeof schema.facilityProvider.$inferInsert) {
958+
const [provider] = await db
959+
.insert(schema.facilityProvider)
960+
.values(data)
961+
.returning();
962+
963+
if (!provider) throw new Error("Failed to create facility provider in test helper");
964+
return provider;
965+
}
966+
967+
export async function createTestVenueAllotment(data: typeof schema.venueAllotment.$inferInsert) {
968+
const [allotment] = await db
969+
.insert(schema.venueAllotment)
970+
.values(data)
971+
.returning();
972+
973+
if (!allotment) throw new Error("Failed to create venue allotment in test helper");
974+
return allotment;
975+
}

0 commit comments

Comments
 (0)