test(db): no await inside a loop in the Postgres tests

This commit is contained in:
Maurice
2026-11-17 14:23:13 +01:00
parent d3cd484519
commit 5938609f69
3 changed files with 59 additions and 59 deletions
+42 -42
View File
@@ -110,41 +110,41 @@ describe('plugin data on Postgres', () => {
const store = data.open('pg-alpha');
// Unqualified, the name finds nothing: only the plugin's schema is on the search_path.
await expect(store.query('SELECT email FROM users')).rejects.toThrow(/does not exist/);
for (const sql of [
'SELECT email FROM public.users',
'SELECT email FROM "public"."users"',
'SELECT email FROM PUBLIC . users',
'SELECT email FROM public/* x */.users',
"SELECT query_to_xml('select email from public.users', true, true, '')",
"SELECT ts_stat('select to_tsvector(email) from public.users')",
'SELECT * FROM pg_catalog.pg_tables',
'SELECT * FROM pg_stats',
'SELECT * FROM information_schema.tables',
"SELECT set_config('search_path', 'public', true)",
"SELECT setval('public.users_id_seq', 1)",
"SELECT 'public.users'::regclass",
'SELECT database_to_xml(true, true, $$x$$)',
"SELECT E'\\'' || 'x'",
]) {
await expect(store.query(sql), sql).rejects.toThrow(/not allowed for plugin databases/);
}
for (const sql of [
'CREATE TEMP TABLE t (x int)',
'SELECT 1 AS x INTO t2',
'CREATE FUNCTION f() RETURNS int LANGUAGE sql AS $$ SELECT 1 $$',
'ALTER TABLE notes SET SCHEMA public',
'ALTER TABLE notes OWNER TO postgres',
'CREATE SCHEMA mine',
'DROP SCHEMA trek_plugin_pg_alpha CASCADE',
'SET search_path TO public',
"COPY notes TO '/tmp/x'",
'CREATE TABLE copy AS SELECT * FROM public.users',
'CREATE VIEW v AS SELECT email FROM public.users',
'GRANT SELECT ON notes TO PUBLIC',
'DROP OWNED BY CURRENT_USER',
]) {
await expect(store.exec(sql), sql).rejects.toThrow(/not allowed/);
}
await Promise.all(
[
'SELECT email FROM public.users',
'SELECT email FROM "public"."users"',
'SELECT email FROM PUBLIC . users',
'SELECT email FROM public/* x */.users',
"SELECT query_to_xml('select email from public.users', true, true, '')",
"SELECT ts_stat('select to_tsvector(email) from public.users')",
'SELECT * FROM pg_catalog.pg_tables',
'SELECT * FROM pg_stats',
'SELECT * FROM information_schema.tables',
"SELECT set_config('search_path', 'public', true)",
"SELECT setval('public.users_id_seq', 1)",
"SELECT 'public.users'::regclass",
'SELECT database_to_xml(true, true, $$x$$)',
"SELECT E'\\'' || 'x'",
].map((sql) => expect(store.query(sql), sql).rejects.toThrow(/not allowed for plugin databases/)),
);
await Promise.all(
[
'CREATE TEMP TABLE t (x int)',
'SELECT 1 AS x INTO t2',
'CREATE FUNCTION f() RETURNS int LANGUAGE sql AS $$ SELECT 1 $$',
'ALTER TABLE notes SET SCHEMA public',
'ALTER TABLE notes OWNER TO postgres',
'CREATE SCHEMA mine',
'DROP SCHEMA trek_plugin_pg_alpha CASCADE',
'SET search_path TO public',
"COPY notes TO '/tmp/x'",
'CREATE TABLE copy AS SELECT * FROM public.users',
'CREATE VIEW v AS SELECT email FROM public.users',
'GRANT SELECT ON notes TO PUBLIC',
'DROP OWNED BY CURRENT_USER',
].map((sql) => expect(store.exec(sql), sql).rejects.toThrow(/not allowed/)),
);
// Nothing of TREK's was touched along the way.
const [{ n }] = await withRequestContext(orm, () =>
orm.em.getConnection().execute<Array<{ n: number }>>('select count(*) as n from users', [], 'all'),
@@ -160,13 +160,13 @@ describe('plugin data on Postgres', () => {
expect(await alpha.query('SELECT body FROM notes ORDER BY id')).toEqual([{ body: 'a' }, { body: 'b' }]);
expect(await beta.query('SELECT body FROM notes')).toEqual([{ body: 'beta secret' }]);
for (const sql of [
'SELECT body FROM trek_plugin_pg_beta.notes',
'SELECT body FROM "trek_plugin_pg_beta".notes',
'SELECT body FROM trek_plugin_not_yet_installed.notes',
]) {
await expect(alpha.query(sql), sql).rejects.toThrow(/another schema/);
}
await Promise.all(
[
'SELECT body FROM trek_plugin_pg_beta.notes',
'SELECT body FROM "trek_plugin_pg_beta".notes',
'SELECT body FROM trek_plugin_not_yet_installed.notes',
].map((sql) => expect(alpha.query(sql), sql).rejects.toThrow(/another schema/)),
);
// Its own schema, qualified, is fine.
expect(await alpha.query('SELECT count(*) AS n FROM trek_plugin_pg_alpha.notes')).toEqual([{ n: 2 }]);
});
+12 -13
View File
@@ -55,25 +55,24 @@ describe('Postgres answers as SQLite does', () => {
const { user } = await makeUser(orm);
const linked = await makeTrip(orm, user.id, { start_date: '2026-09-01', end_date: '2026-09-03' });
const other = await makeTrip(orm, user.id, { start_date: '2026-10-01', end_date: '2026-10-03' });
const stays: number[] = [];
for (const trip of [linked, other]) {
const place = await makePlace(orm, trip.id);
const days = await readTripDays(orm, trip.id);
const stay = await makeDayAccommodation(orm, trip.id, place.id, days[0].id, days[1].id);
stays.push(stay.id);
}
await makeReservation(orm, linked.id, { type: 'hotel', title: 'Hotel', accommodation_id: `${stays[0]}.0` });
const stayOn = async (trip: number): Promise<number> => {
const place = await makePlace(orm, trip);
const days = await readTripDays(orm, trip);
return (await makeDayAccommodation(orm, trip, place.id, days[0].id, days[1].id)).id;
};
const linkedStay = await stayOn(linked.id);
const otherStay = await stayOn(other.id);
await makeReservation(orm, linked.id, { type: 'hotel', title: 'Hotel', accommodation_id: `${linkedStay}.0` });
for (const [trip, stay] of [
[linked.id, stays[0]],
[other.id, stays[1]],
] as const) {
const expectPublicStay = async (trip: number, stay: number): Promise<void> => {
const calendar = await inRequest(() => reservations().listPublicStaysForCalendar(trip));
expect(calendar.map((row) => row.id)).toEqual([stay]);
const shared = await inRequest(() => reservations().listPublicAccommodationsForShare(trip));
expect(shared.map((row) => row.id)).toEqual([stay]);
await expect(inRequest(() => reservations().listPublicStayPlaceIdsForShare(trip))).resolves.toHaveLength(1);
}
};
await expectPublicStay(linked.id, linkedStay);
await expectPublicStay(other.id, otherStay);
});
it('PGSEM-102: the traveller list reports the owner flag as the numbers 1 and 0', async () => {
@@ -209,13 +209,14 @@ describe('bootstrapPostgresSchema', () => {
expect(db.ensureTable).not.toHaveBeenCalled();
});
it('PGBOOT-021: the oldest and the newest supported servers boot', async () => {
for (const version of ['140019', '180001']) {
it.each(['140019', '180001'])(
'PGBOOT-021: the oldest and the newest supported servers boot (%s)',
async (version) => {
const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [], server: { version } });
await bootstrapPostgresSchema(orm, deps());
expect(db.logged).toEqual([M1, M2]);
}
});
},
);
it('PGBOOT-022: the migration history is looked for in the current schema, where the tables are', async () => {
const { orm, db } = fakeOrm({ executed: [M1, M2, M3], pending: [], tables: ['users'], server: { schema: 'trek' } });