mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
place-contrib: seed the repository tests through the row factories, not raw SQL
This commit is contained in:
@@ -5,10 +5,11 @@
|
||||
*/
|
||||
import { PlaceContribOutbox } from '../../../../src/db/entities/PlaceContribOutbox.entity';
|
||||
import { PlaceFieldOrigin } from '../../../../src/db/entities/PlaceFieldOrigin.entity';
|
||||
import { Places } from '../../../../src/db/entities/Places.entity';
|
||||
import type { PlaceContribOutboxRepository } from '../../../../src/db/repositories/PlaceContribOutbox.repository';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createPlace, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { countRows, findRow, insertRow } from '../../../helpers/factories/rows';
|
||||
import { countRows, findRow, insertRow, updateRows } from '../../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
|
||||
@@ -36,13 +37,11 @@ const NOW = '2026-10-30 10:00:00';
|
||||
const LATER = '2026-10-31 10:00:00';
|
||||
|
||||
/** A place named Café Luna, with the shareable columns given (the factory sets only a few). */
|
||||
function aPlace(columns: Partial<Record<'website' | 'phone' | 'osm_id', string>> = {}): number {
|
||||
async function aPlace(columns: Partial<Record<'website' | 'phone' | 'osm_id', string>> = {}): Promise<number> {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const { id } = createPlace(testDb, trip.id, { name: 'Café Luna' });
|
||||
for (const [column, value] of Object.entries(columns)) {
|
||||
testDb.prepare(`UPDATE places SET ${column} = ? WHERE id = ?`).run(value, id);
|
||||
}
|
||||
if (Object.keys(columns).length > 0) await updateRows(t, Places, { id }, columns);
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -65,7 +64,7 @@ async function queue(placeId: number, over: Partial<Parameters<PlaceContribOutbo
|
||||
|
||||
describe('PlaceContribOutboxRepository', () => {
|
||||
it('PCOUTREPO-001: a newer value for the same key and field replaces the queued one and starts its attempts over', async () => {
|
||||
const placeId = aPlace();
|
||||
const placeId = await aPlace();
|
||||
await queue(placeId);
|
||||
await outbox.scheduleRetry(
|
||||
(await outbox.dueFor('osm:node/1', NOW)).map((row) => row.id),
|
||||
@@ -82,7 +81,7 @@ describe('PlaceContribOutboxRepository', () => {
|
||||
});
|
||||
|
||||
it('PCOUTREPO-002: due keys come oldest first, only once due, at most the limit', async () => {
|
||||
const placeId = aPlace();
|
||||
const placeId = await aPlace();
|
||||
await queue(placeId, { contrib_key: 'osm:node/2', next_attempt_at: NOW });
|
||||
await queue(placeId, { contrib_key: 'osm:node/1', next_attempt_at: PAST });
|
||||
await queue(placeId, { contrib_key: 'osm:node/3', next_attempt_at: LATER });
|
||||
@@ -91,7 +90,7 @@ describe('PlaceContribOutboxRepository', () => {
|
||||
});
|
||||
|
||||
it('PCOUTREPO-003: retries count up and wait, a postponement only waits', async () => {
|
||||
const placeId = aPlace();
|
||||
const placeId = await aPlace();
|
||||
await queue(placeId);
|
||||
const [row] = await outbox.dueFor('osm:node/1', NOW);
|
||||
if (!row) throw new Error('the queued value is missing');
|
||||
@@ -106,7 +105,7 @@ describe('PlaceContribOutboxRepository', () => {
|
||||
});
|
||||
|
||||
it('PCOUTREPO-004: values queued before a time come in id order, at most the limit', async () => {
|
||||
const placeId = aPlace();
|
||||
const placeId = await aPlace();
|
||||
await insertRow(t, PlaceContribOutbox, {
|
||||
contrib_key: 'osm:node/1',
|
||||
field: 'phone',
|
||||
@@ -124,8 +123,8 @@ describe('PlaceContribOutboxRepository', () => {
|
||||
});
|
||||
|
||||
it('PCOUTREPO-005: deletes by place and field, by field, by id and all, and an empty list deletes nothing', async () => {
|
||||
const one = aPlace();
|
||||
const two = aPlace();
|
||||
const one = await aPlace();
|
||||
const two = await aPlace();
|
||||
await queue(one, { contrib_key: 'osm:node/1', field: 'website' });
|
||||
await queue(one, { contrib_key: 'osm:node/1', field: 'phone', value: '+49 381 1' });
|
||||
await queue(two, { contrib_key: 'osm:node/2', field: 'website' });
|
||||
@@ -144,7 +143,7 @@ describe('PlaceContribOutboxRepository', () => {
|
||||
});
|
||||
|
||||
it("PCOUTREPO-006: the current state joins the place's values and the field's origin, none where it has none", async () => {
|
||||
const placeId = aPlace({ website: 'https://cafe-luna.de', phone: '+49 381 1' });
|
||||
const placeId = await aPlace({ website: 'https://cafe-luna.de', phone: '+49 381 1' });
|
||||
await insertRow(t, PlaceFieldOrigin, { place: placeId, field: 'website', origin: 'user', value_hash: 'h1' });
|
||||
await queue(placeId, { field: 'website' });
|
||||
await queue(placeId, { field: 'phone', value: '+49 381 1' });
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
*/
|
||||
import { PlaceDetailsCache } from '../../../../src/db/entities/PlaceDetailsCache.entity';
|
||||
import { PlaceFieldOrigin } from '../../../../src/db/entities/PlaceFieldOrigin.entity';
|
||||
import { Places } from '../../../../src/db/entities/Places.entity';
|
||||
import type { PlaceFieldOriginRepository } from '../../../../src/db/repositories/PlaceFieldOrigin.repository';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createPlace, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { insertRow } from '../../../helpers/factories/rows';
|
||||
import { insertRow, updateRows } from '../../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
|
||||
@@ -34,20 +35,18 @@ afterAll(async () => {
|
||||
const AT = '2026-10-30 10:00:00';
|
||||
|
||||
/** A place named Café Luna, with the shareable columns given (the factory sets only a few). */
|
||||
function aPlace(columns: Partial<Record<'website' | 'phone' | 'osm_id', string>> = {}): number {
|
||||
async function aPlace(columns: Partial<Record<'website' | 'phone' | 'osm_id', string>> = {}): Promise<number> {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const { id } = createPlace(testDb, trip.id, { name: 'Café Luna' });
|
||||
for (const [column, value] of Object.entries(columns)) {
|
||||
testDb.prepare(`UPDATE places SET ${column} = ? WHERE id = ?`).run(value, id);
|
||||
}
|
||||
if (Object.keys(columns).length > 0) await updateRows(t, Places, { id }, columns);
|
||||
return id;
|
||||
}
|
||||
|
||||
describe('PlaceFieldOriginRepository', () => {
|
||||
it("PFOREPO-001: reads a place's shareable values, one or many, and null for a place that is gone", async () => {
|
||||
const one = aPlace({ website: 'https://cafe-luna.de', osm_id: 'node/1' });
|
||||
const two = aPlace({ phone: '+49 381 1' });
|
||||
const one = await aPlace({ website: 'https://cafe-luna.de', osm_id: 'node/1' });
|
||||
const two = await aPlace({ phone: '+49 381 1' });
|
||||
expect(await origins.placeValues(one)).toEqual(
|
||||
expect.objectContaining({ id: one, name: 'Café Luna', website: 'https://cafe-luna.de', osm_id: 'node/1' }),
|
||||
);
|
||||
@@ -58,7 +57,7 @@ describe('PlaceFieldOriginRepository', () => {
|
||||
});
|
||||
|
||||
it('PFOREPO-002: an origin written again replaces the old one; an empty list writes nothing', async () => {
|
||||
const placeId = aPlace();
|
||||
const placeId = await aPlace();
|
||||
await origins.upsertOrigins([]);
|
||||
expect(await origins.originsOf(placeId)).toEqual([]);
|
||||
await origins.upsertOrigins([
|
||||
@@ -77,8 +76,8 @@ describe('PlaceFieldOriginRepository', () => {
|
||||
});
|
||||
|
||||
it('PFOREPO-003: deletes the named fields of one place only', async () => {
|
||||
const one = aPlace();
|
||||
const two = aPlace();
|
||||
const one = await aPlace();
|
||||
const two = await aPlace();
|
||||
await origins.upsertOrigins([
|
||||
{ place_id: one, field: 'website', origin: 'user', value_hash: 'h1', updated_at: AT },
|
||||
{ place_id: two, field: 'website', origin: 'user', value_hash: 'h1', updated_at: AT },
|
||||
|
||||
Reference in New Issue
Block a user