Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions __test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Realm } = require('../lib/cjs');
const { Realm } = require('../');

const mongoURL = `mongodb//real-function-url1`;
const ref = new Realm({
Expand Down Expand Up @@ -30,7 +30,7 @@ describe(`generates search query correctly`, () => {
];
const query = ref.query(testQuery);

console.log(`text search query: `, JSON.stringify(query));
console.log(`text query: `, JSON.stringify(query));
it(`should have correct mongo format for searchQuery`, () => {
const expected = {
$search: { text: { query: `room`, path: [`name`] } },
Expand Down Expand Up @@ -127,3 +127,97 @@ describe(`generate geo query correctly`, () => {
expect(query[3]).toStrictEqual(expected);
});
});

describe(`generates range query correctly`, () => {
const testQuery = [
{
id: `rangeQuery`,
value: {
start: 1,
end: 20,
boost: 1,
},
dataField: [`accommodates`],
type: `range`,
},
{
id: `rangeQueryWithAggs`,
value: {
start: 1,
end: 20,
boost: 1,
},
aggregations: [`min`, `max`, `histogram`],
interval: 2,
dataField: [`accommodates`],
type: `range`,
},
{
id: `rangeQueryWithNull`,
value: {
start: 1,
end: 20,
boost: 1,
},
dataField: [`accommodates`],
includeNullValues: true,
type: `range`,
},
];
const query = ref.query(testQuery);

console.log(`range query: `, JSON.stringify(query));

it(`should have correct mongo format for range query`, () => {
const expected = {
$search: {
range: { path: 'accommodates', gte: 1, lte: 20, score: { boost: 1 } },
},
};
expect(query[0]).toStrictEqual(expected);
});

it(`should have correct min query`, () => {
const expected = {
$group: { _id: null, min: { $min: '$accommodates' } },
};
expect(query[6]).toStrictEqual(expected);
});

it(`should have correct max query`, () => {
const expected = { $group: { _id: null, max: { $max: '$accommodates' } } };
expect(query[7]).toStrictEqual(expected);
});

it(`should have correct histogram query`, () => {
const expected = {
$bucket: {
groupBy: '$accommodates',
boundaries: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
default: 'other',
},
};
expect(query[8]).toStrictEqual(expected);
});

it(`should have compound query for includeNullValues `, () => {
const expected = {
$search: {
compound: {
should: [
{
range: {
path: 'accommodates',
gte: 1,
lte: 20,
score: { boost: 1 },
},
},
{ compound: { mustNot: [{ exists: { path: 'accommodates' } }] } },
],
},
},
};
expect(query[9]).toStrictEqual(expected);
});
});
Loading