Skip to content

Commit

Permalink
fix and test model aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
alrik committed Jul 21, 2016
1 parent da9996d commit a846b3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class MongoTenant {
});
}

return super.aggregate(operations);
return super.aggregate.apply(this, operations);
}

static remove(conditions, callback) {
Expand Down
22 changes: 22 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,27 @@ describe('MongoTenant', function() {
});
});
});

it('should bind Model.aggregate() to correct tenant context.', function(done) {
let TestModel = utils.createTestModel({num: Number});

TestModel.create({tenantId: 'tenant1', num: 10}, {tenantId: 'tenant1', num: 12}, {tenantId: 'tenant2', num: 20}, (err) => {
assert(!err, 'Expected creation of 3 test entities to work.');

TestModel.byTenant('tenant1').aggregate({
$group: {
_id: '$tenantId',
sum: {$sum: '$num'}
}},
(err, results) => {
assert(!err, 'Expected Model.aggregate() to work.');
assert.equal(results.length, 1, 'Expected model aggregation to return exactly one result.');
assert.equal(results[0].sum, 22, 'Expected the sum up `num` field for `tenant1` to be 22.');
assert.equal(results[0]._id, 'tenant1', 'Expected the tenant id of aggregated data ser to be `tenant1`.');

done();
});
});
});
});
});

0 comments on commit a846b3d

Please sign in to comment.