Track segments

This commit is contained in:
Eric van der Vlist 2022-11-08 20:21:34 +01:00
parent b79599a350
commit b0f767721a
5 changed files with 107 additions and 2 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 { putNewTrk } from './trk'; import { putNewTrk } from './trk';
import { putNewTrkseg } from './trkseg';
declare global { declare global {
var db: any; var db: any;
@ -100,6 +101,39 @@ Object {
], ],
"total_rows": 5, "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,
}
`); `);
}); });
}); });

View File

@ -8,7 +8,7 @@ declare global {
const originalDb = globalThis.db; const originalDb = globalThis.db;
const originalDateNow = globalThis.Date.now; const originalDateNow = globalThis.Date.now;
describe('The gpx module', () => { describe('The trk module', () => {
beforeEach(() => { beforeEach(() => {
globalThis.db = { put: jest.fn() }; globalThis.db = { put: jest.fn() };
globalThis.Date.now = () => 0; globalThis.Date.now = () => 0;

40
src/db/trkseg.test.ts Normal file
View File

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

26
src/db/trkseg.ts Normal file
View File

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

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

@ -42,7 +42,7 @@ interface Trk {
number?: number; number?: number;
type?: string; type?: string;
extensions?: Extensions; extensions?: Extensions;
trkseg?: any[]; trkseg?: Trkseg[];
} }
interface Link { interface Link {
@ -54,3 +54,8 @@ interface Link {
interface Link_ { interface Link_ {
href: string; href: string;
} }
interface Trkseg {
trkpt: any[];
extensions?: Extensions;
}