From b0f767721a2112dadf6db7664f6187d832954da9 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 8 Nov 2022 20:21:34 +0100 Subject: [PATCH] Track segments --- src/db/lib.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/db/trk.test.ts | 2 +- src/db/trkseg.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/db/trkseg.ts | 26 ++++++++++++++++++++++++++ src/db/types.d.ts | 7 ++++++- 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/db/trkseg.test.ts create mode 100644 src/db/trkseg.ts diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index cdb24c9..630fe48 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 { putNewTrk } from './trk'; +import { putNewTrkseg } from './trkseg'; declare global { var db: any; @@ -100,6 +101,39 @@ Object { ], "total_rows": 5, } +`); + }); + test('returns three rows after a gpx and a track segment have been inserted.', async () => { + await putNewTrkseg(); + 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/2trk/00000", + "key": "gpx/0/2trk/00000", + "value": Object { + "rev": "1-1e197ef8965a436730f80cabaaaa6f70", + }, + }, + Object { + "id": "gpx/0/2trk/00000/00000", + "key": "gpx/0/2trk/00000/00000", + "value": Object { + "rev": "1-33cb714b4d5553cc20fc7a753c63cf51", + }, + }, + ], + "total_rows": 6, +} `); }); }); diff --git a/src/db/trk.test.ts b/src/db/trk.test.ts index b7183c5..917eb39 100644 --- a/src/db/trk.test.ts +++ b/src/db/trk.test.ts @@ -8,7 +8,7 @@ declare global { const originalDb = globalThis.db; const originalDateNow = globalThis.Date.now; -describe('The gpx module', () => { +describe('The trk module', () => { beforeEach(() => { globalThis.db = { put: jest.fn() }; globalThis.Date.now = () => 0; diff --git a/src/db/trkseg.test.ts b/src/db/trkseg.test.ts new file mode 100644 index 0000000..715a3a3 --- /dev/null +++ b/src/db/trkseg.test.ts @@ -0,0 +1,40 @@ +import { putNewTrkseg } from './trkseg'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The trkseg module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new trk when required', async () => { + putNewTrkseg({ gpx: 'gpxId', trk: 'trkid', trkseg: 'trksegid' }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/2trk/trkid/trksegid', + _rev: undefined, + doc: { + trkpt: undefined, + extensions: undefined, + }, + type: 'trkseg', + }); + }); + test('db.put() generates an id for the trk if needed', async () => { + const id = await putNewTrkseg({ gpx: 'gpxId' }); + expect(id).toEqual({ gpx: 'gpxId', trk: '00000', trkseg: '00000' }); + }); + test('db.put() generates ids for both gpx and trk if needed', async () => { + const id = await putNewTrkseg(); + expect(id).toEqual({ gpx: '0', trk: '00000', trkseg: '00000' }); + }); +}); diff --git a/src/db/trkseg.ts b/src/db/trkseg.ts new file mode 100644 index 0000000..e202b32 --- /dev/null +++ b/src/db/trkseg.ts @@ -0,0 +1,26 @@ +import getUri from '../lib/ids'; +import { put } from './lib'; +import { putNewTrk } from './trk'; + +const emptyTrkseg: Trkseg = { + trkpt: undefined, + extensions: undefined, +}; + +export const putNewTrkseg = async (id?: IdTrk | IdGpx | IdTrkseg) => { + let finalId = { ...id }; + if (!('trkseg' in finalId)) { + const trkId = await putNewTrk(id); + finalId = { ...trkId, trkseg: '00000' }; + } + const uri = getUri('trkseg', finalId); + await put( + uri, + 'trkseg', + (trkseg) => { + return trkseg; + }, + emptyTrkseg + ); + return finalId; +}; diff --git a/src/db/types.d.ts b/src/db/types.d.ts index 7bb60c9..445413f 100644 --- a/src/db/types.d.ts +++ b/src/db/types.d.ts @@ -42,7 +42,7 @@ interface Trk { number?: number; type?: string; extensions?: Extensions; - trkseg?: any[]; + trkseg?: Trkseg[]; } interface Link { @@ -54,3 +54,8 @@ interface Link { interface Link_ { href: string; } + +interface Trkseg { + trkpt: any[]; + extensions?: Extensions; +}