Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Levko Kravets <[email protected]>
  • Loading branch information
kravets-levko committed Jul 31, 2024
1 parent e1950c6 commit 116615c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/e2e/iterators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,64 @@ describe('Iterators', () => {
await session.close();
}
});

it('should get all chunks via Nodejs stream', async () => {
const session = await openSession({ arrowEnabled: false });
// @ts-expect-error TS2339: Property context does not exist on type IDBSQLSession
sinon.spy(session.context.driver, 'fetchResults');
try {
const expectedRowsCount = 10;

// set `maxRows` to null to disable direct results so all the data are fetched through `driver.fetchResults`
const operation = await session.executeStatement(`SELECT * FROM range(0, ${expectedRowsCount})`, {
maxRows: null,
});

const expectedRows = Array.from({ length: expectedRowsCount }, (_, id) => ({ id }));
const chunkSize = 4;
const expectedChunks = arrayChunks(expectedRows, chunkSize);

const stream = operation.toNodeStream({
mode: 'chunks',
iteratorOptions: { maxRows: chunkSize },
});

let index = 0;
for await (const chunk of stream) {
expect(chunk).to.deep.equal(expectedChunks[index]);
index += 1;
}

expect(index).to.equal(expectedChunks.length);
} finally {
await session.close();
}
});

it('should get all rows via Nodejs stream', async () => {
const session = await openSession({ arrowEnabled: false });
// @ts-expect-error TS2339: Property context does not exist on type IDBSQLSession
sinon.spy(session.context.driver, 'fetchResults');
try {
const expectedRowsCount = 10;

const operation = await session.executeStatement(`SELECT * FROM range(0, ${expectedRowsCount})`);

const expectedRows = Array.from({ length: expectedRowsCount }, (_, id) => ({ id }));

const stream = operation.toNodeStream({
mode: 'rows',
});

let index = 0;
for await (const row of stream) {
expect(row).to.deep.equal(expectedRows[index]);
index += 1;
}

expect(index).to.equal(expectedRows.length);
} finally {
await session.close();
}
});
});
6 changes: 6 additions & 0 deletions tests/unit/.stubs/OperationStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import IOperation, {
IOperationChunksIterator,
IOperationRowsIterator,
IteratorOptions,
NodeStreamOptions,
} from '../../../lib/contracts/IOperation';
import Status from '../../../lib/dto/Status';
import { OperationChunksIterator, OperationRowsIterator } from '../../../lib/utils/OperationIterator';
import { Readable } from 'node:stream';

export default class OperationStub implements IOperation {
public readonly id: string = '';
Expand Down Expand Up @@ -59,4 +61,8 @@ export default class OperationStub implements IOperation {
public iterateRows(options?: IteratorOptions): IOperationRowsIterator {
return new OperationRowsIterator(this, options);
}

public toNodeStream(options?: NodeStreamOptions): Readable {
throw new Error('Not implemented');
}
}

0 comments on commit 116615c

Please sign in to comment.