|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import * as sinon from 'sinon';
|
3 | 3 |
|
4 |
| -import { BSON, MongoDBResponse, OnDemandDocument } from '../../../mongodb'; |
| 4 | +import { |
| 5 | + BSON, |
| 6 | + BSONError, |
| 7 | + CursorResponse, |
| 8 | + Int32, |
| 9 | + MongoDBResponse, |
| 10 | + MongoUnexpectedServerResponseError, |
| 11 | + OnDemandDocument |
| 12 | +} from '../../../mongodb'; |
5 | 13 |
|
6 | 14 | describe('class MongoDBResponse', () => {
|
7 | 15 | it('is a subclass of OnDemandDocument', () => {
|
@@ -76,3 +84,90 @@ describe('class MongoDBResponse', () => {
|
76 | 84 | });
|
77 | 85 | });
|
78 | 86 | });
|
| 87 | + |
| 88 | +describe('class CursorResponse', () => { |
| 89 | + describe('constructor()', () => { |
| 90 | + it('throws if input does not contain cursor embedded document', () => { |
| 91 | + expect(() => new CursorResponse(BSON.serialize({ ok: 1 }))).to.throw(BSONError); |
| 92 | + }); |
| 93 | + |
| 94 | + it('throws if input does not contain cursor.id int64', () => { |
| 95 | + expect(() => new CursorResponse(BSON.serialize({ ok: 1, cursor: {} }))).to.throw(BSONError); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sets namespace to null if input does not contain cursor.ns', () => { |
| 99 | + expect(new CursorResponse(BSON.serialize({ ok: 1, cursor: { id: 0n, firstBatch: [] } })).ns) |
| 100 | + .to.be.null; |
| 101 | + }); |
| 102 | + |
| 103 | + it('throws if input does not contain firstBatch nor nextBatch', () => { |
| 104 | + expect( |
| 105 | + () => new CursorResponse(BSON.serialize({ ok: 1, cursor: { id: 0n, batch: [] } })) |
| 106 | + ).to.throw(MongoUnexpectedServerResponseError); |
| 107 | + }); |
| 108 | + |
| 109 | + it('reports a length equal to the batch', () => { |
| 110 | + expect( |
| 111 | + new CursorResponse(BSON.serialize({ ok: 1, cursor: { id: 0n, nextBatch: [1, 2, 3] } })) |
| 112 | + ).to.have.lengthOf(3); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('shift()', () => { |
| 117 | + let response; |
| 118 | + |
| 119 | + beforeEach(async function () { |
| 120 | + response = new CursorResponse( |
| 121 | + BSON.serialize({ |
| 122 | + ok: 1, |
| 123 | + cursor: { id: 0n, nextBatch: [{ _id: 1 }, { _id: 2 }, { _id: 3 }] } |
| 124 | + }) |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns a document from the batch', () => { |
| 129 | + expect(response.shift()).to.deep.equal({ _id: 1 }); |
| 130 | + expect(response.shift()).to.deep.equal({ _id: 2 }); |
| 131 | + expect(response.shift()).to.deep.equal({ _id: 3 }); |
| 132 | + expect(response.shift()).to.deep.equal(null); |
| 133 | + }); |
| 134 | + |
| 135 | + it('passes BSON options to deserialization', () => { |
| 136 | + expect(response.shift({ promoteValues: false })).to.deep.equal({ _id: new Int32(1) }); |
| 137 | + expect(response.shift({ promoteValues: true })).to.deep.equal({ _id: 2 }); |
| 138 | + expect(response.shift({ promoteValues: false })).to.deep.equal({ _id: new Int32(3) }); |
| 139 | + expect(response.shift()).to.deep.equal(null); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('clear()', () => { |
| 144 | + let response; |
| 145 | + |
| 146 | + beforeEach(async function () { |
| 147 | + response = new CursorResponse( |
| 148 | + BSON.serialize({ |
| 149 | + ok: 1, |
| 150 | + cursor: { id: 0n, nextBatch: [{ _id: 1 }, { _id: 2 }, { _id: 3 }] } |
| 151 | + }) |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('makes length equal to 0', () => { |
| 156 | + expect(response.clear()).to.be.undefined; |
| 157 | + expect(response).to.have.lengthOf(0); |
| 158 | + }); |
| 159 | + |
| 160 | + it('makes shift return null', () => { |
| 161 | + expect(response.clear()).to.be.undefined; |
| 162 | + expect(response.shift()).to.be.null; |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('pushMany()', () => |
| 167 | + it('throws unsupported error', () => |
| 168 | + expect(CursorResponse.prototype.pushMany).to.throw(/Unsupported/i))); |
| 169 | + |
| 170 | + describe('push()', () => |
| 171 | + it('throws unsupported error', () => |
| 172 | + expect(CursorResponse.prototype.push).to.throw(/Unsupported/i))); |
| 173 | +}); |
0 commit comments