Skip to content

Commit

Permalink
Fixing Custom Agent Context Missing From getOps/getOpsBulk (#488)
Browse files Browse the repository at this point in the history
* Fixing Custom Agent Context Missing From getOps/getOpsBulk

* Adding tests for agentCustom on missing operation calls

* Fixing linting errors in test
  • Loading branch information
dwinrick-lever committed Jun 24, 2021
1 parent e342683 commit 3a641d8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ Backend.prototype._getSnapshotsFromMap = function(ids, snapshotMap) {

Backend.prototype._getSanitizedOps = function(agent, projection, collection, id, from, to, opsOptions, callback) {
var backend = this;
if (!opsOptions) opsOptions = {};
if (agent) opsOptions.agentCustom = agent.custom;
backend.db.getOps(collection, id, from, to, opsOptions, function(err, ops) {
if (err) return callback(err);
backend._sanitizeOps(agent, projection, collection, id, ops, function(err) {
Expand All @@ -315,6 +317,8 @@ Backend.prototype._getSanitizedOps = function(agent, projection, collection, id,

Backend.prototype._getSanitizedOpsBulk = function(agent, projection, collection, fromMap, toMap, opsOptions, callback) {
var backend = this;
if (!opsOptions) opsOptions = {};
if (agent) opsOptions.agentCustom = agent.custom;
backend.db.getOpsBulk(collection, fromMap, toMap, opsOptions, function(err, opsMap) {
if (err) return callback(err);
backend._sanitizeOpsBulk(agent, projection, collection, opsMap, function(err) {
Expand Down
69 changes: 69 additions & 0 deletions test/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,75 @@ describe('Backend', function() {
done();
});
});

it('passes agent.custom and snapshot options to db', function(done) {
var options = {
opsOptions: {metadata: true}
};
var getOpsSpy = sinon.spy(backend.db, 'getOps');
backend.getOps(agent, 'books', '1984', 0, null, options, function(error) {
if (error) return done(error);
expect(getOpsSpy.getCall(0).args[4]).to.deep.equal({
agentCustom: agent.custom,
metadata: true
});
done();
});
});
});

describe('getOpsBulk', function() {
it('fetches all the ops', function(done) {
backend.getOpsBulk(agent, 'books', {
1984: 0
}, {
1984: null
}, null, function(error, opsPerDoc) {
if (error) return done(error);
var ops = opsPerDoc['1984'];
expect(ops).to.have.length(2);
expect(ops[0].create.data).to.eql({title: '1984'});
expect(ops[1].op).to.eql([{p: ['author'], oi: 'George Orwell'}]);
done();
});
});

it('fetches the ops with metadata', function(done) {
var options = {
opsOptions: {metadata: true}
};
backend.getOpsBulk(agent, 'books', {
1984: 0
}, {
1984: null
}, options, function(error, opsPerDoc) {
if (error) return done(error);
var ops = opsPerDoc['1984'];
expect(ops).to.have.length(2);
expect(ops[0].m).to.be.ok;
expect(ops[1].m).to.be.ok;
done();
});
});

it('passes agent.custom and snapshot options to db', function(done) {
var options = {
opsOptions: {metadata: true}
};
var getOpsBulkSpy = sinon.spy(backend.db, 'getOpsBulk');
backend.getOpsBulk(agent, 'books', {
1984: 0
}, {
1984: null
}, options, function(error) {
if (error) return done(error);
expect(getOpsBulkSpy.getCall(0).args[3]).to.deep.equal({
agentCustom: agent.custom,
metadata: true
});
done();
});
});
});

describe('fetch', function() {
Expand Down

0 comments on commit 3a641d8

Please sign in to comment.