This commit is contained in:
Eric van der Vlist 2022-11-08 22:02:06 +01:00
parent 1b9c7fc261
commit 2245c1ff00
5 changed files with 150 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import { initDb } from '.'; 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 { putNewTrk } from './trk'; import { putNewTrk } from './trk';
import { putNewTrkpt } from './trkpt'; import { putNewTrkpt } from './trkpt';
import { putNewTrkseg } from './trkseg'; import { putNewTrkseg } from './trkseg';
@ -203,6 +204,33 @@ Object {
], ],
"total_rows": 5, "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,
}
`); `);
}); });
}); });

48
src/db/rte.test.ts Normal file
View File

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

33
src/db/rte.ts Normal file
View File

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

View File

@ -2,7 +2,7 @@ import getUri from '../lib/ids';
import { putNewGpx } from './gpx'; import { putNewGpx } from './gpx';
import { put } from './lib'; import { put } from './lib';
const emptyTrk: Trk = { export const emptyTrk: Trk = {
name: undefined, name: undefined,
cmt: undefined, cmt: undefined,
desc: undefined, desc: undefined,

40
src/db/types.d.ts vendored
View File

@ -93,3 +93,43 @@ interface Trkpt_ {
lat: number; lat: number;
lon: 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;
}