-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.mjs
executable file
·65 lines (52 loc) · 1.54 KB
/
utils.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const output = document.querySelector('output');
export function log(message, className = 'log') {
const logElem = document.createElement('div');
logElem.classList.add(className);
logElem.textContent = message;
output.append(logElem);
}
export function logResults(totalDuration, results) {
const averageMs = results.reduce((a, b) => a + b, 0) / results.length;
const maxMs = Math.max(...results);
log(
`Test completed in ${totalDuration}ms, average query time ${averageMs}ms, max query time ${maxMs}ms`,
'result'
);
}
export function clear(elem) {
while (elem.children.length) {
elem.lastChild.remove();
}
}
export async function prepareTestDatabase(testData) {
const db = new PouchDB('testdb', { auto_compaction: true });
await db.info();
const { cards, progress, notes } = testData;
for (const card of cards) {
await db.put(card);
}
for (const progressRecord of progress) {
await db.put(progressRecord);
}
for (const note of notes) {
await db.put(note);
}
return db;
}
export function compareQueryResults(queryResults) {
const firstRun = queryResults[0];
let ok = true;
for (let i = 1; i < queryResults.length; i++) {
if (JSON.stringify(queryResults[i]) !== JSON.stringify(firstRun)) {
log(`Result for run #${i + 1} differs from first run. Check console`);
console.log('First run:');
console.log(firstRun);
console.log(`Run #${i + 1}:`);
console.log(queryResults[i]);
ok = false;
}
}
if (ok) {
log('Query results match for all runs');
}
}