Add pipeline key to Aggregate (#4959)

* Add pipeline key to Aggregate

* clean up

* unit tests
This commit is contained in:
Diamond Lewis
2018-08-12 20:05:08 -05:00
committed by GitHub
parent 9e36a3cc85
commit 4802b1caec
4 changed files with 135 additions and 18 deletions

View File

@@ -0,0 +1,69 @@
const AggregateRouter = require('../lib/Routers/AggregateRouter').AggregateRouter;
describe('AggregateRouter', () => {
it('get pipeline from Array', () => {
const body = [{
group: { objectId: {} }
}];
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});
it('get pipeline from Object', () => {
const body = {
group: { objectId: {} }
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});
it('get pipeline from Pipeline Operator (Array)', () => {
const body = {
pipeline: [{
group: { objectId: {} }
}]
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});
it('get pipeline from Pipeline Operator (Object)', () => {
const body = {
pipeline: {
group: { objectId: {} }
}
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});
it('get pipeline fails multiple keys in Array stage ', () => {
const body = [{
group: { objectId: {} },
match: { name: 'Test' },
}];
try {
AggregateRouter.getPipeline(body);
} catch (e) {
expect(e.message).toBe('Pipeline stages should only have one key found group, match');
}
});
it('get pipeline fails multiple keys in Pipeline Operator Array stage ', () => {
const body = {
pipeline: [{
group: { objectId: {} },
match: { name: 'Test' },
}]
};
try {
AggregateRouter.getPipeline(body);
} catch (e) {
expect(e.message).toBe('Pipeline stages should only have one key found group, match');
}
});
});

View File

@@ -100,6 +100,24 @@ describe('Parse.Query Aggregate testing', () => {
}).catch(done.fail);
});
it('group by pipeline operator', async () => {
const options = Object.assign({}, masterKeyOptions, {
body: {
pipeline: {
group: { objectId: '$name' },
}
}
});
const resp = await rp.get(Parse.serverURL + '/aggregate/TestObject', options);
expect(resp.results.length).toBe(3);
expect(resp.results[0].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[1].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[2].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[0].objectId).not.toBe(undefined);
expect(resp.results[1].objectId).not.toBe(undefined);
expect(resp.results[2].objectId).not.toBe(undefined);
});
it('group by empty object', (done) => {
const obj = new TestObject();
const pipeline = [{