Skip to content

Commit

Permalink
FIX #252 RxQuery.or() has no tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Aug 24, 2017
1 parent c32041b commit 63dd886
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/modules/key-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,21 @@ class KeyCompressor {
const selector = {};
Object.keys(queryJSON.selector).forEach(key => {
const value = queryJSON.selector[key];
const transKey = this._transformKey('', '', key.split('.'));
selector[transKey] = value;
if (key.startsWith('$')) {
// $or, $not etc have different structure
const setObj = value.map(obj => {
const newObj = {};
Object.keys(obj).forEach(k => {
const transKey = this._transformKey('', '', k.split('.'));
newObj[transKey] = obj[k];
});
return newObj;
});
selector[key] = setObj;
} else {
const transKey = this._transformKey('', '', key.split('.'));
selector[transKey] = value;
}
});
queryJSON.selector = selector;

Expand Down
32 changes: 30 additions & 2 deletions test/unit/RxCollection.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert';
import memdown from 'memdown';
import randomInt from 'random-int';
import randomToken from 'random-token';
import clone from 'clone';
import platform from 'detect-browser';
import AsyncTestUtil from 'async-test-util';
Expand Down Expand Up @@ -493,6 +494,35 @@ describe('RxCollection.test.js', () => {
});
describe('negative', () => {});
});
describe('.or()', () => {
it('should find the 2 documents with the or-method', async() => {
const c = await humansCollection.create(10);
// add 2 docs to be found
await c.insert({
passportId: randomToken(12),
firstName: 'foobarAlice',
lastName: 'aliceLastName',
age: randomInt(10, 50)
});
await c.insert({
passportId: randomToken(12),
firstName: 'foobarBob',
lastName: 'bobLastName',
age: randomInt(10, 50)
});
const query = c.find().or([{
firstName: 'foobarAlice'
}, {
firstName: 'foobarBob'
}]);
const results = await query.exec();
assert.equal(results.length, 2);
const foundFirstNames = results.map(doc => doc.firstName);
assert.ok(foundFirstNames.includes('foobarAlice'));
assert.ok(foundFirstNames.includes('foobarBob'));
c.database.destroy();
});
});
describe('.sort()', () => {
describe('positive', () => {
it('sort by age desc (with own index-search)', async() => {
Expand Down Expand Up @@ -755,7 +785,6 @@ describe('RxCollection.test.js', () => {
});
});
});

describe('.regex()', () => {
describe('positive', () => {
it('find the one where the regex matches', async() => {
Expand Down Expand Up @@ -798,7 +827,6 @@ describe('RxCollection.test.js', () => {
});
});
});

describe('.remove()', () => {
it('should remove all documents', async() => {
const c = await humansCollection.create(10);
Expand Down

0 comments on commit 63dd886

Please sign in to comment.