More tests

This commit is contained in:
Eric van der Vlist 2023-02-10 08:09:13 +01:00
parent 31669d8aac
commit d8d15d9d83
2 changed files with 129 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import shell from 'shelljs';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import { emptyRte, getRte, putNewRte, putRte } from './rte'; import { getRte, putNewRte, putRte } from './rte';
import { put } from './lib'; import { put } from './lib';
import { emptyWpt } from './wpt'; import { emptyWpt } from './wpt';

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

@ -0,0 +1,128 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import shell from 'shelljs';
import PouchDB from 'pouchdb';
import { appendTrkpt, getTrkseg, putTrkseg } from './trkseg';
import getUri from '../lib/ids';
import { put } from './lib';
import { emptyWpt } from './wpt';
const test = it;
const jest = vi;
declare global {
var db: any;
var dbReady: boolean;
}
const originalDb = globalThis.db;
const originalDateNow = globalThis.Date.now;
describe('The trkseg module with a real db', () => {
beforeEach(async () => {
globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false,
});
globalThis.Date.now = () => 0;
});
afterEach(async () => {
try {
await db.destroy();
} catch (err) {
// console.error(err);
await shell.exec('rm -rf $*$');
}
globalThis.db = undefined;
globalThis.dbReady = false;
globalThis.Date.now = originalDateNow;
});
it('can write and read a trkseg', async () => {
const id = 'gpx/4320836036089189/3trk/000000/000000';
const trkseg: Trkseg = {};
const trksegPut = await putTrkseg(id, trkseg);
const trksegGet = await getTrkseg({ id });
expect(trksegGet).toEqual(trkseg);
});
it('can write and read a trkseg with trkpts', async () => {
const id = 'whatever';
const trkseg: Trkseg = {
trkpt: [
{
$: { lat: 0, lon: 0 },
},
{
$: { lat: 1, lon: 1 },
},
],
};
const trksegPut = await putTrkseg(id, trkseg);
const trksegGet = await getTrkseg({ id });
expect(trksegGet).toEqual(trkseg);
});
it('can read external trkpts ', async () => {
const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000';
const idTrksegpt = 'gpx/4320836036089189/3trk/000000/000000/000035';
const trkpt3: Wpt = {
$: { lat: 3, lon: 3 },
};
const trkseg0: Trkseg = {
trkpt: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
],
};
const trkseg1: Trkseg = {
trkpt: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
trkpt3,
],
};
const trksegPut = await putTrkseg(idTrkseg, trkseg0);
await put(idTrksegpt, 'trkpt', () => trkpt3, emptyWpt);
expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1);
});
it('can can append a new trkpt ', async () => {
const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000';
const trkpt3: Wpt = {
$: { lat: 3, lon: 3 },
};
const trkseg0: Trkseg = {
trkpt: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
],
};
const trkseg1: Trkseg = {
trkpt: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
trkpt3,
],
};
await putTrkseg(idTrkseg, trkseg0);
await appendTrkpt({
trksegIdObj: getUri('trkseg', idTrkseg),
trkpt: trkpt3,
});
expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1);
});
});