Skip to content

Commit

Permalink
Add tests for ResultSlicer
Browse files Browse the repository at this point in the history
Signed-off-by: Levko Kravets <[email protected]>
  • Loading branch information
kravets-levko committed Nov 10, 2023
1 parent 44168c4 commit 68c2225
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/unit/result/ResultSlicer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const { expect } = require('chai');
const sinon = require('sinon');
const ResultSlicer = require('../../../dist/result/ResultSlicer').default;

class ResultsProviderMock {
constructor(chunks) {
this.chunks = chunks;
}

async hasMore() {
return this.chunks.length > 0;
}

async fetchNext() {
return this.chunks.shift() ?? [];
}
}

describe('ResultSlicer', () => {
it('should return chunks of requested size', async () => {
const provider = new ResultsProviderMock([
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
]);

const slicer = new ResultSlicer({}, provider);

const chunk1 = await slicer.fetchNext({ limit: 4 });
expect(chunk1).to.deep.eq([10, 11, 12, 13]);
expect(await slicer.hasMore()).to.be.true;

const chunk2 = await slicer.fetchNext({ limit: 10 });
expect(chunk2).to.deep.eq([14, 15, 20, 21, 22, 23, 24, 25, 30, 31]);
expect(await slicer.hasMore()).to.be.true;

const chunk3 = await slicer.fetchNext({ limit: 10 });
expect(chunk3).to.deep.eq([32, 33, 34, 35]);
expect(await slicer.hasMore()).to.be.false;
});

it('should return raw chunks', async () => {
const provider = new ResultsProviderMock([
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
]);
sinon.spy(provider, 'fetchNext');

const slicer = new ResultSlicer({}, provider);

const chunk1 = await slicer.fetchNext({ limit: 4, disableBuffering: true });
expect(chunk1).to.deep.eq([10, 11, 12, 13, 14, 15]);
expect(await slicer.hasMore()).to.be.true;
expect(provider.fetchNext.callCount).to.be.equal(1);

const chunk2 = await slicer.fetchNext({ limit: 10, disableBuffering: true });
expect(chunk2).to.deep.eq([20, 21, 22, 23, 24, 25]);
expect(await slicer.hasMore()).to.be.true;
expect(provider.fetchNext.callCount).to.be.equal(2);
});

it('should switch between returning sliced and raw chunks', async () => {
const provider = new ResultsProviderMock([
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
]);

const slicer = new ResultSlicer({}, provider);

const chunk1 = await slicer.fetchNext({ limit: 4 });
expect(chunk1).to.deep.eq([10, 11, 12, 13]);
expect(await slicer.hasMore()).to.be.true;

const chunk2 = await slicer.fetchNext({ limit: 10, disableBuffering: true });
expect(chunk2).to.deep.eq([14, 15]);
expect(await slicer.hasMore()).to.be.true;

const chunk3 = await slicer.fetchNext({ limit: 10, disableBuffering: true });
expect(chunk3).to.deep.eq([20, 21, 22, 23, 24, 25]);
expect(await slicer.hasMore()).to.be.true;

const chunk4 = await slicer.fetchNext({ limit: 4 });
expect(chunk4).to.deep.eq([30, 31, 32, 33]);
expect(await slicer.hasMore()).to.be.true;

const chunk5 = await slicer.fetchNext({ limit: 4 });
expect(chunk5).to.deep.eq([34, 35]);
expect(await slicer.hasMore()).to.be.false;
});
});

0 comments on commit 68c2225

Please sign in to comment.