From 2245c1ff005b78c36f5ce96bfc27275e110fd45f Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 8 Nov 2022 22:02:06 +0100 Subject: [PATCH] Routes --- src/db/lib.test.ts | 28 +++++++++++++++++++++++++++ src/db/rte.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++ src/db/rte.ts | 33 +++++++++++++++++++++++++++++++ src/db/trk.ts | 2 +- src/db/types.d.ts | 40 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/db/rte.test.ts create mode 100644 src/db/rte.ts diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index b5effa3..e0c0f89 100644 --- a/src/db/lib.test.ts +++ b/src/db/lib.test.ts @@ -1,6 +1,7 @@ import { initDb } from '.'; import uri from '../lib/ids'; import { getFamily } from './lib'; +import { putNewRte } from './rte'; import { putNewTrk } from './trk'; import { putNewTrkpt } from './trkpt'; import { putNewTrkseg } from './trkseg'; @@ -203,6 +204,33 @@ Object { ], "total_rows": 5, } +`); + }); + + test('returns two rows after a gpx and a route have been inserted.', async () => { + await putNewRte(); + const allDocs: any = await getFamily(uri('gpx', { gpx: '0' })); + expect(allDocs).toMatchInlineSnapshot(` +Object { + "offset": 0, + "rows": Array [ + Object { + "id": "gpx/0", + "key": "gpx/0", + "value": Object { + "rev": "1-98d42c99fcf471a500b0506a60e77559", + }, + }, + Object { + "id": "gpx/0/1rte/00000", + "key": "gpx/0/1rte/00000", + "value": Object { + "rev": "1-bce69fd9ada837c19cf8c1c296f82f92", + }, + }, + ], + "total_rows": 5, +} `); }); }); diff --git a/src/db/rte.test.ts b/src/db/rte.test.ts new file mode 100644 index 0000000..f8a7a70 --- /dev/null +++ b/src/db/rte.test.ts @@ -0,0 +1,48 @@ +import { putNewRte } from './rte'; +import { putNewTrk } from './trk'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The rte module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new rte when required', async () => { + putNewRte({ gpx: 'gpxId', rte: 'rteid' }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/1rte/rteid', + _rev: undefined, + doc: { + cmt: undefined, + desc: undefined, + extensions: undefined, + link: undefined, + name: undefined, + number: 0, + src: undefined, + trkseg: undefined, + type: undefined, + }, + type: 'rte', + }); + }); + test('db.put() generates an id for the trk if needed', async () => { + const id = await putNewRte({ gpx: 'gpxId' }); + expect(id).toEqual({ gpx: 'gpxId', rte: '00000' }); + }); + test('db.put() generates ids for both gpx and trk if needed', async () => { + const id = await putNewRte(); + expect(id).toEqual({ gpx: '0', rte: '00000' }); + }); +}); diff --git a/src/db/rte.ts b/src/db/rte.ts new file mode 100644 index 0000000..08c83e6 --- /dev/null +++ b/src/db/rte.ts @@ -0,0 +1,33 @@ +import getUri from '../lib/ids'; +import { putNewGpx } from './gpx'; +import { put } from './lib'; + +export const emptyRte: Rte = { + name: undefined, + cmt: undefined, + desc: undefined, + src: undefined, + link: undefined, + number: 0, + type: undefined, + extensions: undefined, + rtept: undefined, +}; + +export const putNewRte = async (id?: IdRte | IdGpx) => { + let finalId = { ...id }; + if (!('rte' in finalId)) { + const gpxId = await putNewGpx(id); + finalId = { ...gpxId, rte: '00000' }; + } + const uri = getUri('rte', finalId); + await put( + uri, + 'rte', + (rte) => { + return rte; + }, + emptyRte + ); + return finalId as IdRte; +}; diff --git a/src/db/trk.ts b/src/db/trk.ts index 3a65646..f3f8b71 100644 --- a/src/db/trk.ts +++ b/src/db/trk.ts @@ -2,7 +2,7 @@ import getUri from '../lib/ids'; import { putNewGpx } from './gpx'; import { put } from './lib'; -const emptyTrk: Trk = { +export const emptyTrk: Trk = { name: undefined, cmt: undefined, desc: undefined, diff --git a/src/db/types.d.ts b/src/db/types.d.ts index 44f1968..a6182e3 100644 --- a/src/db/types.d.ts +++ b/src/db/types.d.ts @@ -93,3 +93,43 @@ interface Trkpt_ { lat: number; lon: number; } + +interface Rte { + name?: string; + cmt?: string; + desc?: string; + src?: string; + link?: Link[]; + number?: number; + type?: string; + extensions?: Extensions; + rtept?: Rtept[]; +} + +interface Rtept { + $: Rtept_; + ele?: number; + time?: string; + magvar?: number; + geoidheight?: number; + name?: string; + cmt?: string; + desc?: string; + src?: string; + link?: Link; + sym?: string; + type?: string; + fix?: string; + sat?: number; + hdop?: number; + vdop?: number; + pdop?: number; + ageofdgpsdata?: number; + dgpsid?: number; + extensions?: Extensions; +} + +interface Rtept_ { + lat: number; + lon: number; +}