it('uses the legacy limit object definition', () => { const statement = { getCategory, getOrderings, hasBaseCriteria }; td.when(hasBaseCriteria()).thenReturn(true); td.when(createLimit(statement)).thenReturn('foo'); td.when(getOrderings()).thenReturn([]); FakeCrud.create(statement, { builder: 'FakeStub' }); expect(td.explain(FakeStub.prototype.setLimit).callCount).to.equal(1); return expect(td.explain(FakeStub.prototype.setLimit).calls[0].args[0]).to.equal('foo'); });
it('handles messages fully-contained in a fragment', () => { const network = new EventEmitter(); /* eslint-disable no-unused-vars */ const client = new Client(network); /* eslint-enable no-unused-vars */ // fragment containing two messages const fragment = Buffer.concat([rawMessage1, rawMessage2], rawMessage1.length + rawMessage2.length); network.emit('data', fragment); expect(td.explain(fakeProcess).callCount).to.equal(2); expect(td.explain(fakeProcess).calls[0].args).to.deep.equal([message1]); expect(td.explain(fakeProcess).calls[1].args).to.deep.equal([message2]); });
client.on('started', function() { for (var i = 0; i < data.length; ++i) { lastSubscribedAddress = undefined; client.subscribe.apply(client, data[i].args); var expectedAddress = ((data[i].share) ? ('share:' + data[i].share + ':') : 'private:') + pattern + i; test.deepEqual(lastSubscribedAddress, expectedAddress); } // Restore the saved messenger subscribe implementation client._messenger.createReceiver = savedSubscribeFunction; client.stop(); });
it('creates a Mysqlx.Prepare.OneOfMessage object containing a Mysqlx.Crud.Find query', () => { const type = PrepareStub.OneOfMessage.Type.FIND; const statement = { getType }; const createFind = td.function(); td.replace('../../../../lib/Protocol/Protobuf/Adapters/Crud', { createFind }); td.when(getType()).thenReturn(type); td.when(createFind(statement, 'foo')).thenReturn('bar'); Prepare = require('../../../../lib/Protocol/Protobuf/Adapters/Prepare'); Prepare.createOneOfMessage(statement, 'foo'); expect(td.explain(FakeOneOfMessageStub.prototype.setType).callCount).to.equal(1); expect(td.explain(FakeOneOfMessageStub.prototype.setType).calls[0].args[0]).to.equal(type); expect(td.explain(FakeOneOfMessageStub.prototype.setFind).callCount).to.equal(1); return expect(td.explain(FakeOneOfMessageStub.prototype.setFind).calls[0].args[0]).to.equal('bar'); });
test('Transmitter prints errors on non-OK http responses', async function(t) { nock('http://host') .post('/method') .reply(404, (uri, requestBody) => {}); state.addEvent({foo: 'bar'}); spy.resetHistory(); await transmitter.transmitEvents('http://host/method', 'some-project-id', 'some-agent-id') .then(() => { const calls = spy.getCalls(); t.equal(calls[0].args[0], 'agent:some-agent-id transmitting 1 events to http://host/method with project ID some-project-id.', 'printing count of transmitted events'); t.equal(calls[1].args[0], 'Unexpected response for events transmission: 404 : {"type":"Buffer","data":[]}', 'printing unexpected http responses'); t.end(); nock.cleanAll(); }); });
it('enables expression parsing by default', () => { FakeExpr.createExpr('foo'); expect(td.explain(parse).callCount).to.equal(1); expect(td.explain(parse).calls[0].args[0]).to.equal('foo'); });
it('handles message headers and payloads split between fragments', () => { const network = new EventEmitter(); /* eslint-disable no-unused-vars */ const client = new Client(network); /* eslint-enable no-unused-vars */ const header = rawMessage2.slice(0, 4); // fragment containing the first message and the entire header of the second message const fragment1 = Buffer.concat([rawMessage1, header], rawMessage1.length + header.length); // fragment containing the payload of the second message const fragment2 = rawMessage2.slice(4); network.emit('data', fragment1); network.emit('data', fragment2); expect(td.explain(fakeProcess).callCount).to.equal(2); expect(td.explain(fakeProcess).calls[0].args).to.deep.equal([message1]); expect(td.explain(fakeProcess).calls[1].args).to.deep.equal([message2]); });
it('appends placeholder assignment values of an existing criteria expression', () => { const getCriteriaExpr = td.function(); const setCriteriaExpr = td.function(); const statement = { getCategory, getCriteriaExpr, getOrderings, hasBaseCriteria, setCriteriaExpr }; td.when(getCriteriaExpr()).thenReturn('foo'); td.when(createOperationArgs(statement), { ignoreExtraArgs: true }).thenReturn('bar'); td.when(getOrderings()).thenReturn([]); FakeCrud.create(statement, { appendArgs: true, builder: 'FakeStub' }); expect(td.explain(setCriteriaExpr).callCount).to.equal(1); expect(td.explain(setCriteriaExpr).calls[0].args[0]).to.equal('foo'); expect(td.explain(FakeStub.prototype.setCriteria).callCount).to.equal(1); expect(td.explain(FakeStub.prototype.setCriteria).calls[0].args[0]).to.equal('foo'); expect(td.explain(FakeStub.prototype.setArgsList).callCount).to.equal(1); return expect(td.explain(FakeStub.prototype.setArgsList).calls[0].args[0]).to.equal('bar'); });
it('handles messages split between more than two fragments', () => { const network = new EventEmitter(); /* eslint-disable no-unused-vars */ const client = new Client(network); /* eslint-enable no-unused-vars */ const fragment1 = rawMessage1.slice(0, 4); const fragment2 = rawMessage1.slice(4, 6); const fragment3 = rawMessage1.slice(6, 8); network.emit('data', fragment1); network.emit('data', fragment2); network.emit('data', fragment3); expect(td.explain(fakeProcess).callCount).to.equal(1); expect(td.explain(fakeProcess).calls[0].args).to.deep.equal([message1]); });
it('appends placeholder values as Mysqlx.Sql.StmtExecute.args by default', () => { const statement = { getNamespace, getSQL }; td.when(getNamespace()).thenReturn('foo'); td.when(getSQL()).thenReturn('bar'); td.when(createOperationArgs(statement)).thenReturn('baz'); const proto = Sql.createStmtExecute(statement); expect(proto).to.be.an.instanceOf(FakeStmtExecuteStub); expect(td.explain(FakeStmtExecuteStub.prototype.setNamespace).callCount).to.equal(1); expect(td.explain(FakeStmtExecuteStub.prototype.setNamespace).calls[0].args[0]).to.equal('foo'); expect(td.explain(FakeStmtExecuteStub.prototype.setStmt).callCount).to.equal(1); // eslint-disable-next-line node/no-deprecated-api expect(td.explain(FakeStmtExecuteStub.prototype.setStmt).calls[0].args[0]).to.deep.equal(new Uint8Array(new Buffer('bar'))); expect(td.explain(FakeStmtExecuteStub.prototype.setArgsList).callCount).to.equal(1); expect(td.explain(FakeStmtExecuteStub.prototype.setArgsList).calls[0].args[0]).to.equal('baz'); });
it('creates a Mysqlx.Prepare.OneOfMessage object containing a Mysqlx.Crud.Delete query', () => { const type = PrepareStub.OneOfMessage.Type.DELETE; const statement = { getType }; const createDelete = td.function(); td.replace('../../../../lib/Protocol/Protobuf/Adapters/Crud', { createDelete }); td.when(getType()).thenReturn(type); td.when(createDelete(statement, 'foo')).thenReturn('bar'); Prepare = require('../../../../lib/Protocol/Protobuf/Adapters/Prepare'); Prepare.createOneOfMessage(statement, 'foo'); expect(td.explain(FakeOneOfMessageStub.prototype.setType).callCount).to.equal(1); expect(td.explain(FakeOneOfMessageStub.prototype.setType).calls[0].args[0]).to.equal(type); expect(td.explain(FakeOneOfMessageStub.prototype.setDelete).callCount).to.equal(1); return expect(td.explain(FakeOneOfMessageStub.prototype.setDelete).calls[0].args[0]).to.equal('bar'); });
it('allows to not append placeholder values as Mysqlx.Sql.StmtExecute.args', () => { const statement = { getNamespace, getSQL }; td.when(getNamespace()).thenReturn('foo'); td.when(getSQL()).thenReturn('bar'); td.when(createOperationArgs(statement)).thenReturn('baz'); const proto = Sql.createStmtExecute(statement, { appendArgs: false }); expect(proto).to.be.an.instanceOf(FakeStmtExecuteStub); expect(td.explain(FakeStmtExecuteStub.prototype.setNamespace).callCount).to.equal(1); expect(td.explain(FakeStmtExecuteStub.prototype.setNamespace).calls[0].args[0]).to.equal('foo'); expect(td.explain(FakeStmtExecuteStub.prototype.setStmt).callCount).to.equal(1); // eslint-disable-next-line node/no-deprecated-api expect(td.explain(FakeStmtExecuteStub.prototype.setStmt).calls[0].args[0]).to.deep.equal(new Uint8Array(new Buffer('bar'))); expect(td.explain(FakeStmtExecuteStub.prototype.setArgsList).callCount).to.equal(0); });
it('creates a Mysqlx.Prepare.OneOfMessage object containing a Mysqlx.Crud.Update query', () => { const type = PrepareStub.OneOfMessage.Type.UPDATE; const statement = { getType }; const createUpdate = td.function(); td.replace('../../../../lib/Protocol/Protobuf/Adapters/Crud', { createUpdate }); td.when(getType()).thenReturn(type); td.when(createUpdate(statement, 'foo')).thenReturn('bar'); Prepare = require('../../../../lib/Protocol/Protobuf/Adapters/Prepare'); Prepare.createOneOfMessage(statement, 'foo'); expect(td.explain(FakeOneOfMessageStub.prototype.setType).callCount).to.equal(1); expect(td.explain(FakeOneOfMessageStub.prototype.setType).calls[0].args[0]).to.equal(type); expect(td.explain(FakeOneOfMessageStub.prototype.setUpdate).callCount).to.equal(1); return expect(td.explain(FakeOneOfMessageStub.prototype.setUpdate).calls[0].args[0]).to.equal('bar'); });
it('stringifies non-string input for parsing', () => { FakeExpr.createExpr({ foo: 'bar' }); expect(td.explain(parse).callCount).to.equal(1); expect(td.explain(parse).calls[0].args[0]).to.equal('{"foo":"bar"}'); });
test('Trasmitter prints success on transmitted events', async function(t) { nock('http://host') .post('/method') .reply(200, {}); state.addEvent({foo: 'bar'}); spy.resetHistory(); await transmitter.transmitEvents('http://host/method', 'some-project-id', 'some-agent-id') .then(() => { const calls = spy.getCalls(); t.equal(calls[0].args[0], 'agent:some-agent-id transmitting 1 events to http://host/method with project ID some-project-id.', 'printing count of transmitted events'); t.equal(calls[1].args[0], 'Successfully transmitted events.', 'printing count of transmitted events'); t.end(); nock.cleanAll(); }); });