Going on with tracks.

This commit is contained in:
Eric van der Vlist 2022-11-08 14:16:23 +01:00
parent 26486b65b9
commit a53c0210ea
4 changed files with 106 additions and 40 deletions

View File

@ -16,10 +16,11 @@ describe('The gpx module', () => {
globalThis.db = originalDb; globalThis.db = originalDb;
globalThis.Date.now = originalDateNow; globalThis.Date.now = originalDateNow;
}); });
test('db.put() a new Gpx when required', () => { test('db.put() a new Gpx when required', async () => {
putNewGpx({ gpx: 'whatever' }); await putNewGpx({ gpx: 'whatever' });
expect(globalThis.db.put).toBeCalledWith({ expect(globalThis.db.put).toBeCalledWith({
_id: 'gpx/whatever', _id: 'gpx/whatever',
_rev: undefined,
doc: { doc: {
$: { $: {
creator: 'dyomedea version 0.000002', creator: 'dyomedea version 0.000002',
@ -35,27 +36,27 @@ describe('The gpx module', () => {
'xsi:schemaLocation': 'xsi:schemaLocation':
'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd', 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd',
}, },
extensions: '', extensions: undefined,
metadata: { metadata: {
author: '', author: undefined,
bounds: '', bounds: undefined,
copyright: '', copyright: undefined,
desc: '', desc: undefined,
extensions: '', extensions: undefined,
keywords: '', keywords: undefined,
link: [], link: undefined,
name: '', name: undefined,
time: '1970-01-01T00:00:00.000Z', time: '1970-01-01T00:00:00.000Z',
}, },
rte: [], rte: undefined,
trk: [], trk: undefined,
wpt: [], wpt: undefined,
}, },
type: 'gpx', type: 'gpx',
}); });
}); });
test('db.put() generates an id if needed', () => { test('db.put() generates an id if needed', async () => {
const id = putNewGpx(); const id = await putNewGpx();
expect(id).toEqual({ gpx: '0' }); expect(id).toEqual({ gpx: '0' });
}); });
}); });

View File

@ -15,29 +15,29 @@ const emptyGpx: Gpx = {
'xmlns:dyo': 'http://xmlns.dyomedea.com/', 'xmlns:dyo': 'http://xmlns.dyomedea.com/',
}, },
metadata: { metadata: {
name: '', name: undefined,
desc: '', desc: undefined,
author: '', author: undefined,
copyright: '', copyright: undefined,
link: [], link: undefined,
time: '', time: undefined,
keywords: '', keywords: undefined,
bounds: '', bounds: undefined,
extensions: '', extensions: undefined,
}, },
wpt: [], wpt: undefined,
rte: [], rte: undefined,
trk: [], trk: undefined,
extensions: '', extensions: undefined,
}; };
export const putNewGpx = (id: IdGpx = { gpx: Date.now().toString() }) => { export const putNewGpx = async (id: IdGpx = { gpx: Date.now().toString() }) => {
const uri = getUri('gpx', id); const uri = getUri('gpx', id);
put( await put(
uri, uri,
'gpx', 'gpx',
(gpx) => { (gpx) => {
gpx.metadata!.time = new Date(Date.now()).toISOString(); (gpx.metadata ??= {}).time = new Date(Date.now()).toISOString();
return gpx; return gpx;
}, },
emptyGpx emptyGpx

47
src/db/trk.test.ts Normal file
View File

@ -0,0 +1,47 @@
import { putNewTrk } from './trk';
declare global {
var db: any;
var dbReady: boolean;
}
const originalDb = globalThis.db;
const originalDateNow = globalThis.Date.now;
describe('The gpx module', () => {
beforeEach(() => {
globalThis.db = { put: jest.fn() };
globalThis.Date.now = () => 0;
});
afterEach(() => {
globalThis.db = originalDb;
globalThis.Date.now = originalDateNow;
});
test('db.put() a new Gpx when required', async () => {
putNewTrk({ gpx: 'gpxId', trk: 'trkid' });
await expect(globalThis.db.put).toBeCalledWith({
_id: 'gpx/gpxId/2trk/trkid',
_rev: undefined,
doc: {
cmt: undefined,
desc: undefined,
extensions: undefined,
link: undefined,
name: undefined,
number: 0,
src: undefined,
trkseg: undefined,
type: undefined,
},
type: 'trk',
});
});
test('db.put() generates an id for the trk if needed', async () => {
const id = await putNewTrk({ gpx: 'gpxId' });
expect(id).toEqual({ gpx: 'gpxId', trk: '00000' });
});
test('db.put() generates ids for both gpx and trk if needed', async () => {
const id = await putNewTrk();
expect(id).toEqual({ gpx: '0', trk: '00000' });
});
});

View File

@ -1,15 +1,33 @@
import getUri from '../lib/ids'; import getUri from '../lib/ids';
import { putNewGpx } from './gpx';
import { put } from './lib'; import { put } from './lib';
const emptyTrk: Trk = { const emptyTrk: Trk = {
name: '', name: undefined,
cmt: '', cmt: undefined,
desc: '', desc: undefined,
src: '', src: undefined,
link: [], link: undefined,
number: 0, number: 0,
type: '', type: undefined,
extensions: {}, extensions: undefined,
trkseg: [], trkseg: undefined,
}; };
export const putNewTrk = async (id?: IdTrk | IdGpx) => {
let finalId = { ...id };
if (!('trk' in finalId)) {
const gpxId = await putNewGpx(id);
finalId = { ...gpxId, trk: '00000' };
}
const uri = getUri('trk', finalId);
put(
uri,
'trk',
(trk) => {
return trk;
},
emptyTrk
);
return finalId;
};