|
1 |
| -const { expect } = require('chai'); |
2 |
| -const { OpReply } = require('../../mongodb'); |
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { DocumentSequence, OpMsgRequest, OpReply } from '../../mongodb'; |
3 | 4 |
|
4 | 5 | describe('commands', function () {
|
| 6 | + describe('OpMsgRequest', function () { |
| 7 | + describe('#toBin', function () { |
| 8 | + /** |
| 9 | + * Note that #toBin returns an array of buffers, in this case we are interested in |
| 10 | + * the buffer at index 3 of the array, which is a single buffer of all the |
| 11 | + * document sequence sections. |
| 12 | + */ |
| 13 | + context('when the command has document sequences', function () { |
| 14 | + context('when there is one document sequence', function () { |
| 15 | + const command = { |
| 16 | + test: 1, |
| 17 | + field: new DocumentSequence('test', [{ test: 1 }]) |
| 18 | + }; |
| 19 | + const msg = new OpMsgRequest('admin', command, {}); |
| 20 | + const buffers = msg.toBin(); |
| 21 | + |
| 22 | + it('removes the document sequence fields from the command', function () { |
| 23 | + expect(command).to.not.haveOwnProperty('field'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('sets the document sequence section type to 1', function () { |
| 27 | + // First byte is a one byte type. |
| 28 | + expect(buffers[3][0]).to.equal(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('sets the length of the document sequence', function () { |
| 32 | + // Bytes starting at index 1 is a 4 byte length. |
| 33 | + expect(buffers[3].readInt32LE(1)).to.equal(20); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sets the name of the first field to be replaced', function () { |
| 37 | + // Bytes starting at index 5 is the field name. |
| 38 | + expect(buffers[3].toString('utf8', 5, 10)).to.equal('field'); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + context('when there are multiple document sequences', function () { |
| 43 | + const command = { |
| 44 | + test: 1, |
| 45 | + fieldOne: new DocumentSequence('test', [{ test: 1 }]), |
| 46 | + fieldTwo: new DocumentSequence('test', [{ test: 1 }]) |
| 47 | + }; |
| 48 | + const msg = new OpMsgRequest('admin', command, {}); |
| 49 | + const buffers = msg.toBin(); |
| 50 | + |
| 51 | + it('removes the document sequence fields from the command', function () { |
| 52 | + expect(command).to.not.haveOwnProperty('fieldOne'); |
| 53 | + expect(command).to.not.haveOwnProperty('fieldTwo'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('sets the document sequence sections first type to 1', function () { |
| 57 | + // First byte is a one byte type. |
| 58 | + expect(buffers[3][0]).to.equal(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('sets the length of the first document sequence', function () { |
| 62 | + // Bytes starting at index 1 is a 4 byte length. |
| 63 | + expect(buffers[3].readInt32LE(1)).to.equal(23); |
| 64 | + }); |
| 65 | + |
| 66 | + it('sets the name of the first field to be replaced', function () { |
| 67 | + // Bytes starting at index 5 is the field name. |
| 68 | + expect(buffers[3].toString('utf8', 5, 13)).to.equal('fieldOne'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('sets the document sequence sections second type to 1', function () { |
| 72 | + // First byte is a one byte type. |
| 73 | + expect(buffers[3][28]).to.equal(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('sets the length of the first document sequence', function () { |
| 77 | + // Bytes starting at index 1 is a 4 byte length. |
| 78 | + expect(buffers[3].readInt32LE(29)).to.equal(23); |
| 79 | + }); |
| 80 | + |
| 81 | + it('sets the name of the second field to be replaced', function () { |
| 82 | + // Bytes starting at index 33 is the field name. |
| 83 | + expect(buffers[3].toString('utf8', 33, 41)).to.equal('fieldTwo'); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
5 | 90 | describe('Response', function () {
|
6 | 91 | describe('#parse', function () {
|
7 | 92 | context('when the message body is invalid', function () {
|
|
0 commit comments