More tests and debugging putRtept
This commit is contained in:
1 parent
3f5bb1633c
commit
c30ceecbb1
7 files changed
+370
-52
No files matched your search
+9
-5
@@ -1,7 +1,8 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import shell from 'shelljs';
|
||||
|
||||
import { initDb } from '.';
|
||||
import PouchDB from 'pouchdb';
|
||||
|
||||
import { existsGpx, putNewGpx } from './gpx';
|
||||
|
||||
const test = it;
|
||||
@@ -67,17 +68,20 @@ describe('putNewGpx', () => {
|
||||
|
||||
describe('The gpx module with a real db', () => {
|
||||
beforeEach(async () => {
|
||||
await initDb({});
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: false,
|
||||
});
|
||||
globalThis.Date.now = () => 0;
|
||||
});
|
||||
afterEach(async () => {
|
||||
try {
|
||||
await db.destroy();
|
||||
} catch (err) {
|
||||
// console.error(err);
|
||||
shell.exec('rm -rf $*$');
|
||||
// console.error(err);
|
||||
await shell.exec('rm -rf $*$');
|
||||
}
|
||||
db = undefined;
|
||||
globalThis.db = undefined;
|
||||
globalThis.dbReady = false;
|
||||
globalThis.Date.now = originalDateNow;
|
||||
});
|
||||
|
||||
|
||||
+44
-5
@@ -1,9 +1,11 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import shell from 'shelljs';
|
||||
|
||||
import { initDb } from '.';
|
||||
import PouchDB from 'pouchdb';
|
||||
|
||||
import { emptyRte, getRte, putNewRte, putRte } from './rte';
|
||||
import { put } from './lib';
|
||||
import { emptyWpt } from './wpt';
|
||||
|
||||
const test = it;
|
||||
const jest = vi;
|
||||
@@ -56,17 +58,20 @@ describe('The rte module', () => {
|
||||
|
||||
describe('The rte module with a real db', () => {
|
||||
beforeEach(async () => {
|
||||
await initDb({});
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: false,
|
||||
});
|
||||
globalThis.Date.now = () => 0;
|
||||
});
|
||||
afterEach(async () => {
|
||||
try {
|
||||
await db.destroy();
|
||||
} catch (err) {
|
||||
// console.error(err);
|
||||
shell.exec('rm -rf \$*\$');
|
||||
// console.error(err);
|
||||
await shell.exec('rm -rf $*$');
|
||||
}
|
||||
db = undefined;
|
||||
globalThis.db = undefined;
|
||||
globalThis.dbReady = false;
|
||||
globalThis.Date.now = originalDateNow;
|
||||
});
|
||||
it('can write and read a rte', async () => {
|
||||
@@ -97,4 +102,38 @@ describe('The rte module with a real db', () => {
|
||||
const rteGet = await getRte({ id });
|
||||
expect(rteGet).toEqual(rte);
|
||||
});
|
||||
it('can read external rtepts ', async () => {
|
||||
const idRte = 'gpx/4320836410265485/2rte/000034';
|
||||
const idRtept = 'gpx/4320836410265485/2rte/000034/000035';
|
||||
const rtept3: Wpt = {
|
||||
$: { lat: 3, lon: 3 },
|
||||
};
|
||||
const rte0: Rte = {
|
||||
name: 'A new route',
|
||||
rtept: [
|
||||
{
|
||||
$: { lat: 1, lon: 1 },
|
||||
},
|
||||
{
|
||||
$: { lat: 2, lon: 2 },
|
||||
},
|
||||
],
|
||||
};
|
||||
const rte1: Rte = {
|
||||
name: 'A new route',
|
||||
rtept: [
|
||||
{
|
||||
$: { lat: 1, lon: 1 },
|
||||
},
|
||||
{
|
||||
$: { lat: 2, lon: 2 },
|
||||
},
|
||||
rtept3,
|
||||
],
|
||||
};
|
||||
const rtePut = await putRte({ id: idRte, rte: rte0 });
|
||||
expect(rtePut).toEqual(rte0);
|
||||
await put(idRtept, 'rtept', () => rtept3, emptyWpt);
|
||||
expect(await getRte({ id: idRte })).toEqual(rte1);
|
||||
});
|
||||
});
|
||||
+18
-26
@@ -1,4 +1,4 @@
|
||||
import { debounce, memoize, property, wrap } from 'lodash';
|
||||
import { cloneDeep, debounce, memoize, property, wrap } from 'lodash';
|
||||
import { delay } from '../lib/delay';
|
||||
import getUri from '../lib/ids';
|
||||
import { appendToArray, putNewGpx } from './gpx';
|
||||
@@ -75,32 +75,24 @@ export const getRteDocs: ({
|
||||
const { id } = params;
|
||||
const docs = await getFamily(id, { include_docs: true });
|
||||
console.log({ caller: 'getRteDocs', id, docs });
|
||||
let target: any[];
|
||||
let rte: Rte | undefined = undefined;
|
||||
docs.rows.every((row: any) => {
|
||||
// level 0
|
||||
if (row.doc.type === 'rte') {
|
||||
if (!!rte) {
|
||||
console.error({
|
||||
caller: 'getRte',
|
||||
id,
|
||||
row,
|
||||
target,
|
||||
rte,
|
||||
});
|
||||
return false; // Hack to stop if getFamily fails
|
||||
}
|
||||
target = [row.doc.doc];
|
||||
rte = row.doc.doc;
|
||||
let rte: Rte;
|
||||
if (docs.rows.length === 1) {
|
||||
rte = docs.rows[0].doc.doc;
|
||||
} else {
|
||||
rte = cloneDeep(docs.rows[0].doc.doc);
|
||||
if (!rte.rtept) {
|
||||
rte.rtept = [];
|
||||
}
|
||||
//level 1
|
||||
if (row.doc.type === 'rtept') {
|
||||
target.splice(1);
|
||||
// row.doc.doc.id = row.doc._id;
|
||||
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
|
||||
target.push(row.doc.doc);
|
||||
}
|
||||
return true;
|
||||
docs.rows.slice(1).forEach((row: any) => {
|
||||
rte.rtept.push(row.doc.doc);
|
||||
});
|
||||
}
|
||||
console.log({
|
||||
caller: 'getRteDocs',
|
||||
id,
|
||||
docs,
|
||||
nbRteptIn: docs.rows[0].doc.rtept?.length,
|
||||
nbRteptTotal: rte?.rtept?.length,
|
||||
});
|
||||
return { docs, rte };
|
||||
};
|
||||
|
||||
+73
-8
@@ -1,9 +1,11 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import shell from 'shelljs';
|
||||
import { initDb } from '.';
|
||||
import { getRte, putRte } from './rte';
|
||||
import PouchDB from 'pouchdb';
|
||||
import { getRte, compactRteOrTrksegDebounced, putRte } from './rte';
|
||||
|
||||
import { putRtept } from './rtept';
|
||||
import { get, getFamily, put } from './lib';
|
||||
import { emptyWpt } from './wpt';
|
||||
|
||||
const test = it;
|
||||
const jest = vi;
|
||||
@@ -18,22 +20,28 @@ const originalDateNow = globalThis.Date.now;
|
||||
|
||||
describe('The rtept module with a real db', () => {
|
||||
beforeEach(async () => {
|
||||
await initDb({});
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: false,
|
||||
});
|
||||
globalThis.Date.now = () => 0;
|
||||
vi.mock('compactRteOrTrksegDebounced', () => {
|
||||
console.log({ caller: 'mocked compactRteOrTrksegDebounced' });
|
||||
});
|
||||
});
|
||||
afterEach(async () => {
|
||||
try {
|
||||
await db.destroy();
|
||||
} catch (err) {
|
||||
// console.error(err);
|
||||
shell.exec('rm -rf \$*\$');
|
||||
// console.error(err);
|
||||
await shell.exec('rm -rf $*$');
|
||||
}
|
||||
db = undefined;
|
||||
globalThis.db = undefined;
|
||||
globalThis.dbReady = false;
|
||||
globalThis.Date.now = originalDateNow;
|
||||
});
|
||||
it('can write a rtept inside a rte', async () => {
|
||||
const idRte = 'whatever';
|
||||
const rtept: Rtept = {
|
||||
const rtept: Wpt = {
|
||||
$: { lat: 2, lon: 2 },
|
||||
};
|
||||
const rte0: Rte = {
|
||||
@@ -62,8 +70,65 @@ describe('The rtept module with a real db', () => {
|
||||
const rtePut = await putRte({ id: idRte, rte: rte0 });
|
||||
expect(rtePut).toEqual(rte0);
|
||||
const rtePutRtept = await putRtept({ idRte, index: 1, rtept });
|
||||
expect(rtePutRtept).toEqual(rte1);
|
||||
const rteGetRtept = await getRte({ id: idRte });
|
||||
expect(rteGetRtept).toEqual(rte1);
|
||||
});
|
||||
it('can write a rtept outside a rte', async () => {
|
||||
const idRte = 'gpx/4320836410265485/2rte/000034';
|
||||
const idRtept = 'gpx/4320836410265485/2rte/000034/000035';
|
||||
const rtept3: Wpt = {
|
||||
$: { lat: 3, lon: 3 },
|
||||
};
|
||||
const rtept4: Wpt = {
|
||||
$: { lat: 4, lon: 4 },
|
||||
};
|
||||
const rte0: Rte = {
|
||||
name: 'A new route',
|
||||
rtept: [
|
||||
{
|
||||
$: { lat: 1, lon: 1 },
|
||||
},
|
||||
{
|
||||
$: { lat: 2, lon: 2 },
|
||||
},
|
||||
],
|
||||
};
|
||||
const rte1: Rte = {
|
||||
name: 'A new route',
|
||||
rtept: [
|
||||
{
|
||||
$: { lat: 1, lon: 1 },
|
||||
},
|
||||
{
|
||||
$: { lat: 2, lon: 2 },
|
||||
},
|
||||
rtept3,
|
||||
],
|
||||
};
|
||||
const rte2: Rte = {
|
||||
name: 'A new route',
|
||||
rtept: [
|
||||
{
|
||||
$: { lat: 1, lon: 1 },
|
||||
},
|
||||
{
|
||||
$: { lat: 2, lon: 2 },
|
||||
},
|
||||
rtept4,
|
||||
],
|
||||
};
|
||||
|
||||
const rtePut = await putRte({ id: idRte, rte: rte0 });
|
||||
expect(rtePut).toEqual(rte0);
|
||||
await put(idRtept, 'rtept', () => rtept3, emptyWpt);
|
||||
expect(await getRte({ id: idRte })).toEqual(rte1);
|
||||
await putRtept({
|
||||
idRte,
|
||||
index: 2,
|
||||
rtept: rtept4,
|
||||
});
|
||||
expect((await getFamily(idRte)).rows.length).toEqual(2);
|
||||
expect(await getRte({ id: idRte })).toEqual(rte2);
|
||||
expect((await get(idRtept)).doc).toEqual(rtept4);
|
||||
});
|
||||
});
|
||||
+12
-8
@@ -26,22 +26,26 @@ export const emptyRtept: Wpt = {
|
||||
};
|
||||
|
||||
export const putRtept = async (params: any) => {
|
||||
console.log({ caller: 'putRtept', params });
|
||||
const { idRte, index, rtept } = params;
|
||||
const { docs, rte } = await getRteDocs({ id: idRte });
|
||||
const { docs } = await getRteDocs({ id: idRte });
|
||||
const rte = docs.rows[0].doc.doc;
|
||||
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
|
||||
console.log({ caller: 'putRtept', nbRteptInRte });
|
||||
const nbRteptOutRte = docs.rows.length - 1;
|
||||
const nbRtept = nbRteptInRte + nbRteptOutRte;
|
||||
const positiveIndex = index >= 0 ? index : nbRtept - index;
|
||||
// console.log({
|
||||
// caller: 'putRtept',
|
||||
// nbRteptInRte,
|
||||
// nbRteptOutRte,
|
||||
// nbRtept,
|
||||
// positiveIndex,
|
||||
// });
|
||||
if (positiveIndex < nbRteptInRte) {
|
||||
rte.rtept[positiveIndex] = rtept;
|
||||
await putRte({ id: idRte, rte });
|
||||
} else {
|
||||
let rteptIdObj = getUri('rtept', docs.rows[1].id);
|
||||
rteptIdObj.rtept = rteptIdObj.rtept + positiveIndex - nbRteptInRte;
|
||||
const rteptUri = getUri('rtept', rteptIdObj);
|
||||
await put(rteptUri, 'rtept', () => rtept, emptyRtept);
|
||||
const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id;
|
||||
await put(reptId, 'rtept', () => rtept, emptyRtept);
|
||||
}
|
||||
return rte;
|
||||
return rtept;
|
||||
};
|
||||
Reference in new issue
Block a user