-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
67 lines (59 loc) · 1.67 KB
/
test.js
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
66
67
/* eslint-disable no-console */
const test = require('tape');
const strip = require('strip-ansi');
const flamongo = require('./lib');
const schema = {
name: {
first: 'first',
last: 'last',
},
vegan: 'bool',
happy: {
_type: 'bool',
args: { likelihood: 10 },
},
birthday: 'date',
jobTitle: {
_type: 'enum',
options: ['software developer', 'football player'],
},
friends: [{
name: {
first: 'first',
last: 'last',
},
}],
};
test('check performance of provided indexes', (t) => {
t.plan(1);
const indexKeys = [
{ 'name.last': 1 },
{ 'name.last': 1, vegan: 1 },
{ 'name.first': 1, vegan: 1 },
];
const queries = [
{ 'name.first': 'Richard' },
{ 'name.first': 'John', vegan: false, 'name.last': { $nin: ['Smith'] } },
];
flamongo.explain({ indexKeys, queries, schema }, { verbose: false, preserveData: false }, console.log)
.then((queryResults) => {
const indexUseRegex = /.*Examined [\d]+ keys on index name.first_1_vegan_1 in a forward direction.*/;
t.ok(indexUseRegex.test(strip(queryResults[1].output)));
t.end();
});
});
test('check performance of all possible indexes', (t) => {
t.plan(2);
const queries = [
{ 'name.first': 'Richard' },
{ 'name.first': 'Richard', vegan: false, birthday: { $gt: new Date() } },
{ 'name.first': 'Richard', $or: [{ vegan: false }, { happy: true }] },
];
flamongo.bestIndex({ queries, schema }, { preserveData: true }, console.log)
.then((indexResults) => {
t.ok(indexResults[0][0].metadata.name);
t.ok(indexResults[0][0].time || indexResults[0][0].time === 0);
t.end();
});
});
/* eslint-enable no-console */