This commit is contained in:
Eric van der Vlist 2022-11-08 22:14:29 +01:00
parent 2245c1ff00
commit 441c623999
3 changed files with 137 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import { initDb } from '.';
import uri from '../lib/ids'; import uri from '../lib/ids';
import { getFamily } from './lib'; import { getFamily } from './lib';
import { putNewRte } from './rte'; import { putNewRte } from './rte';
import { putNewRtept } from './rtept';
import { putNewTrk } from './trk'; import { putNewTrk } from './trk';
import { putNewTrkpt } from './trkpt'; import { putNewTrkpt } from './trkpt';
import { putNewTrkseg } from './trkseg'; import { putNewTrkseg } from './trkseg';
@ -231,6 +232,40 @@ Object {
], ],
"total_rows": 5, "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,
}
`); `);
}); });
}); });

58
src/db/rtept.test.ts Normal file
View File

@ -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' });
});
});

44
src/db/rtept.ts Normal file
View File

@ -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;
};