diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index e0c0f89..be629ab 100644 --- a/src/db/lib.test.ts +++ b/src/db/lib.test.ts @@ -2,6 +2,7 @@ import { initDb } from '.'; import uri from '../lib/ids'; import { getFamily } from './lib'; import { putNewRte } from './rte'; +import { putNewRtept } from './rtept'; import { putNewTrk } from './trk'; import { putNewTrkpt } from './trkpt'; import { putNewTrkseg } from './trkseg'; @@ -231,6 +232,40 @@ Object { ], "total_rows": 5, } +`); + }); + + test('returns three rows after a gpx and a route point have been inserted.', async () => { + await putNewRtept(); + 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", + }, + }, + Object { + "id": "gpx/0/1rte/00000/00000", + "key": "gpx/0/1rte/00000/00000", + "value": Object { + "rev": "1-1045e2dc6d2f7927514b02f79d14daa8", + }, + }, + ], + "total_rows": 6, +} `); }); }); diff --git a/src/db/rtept.test.ts b/src/db/rtept.test.ts new file mode 100644 index 0000000..68c8e03 --- /dev/null +++ b/src/db/rtept.test.ts @@ -0,0 +1,58 @@ +import { putNewRtept } from './rtept'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The rtept module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new rtept when required', async () => { + putNewRtept({ gpx: 'gpxId', rte: 'rteid', rtept: 'rteptid' }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/1rte/rteid/rteptid', + _rev: undefined, + doc: { + $: { lat: 0, lon: 0 }, + ageofdgpsdata: undefined, + cmt: undefined, + desc: undefined, + dgpsid: undefined, + ele: undefined, + extensions: undefined, + fix: undefined, + geoidheight: undefined, + hdop: undefined, + link: undefined, + magvar: undefined, + name: undefined, + pdop: undefined, + sat: undefined, + src: undefined, + sym: undefined, + time: undefined, + type: undefined, + vdop: undefined, + }, + type: 'rtept', + }); + }); + test('db.put() generates an id for the rtept if needed', async () => { + const id = await putNewRtept({ gpx: 'gpxId' }); + expect(id).toEqual({ gpx: 'gpxId', rte: '00000', rtept: '00000' }); + }); + test('db.put() generates ids for both gpx and rte if needed', async () => { + const id = await putNewRtept(); + expect(id).toEqual({ gpx: '0', rte: '00000', rtept: '00000' }); + }); +}); diff --git a/src/db/rtept.ts b/src/db/rtept.ts new file mode 100644 index 0000000..709ef8e --- /dev/null +++ b/src/db/rtept.ts @@ -0,0 +1,44 @@ +import getUri from '../lib/ids'; +import { put } from './lib'; +import { putNewRte } from './rte'; + +export const emptyRtept: Wpt = { + $: { lat: 0, lon: 0 }, + ele: undefined, + time: undefined, + magvar: undefined, + geoidheight: undefined, + name: undefined, + cmt: undefined, + desc: undefined, + src: undefined, + link: undefined, + sym: undefined, + type: undefined, + fix: undefined, + sat: undefined, + hdop: undefined, + vdop: undefined, + pdop: undefined, + ageofdgpsdata: undefined, + dgpsid: undefined, + extensions: undefined, +}; + +export const putNewRtept = async (id?: IdGpx | IdRte | IdRtept) => { + let finalId = { ...id }; + if (!('rtept' in finalId)) { + const rteId = await putNewRte(id); + finalId = { ...rteId, rtept: '00000' }; + } + const uri = getUri('rtept', finalId); + await put( + uri, + 'rtept', + (rtept) => { + return rtept; + }, + emptyRtept + ); + return finalId as IdRtept; +};